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