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