1 //===--- TransGCAttrs.cpp - Transformations to ARC mode --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "Transforms.h"
11 #include "Internals.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/Basic/SourceManager.h"
14 #include "clang/Lex/Lexer.h"
15 #include "clang/Sema/SemaDiagnostic.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/TinyPtrVector.h"
18 #include "llvm/Support/SaveAndRestore.h"
20 using namespace clang;
21 using namespace arcmt;
22 using namespace trans;
26 /// Collects all the places where GC attributes __strong/__weak occur.
27 class GCAttrsCollector : public RecursiveASTVisitor<GCAttrsCollector> {
28 MigrationContext &MigrateCtx;
30 std::vector<ObjCPropertyDecl *> &AllProps;
32 typedef RecursiveASTVisitor<GCAttrsCollector> base;
34 GCAttrsCollector(MigrationContext &ctx,
35 std::vector<ObjCPropertyDecl *> &AllProps)
36 : MigrateCtx(ctx), FullyMigratable(false),
37 AllProps(AllProps) { }
39 bool shouldWalkTypesOfTypeLocs() const { return false; }
41 bool VisitAttributedTypeLoc(AttributedTypeLoc TL) {
46 bool TraverseDecl(Decl *D) {
47 if (!D || D->isImplicit())
50 SaveAndRestore<bool> Save(FullyMigratable, isMigratable(D));
52 if (ObjCPropertyDecl *PropD = dyn_cast<ObjCPropertyDecl>(D)) {
53 lookForAttribute(PropD, PropD->getTypeSourceInfo());
54 AllProps.push_back(PropD);
55 } else if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
56 lookForAttribute(DD, DD->getTypeSourceInfo());
58 return base::TraverseDecl(D);
61 void lookForAttribute(Decl *D, TypeSourceInfo *TInfo) {
64 TypeLoc TL = TInfo->getTypeLoc();
66 if (QualifiedTypeLoc QL = TL.getAs<QualifiedTypeLoc>()) {
67 TL = QL.getUnqualifiedLoc();
68 } else if (AttributedTypeLoc Attr = TL.getAs<AttributedTypeLoc>()) {
69 if (handleAttr(Attr, D))
71 TL = Attr.getModifiedLoc();
72 } else if (ArrayTypeLoc Arr = TL.getAs<ArrayTypeLoc>()) {
73 TL = Arr.getElementLoc();
74 } else if (PointerTypeLoc PT = TL.getAs<PointerTypeLoc>()) {
75 TL = PT.getPointeeLoc();
76 } else if (ReferenceTypeLoc RT = TL.getAs<ReferenceTypeLoc>())
77 TL = RT.getPointeeLoc();
83 bool handleAttr(AttributedTypeLoc TL, Decl *D = nullptr) {
84 auto *OwnershipAttr = TL.getAttrAs<ObjCOwnershipAttr>();
88 SourceLocation Loc = OwnershipAttr->getLocation();
89 unsigned RawLoc = Loc.getRawEncoding();
90 if (MigrateCtx.AttrSet.count(RawLoc))
93 ASTContext &Ctx = MigrateCtx.Pass.Ctx;
94 SourceManager &SM = Ctx.getSourceManager();
96 Loc = SM.getImmediateExpansionRange(Loc).getBegin();
97 StringRef Spell = OwnershipAttr->getKind()->getName();
98 MigrationContext::GCAttrOccurrence::AttrKind Kind;
99 if (Spell == "strong")
100 Kind = MigrationContext::GCAttrOccurrence::Strong;
101 else if (Spell == "weak")
102 Kind = MigrationContext::GCAttrOccurrence::Weak;
106 MigrateCtx.AttrSet.insert(RawLoc);
107 MigrateCtx.GCAttrs.push_back(MigrationContext::GCAttrOccurrence());
108 MigrationContext::GCAttrOccurrence &Attr = MigrateCtx.GCAttrs.back();
112 Attr.ModifiedType = TL.getModifiedLoc().getType();
114 Attr.FullyMigratable = FullyMigratable;
118 bool isMigratable(Decl *D) {
119 if (isa<TranslationUnitDecl>(D))
125 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
126 return FD->hasBody();
128 if (ObjCContainerDecl *ContD = dyn_cast<ObjCContainerDecl>(D))
129 return hasObjCImpl(ContD);
131 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
132 for (const auto *MI : RD->methods()) {
133 if (MI->isOutOfLine())
139 return isMigratable(cast<Decl>(D->getDeclContext()));
142 static bool hasObjCImpl(Decl *D) {
145 if (ObjCContainerDecl *ContD = dyn_cast<ObjCContainerDecl>(D)) {
146 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ContD))
147 return ID->getImplementation() != nullptr;
148 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ContD))
149 return CD->getImplementation() != nullptr;
150 return isa<ObjCImplDecl>(ContD);
155 bool isInMainFile(Decl *D) {
159 for (auto I : D->redecls())
160 if (!isInMainFile(I->getLocation()))
166 bool isInMainFile(SourceLocation Loc) {
170 SourceManager &SM = MigrateCtx.Pass.Ctx.getSourceManager();
171 return SM.isInFileID(SM.getExpansionLoc(Loc), SM.getMainFileID());
175 } // anonymous namespace
177 static void errorForGCAttrsOnNonObjC(MigrationContext &MigrateCtx) {
178 TransformActions &TA = MigrateCtx.Pass.TA;
180 for (unsigned i = 0, e = MigrateCtx.GCAttrs.size(); i != e; ++i) {
181 MigrationContext::GCAttrOccurrence &Attr = MigrateCtx.GCAttrs[i];
182 if (Attr.FullyMigratable && Attr.Dcl) {
183 if (Attr.ModifiedType.isNull())
185 if (!Attr.ModifiedType->isObjCRetainableType()) {
186 TA.reportError("GC managed memory will become unmanaged in ARC",
193 static void checkWeakGCAttrs(MigrationContext &MigrateCtx) {
194 TransformActions &TA = MigrateCtx.Pass.TA;
196 for (unsigned i = 0, e = MigrateCtx.GCAttrs.size(); i != e; ++i) {
197 MigrationContext::GCAttrOccurrence &Attr = MigrateCtx.GCAttrs[i];
198 if (Attr.Kind == MigrationContext::GCAttrOccurrence::Weak) {
199 if (Attr.ModifiedType.isNull() ||
200 !Attr.ModifiedType->isObjCRetainableType())
202 if (!canApplyWeak(MigrateCtx.Pass.Ctx, Attr.ModifiedType,
203 /*AllowOnUnknownClass=*/true)) {
204 Transaction Trans(TA);
205 if (!MigrateCtx.RemovedAttrSet.count(Attr.Loc.getRawEncoding()))
206 TA.replaceText(Attr.Loc, "__weak", "__unsafe_unretained");
207 TA.clearDiagnostic(diag::err_arc_weak_no_runtime,
208 diag::err_arc_unsupported_weak_class,
215 typedef llvm::TinyPtrVector<ObjCPropertyDecl *> IndivPropsTy;
217 static void checkAllAtProps(MigrationContext &MigrateCtx,
218 SourceLocation AtLoc,
219 IndivPropsTy &IndProps) {
220 if (IndProps.empty())
223 for (IndivPropsTy::iterator
224 PI = IndProps.begin(), PE = IndProps.end(); PI != PE; ++PI) {
225 QualType T = (*PI)->getType();
226 if (T.isNull() || !T->isObjCRetainableType())
230 SmallVector<std::pair<AttributedTypeLoc, ObjCPropertyDecl *>, 4> ATLs;
231 bool hasWeak = false, hasStrong = false;
232 ObjCPropertyDecl::PropertyAttributeKind
233 Attrs = ObjCPropertyDecl::OBJC_PR_noattr;
234 for (IndivPropsTy::iterator
235 PI = IndProps.begin(), PE = IndProps.end(); PI != PE; ++PI) {
236 ObjCPropertyDecl *PD = *PI;
237 Attrs = PD->getPropertyAttributesAsWritten();
238 TypeSourceInfo *TInfo = PD->getTypeSourceInfo();
241 TypeLoc TL = TInfo->getTypeLoc();
242 if (AttributedTypeLoc ATL =
243 TL.getAs<AttributedTypeLoc>()) {
244 ATLs.push_back(std::make_pair(ATL, PD));
245 if (TInfo->getType().getObjCLifetime() == Qualifiers::OCL_Weak) {
247 } else if (TInfo->getType().getObjCLifetime() == Qualifiers::OCL_Strong)
255 if (hasWeak && hasStrong)
258 TransformActions &TA = MigrateCtx.Pass.TA;
259 Transaction Trans(TA);
261 if (GCAttrsCollector::hasObjCImpl(
262 cast<Decl>(IndProps.front()->getDeclContext()))) {
264 MigrateCtx.AtPropsWeak.insert(AtLoc.getRawEncoding());
267 StringRef toAttr = "strong";
269 if (canApplyWeak(MigrateCtx.Pass.Ctx, IndProps.front()->getType(),
270 /*AllowOnUnkwownClass=*/true))
273 toAttr = "unsafe_unretained";
275 if (Attrs & ObjCPropertyDecl::OBJC_PR_assign)
276 MigrateCtx.rewritePropertyAttribute("assign", toAttr, AtLoc);
278 MigrateCtx.addPropertyAttribute(toAttr, AtLoc);
281 for (unsigned i = 0, e = ATLs.size(); i != e; ++i) {
282 SourceLocation Loc = ATLs[i].first.getAttr()->getLocation();
284 Loc = MigrateCtx.Pass.Ctx.getSourceManager()
285 .getImmediateExpansionRange(Loc)
288 TA.clearDiagnostic(diag::err_objc_property_attr_mutually_exclusive, AtLoc);
289 TA.clearDiagnostic(diag::err_arc_inconsistent_property_ownership,
290 ATLs[i].second->getLocation());
291 MigrateCtx.RemovedAttrSet.insert(Loc.getRawEncoding());
295 static void checkAllProps(MigrationContext &MigrateCtx,
296 std::vector<ObjCPropertyDecl *> &AllProps) {
297 typedef llvm::TinyPtrVector<ObjCPropertyDecl *> IndivPropsTy;
298 llvm::DenseMap<unsigned, IndivPropsTy> AtProps;
300 for (unsigned i = 0, e = AllProps.size(); i != e; ++i) {
301 ObjCPropertyDecl *PD = AllProps[i];
302 if (PD->getPropertyAttributesAsWritten() &
303 (ObjCPropertyDecl::OBJC_PR_assign |
304 ObjCPropertyDecl::OBJC_PR_readonly)) {
305 SourceLocation AtLoc = PD->getAtLoc();
306 if (AtLoc.isInvalid())
308 unsigned RawAt = AtLoc.getRawEncoding();
309 AtProps[RawAt].push_back(PD);
313 for (llvm::DenseMap<unsigned, IndivPropsTy>::iterator
314 I = AtProps.begin(), E = AtProps.end(); I != E; ++I) {
315 SourceLocation AtLoc = SourceLocation::getFromRawEncoding(I->first);
316 IndivPropsTy &IndProps = I->second;
317 checkAllAtProps(MigrateCtx, AtLoc, IndProps);
321 void GCAttrsTraverser::traverseTU(MigrationContext &MigrateCtx) {
322 std::vector<ObjCPropertyDecl *> AllProps;
323 GCAttrsCollector(MigrateCtx, AllProps).TraverseDecl(
324 MigrateCtx.Pass.Ctx.getTranslationUnitDecl());
326 errorForGCAttrsOnNonObjC(MigrateCtx);
327 checkAllProps(MigrateCtx, AllProps);
328 checkWeakGCAttrs(MigrateCtx);
331 void MigrationContext::dumpGCAttrs() {
332 llvm::errs() << "\n################\n";
333 for (unsigned i = 0, e = GCAttrs.size(); i != e; ++i) {
334 GCAttrOccurrence &Attr = GCAttrs[i];
335 llvm::errs() << "KIND: "
336 << (Attr.Kind == GCAttrOccurrence::Strong ? "strong" : "weak");
337 llvm::errs() << "\nLOC: ";
338 Attr.Loc.print(llvm::errs(), Pass.Ctx.getSourceManager());
339 llvm::errs() << "\nTYPE: ";
340 Attr.ModifiedType.dump();
342 llvm::errs() << "DECL:\n";
345 llvm::errs() << "DECL: NONE";
347 llvm::errs() << "\nMIGRATABLE: " << Attr.FullyMigratable;
348 llvm::errs() << "\n----------------\n";
350 llvm::errs() << "\n################\n";