Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 1 | //===--- TransGCAttrs.cpp - Transformations to ARC mode --------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "Transforms.h" |
| 10 | #include "Internals.h" |
Benjamin Kramer | 4ab984e | 2012-07-04 20:19:54 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTContext.h" |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 12 | #include "clang/Basic/SourceManager.h" |
Benjamin Kramer | 4ab984e | 2012-07-04 20:19:54 +0000 | [diff] [blame] | 13 | #include "clang/Lex/Lexer.h" |
Argyrios Kyrtzidis | e80d4f2 | 2011-11-07 18:40:29 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaDiagnostic.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallString.h" |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/TinyPtrVector.h" |
Benjamin Kramer | 4ab984e | 2012-07-04 20:19:54 +0000 | [diff] [blame] | 17 | #include "llvm/Support/SaveAndRestore.h" |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace clang; |
| 20 | using namespace arcmt; |
| 21 | using namespace trans; |
| 22 | |
| 23 | namespace { |
| 24 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 25 | /// Collects all the places where GC attributes __strong/__weak occur. |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 26 | class GCAttrsCollector : public RecursiveASTVisitor<GCAttrsCollector> { |
| 27 | MigrationContext &MigrateCtx; |
| 28 | bool FullyMigratable; |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 29 | std::vector<ObjCPropertyDecl *> &AllProps; |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 30 | |
| 31 | typedef RecursiveASTVisitor<GCAttrsCollector> base; |
| 32 | public: |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 33 | GCAttrsCollector(MigrationContext &ctx, |
| 34 | std::vector<ObjCPropertyDecl *> &AllProps) |
| 35 | : MigrateCtx(ctx), FullyMigratable(false), |
| 36 | AllProps(AllProps) { } |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 37 | |
| 38 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 39 | |
| 40 | bool VisitAttributedTypeLoc(AttributedTypeLoc TL) { |
| 41 | handleAttr(TL); |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | bool TraverseDecl(Decl *D) { |
| 46 | if (!D || D->isImplicit()) |
| 47 | return true; |
| 48 | |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 49 | SaveAndRestore<bool> Save(FullyMigratable, isMigratable(D)); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 50 | |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 51 | if (ObjCPropertyDecl *PropD = dyn_cast<ObjCPropertyDecl>(D)) { |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 52 | lookForAttribute(PropD, PropD->getTypeSourceInfo()); |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 53 | AllProps.push_back(PropD); |
| 54 | } else if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) { |
| 55 | lookForAttribute(DD, DD->getTypeSourceInfo()); |
| 56 | } |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 57 | return base::TraverseDecl(D); |
| 58 | } |
| 59 | |
| 60 | void lookForAttribute(Decl *D, TypeSourceInfo *TInfo) { |
| 61 | if (!TInfo) |
| 62 | return; |
| 63 | TypeLoc TL = TInfo->getTypeLoc(); |
| 64 | while (TL) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 65 | if (QualifiedTypeLoc QL = TL.getAs<QualifiedTypeLoc>()) { |
| 66 | TL = QL.getUnqualifiedLoc(); |
| 67 | } else if (AttributedTypeLoc Attr = TL.getAs<AttributedTypeLoc>()) { |
| 68 | if (handleAttr(Attr, D)) |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 69 | break; |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 70 | TL = Attr.getModifiedLoc(); |
Leonard Chan | c72aaf6 | 2019-05-07 03:20:17 +0000 | [diff] [blame] | 71 | } else if (MacroQualifiedTypeLoc MDTL = |
| 72 | TL.getAs<MacroQualifiedTypeLoc>()) { |
| 73 | TL = MDTL.getInnerLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 74 | } else if (ArrayTypeLoc Arr = TL.getAs<ArrayTypeLoc>()) { |
| 75 | TL = Arr.getElementLoc(); |
| 76 | } else if (PointerTypeLoc PT = TL.getAs<PointerTypeLoc>()) { |
| 77 | TL = PT.getPointeeLoc(); |
| 78 | } else if (ReferenceTypeLoc RT = TL.getAs<ReferenceTypeLoc>()) |
| 79 | TL = RT.getPointeeLoc(); |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 80 | else |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 85 | bool handleAttr(AttributedTypeLoc TL, Decl *D = nullptr) { |
Richard Smith | e43e2b3 | 2018-08-20 21:47:29 +0000 | [diff] [blame] | 86 | auto *OwnershipAttr = TL.getAttrAs<ObjCOwnershipAttr>(); |
| 87 | if (!OwnershipAttr) |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 88 | return false; |
| 89 | |
Richard Smith | e43e2b3 | 2018-08-20 21:47:29 +0000 | [diff] [blame] | 90 | SourceLocation Loc = OwnershipAttr->getLocation(); |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 91 | unsigned RawLoc = Loc.getRawEncoding(); |
| 92 | if (MigrateCtx.AttrSet.count(RawLoc)) |
| 93 | return true; |
| 94 | |
| 95 | ASTContext &Ctx = MigrateCtx.Pass.Ctx; |
| 96 | SourceManager &SM = Ctx.getSourceManager(); |
Argyrios Kyrtzidis | e80d4f2 | 2011-11-07 18:40:29 +0000 | [diff] [blame] | 97 | if (Loc.isMacroID()) |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 98 | Loc = SM.getImmediateExpansionRange(Loc).getBegin(); |
Richard Smith | e43e2b3 | 2018-08-20 21:47:29 +0000 | [diff] [blame] | 99 | StringRef Spell = OwnershipAttr->getKind()->getName(); |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 100 | MigrationContext::GCAttrOccurrence::AttrKind Kind; |
| 101 | if (Spell == "strong") |
| 102 | Kind = MigrationContext::GCAttrOccurrence::Strong; |
| 103 | else if (Spell == "weak") |
| 104 | Kind = MigrationContext::GCAttrOccurrence::Weak; |
| 105 | else |
| 106 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 107 | |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 108 | MigrateCtx.AttrSet.insert(RawLoc); |
| 109 | MigrateCtx.GCAttrs.push_back(MigrationContext::GCAttrOccurrence()); |
| 110 | MigrationContext::GCAttrOccurrence &Attr = MigrateCtx.GCAttrs.back(); |
| 111 | |
| 112 | Attr.Kind = Kind; |
Argyrios Kyrtzidis | e80d4f2 | 2011-11-07 18:40:29 +0000 | [diff] [blame] | 113 | Attr.Loc = Loc; |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 114 | Attr.ModifiedType = TL.getModifiedLoc().getType(); |
| 115 | Attr.Dcl = D; |
| 116 | Attr.FullyMigratable = FullyMigratable; |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | bool isMigratable(Decl *D) { |
| 121 | if (isa<TranslationUnitDecl>(D)) |
| 122 | return false; |
| 123 | |
| 124 | if (isInMainFile(D)) |
| 125 | return true; |
| 126 | |
| 127 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 128 | return FD->hasBody(); |
| 129 | |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 130 | if (ObjCContainerDecl *ContD = dyn_cast<ObjCContainerDecl>(D)) |
| 131 | return hasObjCImpl(ContD); |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 132 | |
| 133 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
Aaron Ballman | 2b124d1 | 2014-03-13 16:36:16 +0000 | [diff] [blame] | 134 | for (const auto *MI : RD->methods()) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 135 | if (MI->isOutOfLine()) |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 136 | return true; |
| 137 | } |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | return isMigratable(cast<Decl>(D->getDeclContext())); |
| 142 | } |
| 143 | |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 144 | static bool hasObjCImpl(Decl *D) { |
| 145 | if (!D) |
| 146 | return false; |
| 147 | if (ObjCContainerDecl *ContD = dyn_cast<ObjCContainerDecl>(D)) { |
| 148 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ContD)) |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 149 | return ID->getImplementation() != nullptr; |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 150 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ContD)) |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 151 | return CD->getImplementation() != nullptr; |
Alexander Kornienko | ad98885 | 2015-11-06 01:26:37 +0000 | [diff] [blame] | 152 | return isa<ObjCImplDecl>(ContD); |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 153 | } |
| 154 | return false; |
| 155 | } |
| 156 | |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 157 | bool isInMainFile(Decl *D) { |
| 158 | if (!D) |
| 159 | return false; |
| 160 | |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 161 | for (auto I : D->redecls()) |
David Blaikie | b15de1e | 2012-05-01 00:48:43 +0000 | [diff] [blame] | 162 | if (!isInMainFile(I->getLocation())) |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 163 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 164 | |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 165 | return true; |
| 166 | } |
| 167 | |
| 168 | bool isInMainFile(SourceLocation Loc) { |
| 169 | if (Loc.isInvalid()) |
| 170 | return false; |
| 171 | |
| 172 | SourceManager &SM = MigrateCtx.Pass.Ctx.getSourceManager(); |
| 173 | return SM.isInFileID(SM.getExpansionLoc(Loc), SM.getMainFileID()); |
| 174 | } |
| 175 | }; |
| 176 | |
| 177 | } // anonymous namespace |
| 178 | |
Argyrios Kyrtzidis | f233dac | 2011-11-06 18:58:23 +0000 | [diff] [blame] | 179 | static void errorForGCAttrsOnNonObjC(MigrationContext &MigrateCtx) { |
| 180 | TransformActions &TA = MigrateCtx.Pass.TA; |
| 181 | |
| 182 | for (unsigned i = 0, e = MigrateCtx.GCAttrs.size(); i != e; ++i) { |
| 183 | MigrationContext::GCAttrOccurrence &Attr = MigrateCtx.GCAttrs[i]; |
| 184 | if (Attr.FullyMigratable && Attr.Dcl) { |
| 185 | if (Attr.ModifiedType.isNull()) |
| 186 | continue; |
| 187 | if (!Attr.ModifiedType->isObjCRetainableType()) { |
| 188 | TA.reportError("GC managed memory will become unmanaged in ARC", |
| 189 | Attr.Loc); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
Argyrios Kyrtzidis | e80d4f2 | 2011-11-07 18:40:29 +0000 | [diff] [blame] | 195 | static void checkWeakGCAttrs(MigrationContext &MigrateCtx) { |
| 196 | TransformActions &TA = MigrateCtx.Pass.TA; |
| 197 | |
| 198 | for (unsigned i = 0, e = MigrateCtx.GCAttrs.size(); i != e; ++i) { |
| 199 | MigrationContext::GCAttrOccurrence &Attr = MigrateCtx.GCAttrs[i]; |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 200 | if (Attr.Kind == MigrationContext::GCAttrOccurrence::Weak) { |
Argyrios Kyrtzidis | e80d4f2 | 2011-11-07 18:40:29 +0000 | [diff] [blame] | 201 | if (Attr.ModifiedType.isNull() || |
| 202 | !Attr.ModifiedType->isObjCRetainableType()) |
| 203 | continue; |
| 204 | if (!canApplyWeak(MigrateCtx.Pass.Ctx, Attr.ModifiedType, |
| 205 | /*AllowOnUnknownClass=*/true)) { |
| 206 | Transaction Trans(TA); |
Argyrios Kyrtzidis | 2519a08 | 2011-11-08 02:02:38 +0000 | [diff] [blame] | 207 | if (!MigrateCtx.RemovedAttrSet.count(Attr.Loc.getRawEncoding())) |
| 208 | TA.replaceText(Attr.Loc, "__weak", "__unsafe_unretained"); |
Argyrios Kyrtzidis | e80d4f2 | 2011-11-07 18:40:29 +0000 | [diff] [blame] | 209 | TA.clearDiagnostic(diag::err_arc_weak_no_runtime, |
| 210 | diag::err_arc_unsupported_weak_class, |
| 211 | Attr.Loc); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 217 | typedef llvm::TinyPtrVector<ObjCPropertyDecl *> IndivPropsTy; |
| 218 | |
| 219 | static void checkAllAtProps(MigrationContext &MigrateCtx, |
| 220 | SourceLocation AtLoc, |
| 221 | IndivPropsTy &IndProps) { |
| 222 | if (IndProps.empty()) |
| 223 | return; |
| 224 | |
| 225 | for (IndivPropsTy::iterator |
| 226 | PI = IndProps.begin(), PE = IndProps.end(); PI != PE; ++PI) { |
| 227 | QualType T = (*PI)->getType(); |
| 228 | if (T.isNull() || !T->isObjCRetainableType()) |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | SmallVector<std::pair<AttributedTypeLoc, ObjCPropertyDecl *>, 4> ATLs; |
| 233 | bool hasWeak = false, hasStrong = false; |
Argyrios Kyrtzidis | eca1f36 | 2011-11-28 02:04:36 +0000 | [diff] [blame] | 234 | ObjCPropertyDecl::PropertyAttributeKind |
| 235 | Attrs = ObjCPropertyDecl::OBJC_PR_noattr; |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 236 | for (IndivPropsTy::iterator |
| 237 | PI = IndProps.begin(), PE = IndProps.end(); PI != PE; ++PI) { |
| 238 | ObjCPropertyDecl *PD = *PI; |
Argyrios Kyrtzidis | eca1f36 | 2011-11-28 02:04:36 +0000 | [diff] [blame] | 239 | Attrs = PD->getPropertyAttributesAsWritten(); |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 240 | TypeSourceInfo *TInfo = PD->getTypeSourceInfo(); |
| 241 | if (!TInfo) |
| 242 | return; |
| 243 | TypeLoc TL = TInfo->getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 244 | if (AttributedTypeLoc ATL = |
| 245 | TL.getAs<AttributedTypeLoc>()) { |
| 246 | ATLs.push_back(std::make_pair(ATL, PD)); |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 247 | if (TInfo->getType().getObjCLifetime() == Qualifiers::OCL_Weak) { |
| 248 | hasWeak = true; |
| 249 | } else if (TInfo->getType().getObjCLifetime() == Qualifiers::OCL_Strong) |
| 250 | hasStrong = true; |
| 251 | else |
| 252 | return; |
| 253 | } |
| 254 | } |
| 255 | if (ATLs.empty()) |
| 256 | return; |
| 257 | if (hasWeak && hasStrong) |
| 258 | return; |
| 259 | |
| 260 | TransformActions &TA = MigrateCtx.Pass.TA; |
| 261 | Transaction Trans(TA); |
| 262 | |
| 263 | if (GCAttrsCollector::hasObjCImpl( |
| 264 | cast<Decl>(IndProps.front()->getDeclContext()))) { |
| 265 | if (hasWeak) |
| 266 | MigrateCtx.AtPropsWeak.insert(AtLoc.getRawEncoding()); |
| 267 | |
| 268 | } else { |
| 269 | StringRef toAttr = "strong"; |
| 270 | if (hasWeak) { |
| 271 | if (canApplyWeak(MigrateCtx.Pass.Ctx, IndProps.front()->getType(), |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 272 | /*AllowOnUnknownClass=*/true)) |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 273 | toAttr = "weak"; |
| 274 | else |
| 275 | toAttr = "unsafe_unretained"; |
| 276 | } |
Argyrios Kyrtzidis | eca1f36 | 2011-11-28 02:04:36 +0000 | [diff] [blame] | 277 | if (Attrs & ObjCPropertyDecl::OBJC_PR_assign) |
| 278 | MigrateCtx.rewritePropertyAttribute("assign", toAttr, AtLoc); |
| 279 | else |
| 280 | MigrateCtx.addPropertyAttribute(toAttr, AtLoc); |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | for (unsigned i = 0, e = ATLs.size(); i != e; ++i) { |
Richard Smith | e43e2b3 | 2018-08-20 21:47:29 +0000 | [diff] [blame] | 284 | SourceLocation Loc = ATLs[i].first.getAttr()->getLocation(); |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 285 | if (Loc.isMacroID()) |
| 286 | Loc = MigrateCtx.Pass.Ctx.getSourceManager() |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 287 | .getImmediateExpansionRange(Loc) |
| 288 | .getBegin(); |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 289 | TA.remove(Loc); |
| 290 | TA.clearDiagnostic(diag::err_objc_property_attr_mutually_exclusive, AtLoc); |
| 291 | TA.clearDiagnostic(diag::err_arc_inconsistent_property_ownership, |
| 292 | ATLs[i].second->getLocation()); |
Argyrios Kyrtzidis | 2519a08 | 2011-11-08 02:02:38 +0000 | [diff] [blame] | 293 | MigrateCtx.RemovedAttrSet.insert(Loc.getRawEncoding()); |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
| 297 | static void checkAllProps(MigrationContext &MigrateCtx, |
| 298 | std::vector<ObjCPropertyDecl *> &AllProps) { |
| 299 | typedef llvm::TinyPtrVector<ObjCPropertyDecl *> IndivPropsTy; |
| 300 | llvm::DenseMap<unsigned, IndivPropsTy> AtProps; |
| 301 | |
| 302 | for (unsigned i = 0, e = AllProps.size(); i != e; ++i) { |
| 303 | ObjCPropertyDecl *PD = AllProps[i]; |
| 304 | if (PD->getPropertyAttributesAsWritten() & |
Argyrios Kyrtzidis | 3fc3dcd | 2011-11-28 00:23:12 +0000 | [diff] [blame] | 305 | (ObjCPropertyDecl::OBJC_PR_assign | |
| 306 | ObjCPropertyDecl::OBJC_PR_readonly)) { |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 307 | SourceLocation AtLoc = PD->getAtLoc(); |
| 308 | if (AtLoc.isInvalid()) |
| 309 | continue; |
| 310 | unsigned RawAt = AtLoc.getRawEncoding(); |
| 311 | AtProps[RawAt].push_back(PD); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | for (llvm::DenseMap<unsigned, IndivPropsTy>::iterator |
| 316 | I = AtProps.begin(), E = AtProps.end(); I != E; ++I) { |
| 317 | SourceLocation AtLoc = SourceLocation::getFromRawEncoding(I->first); |
| 318 | IndivPropsTy &IndProps = I->second; |
| 319 | checkAllAtProps(MigrateCtx, AtLoc, IndProps); |
| 320 | } |
| 321 | } |
| 322 | |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 323 | void GCAttrsTraverser::traverseTU(MigrationContext &MigrateCtx) { |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 324 | std::vector<ObjCPropertyDecl *> AllProps; |
| 325 | GCAttrsCollector(MigrateCtx, AllProps).TraverseDecl( |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 326 | MigrateCtx.Pass.Ctx.getTranslationUnitDecl()); |
Argyrios Kyrtzidis | f233dac | 2011-11-06 18:58:23 +0000 | [diff] [blame] | 327 | |
Argyrios Kyrtzidis | f233dac | 2011-11-06 18:58:23 +0000 | [diff] [blame] | 328 | errorForGCAttrsOnNonObjC(MigrateCtx); |
Argyrios Kyrtzidis | 722d21c | 2011-11-07 18:46:46 +0000 | [diff] [blame] | 329 | checkAllProps(MigrateCtx, AllProps); |
Argyrios Kyrtzidis | 2519a08 | 2011-11-08 02:02:38 +0000 | [diff] [blame] | 330 | checkWeakGCAttrs(MigrateCtx); |
Argyrios Kyrtzidis | 0c233fa | 2011-11-06 18:58:17 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | void MigrationContext::dumpGCAttrs() { |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 334 | llvm::errs() << "\n################\n"; |
Argyrios Kyrtzidis | 0c233fa | 2011-11-06 18:58:17 +0000 | [diff] [blame] | 335 | for (unsigned i = 0, e = GCAttrs.size(); i != e; ++i) { |
| 336 | GCAttrOccurrence &Attr = GCAttrs[i]; |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 337 | llvm::errs() << "KIND: " |
Argyrios Kyrtzidis | 0c233fa | 2011-11-06 18:58:17 +0000 | [diff] [blame] | 338 | << (Attr.Kind == GCAttrOccurrence::Strong ? "strong" : "weak"); |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 339 | llvm::errs() << "\nLOC: "; |
Stephen Kelly | 3124ce7 | 2018-08-15 20:32:06 +0000 | [diff] [blame] | 340 | Attr.Loc.print(llvm::errs(), Pass.Ctx.getSourceManager()); |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 341 | llvm::errs() << "\nTYPE: "; |
| 342 | Attr.ModifiedType.dump(); |
| 343 | if (Attr.Dcl) { |
| 344 | llvm::errs() << "DECL:\n"; |
| 345 | Attr.Dcl->dump(); |
| 346 | } else { |
| 347 | llvm::errs() << "DECL: NONE"; |
| 348 | } |
| 349 | llvm::errs() << "\nMIGRATABLE: " << Attr.FullyMigratable; |
| 350 | llvm::errs() << "\n----------------\n"; |
| 351 | } |
| 352 | llvm::errs() << "\n################\n"; |
Argyrios Kyrtzidis | e43ae79 | 2011-11-06 18:58:03 +0000 | [diff] [blame] | 353 | } |