2011-11-07 02:58:03 +08:00
|
|
|
//===--- TransGCAttrs.cpp - Transformations to ARC mode --------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Transforms.h"
|
|
|
|
#include "Internals.h"
|
2012-07-05 04:19:54 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2011-11-07 02:58:03 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2012-07-05 04:19:54 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2011-11-08 02:40:29 +08:00
|
|
|
#include "clang/Sema/SemaDiagnostic.h"
|
2012-02-04 21:45:25 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2011-11-08 02:46:46 +08:00
|
|
|
#include "llvm/ADT/TinyPtrVector.h"
|
2012-07-05 04:19:54 +08:00
|
|
|
#include "llvm/Support/SaveAndRestore.h"
|
2011-11-07 02:58:03 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace arcmt;
|
|
|
|
using namespace trans;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// \brief Collects all the places where GC attributes __strong/__weak occur.
|
|
|
|
class GCAttrsCollector : public RecursiveASTVisitor<GCAttrsCollector> {
|
|
|
|
MigrationContext &MigrateCtx;
|
|
|
|
bool FullyMigratable;
|
2011-11-08 02:46:46 +08:00
|
|
|
std::vector<ObjCPropertyDecl *> &AllProps;
|
2011-11-07 02:58:03 +08:00
|
|
|
|
|
|
|
typedef RecursiveASTVisitor<GCAttrsCollector> base;
|
|
|
|
public:
|
2011-11-08 02:46:46 +08:00
|
|
|
GCAttrsCollector(MigrationContext &ctx,
|
|
|
|
std::vector<ObjCPropertyDecl *> &AllProps)
|
|
|
|
: MigrateCtx(ctx), FullyMigratable(false),
|
|
|
|
AllProps(AllProps) { }
|
2011-11-07 02:58:03 +08:00
|
|
|
|
|
|
|
bool shouldWalkTypesOfTypeLocs() const { return false; }
|
|
|
|
|
|
|
|
bool VisitAttributedTypeLoc(AttributedTypeLoc TL) {
|
|
|
|
handleAttr(TL);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TraverseDecl(Decl *D) {
|
|
|
|
if (!D || D->isImplicit())
|
|
|
|
return true;
|
|
|
|
|
2011-11-08 02:46:46 +08:00
|
|
|
SaveAndRestore<bool> Save(FullyMigratable, isMigratable(D));
|
2011-11-07 02:58:03 +08:00
|
|
|
|
2011-11-08 02:46:46 +08:00
|
|
|
if (ObjCPropertyDecl *PropD = dyn_cast<ObjCPropertyDecl>(D)) {
|
2011-11-07 02:58:03 +08:00
|
|
|
lookForAttribute(PropD, PropD->getTypeSourceInfo());
|
2011-11-08 02:46:46 +08:00
|
|
|
AllProps.push_back(PropD);
|
|
|
|
} else if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
|
|
|
|
lookForAttribute(DD, DD->getTypeSourceInfo());
|
|
|
|
}
|
2011-11-07 02:58:03 +08:00
|
|
|
return base::TraverseDecl(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
void lookForAttribute(Decl *D, TypeSourceInfo *TInfo) {
|
|
|
|
if (!TInfo)
|
|
|
|
return;
|
|
|
|
TypeLoc TL = TInfo->getTypeLoc();
|
|
|
|
while (TL) {
|
2013-02-19 06:06:02 +08:00
|
|
|
if (QualifiedTypeLoc QL = TL.getAs<QualifiedTypeLoc>()) {
|
|
|
|
TL = QL.getUnqualifiedLoc();
|
|
|
|
} else if (AttributedTypeLoc Attr = TL.getAs<AttributedTypeLoc>()) {
|
|
|
|
if (handleAttr(Attr, D))
|
2011-11-07 02:58:03 +08:00
|
|
|
break;
|
2013-02-19 06:06:02 +08:00
|
|
|
TL = Attr.getModifiedLoc();
|
|
|
|
} else if (ArrayTypeLoc Arr = TL.getAs<ArrayTypeLoc>()) {
|
|
|
|
TL = Arr.getElementLoc();
|
|
|
|
} else if (PointerTypeLoc PT = TL.getAs<PointerTypeLoc>()) {
|
|
|
|
TL = PT.getPointeeLoc();
|
|
|
|
} else if (ReferenceTypeLoc RT = TL.getAs<ReferenceTypeLoc>())
|
|
|
|
TL = RT.getPointeeLoc();
|
2011-11-07 02:58:03 +08:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool handleAttr(AttributedTypeLoc TL, Decl *D = 0) {
|
|
|
|
if (TL.getAttrKind() != AttributedType::attr_objc_ownership)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SourceLocation Loc = TL.getAttrNameLoc();
|
|
|
|
unsigned RawLoc = Loc.getRawEncoding();
|
|
|
|
if (MigrateCtx.AttrSet.count(RawLoc))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
ASTContext &Ctx = MigrateCtx.Pass.Ctx;
|
|
|
|
SourceManager &SM = Ctx.getSourceManager();
|
2011-11-08 02:40:29 +08:00
|
|
|
if (Loc.isMacroID())
|
|
|
|
Loc = SM.getImmediateExpansionRange(Loc).first;
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<32> Buf;
|
2011-11-07 02:58:03 +08:00
|
|
|
bool Invalid = false;
|
|
|
|
StringRef Spell = Lexer::getSpelling(
|
|
|
|
SM.getSpellingLoc(TL.getAttrEnumOperandLoc()),
|
2012-03-11 15:00:24 +08:00
|
|
|
Buf, SM, Ctx.getLangOpts(), &Invalid);
|
2011-11-07 02:58:03 +08:00
|
|
|
if (Invalid)
|
|
|
|
return false;
|
|
|
|
MigrationContext::GCAttrOccurrence::AttrKind Kind;
|
|
|
|
if (Spell == "strong")
|
|
|
|
Kind = MigrationContext::GCAttrOccurrence::Strong;
|
|
|
|
else if (Spell == "weak")
|
|
|
|
Kind = MigrationContext::GCAttrOccurrence::Weak;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MigrateCtx.AttrSet.insert(RawLoc);
|
|
|
|
MigrateCtx.GCAttrs.push_back(MigrationContext::GCAttrOccurrence());
|
|
|
|
MigrationContext::GCAttrOccurrence &Attr = MigrateCtx.GCAttrs.back();
|
|
|
|
|
|
|
|
Attr.Kind = Kind;
|
2011-11-08 02:40:29 +08:00
|
|
|
Attr.Loc = Loc;
|
2011-11-07 02:58:03 +08:00
|
|
|
Attr.ModifiedType = TL.getModifiedLoc().getType();
|
|
|
|
Attr.Dcl = D;
|
|
|
|
Attr.FullyMigratable = FullyMigratable;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isMigratable(Decl *D) {
|
|
|
|
if (isa<TranslationUnitDecl>(D))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (isInMainFile(D))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
|
|
|
|
return FD->hasBody();
|
|
|
|
|
2011-11-08 02:46:46 +08:00
|
|
|
if (ObjCContainerDecl *ContD = dyn_cast<ObjCContainerDecl>(D))
|
|
|
|
return hasObjCImpl(ContD);
|
2011-11-07 02:58:03 +08:00
|
|
|
|
|
|
|
if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
|
|
|
|
for (CXXRecordDecl::method_iterator
|
|
|
|
MI = RD->method_begin(), ME = RD->method_end(); MI != ME; ++MI) {
|
2012-04-30 10:36:29 +08:00
|
|
|
if (MI->isOutOfLine())
|
2011-11-07 02:58:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isMigratable(cast<Decl>(D->getDeclContext()));
|
|
|
|
}
|
|
|
|
|
2011-11-08 02:46:46 +08:00
|
|
|
static bool hasObjCImpl(Decl *D) {
|
|
|
|
if (!D)
|
|
|
|
return false;
|
|
|
|
if (ObjCContainerDecl *ContD = dyn_cast<ObjCContainerDecl>(D)) {
|
|
|
|
if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ContD))
|
|
|
|
return ID->getImplementation() != 0;
|
|
|
|
if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ContD))
|
|
|
|
return CD->getImplementation() != 0;
|
|
|
|
if (isa<ObjCImplDecl>(ContD))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-07 02:58:03 +08:00
|
|
|
bool isInMainFile(Decl *D) {
|
|
|
|
if (!D)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (Decl::redecl_iterator
|
|
|
|
I = D->redecls_begin(), E = D->redecls_end(); I != E; ++I)
|
2012-05-01 08:48:43 +08:00
|
|
|
if (!isInMainFile(I->getLocation()))
|
2011-11-07 02:58:03 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isInMainFile(SourceLocation Loc) {
|
|
|
|
if (Loc.isInvalid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SourceManager &SM = MigrateCtx.Pass.Ctx.getSourceManager();
|
|
|
|
return SM.isInFileID(SM.getExpansionLoc(Loc), SM.getMainFileID());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2011-11-07 02:58:23 +08:00
|
|
|
static void errorForGCAttrsOnNonObjC(MigrationContext &MigrateCtx) {
|
|
|
|
TransformActions &TA = MigrateCtx.Pass.TA;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = MigrateCtx.GCAttrs.size(); i != e; ++i) {
|
|
|
|
MigrationContext::GCAttrOccurrence &Attr = MigrateCtx.GCAttrs[i];
|
|
|
|
if (Attr.FullyMigratable && Attr.Dcl) {
|
|
|
|
if (Attr.ModifiedType.isNull())
|
|
|
|
continue;
|
|
|
|
if (!Attr.ModifiedType->isObjCRetainableType()) {
|
|
|
|
TA.reportError("GC managed memory will become unmanaged in ARC",
|
|
|
|
Attr.Loc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-08 02:40:29 +08:00
|
|
|
static void checkWeakGCAttrs(MigrationContext &MigrateCtx) {
|
|
|
|
TransformActions &TA = MigrateCtx.Pass.TA;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = MigrateCtx.GCAttrs.size(); i != e; ++i) {
|
|
|
|
MigrationContext::GCAttrOccurrence &Attr = MigrateCtx.GCAttrs[i];
|
2011-11-08 02:46:46 +08:00
|
|
|
if (Attr.Kind == MigrationContext::GCAttrOccurrence::Weak) {
|
2011-11-08 02:40:29 +08:00
|
|
|
if (Attr.ModifiedType.isNull() ||
|
|
|
|
!Attr.ModifiedType->isObjCRetainableType())
|
|
|
|
continue;
|
|
|
|
if (!canApplyWeak(MigrateCtx.Pass.Ctx, Attr.ModifiedType,
|
|
|
|
/*AllowOnUnknownClass=*/true)) {
|
|
|
|
Transaction Trans(TA);
|
2011-11-08 10:02:38 +08:00
|
|
|
if (!MigrateCtx.RemovedAttrSet.count(Attr.Loc.getRawEncoding()))
|
|
|
|
TA.replaceText(Attr.Loc, "__weak", "__unsafe_unretained");
|
2011-11-08 02:40:29 +08:00
|
|
|
TA.clearDiagnostic(diag::err_arc_weak_no_runtime,
|
|
|
|
diag::err_arc_unsupported_weak_class,
|
|
|
|
Attr.Loc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-08 02:46:46 +08:00
|
|
|
typedef llvm::TinyPtrVector<ObjCPropertyDecl *> IndivPropsTy;
|
|
|
|
|
|
|
|
static void checkAllAtProps(MigrationContext &MigrateCtx,
|
|
|
|
SourceLocation AtLoc,
|
|
|
|
IndivPropsTy &IndProps) {
|
|
|
|
if (IndProps.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (IndivPropsTy::iterator
|
|
|
|
PI = IndProps.begin(), PE = IndProps.end(); PI != PE; ++PI) {
|
|
|
|
QualType T = (*PI)->getType();
|
|
|
|
if (T.isNull() || !T->isObjCRetainableType())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<std::pair<AttributedTypeLoc, ObjCPropertyDecl *>, 4> ATLs;
|
|
|
|
bool hasWeak = false, hasStrong = false;
|
2011-11-28 10:04:36 +08:00
|
|
|
ObjCPropertyDecl::PropertyAttributeKind
|
|
|
|
Attrs = ObjCPropertyDecl::OBJC_PR_noattr;
|
2011-11-08 02:46:46 +08:00
|
|
|
for (IndivPropsTy::iterator
|
|
|
|
PI = IndProps.begin(), PE = IndProps.end(); PI != PE; ++PI) {
|
|
|
|
ObjCPropertyDecl *PD = *PI;
|
2011-11-28 10:04:36 +08:00
|
|
|
Attrs = PD->getPropertyAttributesAsWritten();
|
2011-11-08 02:46:46 +08:00
|
|
|
TypeSourceInfo *TInfo = PD->getTypeSourceInfo();
|
|
|
|
if (!TInfo)
|
|
|
|
return;
|
|
|
|
TypeLoc TL = TInfo->getTypeLoc();
|
2013-02-19 06:06:02 +08:00
|
|
|
if (AttributedTypeLoc ATL =
|
|
|
|
TL.getAs<AttributedTypeLoc>()) {
|
|
|
|
ATLs.push_back(std::make_pair(ATL, PD));
|
2011-11-08 02:46:46 +08:00
|
|
|
if (TInfo->getType().getObjCLifetime() == Qualifiers::OCL_Weak) {
|
|
|
|
hasWeak = true;
|
|
|
|
} else if (TInfo->getType().getObjCLifetime() == Qualifiers::OCL_Strong)
|
|
|
|
hasStrong = true;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ATLs.empty())
|
|
|
|
return;
|
|
|
|
if (hasWeak && hasStrong)
|
|
|
|
return;
|
|
|
|
|
|
|
|
TransformActions &TA = MigrateCtx.Pass.TA;
|
|
|
|
Transaction Trans(TA);
|
|
|
|
|
|
|
|
if (GCAttrsCollector::hasObjCImpl(
|
|
|
|
cast<Decl>(IndProps.front()->getDeclContext()))) {
|
|
|
|
if (hasWeak)
|
|
|
|
MigrateCtx.AtPropsWeak.insert(AtLoc.getRawEncoding());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
StringRef toAttr = "strong";
|
|
|
|
if (hasWeak) {
|
|
|
|
if (canApplyWeak(MigrateCtx.Pass.Ctx, IndProps.front()->getType(),
|
|
|
|
/*AllowOnUnkwownClass=*/true))
|
|
|
|
toAttr = "weak";
|
|
|
|
else
|
|
|
|
toAttr = "unsafe_unretained";
|
|
|
|
}
|
2011-11-28 10:04:36 +08:00
|
|
|
if (Attrs & ObjCPropertyDecl::OBJC_PR_assign)
|
|
|
|
MigrateCtx.rewritePropertyAttribute("assign", toAttr, AtLoc);
|
|
|
|
else
|
|
|
|
MigrateCtx.addPropertyAttribute(toAttr, AtLoc);
|
2011-11-08 02:46:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = ATLs.size(); i != e; ++i) {
|
|
|
|
SourceLocation Loc = ATLs[i].first.getAttrNameLoc();
|
|
|
|
if (Loc.isMacroID())
|
|
|
|
Loc = MigrateCtx.Pass.Ctx.getSourceManager()
|
|
|
|
.getImmediateExpansionRange(Loc).first;
|
|
|
|
TA.remove(Loc);
|
|
|
|
TA.clearDiagnostic(diag::err_objc_property_attr_mutually_exclusive, AtLoc);
|
|
|
|
TA.clearDiagnostic(diag::err_arc_inconsistent_property_ownership,
|
|
|
|
ATLs[i].second->getLocation());
|
2011-11-08 10:02:38 +08:00
|
|
|
MigrateCtx.RemovedAttrSet.insert(Loc.getRawEncoding());
|
2011-11-08 02:46:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void checkAllProps(MigrationContext &MigrateCtx,
|
|
|
|
std::vector<ObjCPropertyDecl *> &AllProps) {
|
|
|
|
typedef llvm::TinyPtrVector<ObjCPropertyDecl *> IndivPropsTy;
|
|
|
|
llvm::DenseMap<unsigned, IndivPropsTy> AtProps;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = AllProps.size(); i != e; ++i) {
|
|
|
|
ObjCPropertyDecl *PD = AllProps[i];
|
|
|
|
if (PD->getPropertyAttributesAsWritten() &
|
2011-11-28 08:23:12 +08:00
|
|
|
(ObjCPropertyDecl::OBJC_PR_assign |
|
|
|
|
ObjCPropertyDecl::OBJC_PR_readonly)) {
|
2011-11-08 02:46:46 +08:00
|
|
|
SourceLocation AtLoc = PD->getAtLoc();
|
|
|
|
if (AtLoc.isInvalid())
|
|
|
|
continue;
|
|
|
|
unsigned RawAt = AtLoc.getRawEncoding();
|
|
|
|
AtProps[RawAt].push_back(PD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (llvm::DenseMap<unsigned, IndivPropsTy>::iterator
|
|
|
|
I = AtProps.begin(), E = AtProps.end(); I != E; ++I) {
|
|
|
|
SourceLocation AtLoc = SourceLocation::getFromRawEncoding(I->first);
|
|
|
|
IndivPropsTy &IndProps = I->second;
|
|
|
|
checkAllAtProps(MigrateCtx, AtLoc, IndProps);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-07 02:58:03 +08:00
|
|
|
void GCAttrsTraverser::traverseTU(MigrationContext &MigrateCtx) {
|
2011-11-08 02:46:46 +08:00
|
|
|
std::vector<ObjCPropertyDecl *> AllProps;
|
|
|
|
GCAttrsCollector(MigrateCtx, AllProps).TraverseDecl(
|
2011-11-07 02:58:03 +08:00
|
|
|
MigrateCtx.Pass.Ctx.getTranslationUnitDecl());
|
2011-11-07 02:58:23 +08:00
|
|
|
|
|
|
|
errorForGCAttrsOnNonObjC(MigrateCtx);
|
2011-11-08 02:46:46 +08:00
|
|
|
checkAllProps(MigrateCtx, AllProps);
|
2011-11-08 10:02:38 +08:00
|
|
|
checkWeakGCAttrs(MigrateCtx);
|
2011-11-07 02:58:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MigrationContext::dumpGCAttrs() {
|
2011-11-07 02:58:03 +08:00
|
|
|
llvm::errs() << "\n################\n";
|
2011-11-07 02:58:17 +08:00
|
|
|
for (unsigned i = 0, e = GCAttrs.size(); i != e; ++i) {
|
|
|
|
GCAttrOccurrence &Attr = GCAttrs[i];
|
2011-11-07 02:58:03 +08:00
|
|
|
llvm::errs() << "KIND: "
|
2011-11-07 02:58:17 +08:00
|
|
|
<< (Attr.Kind == GCAttrOccurrence::Strong ? "strong" : "weak");
|
2011-11-07 02:58:03 +08:00
|
|
|
llvm::errs() << "\nLOC: ";
|
2011-11-07 02:58:17 +08:00
|
|
|
Attr.Loc.dump(Pass.Ctx.getSourceManager());
|
2011-11-07 02:58:03 +08:00
|
|
|
llvm::errs() << "\nTYPE: ";
|
|
|
|
Attr.ModifiedType.dump();
|
|
|
|
if (Attr.Dcl) {
|
|
|
|
llvm::errs() << "DECL:\n";
|
|
|
|
Attr.Dcl->dump();
|
|
|
|
} else {
|
|
|
|
llvm::errs() << "DECL: NONE";
|
|
|
|
}
|
|
|
|
llvm::errs() << "\nMIGRATABLE: " << Attr.FullyMigratable;
|
|
|
|
llvm::errs() << "\n----------------\n";
|
|
|
|
}
|
|
|
|
llvm::errs() << "\n################\n";
|
|
|
|
}
|