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" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTConsumer.h" |
| 12 | #include "clang/Basic/DiagnosticCategories.h" |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 13 | #include "clang/Frontend/ASTUnit.h" |
| 14 | #include "clang/Frontend/CompilerInstance.h" |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/FrontendAction.h" |
Argyrios Kyrtzidis | d571363 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 16 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/Utils.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Preprocessor.h" |
Ted Kremenek | cdf8149 | 2012-09-01 05:09:24 +0000 | [diff] [blame] | 19 | #include "clang/Rewrite/Core/Rewriter.h" |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 20 | #include "clang/Sema/SemaDiagnostic.h" |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "llvm/Support/MemoryBuffer.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. |
Joao Matos | 0e167f7 | 2012-08-31 17:28:09 +0000 | [diff] [blame] | 45 | I = List.erase(eraseS, I); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 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; |
Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 94 | DiagnosticConsumer &DiagClient; |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 95 | CapturedDiagList &CapturedDiags; |
Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 96 | bool HasBegunSourceFile; |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 97 | public: |
David Blaikie | a24a0bc | 2011-09-25 23:54:33 +0000 | [diff] [blame] | 98 | CaptureDiagnosticConsumer(DiagnosticsEngine &diags, |
Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 99 | DiagnosticConsumer &client, |
| 100 | CapturedDiagList &capturedDiags) |
| 101 | : Diags(diags), DiagClient(client), CapturedDiags(capturedDiags), |
| 102 | HasBegunSourceFile(false) { } |
| 103 | |
| 104 | virtual void BeginSourceFile(const LangOptions &Opts, |
| 105 | const Preprocessor *PP) { |
| 106 | // Pass BeginSourceFile message onto DiagClient on first call. |
| 107 | // The corresponding EndSourceFile call will be made from an |
| 108 | // explicit call to FinishCapture. |
| 109 | if (!HasBegunSourceFile) { |
| 110 | DiagClient.BeginSourceFile(Opts, PP); |
| 111 | HasBegunSourceFile = true; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void FinishCapture() { |
| 116 | // Call EndSourceFile on DiagClient on completion of capture to |
| 117 | // enable VerifyDiagnosticConsumer to check diagnostics *after* |
| 118 | // it has received the diagnostic list. |
| 119 | if (HasBegunSourceFile) { |
| 120 | DiagClient.EndSourceFile(); |
| 121 | HasBegunSourceFile = false; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | virtual ~CaptureDiagnosticConsumer() { |
| 126 | assert(!HasBegunSourceFile && "FinishCapture not called!"); |
| 127 | } |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 128 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 129 | virtual void HandleDiagnostic(DiagnosticsEngine::Level level, |
David Blaikie | b578432 | 2011-09-26 01:18:08 +0000 | [diff] [blame] | 130 | const Diagnostic &Info) { |
Ted Kremenek | 337c5b8 | 2011-10-20 05:07:47 +0000 | [diff] [blame] | 131 | if (DiagnosticIDs::isARCDiagnostic(Info.getID()) || |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 132 | level >= DiagnosticsEngine::Error || level == DiagnosticsEngine::Note) { |
Argyrios Kyrtzidis | 1f73216 | 2012-12-12 22:48:28 +0000 | [diff] [blame^] | 133 | if (Info.getLocation().isValid()) |
| 134 | CapturedDiags.push_back(StoredDiagnostic(level, Info)); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 135 | return; |
| 136 | } |
| 137 | |
| 138 | // Non-ARC warnings are ignored. |
| 139 | Diags.setLastDiagnosticIgnored(); |
| 140 | } |
Douglas Gregor | d0e9e3a | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 141 | |
| 142 | DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const { |
| 143 | // Just drop any diagnostics that come from cloned consumers; they'll |
| 144 | // have different source managers anyway. |
| 145 | return new IgnoringDiagConsumer(); |
| 146 | } |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | } // end anonymous namespace |
| 150 | |
Argyrios Kyrtzidis | 81a3590 | 2011-06-20 19:59:52 +0000 | [diff] [blame] | 151 | static bool HasARCRuntime(CompilerInvocation &origCI) { |
| 152 | // This duplicates some functionality from Darwin::AddDeploymentTarget |
| 153 | // but this function is well defined, so keep it decoupled from the driver |
| 154 | // and avoid unrelated complications. |
Argyrios Kyrtzidis | 81a3590 | 2011-06-20 19:59:52 +0000 | [diff] [blame] | 155 | llvm::Triple triple(origCI.getTargetOpts().Triple); |
| 156 | |
| 157 | if (triple.getOS() == llvm::Triple::IOS) |
| 158 | return triple.getOSMajorVersion() >= 5; |
| 159 | |
| 160 | if (triple.getOS() == llvm::Triple::Darwin) |
| 161 | return triple.getOSMajorVersion() >= 11; |
| 162 | |
| 163 | if (triple.getOS() == llvm::Triple::MacOSX) { |
| 164 | unsigned Major, Minor, Micro; |
| 165 | triple.getOSVersion(Major, Minor, Micro); |
| 166 | return Major > 10 || (Major == 10 && Minor >= 7); |
| 167 | } |
| 168 | |
| 169 | return false; |
| 170 | } |
| 171 | |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 172 | static CompilerInvocation * |
| 173 | createInvocationForMigration(CompilerInvocation &origCI) { |
Dylan Noblesmith | e277899 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 174 | OwningPtr<CompilerInvocation> CInvok; |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 175 | CInvok.reset(new CompilerInvocation(origCI)); |
| 176 | CInvok->getPreprocessorOpts().ImplicitPCHInclude = std::string(); |
| 177 | CInvok->getPreprocessorOpts().ImplicitPTHInclude = std::string(); |
| 178 | std::string define = getARCMTMacroName(); |
| 179 | define += '='; |
| 180 | CInvok->getPreprocessorOpts().addMacroDef(define); |
Ted Kremenek | 8cf47df | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 181 | CInvok->getLangOpts()->ObjCAutoRefCount = true; |
| 182 | CInvok->getLangOpts()->setGC(LangOptions::NonGC); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 183 | CInvok->getDiagnosticOpts().ErrorLimit = 0; |
Argyrios Kyrtzidis | aa38f69 | 2012-06-20 01:46:26 +0000 | [diff] [blame] | 184 | CInvok->getDiagnosticOpts().PedanticErrors = 0; |
Argyrios Kyrtzidis | 692bf8c | 2012-06-20 01:10:40 +0000 | [diff] [blame] | 185 | |
| 186 | // Ignore -Werror flags when migrating. |
| 187 | std::vector<std::string> WarnOpts; |
| 188 | for (std::vector<std::string>::iterator |
| 189 | I = CInvok->getDiagnosticOpts().Warnings.begin(), |
| 190 | E = CInvok->getDiagnosticOpts().Warnings.end(); I != E; ++I) { |
| 191 | if (!StringRef(*I).startswith("error")) |
| 192 | WarnOpts.push_back(*I); |
| 193 | } |
| 194 | WarnOpts.push_back("error=arc-unsafe-retained-assign"); |
Argyrios Kyrtzidis | aa38f69 | 2012-06-20 01:46:26 +0000 | [diff] [blame] | 195 | CInvok->getDiagnosticOpts().Warnings = llvm_move(WarnOpts); |
Argyrios Kyrtzidis | 692bf8c | 2012-06-20 01:10:40 +0000 | [diff] [blame] | 196 | |
John McCall | 3deb1ad | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 197 | CInvok->getLangOpts()->ObjCARCWeak = HasARCRuntime(origCI); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 198 | |
| 199 | return CInvok.take(); |
| 200 | } |
| 201 | |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 202 | static void emitPremigrationErrors(const CapturedDiagList &arcDiags, |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 203 | DiagnosticOptions *diagOpts, |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 204 | Preprocessor &PP) { |
Argyrios Kyrtzidis | d571363 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 205 | TextDiagnosticPrinter printer(llvm::errs(), diagOpts); |
Dylan Noblesmith | c95d819 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 206 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 207 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 208 | new DiagnosticsEngine(DiagID, diagOpts, &printer, |
| 209 | /*ShouldOwnClient=*/false)); |
Argyrios Kyrtzidis | d571363 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 210 | Diags->setSourceManager(&PP.getSourceManager()); |
| 211 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 212 | printer.BeginSourceFile(PP.getLangOpts(), &PP); |
Argyrios Kyrtzidis | d571363 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 213 | arcDiags.reportDiagnostics(*Diags); |
| 214 | printer.EndSourceFile(); |
| 215 | } |
| 216 | |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 217 | //===----------------------------------------------------------------------===// |
| 218 | // checkForManualIssues. |
| 219 | //===----------------------------------------------------------------------===// |
| 220 | |
| 221 | bool arcmt::checkForManualIssues(CompilerInvocation &origCI, |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 222 | const FrontendInputFile &Input, |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 223 | DiagnosticConsumer *DiagClient, |
Argyrios Kyrtzidis | d571363 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 224 | bool emitPremigrationARCErrors, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 225 | StringRef plistOut) { |
Ted Kremenek | 8cf47df | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 226 | if (!origCI.getLangOpts()->ObjC1) |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 227 | return false; |
| 228 | |
Ted Kremenek | 8cf47df | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 229 | LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC(); |
Fariborz Jahanian | aa7b9aa | 2012-01-25 00:20:29 +0000 | [diff] [blame] | 230 | bool NoNSAllocReallocError = origCI.getMigratorOpts().NoNSAllocReallocError; |
Fariborz Jahanian | 0c859d6 | 2012-01-26 00:08:04 +0000 | [diff] [blame] | 231 | bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval; |
Argyrios Kyrtzidis | d208ef9 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 232 | |
Fariborz Jahanian | 48fd81b | 2012-01-26 20:57:58 +0000 | [diff] [blame] | 233 | std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode, |
| 234 | NoFinalizeRemoval); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 235 | assert(!transforms.empty()); |
| 236 | |
Dylan Noblesmith | e277899 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 237 | OwningPtr<CompilerInvocation> CInvok; |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 238 | CInvok.reset(createInvocationForMigration(origCI)); |
| 239 | CInvok->getFrontendOpts().Inputs.clear(); |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 240 | CInvok->getFrontendOpts().Inputs.push_back(Input); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 241 | |
| 242 | CapturedDiagList capturedDiags; |
| 243 | |
| 244 | assert(DiagClient); |
Dylan Noblesmith | c95d819 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 245 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 246 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 247 | new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(), |
| 248 | DiagClient, /*ShouldOwnClient=*/false)); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 249 | |
| 250 | // Filter of all diagnostics. |
Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 251 | CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 252 | Diags->setClient(&errRec, /*ShouldOwnClient=*/false); |
| 253 | |
Dylan Noblesmith | e277899 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 254 | OwningPtr<ASTUnit> Unit( |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 255 | ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags)); |
Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 256 | if (!Unit) { |
| 257 | errRec.FinishCapture(); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 258 | return true; |
Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 259 | } |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 260 | |
| 261 | // Don't filter diagnostics anymore. |
| 262 | Diags->setClient(DiagClient, /*ShouldOwnClient=*/false); |
| 263 | |
| 264 | ASTContext &Ctx = Unit->getASTContext(); |
| 265 | |
| 266 | if (Diags->hasFatalErrorOccurred()) { |
| 267 | Diags->Reset(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 268 | DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor()); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 269 | capturedDiags.reportDiagnostics(*Diags); |
| 270 | DiagClient->EndSourceFile(); |
Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 271 | errRec.FinishCapture(); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 272 | return true; |
| 273 | } |
| 274 | |
Argyrios Kyrtzidis | d571363 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 275 | if (emitPremigrationARCErrors) |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 276 | emitPremigrationErrors(capturedDiags, &origCI.getDiagnosticOpts(), |
Argyrios Kyrtzidis | d571363 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 277 | Unit->getPreprocessor()); |
| 278 | if (!plistOut.empty()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 279 | SmallVector<StoredDiagnostic, 8> arcDiags; |
Argyrios Kyrtzidis | d571363 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 280 | for (CapturedDiagList::iterator |
| 281 | I = capturedDiags.begin(), E = capturedDiags.end(); I != E; ++I) |
| 282 | arcDiags.push_back(*I); |
| 283 | writeARCDiagsToPlist(plistOut, arcDiags, |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 284 | Ctx.getSourceManager(), Ctx.getLangOpts()); |
Argyrios Kyrtzidis | d571363 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 285 | } |
| 286 | |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 287 | // After parsing of source files ended, we want to reuse the |
| 288 | // diagnostics objects to emit further diagnostics. |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 289 | // We call BeginSourceFile because DiagnosticConsumer requires that |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 290 | // diagnostics with source range information are emitted only in between |
| 291 | // BeginSourceFile() and EndSourceFile(). |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 292 | DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor()); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 293 | |
| 294 | // No macros will be added since we are just checking and we won't modify |
| 295 | // source code. |
| 296 | std::vector<SourceLocation> ARCMTMacroLocs; |
| 297 | |
| 298 | TransformActions testAct(*Diags, capturedDiags, Ctx, Unit->getPreprocessor()); |
Argyrios Kyrtzidis | d208ef9 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 299 | MigrationPass pass(Ctx, OrigGCMode, Unit->getSema(), testAct, ARCMTMacroLocs); |
Fariborz Jahanian | aa7b9aa | 2012-01-25 00:20:29 +0000 | [diff] [blame] | 300 | pass.setNSAllocReallocError(NoNSAllocReallocError); |
Fariborz Jahanian | 0c859d6 | 2012-01-26 00:08:04 +0000 | [diff] [blame] | 301 | pass.setNoFinalizeRemoval(NoFinalizeRemoval); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 302 | |
| 303 | for (unsigned i=0, e = transforms.size(); i != e; ++i) |
| 304 | transforms[i](pass); |
| 305 | |
| 306 | capturedDiags.reportDiagnostics(*Diags); |
| 307 | |
| 308 | DiagClient->EndSourceFile(); |
Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 309 | errRec.FinishCapture(); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 310 | |
Argyrios Kyrtzidis | e2e40b4 | 2011-07-14 00:17:54 +0000 | [diff] [blame] | 311 | // If we are migrating code that gets the '-fobjc-arc' flag, make sure |
| 312 | // 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] | 313 | origCI.getLangOpts()->ObjCAutoRefCount = false; |
Argyrios Kyrtzidis | e2e40b4 | 2011-07-14 00:17:54 +0000 | [diff] [blame] | 314 | |
Argyrios Kyrtzidis | 73a0d32 | 2011-07-18 07:44:45 +0000 | [diff] [blame] | 315 | return capturedDiags.hasErrors() || testAct.hasReportedErrors(); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | //===----------------------------------------------------------------------===// |
| 319 | // applyTransformations. |
| 320 | //===----------------------------------------------------------------------===// |
| 321 | |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 322 | static bool applyTransforms(CompilerInvocation &origCI, |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 323 | const FrontendInputFile &Input, |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 324 | DiagnosticConsumer *DiagClient, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 325 | StringRef outputDir, |
Argyrios Kyrtzidis | d571363 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 326 | bool emitPremigrationARCErrors, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 327 | StringRef plistOut) { |
Ted Kremenek | 8cf47df | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 328 | if (!origCI.getLangOpts()->ObjC1) |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 329 | return false; |
| 330 | |
Ted Kremenek | 8cf47df | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 331 | LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC(); |
Argyrios Kyrtzidis | d208ef9 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 332 | |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 333 | // Make sure checking is successful first. |
| 334 | CompilerInvocation CInvokForCheck(origCI); |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 335 | if (arcmt::checkForManualIssues(CInvokForCheck, Input, DiagClient, |
Argyrios Kyrtzidis | d571363 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 336 | emitPremigrationARCErrors, plistOut)) |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 337 | return true; |
| 338 | |
| 339 | CompilerInvocation CInvok(origCI); |
| 340 | CInvok.getFrontendOpts().Inputs.clear(); |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 341 | CInvok.getFrontendOpts().Inputs.push_back(Input); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 342 | |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 343 | MigrationProcess migration(CInvok, DiagClient, outputDir); |
Fariborz Jahanian | 48fd81b | 2012-01-26 20:57:58 +0000 | [diff] [blame] | 344 | bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval; |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 345 | |
Fariborz Jahanian | 48fd81b | 2012-01-26 20:57:58 +0000 | [diff] [blame] | 346 | std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode, |
| 347 | NoFinalizeRemoval); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 348 | assert(!transforms.empty()); |
| 349 | |
| 350 | for (unsigned i=0, e = transforms.size(); i != e; ++i) { |
| 351 | bool err = migration.applyTransform(transforms[i]); |
| 352 | if (err) return true; |
| 353 | } |
| 354 | |
Dylan Noblesmith | c95d819 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 355 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 356 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 357 | new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(), |
| 358 | DiagClient, /*ShouldOwnClient=*/false)); |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 359 | |
| 360 | if (outputDir.empty()) { |
Ted Kremenek | 8cf47df | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 361 | origCI.getLangOpts()->ObjCAutoRefCount = true; |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 362 | return migration.getRemapper().overwriteOriginal(*Diags); |
Argyrios Kyrtzidis | e2e40b4 | 2011-07-14 00:17:54 +0000 | [diff] [blame] | 363 | } else { |
| 364 | // If we are migrating code that gets the '-fobjc-arc' flag, make sure |
| 365 | // 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] | 366 | origCI.getLangOpts()->ObjCAutoRefCount = false; |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 367 | return migration.getRemapper().flushToDisk(outputDir, *Diags); |
Argyrios Kyrtzidis | e2e40b4 | 2011-07-14 00:17:54 +0000 | [diff] [blame] | 368 | } |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | bool arcmt::applyTransformations(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) { |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 374 | return applyTransforms(origCI, Input, DiagClient, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 375 | StringRef(), false, StringRef()); |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | bool arcmt::migrateWithTemporaryFiles(CompilerInvocation &origCI, |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 379 | const FrontendInputFile &Input, |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 380 | DiagnosticConsumer *DiagClient, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 381 | StringRef outputDir, |
Argyrios Kyrtzidis | d571363 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 382 | bool emitPremigrationARCErrors, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 383 | StringRef plistOut) { |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 384 | assert(!outputDir.empty() && "Expected output directory path"); |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 385 | return applyTransforms(origCI, Input, DiagClient, |
Argyrios Kyrtzidis | d571363 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 386 | outputDir, emitPremigrationARCErrors, plistOut); |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | bool arcmt::getFileRemappings(std::vector<std::pair<std::string,std::string> > & |
| 390 | remap, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 391 | StringRef outputDir, |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 392 | DiagnosticConsumer *DiagClient) { |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 393 | assert(!outputDir.empty()); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 394 | |
Dylan Noblesmith | c95d819 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 395 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 396 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 397 | new DiagnosticsEngine(DiagID, new DiagnosticOptions, |
| 398 | DiagClient, /*ShouldOwnClient=*/false)); |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 399 | |
| 400 | FileRemapper remapper; |
| 401 | bool err = remapper.initFromDisk(outputDir, *Diags, |
| 402 | /*ignoreIfFilesChanged=*/true); |
| 403 | if (err) |
| 404 | return true; |
| 405 | |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 406 | PreprocessorOptions PPOpts; |
| 407 | remapper.applyMappings(PPOpts); |
| 408 | remap = PPOpts.RemappedFiles; |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 409 | |
| 410 | return false; |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 413 | bool arcmt::getFileRemappingsFromFileList( |
| 414 | std::vector<std::pair<std::string,std::string> > &remap, |
| 415 | ArrayRef<StringRef> remapFiles, |
| 416 | DiagnosticConsumer *DiagClient) { |
| 417 | bool hasErrorOccurred = false; |
| 418 | llvm::StringMap<bool> Uniquer; |
| 419 | |
| 420 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 421 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 422 | new DiagnosticsEngine(DiagID, new DiagnosticOptions, |
| 423 | DiagClient, /*ShouldOwnClient=*/false)); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 424 | |
| 425 | for (ArrayRef<StringRef>::iterator |
| 426 | I = remapFiles.begin(), E = remapFiles.end(); I != E; ++I) { |
| 427 | StringRef file = *I; |
| 428 | |
| 429 | FileRemapper remapper; |
| 430 | bool err = remapper.initFromFile(file, *Diags, |
| 431 | /*ignoreIfFilesChanged=*/true); |
| 432 | hasErrorOccurred = hasErrorOccurred || err; |
| 433 | if (err) |
| 434 | continue; |
| 435 | |
| 436 | PreprocessorOptions PPOpts; |
| 437 | remapper.applyMappings(PPOpts); |
| 438 | for (PreprocessorOptions::remapped_file_iterator |
| 439 | RI = PPOpts.remapped_file_begin(), RE = PPOpts.remapped_file_end(); |
| 440 | RI != RE; ++RI) { |
| 441 | bool &inserted = Uniquer[RI->first]; |
| 442 | if (inserted) |
| 443 | continue; |
| 444 | inserted = true; |
| 445 | remap.push_back(*RI); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | return hasErrorOccurred; |
| 450 | } |
| 451 | |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 452 | //===----------------------------------------------------------------------===// |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 453 | // CollectTransformActions. |
| 454 | //===----------------------------------------------------------------------===// |
| 455 | |
| 456 | namespace { |
| 457 | |
| 458 | class ARCMTMacroTrackerPPCallbacks : public PPCallbacks { |
| 459 | std::vector<SourceLocation> &ARCMTMacroLocs; |
| 460 | |
| 461 | public: |
| 462 | ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs) |
| 463 | : ARCMTMacroLocs(ARCMTMacroLocs) { } |
| 464 | |
Argyrios Kyrtzidis | 85a14bb | 2011-08-18 01:05:45 +0000 | [diff] [blame] | 465 | virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo *MI, |
| 466 | SourceRange Range) { |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 467 | if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName()) |
| 468 | ARCMTMacroLocs.push_back(MacroNameTok.getLocation()); |
| 469 | } |
| 470 | }; |
| 471 | |
| 472 | class ARCMTMacroTrackerAction : public ASTFrontendAction { |
| 473 | std::vector<SourceLocation> &ARCMTMacroLocs; |
| 474 | |
| 475 | public: |
| 476 | ARCMTMacroTrackerAction(std::vector<SourceLocation> &ARCMTMacroLocs) |
| 477 | : ARCMTMacroLocs(ARCMTMacroLocs) { } |
| 478 | |
| 479 | virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 480 | StringRef InFile) { |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 481 | CI.getPreprocessor().addPPCallbacks( |
| 482 | new ARCMTMacroTrackerPPCallbacks(ARCMTMacroLocs)); |
| 483 | return new ASTConsumer(); |
| 484 | } |
| 485 | }; |
| 486 | |
| 487 | class RewritesApplicator : public TransformActions::RewriteReceiver { |
| 488 | Rewriter &rewriter; |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 489 | MigrationProcess::RewriteListener *Listener; |
| 490 | |
| 491 | public: |
| 492 | RewritesApplicator(Rewriter &rewriter, ASTContext &ctx, |
| 493 | MigrationProcess::RewriteListener *listener) |
Benjamin Kramer | d1d76b2 | 2012-06-06 17:32:50 +0000 | [diff] [blame] | 494 | : rewriter(rewriter), Listener(listener) { |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 495 | if (Listener) |
| 496 | Listener->start(ctx); |
| 497 | } |
| 498 | ~RewritesApplicator() { |
| 499 | if (Listener) |
| 500 | Listener->finish(); |
| 501 | } |
| 502 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 503 | virtual void insert(SourceLocation loc, StringRef text) { |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 504 | bool err = rewriter.InsertText(loc, text, /*InsertAfter=*/true, |
| 505 | /*indentNewLines=*/true); |
| 506 | if (!err && Listener) |
| 507 | Listener->insert(loc, text); |
| 508 | } |
| 509 | |
| 510 | virtual void remove(CharSourceRange range) { |
| 511 | Rewriter::RewriteOptions removeOpts; |
| 512 | removeOpts.IncludeInsertsAtBeginOfRange = false; |
| 513 | removeOpts.IncludeInsertsAtEndOfRange = false; |
| 514 | removeOpts.RemoveLineIfEmpty = true; |
| 515 | |
| 516 | bool err = rewriter.RemoveText(range, removeOpts); |
| 517 | if (!err && Listener) |
| 518 | Listener->remove(range); |
| 519 | } |
| 520 | |
| 521 | virtual void increaseIndentation(CharSourceRange range, |
| 522 | SourceLocation parentIndent) { |
| 523 | rewriter.IncreaseIndentation(range, parentIndent); |
| 524 | } |
| 525 | }; |
| 526 | |
| 527 | } // end anonymous namespace. |
| 528 | |
| 529 | /// \brief Anchor for VTable. |
| 530 | MigrationProcess::RewriteListener::~RewriteListener() { } |
| 531 | |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 532 | MigrationProcess::MigrationProcess(const CompilerInvocation &CI, |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 533 | DiagnosticConsumer *diagClient, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 534 | StringRef outputDir) |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 535 | : OrigCI(CI), DiagClient(diagClient) { |
| 536 | if (!outputDir.empty()) { |
Dylan Noblesmith | c95d819 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 537 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 538 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 539 | new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(), |
| 540 | DiagClient, /*ShouldOwnClient=*/false)); |
Argyrios Kyrtzidis | 7fbd97f | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 541 | Remapper.initFromDisk(outputDir, *Diags, /*ignoreIfFilesChanges=*/true); |
| 542 | } |
| 543 | } |
| 544 | |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 545 | bool MigrationProcess::applyTransform(TransformFn trans, |
| 546 | RewriteListener *listener) { |
Dylan Noblesmith | e277899 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 547 | OwningPtr<CompilerInvocation> CInvok; |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 548 | CInvok.reset(createInvocationForMigration(OrigCI)); |
| 549 | CInvok->getDiagnosticOpts().IgnoreWarnings = true; |
| 550 | |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 551 | Remapper.applyMappings(CInvok->getPreprocessorOpts()); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 552 | |
| 553 | CapturedDiagList capturedDiags; |
| 554 | std::vector<SourceLocation> ARCMTMacroLocs; |
| 555 | |
| 556 | assert(DiagClient); |
Dylan Noblesmith | c95d819 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 557 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 558 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 559 | new DiagnosticsEngine(DiagID, new DiagnosticOptions, |
| 560 | DiagClient, /*ShouldOwnClient=*/false)); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 561 | |
| 562 | // Filter of all diagnostics. |
Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 563 | CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 564 | Diags->setClient(&errRec, /*ShouldOwnClient=*/false); |
| 565 | |
Dylan Noblesmith | e277899 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 566 | OwningPtr<ARCMTMacroTrackerAction> ASTAction; |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 567 | ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs)); |
| 568 | |
Dylan Noblesmith | e277899 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 569 | OwningPtr<ASTUnit> Unit( |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 570 | ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags, |
| 571 | ASTAction.get())); |
Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 572 | if (!Unit) { |
| 573 | errRec.FinishCapture(); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 574 | return true; |
Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 575 | } |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 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 | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 585 | DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor()); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 586 | capturedDiags.reportDiagnostics(*Diags); |
| 587 | DiagClient->EndSourceFile(); |
Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 588 | errRec.FinishCapture(); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 589 | return true; |
| 590 | } |
| 591 | |
| 592 | // After parsing of source files ended, we want to reuse the |
| 593 | // diagnostics objects to emit further diagnostics. |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 594 | // We call BeginSourceFile because DiagnosticConsumer requires that |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 595 | // diagnostics with source range information are emitted only in between |
| 596 | // BeginSourceFile() and EndSourceFile(). |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 597 | DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor()); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 598 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 599 | Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts()); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 600 | TransformActions TA(*Diags, capturedDiags, Ctx, Unit->getPreprocessor()); |
Ted Kremenek | 8cf47df | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 601 | MigrationPass pass(Ctx, OrigCI.getLangOpts()->getGC(), |
Argyrios Kyrtzidis | d208ef9 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 602 | Unit->getSema(), TA, ARCMTMacroLocs); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 603 | |
| 604 | trans(pass); |
| 605 | |
| 606 | { |
| 607 | RewritesApplicator applicator(rewriter, Ctx, listener); |
| 608 | TA.applyRewrites(applicator); |
| 609 | } |
| 610 | |
| 611 | DiagClient->EndSourceFile(); |
Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 612 | errRec.FinishCapture(); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 613 | |
| 614 | if (DiagClient->getNumErrors()) |
| 615 | return true; |
| 616 | |
| 617 | for (Rewriter::buffer_iterator |
| 618 | I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) { |
| 619 | FileID FID = I->first; |
| 620 | RewriteBuffer &buf = I->second; |
| 621 | const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID); |
| 622 | assert(file); |
| 623 | std::string newFname = file->getName(); |
| 624 | newFname += "-trans"; |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 625 | SmallString<512> newText; |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 626 | llvm::raw_svector_ostream vecOS(newText); |
| 627 | buf.write(vecOS); |
| 628 | vecOS.flush(); |
| 629 | llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy( |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 630 | StringRef(newText.data(), newText.size()), newFname); |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 631 | SmallString<64> filePath(file->getName()); |
John McCall | d70fb98 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 632 | Unit->getFileManager().FixupRelativePath(filePath); |
| 633 | Remapper.remap(filePath.str(), memBuf); |
| 634 | } |
| 635 | |
| 636 | return false; |
| 637 | } |