John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 1 | //===--- ARCMT.cpp - Migration to ARC mode --------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "Internals.h" |
| 11 | #include "clang/Frontend/ASTUnit.h" |
| 12 | #include "clang/Frontend/CompilerInstance.h" |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 13 | #include "clang/Frontend/FrontendAction.h" |
Argyrios Kyrtzidis | d571363 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/Utils.h" |
| 16 | #include "clang/AST/ASTConsumer.h" |
| 17 | #include "clang/Rewrite/Rewriter.h" |
| 18 | #include "clang/Sema/SemaDiagnostic.h" |
| 19 | #include "clang/Basic/DiagnosticCategories.h" |
| 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | #include "llvm/Support/MemoryBuffer.h" |
| 22 | #include "llvm/ADT/Triple.h" |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | using namespace arcmt; |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 54b1677 | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 26 | bool CapturedDiagList::clearDiagnostic(ArrayRef<unsigned> IDs, |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 27 | SourceRange range) { |
| 28 | if (range.isInvalid()) |
| 29 | return false; |
| 30 | |
| 31 | bool cleared = false; |
| 32 | ListTy::iterator I = List.begin(); |
| 33 | while (I != List.end()) { |
| 34 | FullSourceLoc diagLoc = I->getLocation(); |
| 35 | if ((IDs.empty() || // empty means clear all diagnostics in the range. |
| 36 | std::find(IDs.begin(), IDs.end(), I->getID()) != IDs.end()) && |
| 37 | !diagLoc.isBeforeInTranslationUnitThan(range.getBegin()) && |
| 38 | (diagLoc == range.getEnd() || |
| 39 | diagLoc.isBeforeInTranslationUnitThan(range.getEnd()))) { |
| 40 | cleared = true; |
| 41 | ListTy::iterator eraseS = I++; |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 42 | while (I != List.end() && I->getLevel() == DiagnosticsEngine::Note) |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 43 | ++I; |
| 44 | // Clear the diagnostic and any notes following it. |
| 45 | List.erase(eraseS, I); |
| 46 | continue; |
| 47 | } |
| 48 | |
| 49 | ++I; |
| 50 | } |
| 51 | |
| 52 | return cleared; |
| 53 | } |
| 54 | |
Chris Lattner | 54b1677 | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 55 | bool CapturedDiagList::hasDiagnostic(ArrayRef<unsigned> IDs, |
Argyrios Kyrtzidis | 0f3f9f7 | 2011-06-18 00:53:34 +0000 | [diff] [blame] | 56 | SourceRange range) const { |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 57 | if (range.isInvalid()) |
| 58 | return false; |
| 59 | |
Argyrios Kyrtzidis | 0f3f9f7 | 2011-06-18 00:53:34 +0000 | [diff] [blame] | 60 | ListTy::const_iterator I = List.begin(); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 61 | while (I != List.end()) { |
| 62 | FullSourceLoc diagLoc = I->getLocation(); |
| 63 | if ((IDs.empty() || // empty means any diagnostic in the range. |
| 64 | std::find(IDs.begin(), IDs.end(), I->getID()) != IDs.end()) && |
| 65 | !diagLoc.isBeforeInTranslationUnitThan(range.getBegin()) && |
| 66 | (diagLoc == range.getEnd() || |
| 67 | diagLoc.isBeforeInTranslationUnitThan(range.getEnd()))) { |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | ++I; |
| 72 | } |
| 73 | |
| 74 | return false; |
| 75 | } |
| 76 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 77 | void CapturedDiagList::reportDiagnostics(DiagnosticsEngine &Diags) const { |
Argyrios Kyrtzidis | 0f3f9f7 | 2011-06-18 00:53:34 +0000 | [diff] [blame] | 78 | for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I) |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 79 | Diags.Report(*I); |
| 80 | } |
| 81 | |
Argyrios Kyrtzidis | 90b6a2a | 2011-06-18 00:53:41 +0000 | [diff] [blame] | 82 | bool CapturedDiagList::hasErrors() const { |
| 83 | for (ListTy::const_iterator I = List.begin(), E = List.end(); I != E; ++I) |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 84 | if (I->getLevel() >= DiagnosticsEngine::Error) |
Argyrios Kyrtzidis | 90b6a2a | 2011-06-18 00:53:41 +0000 | [diff] [blame] | 85 | return true; |
| 86 | |
| 87 | return false; |
| 88 | } |
| 89 | |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 90 | namespace { |
| 91 | |
David Blaikie | a24a0bc | 2011-09-25 23:54:33 +0000 | [diff] [blame] | 92 | class CaptureDiagnosticConsumer : public DiagnosticConsumer { |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 93 | DiagnosticsEngine &Diags; |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 94 | CapturedDiagList &CapturedDiags; |
| 95 | public: |
David Blaikie | a24a0bc | 2011-09-25 23:54:33 +0000 | [diff] [blame] | 96 | CaptureDiagnosticConsumer(DiagnosticsEngine &diags, |
Douglas Gregor | d0e9e3a | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 97 | CapturedDiagList &capturedDiags) |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 98 | : Diags(diags), CapturedDiags(capturedDiags) { } |
| 99 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 100 | virtual void HandleDiagnostic(DiagnosticsEngine::Level level, |
David Blaikie | b578432 | 2011-09-26 01:18:08 +0000 | [diff] [blame] | 101 | const Diagnostic &Info) { |
Ted Kremenek | 337c5b8 | 2011-10-20 05:07:47 +0000 | [diff] [blame] | 102 | if (DiagnosticIDs::isARCDiagnostic(Info.getID()) || |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 103 | level >= DiagnosticsEngine::Error || level == DiagnosticsEngine::Note) { |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 104 | CapturedDiags.push_back(StoredDiagnostic(level, Info)); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | // Non-ARC warnings are ignored. |
| 109 | Diags.setLastDiagnosticIgnored(); |
| 110 | } |
Douglas Gregor | d0e9e3a | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 111 | |
| 112 | DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const { |
| 113 | // Just drop any diagnostics that come from cloned consumers; they'll |
| 114 | // have different source managers anyway. |
| 115 | return new IgnoringDiagConsumer(); |
| 116 | } |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | } // end anonymous namespace |
| 120 | |
Argyrios Kyrtzidis | 697729a | 2011-10-18 00:22:49 +0000 | [diff] [blame] | 121 | static inline StringRef SimulatorVersionDefineName() { |
| 122 | return "__IPHONE_OS_VERSION_MIN_REQUIRED="; |
| 123 | } |
| 124 | |
| 125 | /// \brief Parse the simulator version define: |
| 126 | /// __IPHONE_OS_VERSION_MIN_REQUIRED=([0-9])([0-9][0-9])([0-9][0-9]) |
| 127 | // and return the grouped values as integers, e.g: |
| 128 | // __IPHONE_OS_VERSION_MIN_REQUIRED=40201 |
| 129 | // will return Major=4, Minor=2, Micro=1. |
| 130 | static bool GetVersionFromSimulatorDefine(StringRef define, |
| 131 | unsigned &Major, unsigned &Minor, |
| 132 | unsigned &Micro) { |
| 133 | assert(define.startswith(SimulatorVersionDefineName())); |
| 134 | StringRef name, version; |
| 135 | llvm::tie(name, version) = define.split('='); |
| 136 | if (version.empty()) |
| 137 | return false; |
| 138 | std::string verstr = version.str(); |
| 139 | char *end; |
| 140 | unsigned num = (unsigned) strtol(verstr.c_str(), &end, 10); |
| 141 | if (*end != '\0') |
| 142 | return false; |
| 143 | Major = num / 10000; |
| 144 | num = num % 10000; |
| 145 | Minor = num / 100; |
| 146 | Micro = num % 100; |
| 147 | return true; |
| 148 | } |
| 149 | |
Argyrios Kyrtzidis | 81a3590 | 2011-06-20 19:59:52 +0000 | [diff] [blame] | 150 | static bool HasARCRuntime(CompilerInvocation &origCI) { |
| 151 | // This duplicates some functionality from Darwin::AddDeploymentTarget |
| 152 | // but this function is well defined, so keep it decoupled from the driver |
| 153 | // and avoid unrelated complications. |
| 154 | |
Argyrios Kyrtzidis | 697729a | 2011-10-18 00:22:49 +0000 | [diff] [blame] | 155 | for (unsigned i = 0, e = origCI.getPreprocessorOpts().Macros.size(); |
| 156 | i != e; ++i) { |
| 157 | StringRef define = origCI.getPreprocessorOpts().Macros[i].first; |
| 158 | bool isUndef = origCI.getPreprocessorOpts().Macros[i].second; |
| 159 | if (isUndef) |
| 160 | continue; |
| 161 | if (!define.startswith(SimulatorVersionDefineName())) |
| 162 | continue; |
| 163 | unsigned Major = 0, Minor = 0, Micro = 0; |
| 164 | if (GetVersionFromSimulatorDefine(define, Major, Minor, Micro) && |
| 165 | Major < 10 && Minor < 100 && Micro < 100) |
| 166 | return Major >= 5; |
| 167 | } |
| 168 | |
Argyrios Kyrtzidis | 81a3590 | 2011-06-20 19:59:52 +0000 | [diff] [blame] | 169 | llvm::Triple triple(origCI.getTargetOpts().Triple); |
| 170 | |
| 171 | if (triple.getOS() == llvm::Triple::IOS) |
| 172 | return triple.getOSMajorVersion() >= 5; |
| 173 | |
| 174 | if (triple.getOS() == llvm::Triple::Darwin) |
| 175 | return triple.getOSMajorVersion() >= 11; |
| 176 | |
| 177 | if (triple.getOS() == llvm::Triple::MacOSX) { |
| 178 | unsigned Major, Minor, Micro; |
| 179 | triple.getOSVersion(Major, Minor, Micro); |
| 180 | return Major > 10 || (Major == 10 && Minor >= 7); |
| 181 | } |
| 182 | |
| 183 | return false; |
| 184 | } |
| 185 | |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 186 | static CompilerInvocation * |
| 187 | createInvocationForMigration(CompilerInvocation &origCI) { |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 188 | llvm::OwningPtr<CompilerInvocation> CInvok; |
| 189 | CInvok.reset(new CompilerInvocation(origCI)); |
| 190 | CInvok->getPreprocessorOpts().ImplicitPCHInclude = std::string(); |
| 191 | CInvok->getPreprocessorOpts().ImplicitPTHInclude = std::string(); |
| 192 | std::string define = getARCMTMacroName(); |
| 193 | define += '='; |
| 194 | CInvok->getPreprocessorOpts().addMacroDef(define); |
Ted Kremenek | 8cf47df | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 195 | CInvok->getLangOpts()->ObjCAutoRefCount = true; |
| 196 | CInvok->getLangOpts()->setGC(LangOptions::NonGC); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 197 | CInvok->getDiagnosticOpts().ErrorLimit = 0; |
Argyrios Kyrtzidis | 59b5f35 | 2011-06-22 18:03:59 +0000 | [diff] [blame] | 198 | CInvok->getDiagnosticOpts().Warnings.push_back( |
| 199 | "error=arc-unsafe-retained-assign"); |
Ted Kremenek | 8cf47df | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 200 | CInvok->getLangOpts()->ObjCRuntimeHasWeak = HasARCRuntime(origCI); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 201 | |
| 202 | return CInvok.take(); |
| 203 | } |
| 204 | |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 205 | static void emitPremigrationErrors(const CapturedDiagList &arcDiags, |
| 206 | const DiagnosticOptions &diagOpts, |
| 207 | Preprocessor &PP) { |
Argyrios Kyrtzidis | d571363 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 208 | TextDiagnosticPrinter printer(llvm::errs(), diagOpts); |
| 209 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 210 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
| 211 | new DiagnosticsEngine(DiagID, &printer, /*ShouldOwnClient=*/false)); |
Argyrios Kyrtzidis | d571363 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 212 | Diags->setSourceManager(&PP.getSourceManager()); |
| 213 | |
| 214 | printer.BeginSourceFile(PP.getLangOptions(), &PP); |
| 215 | arcDiags.reportDiagnostics(*Diags); |
| 216 | printer.EndSourceFile(); |
| 217 | } |
| 218 | |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 219 | //===----------------------------------------------------------------------===// |
| 220 | // checkForManualIssues. |
| 221 | //===----------------------------------------------------------------------===// |
| 222 | |
| 223 | bool arcmt::checkForManualIssues(CompilerInvocation &origCI, |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 224 | const FrontendInputFile &Input, |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 225 | DiagnosticConsumer *DiagClient, |
Argyrios Kyrtzidis | d571363 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 226 | bool emitPremigrationARCErrors, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 227 | StringRef plistOut) { |
Ted Kremenek | 8cf47df | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 228 | if (!origCI.getLangOpts()->ObjC1) |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 229 | return false; |
| 230 | |
Ted Kremenek | 8cf47df | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 231 | LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC(); |
Fariborz Jahanian | aa7b9aa | 2012-01-25 00:20:29 +0000 | [diff] [blame] | 232 | bool NoNSAllocReallocError = origCI.getMigratorOpts().NoNSAllocReallocError; |
Fariborz Jahanian | 0c859d6 | 2012-01-26 00:08:04 +0000 | [diff] [blame] | 233 | bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval; |
Argyrios Kyrtzidis | d208ef9 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 234 | |
| 235 | std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 236 | assert(!transforms.empty()); |
| 237 | |
| 238 | llvm::OwningPtr<CompilerInvocation> CInvok; |
| 239 | CInvok.reset(createInvocationForMigration(origCI)); |
| 240 | CInvok->getFrontendOpts().Inputs.clear(); |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 241 | CInvok->getFrontendOpts().Inputs.push_back(Input); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 242 | |
| 243 | CapturedDiagList capturedDiags; |
| 244 | |
| 245 | assert(DiagClient); |
| 246 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 247 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
| 248 | new DiagnosticsEngine(DiagID, DiagClient, /*ShouldOwnClient=*/false)); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 249 | |
| 250 | // Filter of all diagnostics. |
David Blaikie | a24a0bc | 2011-09-25 23:54:33 +0000 | [diff] [blame] | 251 | CaptureDiagnosticConsumer errRec(*Diags, capturedDiags); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 252 | Diags->setClient(&errRec, /*ShouldOwnClient=*/false); |
| 253 | |
| 254 | llvm::OwningPtr<ASTUnit> Unit( |
| 255 | ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags)); |
| 256 | if (!Unit) |
| 257 | return true; |
| 258 | |
| 259 | // Don't filter diagnostics anymore. |
| 260 | Diags->setClient(DiagClient, /*ShouldOwnClient=*/false); |
| 261 | |
| 262 | ASTContext &Ctx = Unit->getASTContext(); |
| 263 | |
| 264 | if (Diags->hasFatalErrorOccurred()) { |
| 265 | Diags->Reset(); |
| 266 | DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor()); |
| 267 | capturedDiags.reportDiagnostics(*Diags); |
| 268 | DiagClient->EndSourceFile(); |
| 269 | return true; |
| 270 | } |
| 271 | |
Argyrios Kyrtzidis | d571363 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 272 | if (emitPremigrationARCErrors) |
| 273 | emitPremigrationErrors(capturedDiags, origCI.getDiagnosticOpts(), |
| 274 | Unit->getPreprocessor()); |
| 275 | if (!plistOut.empty()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 276 | SmallVector<StoredDiagnostic, 8> arcDiags; |
Argyrios Kyrtzidis | d571363 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 277 | for (CapturedDiagList::iterator |
| 278 | I = capturedDiags.begin(), E = capturedDiags.end(); I != E; ++I) |
| 279 | arcDiags.push_back(*I); |
| 280 | writeARCDiagsToPlist(plistOut, arcDiags, |
| 281 | Ctx.getSourceManager(), Ctx.getLangOptions()); |
| 282 | } |
| 283 | |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 284 | // After parsing of source files ended, we want to reuse the |
| 285 | // diagnostics objects to emit further diagnostics. |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 286 | // We call BeginSourceFile because DiagnosticConsumer requires that |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 287 | // diagnostics with source range information are emitted only in between |
| 288 | // BeginSourceFile() and EndSourceFile(). |
| 289 | DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor()); |
| 290 | |
| 291 | // No macros will be added since we are just checking and we won't modify |
| 292 | // source code. |
| 293 | std::vector<SourceLocation> ARCMTMacroLocs; |
| 294 | |
| 295 | TransformActions testAct(*Diags, capturedDiags, Ctx, Unit->getPreprocessor()); |
Argyrios Kyrtzidis | d208ef9 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 296 | MigrationPass pass(Ctx, OrigGCMode, Unit->getSema(), testAct, ARCMTMacroLocs); |
Fariborz Jahanian | aa7b9aa | 2012-01-25 00:20:29 +0000 | [diff] [blame] | 297 | pass.setNSAllocReallocError(NoNSAllocReallocError); |
Fariborz Jahanian | 0c859d6 | 2012-01-26 00:08:04 +0000 | [diff] [blame] | 298 | pass.setNoFinalizeRemoval(NoFinalizeRemoval); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 299 | |
| 300 | for (unsigned i=0, e = transforms.size(); i != e; ++i) |
| 301 | transforms[i](pass); |
| 302 | |
| 303 | capturedDiags.reportDiagnostics(*Diags); |
| 304 | |
| 305 | DiagClient->EndSourceFile(); |
| 306 | |
Argyrios Kyrtzidis | e2e40b4 | 2011-07-14 00:17:54 +0000 | [diff] [blame] | 307 | // If we are migrating code that gets the '-fobjc-arc' flag, make sure |
| 308 | // to remove it so that we don't get errors from normal compilation. |
Ted Kremenek | 8cf47df | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 309 | origCI.getLangOpts()->ObjCAutoRefCount = false; |
Argyrios Kyrtzidis | e2e40b4 | 2011-07-14 00:17:54 +0000 | [diff] [blame] | 310 | |
Argyrios Kyrtzidis | 73a0d32 | 2011-07-18 07:44:45 +0000 | [diff] [blame] | 311 | return capturedDiags.hasErrors() || testAct.hasReportedErrors(); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | //===----------------------------------------------------------------------===// |
| 315 | // applyTransformations. |
| 316 | //===----------------------------------------------------------------------===// |
| 317 | |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 318 | static bool applyTransforms(CompilerInvocation &origCI, |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 319 | const FrontendInputFile &Input, |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 320 | DiagnosticConsumer *DiagClient, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 321 | StringRef outputDir, |
Argyrios Kyrtzidis | d571363 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 322 | bool emitPremigrationARCErrors, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 323 | StringRef plistOut) { |
Ted Kremenek | 8cf47df | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 324 | if (!origCI.getLangOpts()->ObjC1) |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 325 | return false; |
| 326 | |
Ted Kremenek | 8cf47df | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 327 | LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC(); |
Argyrios Kyrtzidis | d208ef9 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 328 | |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 329 | // Make sure checking is successful first. |
| 330 | CompilerInvocation CInvokForCheck(origCI); |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 331 | if (arcmt::checkForManualIssues(CInvokForCheck, Input, DiagClient, |
Argyrios Kyrtzidis | d571363 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 332 | emitPremigrationARCErrors, plistOut)) |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 333 | return true; |
| 334 | |
| 335 | CompilerInvocation CInvok(origCI); |
| 336 | CInvok.getFrontendOpts().Inputs.clear(); |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 337 | CInvok.getFrontendOpts().Inputs.push_back(Input); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 338 | |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 339 | MigrationProcess migration(CInvok, DiagClient, outputDir); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 340 | |
Argyrios Kyrtzidis | d208ef9 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 341 | std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 342 | assert(!transforms.empty()); |
| 343 | |
| 344 | for (unsigned i=0, e = transforms.size(); i != e; ++i) { |
| 345 | bool err = migration.applyTransform(transforms[i]); |
| 346 | if (err) return true; |
| 347 | } |
| 348 | |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 349 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 350 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
| 351 | new DiagnosticsEngine(DiagID, DiagClient, /*ShouldOwnClient=*/false)); |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 352 | |
| 353 | if (outputDir.empty()) { |
Ted Kremenek | 8cf47df | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 354 | origCI.getLangOpts()->ObjCAutoRefCount = true; |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 355 | return migration.getRemapper().overwriteOriginal(*Diags); |
Argyrios Kyrtzidis | e2e40b4 | 2011-07-14 00:17:54 +0000 | [diff] [blame] | 356 | } else { |
| 357 | // If we are migrating code that gets the '-fobjc-arc' flag, make sure |
| 358 | // to remove it so that we don't get errors from normal compilation. |
Ted Kremenek | 8cf47df | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 359 | origCI.getLangOpts()->ObjCAutoRefCount = false; |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 360 | return migration.getRemapper().flushToDisk(outputDir, *Diags); |
Argyrios Kyrtzidis | e2e40b4 | 2011-07-14 00:17:54 +0000 | [diff] [blame] | 361 | } |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | bool arcmt::applyTransformations(CompilerInvocation &origCI, |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 365 | const FrontendInputFile &Input, |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 366 | DiagnosticConsumer *DiagClient) { |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 367 | return applyTransforms(origCI, Input, DiagClient, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 368 | StringRef(), false, StringRef()); |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | bool arcmt::migrateWithTemporaryFiles(CompilerInvocation &origCI, |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 372 | const FrontendInputFile &Input, |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 373 | DiagnosticConsumer *DiagClient, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 374 | StringRef outputDir, |
Argyrios Kyrtzidis | d571363 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 375 | bool emitPremigrationARCErrors, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 376 | StringRef plistOut) { |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 377 | assert(!outputDir.empty() && "Expected output directory path"); |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 378 | return applyTransforms(origCI, Input, DiagClient, |
Argyrios Kyrtzidis | d571363 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 379 | outputDir, emitPremigrationARCErrors, plistOut); |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | bool arcmt::getFileRemappings(std::vector<std::pair<std::string,std::string> > & |
| 383 | remap, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 384 | StringRef outputDir, |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 385 | DiagnosticConsumer *DiagClient) { |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 386 | assert(!outputDir.empty()); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 387 | |
| 388 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 389 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
| 390 | new DiagnosticsEngine(DiagID, DiagClient, /*ShouldOwnClient=*/false)); |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 391 | |
| 392 | FileRemapper remapper; |
| 393 | bool err = remapper.initFromDisk(outputDir, *Diags, |
| 394 | /*ignoreIfFilesChanged=*/true); |
| 395 | if (err) |
| 396 | return true; |
| 397 | |
| 398 | CompilerInvocation CI; |
| 399 | remapper.applyMappings(CI); |
| 400 | remap = CI.getPreprocessorOpts().RemappedFiles; |
| 401 | |
| 402 | return false; |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | //===----------------------------------------------------------------------===// |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 406 | // CollectTransformActions. |
| 407 | //===----------------------------------------------------------------------===// |
| 408 | |
| 409 | namespace { |
| 410 | |
| 411 | class ARCMTMacroTrackerPPCallbacks : public PPCallbacks { |
| 412 | std::vector<SourceLocation> &ARCMTMacroLocs; |
| 413 | |
| 414 | public: |
| 415 | ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs) |
| 416 | : ARCMTMacroLocs(ARCMTMacroLocs) { } |
| 417 | |
Argyrios Kyrtzidis | 85a14bb | 2011-08-18 01:05:45 +0000 | [diff] [blame] | 418 | virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo *MI, |
| 419 | SourceRange Range) { |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 420 | if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName()) |
| 421 | ARCMTMacroLocs.push_back(MacroNameTok.getLocation()); |
| 422 | } |
| 423 | }; |
| 424 | |
| 425 | class ARCMTMacroTrackerAction : public ASTFrontendAction { |
| 426 | std::vector<SourceLocation> &ARCMTMacroLocs; |
| 427 | |
| 428 | public: |
| 429 | ARCMTMacroTrackerAction(std::vector<SourceLocation> &ARCMTMacroLocs) |
| 430 | : ARCMTMacroLocs(ARCMTMacroLocs) { } |
| 431 | |
| 432 | virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 433 | StringRef InFile) { |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 434 | CI.getPreprocessor().addPPCallbacks( |
| 435 | new ARCMTMacroTrackerPPCallbacks(ARCMTMacroLocs)); |
| 436 | return new ASTConsumer(); |
| 437 | } |
| 438 | }; |
| 439 | |
| 440 | class RewritesApplicator : public TransformActions::RewriteReceiver { |
| 441 | Rewriter &rewriter; |
| 442 | ASTContext &Ctx; |
| 443 | MigrationProcess::RewriteListener *Listener; |
| 444 | |
| 445 | public: |
| 446 | RewritesApplicator(Rewriter &rewriter, ASTContext &ctx, |
| 447 | MigrationProcess::RewriteListener *listener) |
| 448 | : rewriter(rewriter), Ctx(ctx), Listener(listener) { |
| 449 | if (Listener) |
| 450 | Listener->start(ctx); |
| 451 | } |
| 452 | ~RewritesApplicator() { |
| 453 | if (Listener) |
| 454 | Listener->finish(); |
| 455 | } |
| 456 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 457 | virtual void insert(SourceLocation loc, StringRef text) { |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 458 | bool err = rewriter.InsertText(loc, text, /*InsertAfter=*/true, |
| 459 | /*indentNewLines=*/true); |
| 460 | if (!err && Listener) |
| 461 | Listener->insert(loc, text); |
| 462 | } |
| 463 | |
| 464 | virtual void remove(CharSourceRange range) { |
| 465 | Rewriter::RewriteOptions removeOpts; |
| 466 | removeOpts.IncludeInsertsAtBeginOfRange = false; |
| 467 | removeOpts.IncludeInsertsAtEndOfRange = false; |
| 468 | removeOpts.RemoveLineIfEmpty = true; |
| 469 | |
| 470 | bool err = rewriter.RemoveText(range, removeOpts); |
| 471 | if (!err && Listener) |
| 472 | Listener->remove(range); |
| 473 | } |
| 474 | |
| 475 | virtual void increaseIndentation(CharSourceRange range, |
| 476 | SourceLocation parentIndent) { |
| 477 | rewriter.IncreaseIndentation(range, parentIndent); |
| 478 | } |
| 479 | }; |
| 480 | |
| 481 | } // end anonymous namespace. |
| 482 | |
| 483 | /// \brief Anchor for VTable. |
| 484 | MigrationProcess::RewriteListener::~RewriteListener() { } |
| 485 | |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 486 | MigrationProcess::MigrationProcess(const CompilerInvocation &CI, |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 487 | DiagnosticConsumer *diagClient, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 488 | StringRef outputDir) |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 489 | : OrigCI(CI), DiagClient(diagClient) { |
| 490 | if (!outputDir.empty()) { |
| 491 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 492 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
| 493 | new DiagnosticsEngine(DiagID, DiagClient, /*ShouldOwnClient=*/false)); |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 494 | Remapper.initFromDisk(outputDir, *Diags, /*ignoreIfFilesChanges=*/true); |
| 495 | } |
| 496 | } |
| 497 | |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 498 | bool MigrationProcess::applyTransform(TransformFn trans, |
| 499 | RewriteListener *listener) { |
| 500 | llvm::OwningPtr<CompilerInvocation> CInvok; |
| 501 | CInvok.reset(createInvocationForMigration(OrigCI)); |
| 502 | CInvok->getDiagnosticOpts().IgnoreWarnings = true; |
| 503 | |
| 504 | Remapper.applyMappings(*CInvok); |
| 505 | |
| 506 | CapturedDiagList capturedDiags; |
| 507 | std::vector<SourceLocation> ARCMTMacroLocs; |
| 508 | |
| 509 | assert(DiagClient); |
| 510 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 511 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
| 512 | new DiagnosticsEngine(DiagID, DiagClient, /*ShouldOwnClient=*/false)); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 513 | |
| 514 | // Filter of all diagnostics. |
David Blaikie | a24a0bc | 2011-09-25 23:54:33 +0000 | [diff] [blame] | 515 | CaptureDiagnosticConsumer errRec(*Diags, capturedDiags); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 516 | Diags->setClient(&errRec, /*ShouldOwnClient=*/false); |
| 517 | |
| 518 | llvm::OwningPtr<ARCMTMacroTrackerAction> ASTAction; |
| 519 | ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs)); |
| 520 | |
| 521 | llvm::OwningPtr<ASTUnit> Unit( |
| 522 | ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags, |
| 523 | ASTAction.get())); |
| 524 | if (!Unit) |
| 525 | return true; |
| 526 | Unit->setOwnsRemappedFileBuffers(false); // FileRemapper manages that. |
| 527 | |
| 528 | // Don't filter diagnostics anymore. |
| 529 | Diags->setClient(DiagClient, /*ShouldOwnClient=*/false); |
| 530 | |
| 531 | ASTContext &Ctx = Unit->getASTContext(); |
| 532 | |
| 533 | if (Diags->hasFatalErrorOccurred()) { |
| 534 | Diags->Reset(); |
| 535 | DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor()); |
| 536 | capturedDiags.reportDiagnostics(*Diags); |
| 537 | DiagClient->EndSourceFile(); |
| 538 | return true; |
| 539 | } |
| 540 | |
| 541 | // After parsing of source files ended, we want to reuse the |
| 542 | // diagnostics objects to emit further diagnostics. |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 543 | // We call BeginSourceFile because DiagnosticConsumer requires that |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 544 | // diagnostics with source range information are emitted only in between |
| 545 | // BeginSourceFile() and EndSourceFile(). |
| 546 | DiagClient->BeginSourceFile(Ctx.getLangOptions(), &Unit->getPreprocessor()); |
| 547 | |
| 548 | Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOptions()); |
| 549 | TransformActions TA(*Diags, capturedDiags, Ctx, Unit->getPreprocessor()); |
Ted Kremenek | 8cf47df | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 550 | MigrationPass pass(Ctx, OrigCI.getLangOpts()->getGC(), |
Argyrios Kyrtzidis | d208ef9 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 551 | Unit->getSema(), TA, ARCMTMacroLocs); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 552 | |
| 553 | trans(pass); |
| 554 | |
| 555 | { |
| 556 | RewritesApplicator applicator(rewriter, Ctx, listener); |
| 557 | TA.applyRewrites(applicator); |
| 558 | } |
| 559 | |
| 560 | DiagClient->EndSourceFile(); |
| 561 | |
| 562 | if (DiagClient->getNumErrors()) |
| 563 | return true; |
| 564 | |
| 565 | for (Rewriter::buffer_iterator |
| 566 | I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) { |
| 567 | FileID FID = I->first; |
| 568 | RewriteBuffer &buf = I->second; |
| 569 | const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID); |
| 570 | assert(file); |
| 571 | std::string newFname = file->getName(); |
| 572 | newFname += "-trans"; |
| 573 | llvm::SmallString<512> newText; |
| 574 | llvm::raw_svector_ostream vecOS(newText); |
| 575 | buf.write(vecOS); |
| 576 | vecOS.flush(); |
| 577 | llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy( |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 578 | StringRef(newText.data(), newText.size()), newFname); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 579 | llvm::SmallString<64> filePath(file->getName()); |
| 580 | Unit->getFileManager().FixupRelativePath(filePath); |
| 581 | Remapper.remap(filePath.str(), memBuf); |
| 582 | } |
| 583 | |
| 584 | return false; |
| 585 | } |