John McCall | 8f0e8d2 | 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 | 1f6b2b5 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 13 | #include "clang/Frontend/FrontendAction.h" |
Argyrios Kyrtzidis | 7ee2049 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
John McCall | 8f0e8d2 | 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 | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | using namespace arcmt; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 26 | bool CapturedDiagList::clearDiagnostic(ArrayRef<unsigned> IDs, |
John McCall | 8f0e8d2 | 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 | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 42 | while (I != List.end() && I->getLevel() == DiagnosticsEngine::Note) |
John McCall | 8f0e8d2 | 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 | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 55 | bool CapturedDiagList::hasDiagnostic(ArrayRef<unsigned> IDs, |
Argyrios Kyrtzidis | 60a5e3f | 2011-06-18 00:53:34 +0000 | [diff] [blame] | 56 | SourceRange range) const { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 57 | if (range.isInvalid()) |
| 58 | return false; |
| 59 | |
Argyrios Kyrtzidis | 60a5e3f | 2011-06-18 00:53:34 +0000 | [diff] [blame] | 60 | ListTy::const_iterator I = List.begin(); |
John McCall | 8f0e8d2 | 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 | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 77 | void CapturedDiagList::reportDiagnostics(DiagnosticsEngine &Diags) const { |
Argyrios Kyrtzidis | 60a5e3f | 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 | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 79 | Diags.Report(*I); |
| 80 | } |
| 81 | |
Argyrios Kyrtzidis | e665d69 | 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 | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 84 | if (I->getLevel() >= DiagnosticsEngine::Error) |
Argyrios Kyrtzidis | e665d69 | 2011-06-18 00:53:41 +0000 | [diff] [blame] | 85 | return true; |
| 86 | |
| 87 | return false; |
| 88 | } |
| 89 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 90 | namespace { |
| 91 | |
David Blaikie | aee37ba | 2011-09-25 23:54:33 +0000 | [diff] [blame] | 92 | class CaptureDiagnosticConsumer : public DiagnosticConsumer { |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 93 | DiagnosticsEngine &Diags; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 94 | CapturedDiagList &CapturedDiags; |
| 95 | public: |
David Blaikie | aee37ba | 2011-09-25 23:54:33 +0000 | [diff] [blame] | 96 | CaptureDiagnosticConsumer(DiagnosticsEngine &diags, |
Douglas Gregor | aee526e | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 97 | CapturedDiagList &capturedDiags) |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 98 | : Diags(diags), CapturedDiags(capturedDiags) { } |
| 99 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 100 | virtual void HandleDiagnostic(DiagnosticsEngine::Level level, |
David Blaikie | 40847cf | 2011-09-26 01:18:08 +0000 | [diff] [blame] | 101 | const Diagnostic &Info) { |
Ted Kremenek | afdc21a | 2011-10-20 05:07:47 +0000 | [diff] [blame] | 102 | if (DiagnosticIDs::isARCDiagnostic(Info.getID()) || |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 103 | level >= DiagnosticsEngine::Error || level == DiagnosticsEngine::Note) { |
John McCall | 8f0e8d2 | 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 | aee526e | 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 | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | } // end anonymous namespace |
| 120 | |
Argyrios Kyrtzidis | dceb11f | 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 | 9c47973 | 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 | dceb11f | 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 | 9c47973 | 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 | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 186 | static CompilerInvocation * |
| 187 | createInvocationForMigration(CompilerInvocation &origCI) { |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 188 | OwningPtr<CompilerInvocation> CInvok; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 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 | d3b74d9 | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 195 | CInvok->getLangOpts()->ObjCAutoRefCount = true; |
| 196 | CInvok->getLangOpts()->setGC(LangOptions::NonGC); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 197 | CInvok->getDiagnosticOpts().ErrorLimit = 0; |
Argyrios Kyrtzidis | ffe76dd | 2012-06-20 01:10:40 +0000 | [diff] [blame^] | 198 | |
| 199 | // Ignore -Werror flags when migrating. |
| 200 | std::vector<std::string> WarnOpts; |
| 201 | for (std::vector<std::string>::iterator |
| 202 | I = CInvok->getDiagnosticOpts().Warnings.begin(), |
| 203 | E = CInvok->getDiagnosticOpts().Warnings.end(); I != E; ++I) { |
| 204 | if (!StringRef(*I).startswith("error")) |
| 205 | WarnOpts.push_back(*I); |
| 206 | } |
| 207 | WarnOpts.push_back("error=arc-unsafe-retained-assign"); |
| 208 | CInvok->getDiagnosticOpts().Warnings = WarnOpts; |
| 209 | |
Ted Kremenek | d3b74d9 | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 210 | CInvok->getLangOpts()->ObjCRuntimeHasWeak = HasARCRuntime(origCI); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 211 | |
| 212 | return CInvok.take(); |
| 213 | } |
| 214 | |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 215 | static void emitPremigrationErrors(const CapturedDiagList &arcDiags, |
| 216 | const DiagnosticOptions &diagOpts, |
| 217 | Preprocessor &PP) { |
Argyrios Kyrtzidis | 7ee2049 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 218 | TextDiagnosticPrinter printer(llvm::errs(), diagOpts); |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 219 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 220 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 221 | new DiagnosticsEngine(DiagID, &printer, /*ShouldOwnClient=*/false)); |
Argyrios Kyrtzidis | 7ee2049 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 222 | Diags->setSourceManager(&PP.getSourceManager()); |
| 223 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 224 | printer.BeginSourceFile(PP.getLangOpts(), &PP); |
Argyrios Kyrtzidis | 7ee2049 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 225 | arcDiags.reportDiagnostics(*Diags); |
| 226 | printer.EndSourceFile(); |
| 227 | } |
| 228 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 229 | //===----------------------------------------------------------------------===// |
| 230 | // checkForManualIssues. |
| 231 | //===----------------------------------------------------------------------===// |
| 232 | |
| 233 | bool arcmt::checkForManualIssues(CompilerInvocation &origCI, |
Douglas Gregor | 1f6b2b5 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 234 | const FrontendInputFile &Input, |
David Blaikie | 78ad0b9 | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 235 | DiagnosticConsumer *DiagClient, |
Argyrios Kyrtzidis | 7ee2049 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 236 | bool emitPremigrationARCErrors, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 237 | StringRef plistOut) { |
Ted Kremenek | d3b74d9 | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 238 | if (!origCI.getLangOpts()->ObjC1) |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 239 | return false; |
| 240 | |
Ted Kremenek | d3b74d9 | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 241 | LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC(); |
Fariborz Jahanian | b5c6bab | 2012-01-25 00:20:29 +0000 | [diff] [blame] | 242 | bool NoNSAllocReallocError = origCI.getMigratorOpts().NoNSAllocReallocError; |
Fariborz Jahanian | 26f0e4e | 2012-01-26 00:08:04 +0000 | [diff] [blame] | 243 | bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval; |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 244 | |
Fariborz Jahanian | bbdfad5 | 2012-01-26 20:57:58 +0000 | [diff] [blame] | 245 | std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode, |
| 246 | NoFinalizeRemoval); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 247 | assert(!transforms.empty()); |
| 248 | |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 249 | OwningPtr<CompilerInvocation> CInvok; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 250 | CInvok.reset(createInvocationForMigration(origCI)); |
| 251 | CInvok->getFrontendOpts().Inputs.clear(); |
Douglas Gregor | 1f6b2b5 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 252 | CInvok->getFrontendOpts().Inputs.push_back(Input); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 253 | |
| 254 | CapturedDiagList capturedDiags; |
| 255 | |
| 256 | assert(DiagClient); |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 257 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 258 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 259 | new DiagnosticsEngine(DiagID, DiagClient, /*ShouldOwnClient=*/false)); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 260 | |
| 261 | // Filter of all diagnostics. |
David Blaikie | aee37ba | 2011-09-25 23:54:33 +0000 | [diff] [blame] | 262 | CaptureDiagnosticConsumer errRec(*Diags, capturedDiags); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 263 | Diags->setClient(&errRec, /*ShouldOwnClient=*/false); |
| 264 | |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 265 | OwningPtr<ASTUnit> Unit( |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 266 | ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags)); |
| 267 | if (!Unit) |
| 268 | return true; |
| 269 | |
| 270 | // Don't filter diagnostics anymore. |
| 271 | Diags->setClient(DiagClient, /*ShouldOwnClient=*/false); |
| 272 | |
| 273 | ASTContext &Ctx = Unit->getASTContext(); |
| 274 | |
| 275 | if (Diags->hasFatalErrorOccurred()) { |
| 276 | Diags->Reset(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 277 | DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor()); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 278 | capturedDiags.reportDiagnostics(*Diags); |
| 279 | DiagClient->EndSourceFile(); |
| 280 | return true; |
| 281 | } |
| 282 | |
Argyrios Kyrtzidis | 7ee2049 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 283 | if (emitPremigrationARCErrors) |
| 284 | emitPremigrationErrors(capturedDiags, origCI.getDiagnosticOpts(), |
| 285 | Unit->getPreprocessor()); |
| 286 | if (!plistOut.empty()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 287 | SmallVector<StoredDiagnostic, 8> arcDiags; |
Argyrios Kyrtzidis | 7ee2049 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 288 | for (CapturedDiagList::iterator |
| 289 | I = capturedDiags.begin(), E = capturedDiags.end(); I != E; ++I) |
| 290 | arcDiags.push_back(*I); |
| 291 | writeARCDiagsToPlist(plistOut, arcDiags, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 292 | Ctx.getSourceManager(), Ctx.getLangOpts()); |
Argyrios Kyrtzidis | 7ee2049 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 293 | } |
| 294 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 295 | // After parsing of source files ended, we want to reuse the |
| 296 | // diagnostics objects to emit further diagnostics. |
David Blaikie | 78ad0b9 | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 297 | // We call BeginSourceFile because DiagnosticConsumer requires that |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 298 | // diagnostics with source range information are emitted only in between |
| 299 | // BeginSourceFile() and EndSourceFile(). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 300 | DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor()); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 301 | |
| 302 | // No macros will be added since we are just checking and we won't modify |
| 303 | // source code. |
| 304 | std::vector<SourceLocation> ARCMTMacroLocs; |
| 305 | |
| 306 | TransformActions testAct(*Diags, capturedDiags, Ctx, Unit->getPreprocessor()); |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 307 | MigrationPass pass(Ctx, OrigGCMode, Unit->getSema(), testAct, ARCMTMacroLocs); |
Fariborz Jahanian | b5c6bab | 2012-01-25 00:20:29 +0000 | [diff] [blame] | 308 | pass.setNSAllocReallocError(NoNSAllocReallocError); |
Fariborz Jahanian | 26f0e4e | 2012-01-26 00:08:04 +0000 | [diff] [blame] | 309 | pass.setNoFinalizeRemoval(NoFinalizeRemoval); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 310 | |
| 311 | for (unsigned i=0, e = transforms.size(); i != e; ++i) |
| 312 | transforms[i](pass); |
| 313 | |
| 314 | capturedDiags.reportDiagnostics(*Diags); |
| 315 | |
| 316 | DiagClient->EndSourceFile(); |
| 317 | |
Argyrios Kyrtzidis | a944b12 | 2011-07-14 00:17:54 +0000 | [diff] [blame] | 318 | // If we are migrating code that gets the '-fobjc-arc' flag, make sure |
| 319 | // to remove it so that we don't get errors from normal compilation. |
Ted Kremenek | d3b74d9 | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 320 | origCI.getLangOpts()->ObjCAutoRefCount = false; |
Argyrios Kyrtzidis | a944b12 | 2011-07-14 00:17:54 +0000 | [diff] [blame] | 321 | |
Argyrios Kyrtzidis | fd10398 | 2011-07-18 07:44:45 +0000 | [diff] [blame] | 322 | return capturedDiags.hasErrors() || testAct.hasReportedErrors(); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | //===----------------------------------------------------------------------===// |
| 326 | // applyTransformations. |
| 327 | //===----------------------------------------------------------------------===// |
| 328 | |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 329 | static bool applyTransforms(CompilerInvocation &origCI, |
Douglas Gregor | 1f6b2b5 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 330 | const FrontendInputFile &Input, |
David Blaikie | 78ad0b9 | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 331 | DiagnosticConsumer *DiagClient, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 332 | StringRef outputDir, |
Argyrios Kyrtzidis | 7ee2049 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 333 | bool emitPremigrationARCErrors, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 334 | StringRef plistOut) { |
Ted Kremenek | d3b74d9 | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 335 | if (!origCI.getLangOpts()->ObjC1) |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 336 | return false; |
| 337 | |
Ted Kremenek | d3b74d9 | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 338 | LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC(); |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 339 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 340 | // Make sure checking is successful first. |
| 341 | CompilerInvocation CInvokForCheck(origCI); |
Douglas Gregor | 1f6b2b5 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 342 | if (arcmt::checkForManualIssues(CInvokForCheck, Input, DiagClient, |
Argyrios Kyrtzidis | 7ee2049 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 343 | emitPremigrationARCErrors, plistOut)) |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 344 | return true; |
| 345 | |
| 346 | CompilerInvocation CInvok(origCI); |
| 347 | CInvok.getFrontendOpts().Inputs.clear(); |
Douglas Gregor | 1f6b2b5 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 348 | CInvok.getFrontendOpts().Inputs.push_back(Input); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 349 | |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 350 | MigrationProcess migration(CInvok, DiagClient, outputDir); |
Fariborz Jahanian | bbdfad5 | 2012-01-26 20:57:58 +0000 | [diff] [blame] | 351 | bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 352 | |
Fariborz Jahanian | bbdfad5 | 2012-01-26 20:57:58 +0000 | [diff] [blame] | 353 | std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode, |
| 354 | NoFinalizeRemoval); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 355 | assert(!transforms.empty()); |
| 356 | |
| 357 | for (unsigned i=0, e = transforms.size(); i != e; ++i) { |
| 358 | bool err = migration.applyTransform(transforms[i]); |
| 359 | if (err) return true; |
| 360 | } |
| 361 | |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 362 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 363 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 364 | new DiagnosticsEngine(DiagID, DiagClient, /*ShouldOwnClient=*/false)); |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 365 | |
| 366 | if (outputDir.empty()) { |
Ted Kremenek | d3b74d9 | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 367 | origCI.getLangOpts()->ObjCAutoRefCount = true; |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 368 | return migration.getRemapper().overwriteOriginal(*Diags); |
Argyrios Kyrtzidis | a944b12 | 2011-07-14 00:17:54 +0000 | [diff] [blame] | 369 | } else { |
| 370 | // If we are migrating code that gets the '-fobjc-arc' flag, make sure |
| 371 | // to remove it so that we don't get errors from normal compilation. |
Ted Kremenek | d3b74d9 | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 372 | origCI.getLangOpts()->ObjCAutoRefCount = false; |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 373 | return migration.getRemapper().flushToDisk(outputDir, *Diags); |
Argyrios Kyrtzidis | a944b12 | 2011-07-14 00:17:54 +0000 | [diff] [blame] | 374 | } |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | bool arcmt::applyTransformations(CompilerInvocation &origCI, |
Douglas Gregor | 1f6b2b5 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 378 | const FrontendInputFile &Input, |
David Blaikie | 78ad0b9 | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 379 | DiagnosticConsumer *DiagClient) { |
Douglas Gregor | 1f6b2b5 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 380 | return applyTransforms(origCI, Input, DiagClient, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 381 | StringRef(), false, StringRef()); |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | bool arcmt::migrateWithTemporaryFiles(CompilerInvocation &origCI, |
Douglas Gregor | 1f6b2b5 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 385 | const FrontendInputFile &Input, |
David Blaikie | 78ad0b9 | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 386 | DiagnosticConsumer *DiagClient, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 387 | StringRef outputDir, |
Argyrios Kyrtzidis | 7ee2049 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 388 | bool emitPremigrationARCErrors, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 389 | StringRef plistOut) { |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 390 | assert(!outputDir.empty() && "Expected output directory path"); |
Douglas Gregor | 1f6b2b5 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 391 | return applyTransforms(origCI, Input, DiagClient, |
Argyrios Kyrtzidis | 7ee2049 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 392 | outputDir, emitPremigrationARCErrors, plistOut); |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | bool arcmt::getFileRemappings(std::vector<std::pair<std::string,std::string> > & |
| 396 | remap, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 397 | StringRef outputDir, |
David Blaikie | 78ad0b9 | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 398 | DiagnosticConsumer *DiagClient) { |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 399 | assert(!outputDir.empty()); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 400 | |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 401 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 402 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 403 | new DiagnosticsEngine(DiagID, DiagClient, /*ShouldOwnClient=*/false)); |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 404 | |
| 405 | FileRemapper remapper; |
| 406 | bool err = remapper.initFromDisk(outputDir, *Diags, |
| 407 | /*ignoreIfFilesChanged=*/true); |
| 408 | if (err) |
| 409 | return true; |
| 410 | |
Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 411 | PreprocessorOptions PPOpts; |
| 412 | remapper.applyMappings(PPOpts); |
| 413 | remap = PPOpts.RemappedFiles; |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 414 | |
| 415 | return false; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 418 | bool arcmt::getFileRemappingsFromFileList( |
| 419 | std::vector<std::pair<std::string,std::string> > &remap, |
| 420 | ArrayRef<StringRef> remapFiles, |
| 421 | DiagnosticConsumer *DiagClient) { |
| 422 | bool hasErrorOccurred = false; |
| 423 | llvm::StringMap<bool> Uniquer; |
| 424 | |
| 425 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 426 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
| 427 | new DiagnosticsEngine(DiagID, DiagClient, /*ShouldOwnClient=*/false)); |
| 428 | |
| 429 | for (ArrayRef<StringRef>::iterator |
| 430 | I = remapFiles.begin(), E = remapFiles.end(); I != E; ++I) { |
| 431 | StringRef file = *I; |
| 432 | |
| 433 | FileRemapper remapper; |
| 434 | bool err = remapper.initFromFile(file, *Diags, |
| 435 | /*ignoreIfFilesChanged=*/true); |
| 436 | hasErrorOccurred = hasErrorOccurred || err; |
| 437 | if (err) |
| 438 | continue; |
| 439 | |
| 440 | PreprocessorOptions PPOpts; |
| 441 | remapper.applyMappings(PPOpts); |
| 442 | for (PreprocessorOptions::remapped_file_iterator |
| 443 | RI = PPOpts.remapped_file_begin(), RE = PPOpts.remapped_file_end(); |
| 444 | RI != RE; ++RI) { |
| 445 | bool &inserted = Uniquer[RI->first]; |
| 446 | if (inserted) |
| 447 | continue; |
| 448 | inserted = true; |
| 449 | remap.push_back(*RI); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | return hasErrorOccurred; |
| 454 | } |
| 455 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 456 | //===----------------------------------------------------------------------===// |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 457 | // CollectTransformActions. |
| 458 | //===----------------------------------------------------------------------===// |
| 459 | |
| 460 | namespace { |
| 461 | |
| 462 | class ARCMTMacroTrackerPPCallbacks : public PPCallbacks { |
| 463 | std::vector<SourceLocation> &ARCMTMacroLocs; |
| 464 | |
| 465 | public: |
| 466 | ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs) |
| 467 | : ARCMTMacroLocs(ARCMTMacroLocs) { } |
| 468 | |
Argyrios Kyrtzidis | 1b2d536 | 2011-08-18 01:05:45 +0000 | [diff] [blame] | 469 | virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo *MI, |
| 470 | SourceRange Range) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 471 | if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName()) |
| 472 | ARCMTMacroLocs.push_back(MacroNameTok.getLocation()); |
| 473 | } |
| 474 | }; |
| 475 | |
| 476 | class ARCMTMacroTrackerAction : public ASTFrontendAction { |
| 477 | std::vector<SourceLocation> &ARCMTMacroLocs; |
| 478 | |
| 479 | public: |
| 480 | ARCMTMacroTrackerAction(std::vector<SourceLocation> &ARCMTMacroLocs) |
| 481 | : ARCMTMacroLocs(ARCMTMacroLocs) { } |
| 482 | |
| 483 | virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 484 | StringRef InFile) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 485 | CI.getPreprocessor().addPPCallbacks( |
| 486 | new ARCMTMacroTrackerPPCallbacks(ARCMTMacroLocs)); |
| 487 | return new ASTConsumer(); |
| 488 | } |
| 489 | }; |
| 490 | |
| 491 | class RewritesApplicator : public TransformActions::RewriteReceiver { |
| 492 | Rewriter &rewriter; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 493 | MigrationProcess::RewriteListener *Listener; |
| 494 | |
| 495 | public: |
| 496 | RewritesApplicator(Rewriter &rewriter, ASTContext &ctx, |
| 497 | MigrationProcess::RewriteListener *listener) |
Benjamin Kramer | facde17 | 2012-06-06 17:32:50 +0000 | [diff] [blame] | 498 | : rewriter(rewriter), Listener(listener) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 499 | if (Listener) |
| 500 | Listener->start(ctx); |
| 501 | } |
| 502 | ~RewritesApplicator() { |
| 503 | if (Listener) |
| 504 | Listener->finish(); |
| 505 | } |
| 506 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 507 | virtual void insert(SourceLocation loc, StringRef text) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 508 | bool err = rewriter.InsertText(loc, text, /*InsertAfter=*/true, |
| 509 | /*indentNewLines=*/true); |
| 510 | if (!err && Listener) |
| 511 | Listener->insert(loc, text); |
| 512 | } |
| 513 | |
| 514 | virtual void remove(CharSourceRange range) { |
| 515 | Rewriter::RewriteOptions removeOpts; |
| 516 | removeOpts.IncludeInsertsAtBeginOfRange = false; |
| 517 | removeOpts.IncludeInsertsAtEndOfRange = false; |
| 518 | removeOpts.RemoveLineIfEmpty = true; |
| 519 | |
| 520 | bool err = rewriter.RemoveText(range, removeOpts); |
| 521 | if (!err && Listener) |
| 522 | Listener->remove(range); |
| 523 | } |
| 524 | |
| 525 | virtual void increaseIndentation(CharSourceRange range, |
| 526 | SourceLocation parentIndent) { |
| 527 | rewriter.IncreaseIndentation(range, parentIndent); |
| 528 | } |
| 529 | }; |
| 530 | |
| 531 | } // end anonymous namespace. |
| 532 | |
| 533 | /// \brief Anchor for VTable. |
| 534 | MigrationProcess::RewriteListener::~RewriteListener() { } |
| 535 | |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 536 | MigrationProcess::MigrationProcess(const CompilerInvocation &CI, |
David Blaikie | 78ad0b9 | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 537 | DiagnosticConsumer *diagClient, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 538 | StringRef outputDir) |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 539 | : OrigCI(CI), DiagClient(diagClient) { |
| 540 | if (!outputDir.empty()) { |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 541 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 542 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 543 | new DiagnosticsEngine(DiagID, DiagClient, /*ShouldOwnClient=*/false)); |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 544 | Remapper.initFromDisk(outputDir, *Diags, /*ignoreIfFilesChanges=*/true); |
| 545 | } |
| 546 | } |
| 547 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 548 | bool MigrationProcess::applyTransform(TransformFn trans, |
| 549 | RewriteListener *listener) { |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 550 | OwningPtr<CompilerInvocation> CInvok; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 551 | CInvok.reset(createInvocationForMigration(OrigCI)); |
| 552 | CInvok->getDiagnosticOpts().IgnoreWarnings = true; |
| 553 | |
Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 554 | Remapper.applyMappings(CInvok->getPreprocessorOpts()); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 555 | |
| 556 | CapturedDiagList capturedDiags; |
| 557 | std::vector<SourceLocation> ARCMTMacroLocs; |
| 558 | |
| 559 | assert(DiagClient); |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 560 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 561 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 562 | new DiagnosticsEngine(DiagID, DiagClient, /*ShouldOwnClient=*/false)); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 563 | |
| 564 | // Filter of all diagnostics. |
David Blaikie | aee37ba | 2011-09-25 23:54:33 +0000 | [diff] [blame] | 565 | CaptureDiagnosticConsumer errRec(*Diags, capturedDiags); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 566 | Diags->setClient(&errRec, /*ShouldOwnClient=*/false); |
| 567 | |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 568 | OwningPtr<ARCMTMacroTrackerAction> ASTAction; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 569 | ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs)); |
| 570 | |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 571 | OwningPtr<ASTUnit> Unit( |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 572 | ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags, |
| 573 | ASTAction.get())); |
| 574 | if (!Unit) |
| 575 | return true; |
| 576 | Unit->setOwnsRemappedFileBuffers(false); // FileRemapper manages that. |
| 577 | |
| 578 | // Don't filter diagnostics anymore. |
| 579 | Diags->setClient(DiagClient, /*ShouldOwnClient=*/false); |
| 580 | |
| 581 | ASTContext &Ctx = Unit->getASTContext(); |
| 582 | |
| 583 | if (Diags->hasFatalErrorOccurred()) { |
| 584 | Diags->Reset(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 585 | DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor()); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 586 | capturedDiags.reportDiagnostics(*Diags); |
| 587 | DiagClient->EndSourceFile(); |
| 588 | return true; |
| 589 | } |
| 590 | |
| 591 | // After parsing of source files ended, we want to reuse the |
| 592 | // diagnostics objects to emit further diagnostics. |
David Blaikie | 78ad0b9 | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 593 | // We call BeginSourceFile because DiagnosticConsumer requires that |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 594 | // diagnostics with source range information are emitted only in between |
| 595 | // BeginSourceFile() and EndSourceFile(). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 596 | DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor()); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 597 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 598 | Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts()); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 599 | TransformActions TA(*Diags, capturedDiags, Ctx, Unit->getPreprocessor()); |
Ted Kremenek | d3b74d9 | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 600 | MigrationPass pass(Ctx, OrigCI.getLangOpts()->getGC(), |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 601 | Unit->getSema(), TA, ARCMTMacroLocs); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 602 | |
| 603 | trans(pass); |
| 604 | |
| 605 | { |
| 606 | RewritesApplicator applicator(rewriter, Ctx, listener); |
| 607 | TA.applyRewrites(applicator); |
| 608 | } |
| 609 | |
| 610 | DiagClient->EndSourceFile(); |
| 611 | |
| 612 | if (DiagClient->getNumErrors()) |
| 613 | return true; |
| 614 | |
| 615 | for (Rewriter::buffer_iterator |
| 616 | I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) { |
| 617 | FileID FID = I->first; |
| 618 | RewriteBuffer &buf = I->second; |
| 619 | const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID); |
| 620 | assert(file); |
| 621 | std::string newFname = file->getName(); |
| 622 | newFname += "-trans"; |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 623 | SmallString<512> newText; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 624 | llvm::raw_svector_ostream vecOS(newText); |
| 625 | buf.write(vecOS); |
| 626 | vecOS.flush(); |
| 627 | llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy( |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 628 | StringRef(newText.data(), newText.size()), newFname); |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 629 | SmallString<64> filePath(file->getName()); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 630 | Unit->getFileManager().FixupRelativePath(filePath); |
| 631 | Remapper.remap(filePath.str(), memBuf); |
| 632 | } |
| 633 | |
| 634 | return false; |
| 635 | } |