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" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTConsumer.h" |
| 12 | #include "clang/Basic/DiagnosticCategories.h" |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 13 | #include "clang/Frontend/ASTUnit.h" |
| 14 | #include "clang/Frontend/CompilerInstance.h" |
Douglas Gregor | 1f6b2b5 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/FrontendAction.h" |
Argyrios Kyrtzidis | 7ee2049 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 16 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/Utils.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Preprocessor.h" |
Ted Kremenek | 305c613 | 2012-09-01 05:09:24 +0000 | [diff] [blame] | 19 | #include "clang/Rewrite/Core/Rewriter.h" |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 20 | #include "clang/Sema/SemaDiagnostic.h" |
Argyrios Kyrtzidis | ed351e6 | 2013-02-05 16:37:00 +0000 | [diff] [blame] | 21 | #include "clang/Serialization/ASTReader.h" |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | using namespace arcmt; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 27 | bool CapturedDiagList::clearDiagnostic(ArrayRef<unsigned> IDs, |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 28 | SourceRange range) { |
| 29 | if (range.isInvalid()) |
| 30 | return false; |
| 31 | |
| 32 | bool cleared = false; |
| 33 | ListTy::iterator I = List.begin(); |
| 34 | while (I != List.end()) { |
| 35 | FullSourceLoc diagLoc = I->getLocation(); |
| 36 | if ((IDs.empty() || // empty means clear all diagnostics in the range. |
| 37 | std::find(IDs.begin(), IDs.end(), I->getID()) != IDs.end()) && |
| 38 | !diagLoc.isBeforeInTranslationUnitThan(range.getBegin()) && |
| 39 | (diagLoc == range.getEnd() || |
| 40 | diagLoc.isBeforeInTranslationUnitThan(range.getEnd()))) { |
| 41 | cleared = true; |
| 42 | ListTy::iterator eraseS = I++; |
Argyrios Kyrtzidis | ea2224d | 2013-01-04 18:30:08 +0000 | [diff] [blame] | 43 | if (eraseS->getLevel() != DiagnosticsEngine::Note) |
| 44 | while (I != List.end() && I->getLevel() == DiagnosticsEngine::Note) |
| 45 | ++I; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 46 | // Clear the diagnostic and any notes following it. |
Joao Matos | e4d90e1 | 2012-08-31 17:28:09 +0000 | [diff] [blame] | 47 | I = List.erase(eraseS, I); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 48 | continue; |
| 49 | } |
| 50 | |
| 51 | ++I; |
| 52 | } |
| 53 | |
| 54 | return cleared; |
| 55 | } |
| 56 | |
Chris Lattner | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 57 | bool CapturedDiagList::hasDiagnostic(ArrayRef<unsigned> IDs, |
Argyrios Kyrtzidis | 60a5e3f | 2011-06-18 00:53:34 +0000 | [diff] [blame] | 58 | SourceRange range) const { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 59 | if (range.isInvalid()) |
| 60 | return false; |
| 61 | |
Argyrios Kyrtzidis | 60a5e3f | 2011-06-18 00:53:34 +0000 | [diff] [blame] | 62 | ListTy::const_iterator I = List.begin(); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 63 | while (I != List.end()) { |
| 64 | FullSourceLoc diagLoc = I->getLocation(); |
| 65 | if ((IDs.empty() || // empty means any diagnostic in the range. |
| 66 | std::find(IDs.begin(), IDs.end(), I->getID()) != IDs.end()) && |
| 67 | !diagLoc.isBeforeInTranslationUnitThan(range.getBegin()) && |
| 68 | (diagLoc == range.getEnd() || |
| 69 | diagLoc.isBeforeInTranslationUnitThan(range.getEnd()))) { |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | ++I; |
| 74 | } |
| 75 | |
| 76 | return false; |
| 77 | } |
| 78 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 79 | void CapturedDiagList::reportDiagnostics(DiagnosticsEngine &Diags) const { |
Argyrios Kyrtzidis | 60a5e3f | 2011-06-18 00:53:34 +0000 | [diff] [blame] | 80 | 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] | 81 | Diags.Report(*I); |
| 82 | } |
| 83 | |
Argyrios Kyrtzidis | e665d69 | 2011-06-18 00:53:41 +0000 | [diff] [blame] | 84 | bool CapturedDiagList::hasErrors() const { |
| 85 | 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] | 86 | if (I->getLevel() >= DiagnosticsEngine::Error) |
Argyrios Kyrtzidis | e665d69 | 2011-06-18 00:53:41 +0000 | [diff] [blame] | 87 | return true; |
| 88 | |
| 89 | return false; |
| 90 | } |
| 91 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 92 | namespace { |
| 93 | |
David Blaikie | aee37ba | 2011-09-25 23:54:33 +0000 | [diff] [blame] | 94 | class CaptureDiagnosticConsumer : public DiagnosticConsumer { |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 95 | DiagnosticsEngine &Diags; |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 96 | DiagnosticConsumer &DiagClient; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 97 | CapturedDiagList &CapturedDiags; |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 98 | bool HasBegunSourceFile; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 99 | public: |
David Blaikie | aee37ba | 2011-09-25 23:54:33 +0000 | [diff] [blame] | 100 | CaptureDiagnosticConsumer(DiagnosticsEngine &diags, |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 101 | DiagnosticConsumer &client, |
| 102 | CapturedDiagList &capturedDiags) |
| 103 | : Diags(diags), DiagClient(client), CapturedDiags(capturedDiags), |
| 104 | HasBegunSourceFile(false) { } |
| 105 | |
| 106 | virtual void BeginSourceFile(const LangOptions &Opts, |
| 107 | const Preprocessor *PP) { |
| 108 | // Pass BeginSourceFile message onto DiagClient on first call. |
| 109 | // The corresponding EndSourceFile call will be made from an |
| 110 | // explicit call to FinishCapture. |
| 111 | if (!HasBegunSourceFile) { |
| 112 | DiagClient.BeginSourceFile(Opts, PP); |
| 113 | HasBegunSourceFile = true; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void FinishCapture() { |
| 118 | // Call EndSourceFile on DiagClient on completion of capture to |
| 119 | // enable VerifyDiagnosticConsumer to check diagnostics *after* |
| 120 | // it has received the diagnostic list. |
| 121 | if (HasBegunSourceFile) { |
| 122 | DiagClient.EndSourceFile(); |
| 123 | HasBegunSourceFile = false; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | virtual ~CaptureDiagnosticConsumer() { |
| 128 | assert(!HasBegunSourceFile && "FinishCapture not called!"); |
| 129 | } |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 130 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 131 | virtual void HandleDiagnostic(DiagnosticsEngine::Level level, |
David Blaikie | 40847cf | 2011-09-26 01:18:08 +0000 | [diff] [blame] | 132 | const Diagnostic &Info) { |
Ted Kremenek | afdc21a | 2011-10-20 05:07:47 +0000 | [diff] [blame] | 133 | if (DiagnosticIDs::isARCDiagnostic(Info.getID()) || |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 134 | level >= DiagnosticsEngine::Error || level == DiagnosticsEngine::Note) { |
Argyrios Kyrtzidis | 489feee | 2012-12-12 22:48:28 +0000 | [diff] [blame] | 135 | if (Info.getLocation().isValid()) |
| 136 | CapturedDiags.push_back(StoredDiagnostic(level, Info)); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 137 | return; |
| 138 | } |
| 139 | |
| 140 | // Non-ARC warnings are ignored. |
| 141 | Diags.setLastDiagnosticIgnored(); |
| 142 | } |
| 143 | }; |
| 144 | |
| 145 | } // end anonymous namespace |
| 146 | |
Argyrios Kyrtzidis | 9c47973 | 2011-06-20 19:59:52 +0000 | [diff] [blame] | 147 | static bool HasARCRuntime(CompilerInvocation &origCI) { |
| 148 | // This duplicates some functionality from Darwin::AddDeploymentTarget |
| 149 | // but this function is well defined, so keep it decoupled from the driver |
| 150 | // and avoid unrelated complications. |
Argyrios Kyrtzidis | 9c47973 | 2011-06-20 19:59:52 +0000 | [diff] [blame] | 151 | llvm::Triple triple(origCI.getTargetOpts().Triple); |
| 152 | |
Cameron Esfahani | 57b1da1 | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 153 | if (triple.isiOS()) |
Argyrios Kyrtzidis | 9c47973 | 2011-06-20 19:59:52 +0000 | [diff] [blame] | 154 | return triple.getOSMajorVersion() >= 5; |
| 155 | |
| 156 | if (triple.getOS() == llvm::Triple::Darwin) |
| 157 | return triple.getOSMajorVersion() >= 11; |
| 158 | |
| 159 | if (triple.getOS() == llvm::Triple::MacOSX) { |
| 160 | unsigned Major, Minor, Micro; |
| 161 | triple.getOSVersion(Major, Minor, Micro); |
| 162 | return Major > 10 || (Major == 10 && Minor >= 7); |
| 163 | } |
| 164 | |
| 165 | return false; |
| 166 | } |
| 167 | |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 168 | static CompilerInvocation * |
| 169 | createInvocationForMigration(CompilerInvocation &origCI) { |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 170 | OwningPtr<CompilerInvocation> CInvok; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 171 | CInvok.reset(new CompilerInvocation(origCI)); |
Argyrios Kyrtzidis | ed351e6 | 2013-02-05 16:37:00 +0000 | [diff] [blame] | 172 | PreprocessorOptions &PPOpts = CInvok->getPreprocessorOpts(); |
| 173 | if (!PPOpts.ImplicitPCHInclude.empty()) { |
| 174 | // We can't use a PCH because it was likely built in non-ARC mode and we |
| 175 | // want to parse in ARC. Include the original header. |
| 176 | FileManager FileMgr(origCI.getFileSystemOpts()); |
| 177 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 178 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
| 179 | new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(), |
| 180 | new IgnoringDiagConsumer())); |
| 181 | std::string OriginalFile = |
| 182 | ASTReader::getOriginalSourceFile(PPOpts.ImplicitPCHInclude, |
| 183 | FileMgr, *Diags); |
| 184 | if (!OriginalFile.empty()) |
| 185 | PPOpts.Includes.insert(PPOpts.Includes.begin(), OriginalFile); |
| 186 | PPOpts.ImplicitPCHInclude.clear(); |
| 187 | } |
| 188 | // FIXME: Get the original header of a PTH as well. |
| 189 | CInvok->getPreprocessorOpts().ImplicitPTHInclude.clear(); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 190 | std::string define = getARCMTMacroName(); |
| 191 | define += '='; |
| 192 | CInvok->getPreprocessorOpts().addMacroDef(define); |
Ted Kremenek | d3b74d9 | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 193 | CInvok->getLangOpts()->ObjCAutoRefCount = true; |
| 194 | CInvok->getLangOpts()->setGC(LangOptions::NonGC); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 195 | CInvok->getDiagnosticOpts().ErrorLimit = 0; |
Argyrios Kyrtzidis | 1912982 | 2012-06-20 01:46:26 +0000 | [diff] [blame] | 196 | CInvok->getDiagnosticOpts().PedanticErrors = 0; |
Argyrios Kyrtzidis | ffe76dd | 2012-06-20 01:10:40 +0000 | [diff] [blame] | 197 | |
| 198 | // Ignore -Werror flags when migrating. |
| 199 | std::vector<std::string> WarnOpts; |
| 200 | for (std::vector<std::string>::iterator |
| 201 | I = CInvok->getDiagnosticOpts().Warnings.begin(), |
| 202 | E = CInvok->getDiagnosticOpts().Warnings.end(); I != E; ++I) { |
| 203 | if (!StringRef(*I).startswith("error")) |
| 204 | WarnOpts.push_back(*I); |
| 205 | } |
| 206 | WarnOpts.push_back("error=arc-unsafe-retained-assign"); |
Argyrios Kyrtzidis | 1912982 | 2012-06-20 01:46:26 +0000 | [diff] [blame] | 207 | CInvok->getDiagnosticOpts().Warnings = llvm_move(WarnOpts); |
Argyrios Kyrtzidis | ffe76dd | 2012-06-20 01:10:40 +0000 | [diff] [blame] | 208 | |
John McCall | 0a7dd78 | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 209 | CInvok->getLangOpts()->ObjCARCWeak = HasARCRuntime(origCI); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 210 | |
| 211 | return CInvok.take(); |
| 212 | } |
| 213 | |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 214 | static void emitPremigrationErrors(const CapturedDiagList &arcDiags, |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 215 | DiagnosticOptions *diagOpts, |
Benjamin Kramer | 39997fc | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 216 | Preprocessor &PP) { |
Argyrios Kyrtzidis | 7ee2049 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 217 | TextDiagnosticPrinter printer(llvm::errs(), diagOpts); |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 218 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 219 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 220 | new DiagnosticsEngine(DiagID, diagOpts, &printer, |
| 221 | /*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( |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 259 | new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(), |
| 260 | DiagClient, /*ShouldOwnClient=*/false)); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 261 | |
| 262 | // Filter of all diagnostics. |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 263 | CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 264 | Diags->setClient(&errRec, /*ShouldOwnClient=*/false); |
| 265 | |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 266 | OwningPtr<ASTUnit> Unit( |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 267 | ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags)); |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 268 | if (!Unit) { |
| 269 | errRec.FinishCapture(); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 270 | return true; |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 271 | } |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 272 | |
| 273 | // Don't filter diagnostics anymore. |
| 274 | Diags->setClient(DiagClient, /*ShouldOwnClient=*/false); |
| 275 | |
| 276 | ASTContext &Ctx = Unit->getASTContext(); |
| 277 | |
| 278 | if (Diags->hasFatalErrorOccurred()) { |
| 279 | Diags->Reset(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 280 | DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor()); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 281 | capturedDiags.reportDiagnostics(*Diags); |
| 282 | DiagClient->EndSourceFile(); |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 283 | errRec.FinishCapture(); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 284 | return true; |
| 285 | } |
| 286 | |
Argyrios Kyrtzidis | 7ee2049 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 287 | if (emitPremigrationARCErrors) |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 288 | emitPremigrationErrors(capturedDiags, &origCI.getDiagnosticOpts(), |
Argyrios Kyrtzidis | 7ee2049 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 289 | Unit->getPreprocessor()); |
| 290 | if (!plistOut.empty()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 291 | SmallVector<StoredDiagnostic, 8> arcDiags; |
Argyrios Kyrtzidis | 7ee2049 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 292 | for (CapturedDiagList::iterator |
| 293 | I = capturedDiags.begin(), E = capturedDiags.end(); I != E; ++I) |
| 294 | arcDiags.push_back(*I); |
| 295 | writeARCDiagsToPlist(plistOut, arcDiags, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 296 | Ctx.getSourceManager(), Ctx.getLangOpts()); |
Argyrios Kyrtzidis | 7ee2049 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 297 | } |
| 298 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 299 | // After parsing of source files ended, we want to reuse the |
| 300 | // diagnostics objects to emit further diagnostics. |
David Blaikie | 78ad0b9 | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 301 | // We call BeginSourceFile because DiagnosticConsumer requires that |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 302 | // diagnostics with source range information are emitted only in between |
| 303 | // BeginSourceFile() and EndSourceFile(). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 304 | DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor()); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 305 | |
| 306 | // No macros will be added since we are just checking and we won't modify |
| 307 | // source code. |
| 308 | std::vector<SourceLocation> ARCMTMacroLocs; |
| 309 | |
| 310 | TransformActions testAct(*Diags, capturedDiags, Ctx, Unit->getPreprocessor()); |
Argyrios Kyrtzidis | ea2224d | 2013-01-04 18:30:08 +0000 | [diff] [blame] | 311 | MigrationPass pass(Ctx, OrigGCMode, Unit->getSema(), testAct, capturedDiags, |
| 312 | ARCMTMacroLocs); |
Fariborz Jahanian | b5c6bab | 2012-01-25 00:20:29 +0000 | [diff] [blame] | 313 | pass.setNSAllocReallocError(NoNSAllocReallocError); |
Fariborz Jahanian | 26f0e4e | 2012-01-26 00:08:04 +0000 | [diff] [blame] | 314 | pass.setNoFinalizeRemoval(NoFinalizeRemoval); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 315 | |
| 316 | for (unsigned i=0, e = transforms.size(); i != e; ++i) |
| 317 | transforms[i](pass); |
| 318 | |
| 319 | capturedDiags.reportDiagnostics(*Diags); |
| 320 | |
| 321 | DiagClient->EndSourceFile(); |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 322 | errRec.FinishCapture(); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 323 | |
Argyrios Kyrtzidis | fd10398 | 2011-07-18 07:44:45 +0000 | [diff] [blame] | 324 | return capturedDiags.hasErrors() || testAct.hasReportedErrors(); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | //===----------------------------------------------------------------------===// |
| 328 | // applyTransformations. |
| 329 | //===----------------------------------------------------------------------===// |
| 330 | |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 331 | static bool applyTransforms(CompilerInvocation &origCI, |
Douglas Gregor | 1f6b2b5 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 332 | const FrontendInputFile &Input, |
David Blaikie | 78ad0b9 | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 333 | DiagnosticConsumer *DiagClient, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 334 | StringRef outputDir, |
Argyrios Kyrtzidis | 7ee2049 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 335 | bool emitPremigrationARCErrors, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 336 | StringRef plistOut) { |
Ted Kremenek | d3b74d9 | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 337 | if (!origCI.getLangOpts()->ObjC1) |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 338 | return false; |
| 339 | |
Ted Kremenek | d3b74d9 | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 340 | LangOptions::GCMode OrigGCMode = origCI.getLangOpts()->getGC(); |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 341 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 342 | // Make sure checking is successful first. |
| 343 | CompilerInvocation CInvokForCheck(origCI); |
Douglas Gregor | 1f6b2b5 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 344 | if (arcmt::checkForManualIssues(CInvokForCheck, Input, DiagClient, |
Argyrios Kyrtzidis | 7ee2049 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 345 | emitPremigrationARCErrors, plistOut)) |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 346 | return true; |
| 347 | |
| 348 | CompilerInvocation CInvok(origCI); |
| 349 | CInvok.getFrontendOpts().Inputs.clear(); |
Douglas Gregor | 1f6b2b5 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 350 | CInvok.getFrontendOpts().Inputs.push_back(Input); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 351 | |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 352 | MigrationProcess migration(CInvok, DiagClient, outputDir); |
Fariborz Jahanian | bbdfad5 | 2012-01-26 20:57:58 +0000 | [diff] [blame] | 353 | bool NoFinalizeRemoval = origCI.getMigratorOpts().NoFinalizeRemoval; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 354 | |
Fariborz Jahanian | bbdfad5 | 2012-01-26 20:57:58 +0000 | [diff] [blame] | 355 | std::vector<TransformFn> transforms = arcmt::getAllTransformations(OrigGCMode, |
| 356 | NoFinalizeRemoval); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 357 | assert(!transforms.empty()); |
| 358 | |
| 359 | for (unsigned i=0, e = transforms.size(); i != e; ++i) { |
| 360 | bool err = migration.applyTransform(transforms[i]); |
| 361 | if (err) return true; |
| 362 | } |
| 363 | |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 364 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 365 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 366 | new DiagnosticsEngine(DiagID, &origCI.getDiagnosticOpts(), |
| 367 | DiagClient, /*ShouldOwnClient=*/false)); |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 368 | |
| 369 | if (outputDir.empty()) { |
Ted Kremenek | d3b74d9 | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 370 | origCI.getLangOpts()->ObjCAutoRefCount = true; |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 371 | return migration.getRemapper().overwriteOriginal(*Diags); |
Argyrios Kyrtzidis | a944b12 | 2011-07-14 00:17:54 +0000 | [diff] [blame] | 372 | } else { |
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( |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 403 | new DiagnosticsEngine(DiagID, new DiagnosticOptions, |
| 404 | DiagClient, /*ShouldOwnClient=*/false)); |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 405 | |
| 406 | FileRemapper remapper; |
| 407 | bool err = remapper.initFromDisk(outputDir, *Diags, |
| 408 | /*ignoreIfFilesChanged=*/true); |
| 409 | if (err) |
| 410 | return true; |
| 411 | |
Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 412 | PreprocessorOptions PPOpts; |
| 413 | remapper.applyMappings(PPOpts); |
| 414 | remap = PPOpts.RemappedFiles; |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 415 | |
| 416 | return false; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 419 | bool arcmt::getFileRemappingsFromFileList( |
| 420 | std::vector<std::pair<std::string,std::string> > &remap, |
| 421 | ArrayRef<StringRef> remapFiles, |
| 422 | DiagnosticConsumer *DiagClient) { |
| 423 | bool hasErrorOccurred = false; |
| 424 | llvm::StringMap<bool> Uniquer; |
| 425 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 426 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 427 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 428 | new DiagnosticsEngine(DiagID, new DiagnosticOptions, |
| 429 | DiagClient, /*ShouldOwnClient=*/false)); |
Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 430 | |
| 431 | for (ArrayRef<StringRef>::iterator |
| 432 | I = remapFiles.begin(), E = remapFiles.end(); I != E; ++I) { |
| 433 | StringRef file = *I; |
| 434 | |
| 435 | FileRemapper remapper; |
| 436 | bool err = remapper.initFromFile(file, *Diags, |
| 437 | /*ignoreIfFilesChanged=*/true); |
| 438 | hasErrorOccurred = hasErrorOccurred || err; |
| 439 | if (err) |
| 440 | continue; |
| 441 | |
| 442 | PreprocessorOptions PPOpts; |
| 443 | remapper.applyMappings(PPOpts); |
| 444 | for (PreprocessorOptions::remapped_file_iterator |
| 445 | RI = PPOpts.remapped_file_begin(), RE = PPOpts.remapped_file_end(); |
| 446 | RI != RE; ++RI) { |
| 447 | bool &inserted = Uniquer[RI->first]; |
| 448 | if (inserted) |
| 449 | continue; |
| 450 | inserted = true; |
| 451 | remap.push_back(*RI); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | return hasErrorOccurred; |
| 456 | } |
| 457 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 458 | //===----------------------------------------------------------------------===// |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 459 | // CollectTransformActions. |
| 460 | //===----------------------------------------------------------------------===// |
| 461 | |
| 462 | namespace { |
| 463 | |
| 464 | class ARCMTMacroTrackerPPCallbacks : public PPCallbacks { |
| 465 | std::vector<SourceLocation> &ARCMTMacroLocs; |
| 466 | |
| 467 | public: |
| 468 | ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs) |
| 469 | : ARCMTMacroLocs(ARCMTMacroLocs) { } |
| 470 | |
Argyrios Kyrtzidis | c515978 | 2013-02-24 00:05:14 +0000 | [diff] [blame] | 471 | virtual void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD, |
Argyrios Kyrtzidis | dd08a0c | 2013-05-03 22:31:32 +0000 | [diff] [blame] | 472 | SourceRange Range, const MacroArgs *Args) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 473 | if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName()) |
| 474 | ARCMTMacroLocs.push_back(MacroNameTok.getLocation()); |
| 475 | } |
| 476 | }; |
| 477 | |
| 478 | class ARCMTMacroTrackerAction : public ASTFrontendAction { |
| 479 | std::vector<SourceLocation> &ARCMTMacroLocs; |
| 480 | |
| 481 | public: |
| 482 | ARCMTMacroTrackerAction(std::vector<SourceLocation> &ARCMTMacroLocs) |
| 483 | : ARCMTMacroLocs(ARCMTMacroLocs) { } |
| 484 | |
| 485 | virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 486 | StringRef InFile) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 487 | CI.getPreprocessor().addPPCallbacks( |
| 488 | new ARCMTMacroTrackerPPCallbacks(ARCMTMacroLocs)); |
| 489 | return new ASTConsumer(); |
| 490 | } |
| 491 | }; |
| 492 | |
| 493 | class RewritesApplicator : public TransformActions::RewriteReceiver { |
| 494 | Rewriter &rewriter; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 495 | MigrationProcess::RewriteListener *Listener; |
| 496 | |
| 497 | public: |
| 498 | RewritesApplicator(Rewriter &rewriter, ASTContext &ctx, |
| 499 | MigrationProcess::RewriteListener *listener) |
Benjamin Kramer | facde17 | 2012-06-06 17:32:50 +0000 | [diff] [blame] | 500 | : rewriter(rewriter), Listener(listener) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 501 | if (Listener) |
| 502 | Listener->start(ctx); |
| 503 | } |
| 504 | ~RewritesApplicator() { |
| 505 | if (Listener) |
| 506 | Listener->finish(); |
| 507 | } |
| 508 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 509 | virtual void insert(SourceLocation loc, StringRef text) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 510 | bool err = rewriter.InsertText(loc, text, /*InsertAfter=*/true, |
| 511 | /*indentNewLines=*/true); |
| 512 | if (!err && Listener) |
| 513 | Listener->insert(loc, text); |
| 514 | } |
| 515 | |
| 516 | virtual void remove(CharSourceRange range) { |
| 517 | Rewriter::RewriteOptions removeOpts; |
| 518 | removeOpts.IncludeInsertsAtBeginOfRange = false; |
| 519 | removeOpts.IncludeInsertsAtEndOfRange = false; |
| 520 | removeOpts.RemoveLineIfEmpty = true; |
| 521 | |
| 522 | bool err = rewriter.RemoveText(range, removeOpts); |
| 523 | if (!err && Listener) |
| 524 | Listener->remove(range); |
| 525 | } |
| 526 | |
| 527 | virtual void increaseIndentation(CharSourceRange range, |
| 528 | SourceLocation parentIndent) { |
| 529 | rewriter.IncreaseIndentation(range, parentIndent); |
| 530 | } |
| 531 | }; |
| 532 | |
| 533 | } // end anonymous namespace. |
| 534 | |
| 535 | /// \brief Anchor for VTable. |
| 536 | MigrationProcess::RewriteListener::~RewriteListener() { } |
| 537 | |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 538 | MigrationProcess::MigrationProcess(const CompilerInvocation &CI, |
David Blaikie | 78ad0b9 | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 539 | DiagnosticConsumer *diagClient, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 540 | StringRef outputDir) |
Argyrios Kyrtzidis | 0474cfd | 2013-07-22 18:13:54 +0000 | [diff] [blame] | 541 | : OrigCI(CI), DiagClient(diagClient), HadARCErrors(false) { |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 542 | if (!outputDir.empty()) { |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 543 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 544 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 545 | new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(), |
| 546 | DiagClient, /*ShouldOwnClient=*/false)); |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 547 | Remapper.initFromDisk(outputDir, *Diags, /*ignoreIfFilesChanges=*/true); |
| 548 | } |
| 549 | } |
| 550 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 551 | bool MigrationProcess::applyTransform(TransformFn trans, |
| 552 | RewriteListener *listener) { |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 553 | OwningPtr<CompilerInvocation> CInvok; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 554 | CInvok.reset(createInvocationForMigration(OrigCI)); |
| 555 | CInvok->getDiagnosticOpts().IgnoreWarnings = true; |
| 556 | |
Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 557 | Remapper.applyMappings(CInvok->getPreprocessorOpts()); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 558 | |
| 559 | CapturedDiagList capturedDiags; |
| 560 | std::vector<SourceLocation> ARCMTMacroLocs; |
| 561 | |
| 562 | assert(DiagClient); |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 563 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 564 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 565 | new DiagnosticsEngine(DiagID, new DiagnosticOptions, |
| 566 | DiagClient, /*ShouldOwnClient=*/false)); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 567 | |
| 568 | // Filter of all diagnostics. |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 569 | CaptureDiagnosticConsumer errRec(*Diags, *DiagClient, capturedDiags); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 570 | Diags->setClient(&errRec, /*ShouldOwnClient=*/false); |
| 571 | |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 572 | OwningPtr<ARCMTMacroTrackerAction> ASTAction; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 573 | ASTAction.reset(new ARCMTMacroTrackerAction(ARCMTMacroLocs)); |
| 574 | |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 575 | OwningPtr<ASTUnit> Unit( |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 576 | ASTUnit::LoadFromCompilerInvocationAction(CInvok.take(), Diags, |
| 577 | ASTAction.get())); |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 578 | if (!Unit) { |
| 579 | errRec.FinishCapture(); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 580 | return true; |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 581 | } |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 582 | Unit->setOwnsRemappedFileBuffers(false); // FileRemapper manages that. |
| 583 | |
Argyrios Kyrtzidis | 0474cfd | 2013-07-22 18:13:54 +0000 | [diff] [blame] | 584 | HadARCErrors = HadARCErrors || capturedDiags.hasErrors(); |
| 585 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 586 | // Don't filter diagnostics anymore. |
| 587 | Diags->setClient(DiagClient, /*ShouldOwnClient=*/false); |
| 588 | |
| 589 | ASTContext &Ctx = Unit->getASTContext(); |
| 590 | |
| 591 | if (Diags->hasFatalErrorOccurred()) { |
| 592 | Diags->Reset(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 593 | DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor()); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 594 | capturedDiags.reportDiagnostics(*Diags); |
| 595 | DiagClient->EndSourceFile(); |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 596 | errRec.FinishCapture(); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 597 | return true; |
| 598 | } |
| 599 | |
| 600 | // After parsing of source files ended, we want to reuse the |
| 601 | // diagnostics objects to emit further diagnostics. |
David Blaikie | 78ad0b9 | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 602 | // We call BeginSourceFile because DiagnosticConsumer requires that |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 603 | // diagnostics with source range information are emitted only in between |
| 604 | // BeginSourceFile() and EndSourceFile(). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 605 | DiagClient->BeginSourceFile(Ctx.getLangOpts(), &Unit->getPreprocessor()); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 606 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 607 | Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts()); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 608 | TransformActions TA(*Diags, capturedDiags, Ctx, Unit->getPreprocessor()); |
Ted Kremenek | d3b74d9 | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 609 | MigrationPass pass(Ctx, OrigCI.getLangOpts()->getGC(), |
Argyrios Kyrtzidis | ea2224d | 2013-01-04 18:30:08 +0000 | [diff] [blame] | 610 | Unit->getSema(), TA, capturedDiags, ARCMTMacroLocs); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 611 | |
| 612 | trans(pass); |
| 613 | |
| 614 | { |
| 615 | RewritesApplicator applicator(rewriter, Ctx, listener); |
| 616 | TA.applyRewrites(applicator); |
| 617 | } |
| 618 | |
| 619 | DiagClient->EndSourceFile(); |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 620 | errRec.FinishCapture(); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 621 | |
| 622 | if (DiagClient->getNumErrors()) |
| 623 | return true; |
| 624 | |
| 625 | for (Rewriter::buffer_iterator |
| 626 | I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) { |
| 627 | FileID FID = I->first; |
| 628 | RewriteBuffer &buf = I->second; |
| 629 | const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID); |
| 630 | assert(file); |
| 631 | std::string newFname = file->getName(); |
| 632 | newFname += "-trans"; |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 633 | SmallString<512> newText; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 634 | llvm::raw_svector_ostream vecOS(newText); |
| 635 | buf.write(vecOS); |
| 636 | vecOS.flush(); |
| 637 | llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy( |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 638 | StringRef(newText.data(), newText.size()), newFname); |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 639 | SmallString<64> filePath(file->getName()); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 640 | Unit->getFileManager().FixupRelativePath(filePath); |
| 641 | Remapper.remap(filePath.str(), memBuf); |
| 642 | } |
| 643 | |
| 644 | return false; |
| 645 | } |