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