John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 1 | //===-- arcmt-test.cpp - ARC Migration Tool testbed -----------------------===// |
| 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 "clang/ARCMigrate/ARCMT.h" |
| 11 | #include "clang/Frontend/ASTUnit.h" |
| 12 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
| 13 | #include "clang/Frontend/VerifyDiagnosticsClient.h" |
| 14 | #include "clang/Frontend/Utils.h" |
| 15 | #include "clang/Lex/Preprocessor.h" |
| 16 | #include "llvm/Support/MemoryBuffer.h" |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 17 | #include "llvm/Support/FileSystem.h" |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Signals.h" |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 19 | #include "llvm/Support/system_error.h" |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
| 22 | using namespace arcmt; |
| 23 | |
| 24 | static llvm::cl::opt<bool> |
| 25 | CheckOnly("check-only", |
| 26 | llvm::cl::desc("Just check for issues that need to be handled manually")); |
| 27 | |
| 28 | //static llvm::cl::opt<bool> |
| 29 | //TestResultForARC("test-result", |
| 30 | //llvm::cl::desc("Test the result of transformations by parsing it in ARC mode")); |
| 31 | |
| 32 | static llvm::cl::opt<bool> |
| 33 | OutputTransformations("output-transformations", |
| 34 | llvm::cl::desc("Print the source transformations")); |
| 35 | |
| 36 | static llvm::cl::opt<bool> |
| 37 | VerifyDiags("verify",llvm::cl::desc("Verify emitted diagnostics and warnings")); |
| 38 | |
| 39 | static llvm::cl::opt<bool> |
| 40 | VerboseOpt("v", llvm::cl::desc("Enable verbose output")); |
| 41 | |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 42 | static llvm::cl::opt<bool> |
| 43 | VerifyTransformedFiles("verify-transformed-files", |
| 44 | llvm::cl::desc("Read pairs of file mappings (typically the output of " |
| 45 | "c-arcmt-test) and compare their contents with the filenames " |
| 46 | "provided in command-line")); |
| 47 | |
| 48 | static llvm::cl::opt<std::string> |
| 49 | RemappingsFile("remappings-file", |
| 50 | llvm::cl::desc("Pairs of file mappings (typically the output of " |
| 51 | "c-arcmt-test)")); |
| 52 | |
| 53 | static llvm::cl::list<std::string> |
| 54 | ResultFiles(llvm::cl::Positional, llvm::cl::desc("<filename>...")); |
| 55 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 56 | static llvm::cl::extrahelp extraHelp( |
| 57 | "\nusage with compiler args: arcmt-test [options] --args [compiler flags]\n"); |
| 58 | |
| 59 | // This function isn't referenced outside its translation unit, but it |
| 60 | // can't use the "static" keyword because its address is used for |
| 61 | // GetMainExecutable (since some platforms don't support taking the |
| 62 | // address of main, and some platforms can't implement GetMainExecutable |
| 63 | // without being given the address of a function in the main executable). |
| 64 | llvm::sys::Path GetExecutablePath(const char *Argv0) { |
| 65 | // This just needs to be some symbol in the binary; C++ doesn't |
| 66 | // allow taking the address of ::main however. |
| 67 | void *MainAddr = (void*) (intptr_t) GetExecutablePath; |
| 68 | return llvm::sys::Path::GetMainExecutable(Argv0, MainAddr); |
| 69 | } |
| 70 | |
| 71 | static void printSourceLocation(SourceLocation loc, ASTContext &Ctx, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 72 | raw_ostream &OS); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 73 | static void printSourceRange(CharSourceRange range, ASTContext &Ctx, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 74 | raw_ostream &OS); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 75 | |
| 76 | namespace { |
| 77 | |
| 78 | class PrintTransforms : public MigrationProcess::RewriteListener { |
| 79 | ASTContext *Ctx; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 80 | raw_ostream &OS; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 81 | |
| 82 | public: |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 83 | PrintTransforms(raw_ostream &OS) |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 84 | : Ctx(0), OS(OS) { } |
| 85 | |
| 86 | virtual void start(ASTContext &ctx) { Ctx = &ctx; } |
| 87 | virtual void finish() { Ctx = 0; } |
| 88 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 89 | virtual void insert(SourceLocation loc, StringRef text) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 90 | assert(Ctx); |
| 91 | OS << "Insert: "; |
| 92 | printSourceLocation(loc, *Ctx, OS); |
| 93 | OS << " \"" << text << "\"\n"; |
| 94 | } |
| 95 | |
| 96 | virtual void remove(CharSourceRange range) { |
| 97 | assert(Ctx); |
| 98 | OS << "Remove: "; |
| 99 | printSourceRange(range, *Ctx, OS); |
| 100 | OS << '\n'; |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | } // anonymous namespace |
| 105 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 106 | static bool checkForMigration(StringRef resourcesPath, |
Chris Lattner | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 107 | ArrayRef<const char *> Args) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 108 | DiagnosticClient *DiagClient = |
| 109 | new TextDiagnosticPrinter(llvm::errs(), DiagnosticOptions()); |
| 110 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 111 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags(new Diagnostic(DiagID, DiagClient)); |
| 112 | // Chain in -verify checker, if requested. |
| 113 | VerifyDiagnosticsClient *verifyDiag = 0; |
| 114 | if (VerifyDiags) { |
| 115 | verifyDiag = new VerifyDiagnosticsClient(*Diags, Diags->takeClient()); |
| 116 | Diags->setClient(verifyDiag); |
| 117 | } |
| 118 | |
Argyrios Kyrtzidis | eaed19e | 2011-06-16 00:53:46 +0000 | [diff] [blame] | 119 | CompilerInvocation CI; |
| 120 | CompilerInvocation::CreateFromArgs(CI, Args.begin(), Args.end(), *Diags); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 121 | |
Argyrios Kyrtzidis | eaed19e | 2011-06-16 00:53:46 +0000 | [diff] [blame] | 122 | if (CI.getFrontendOpts().Inputs.empty()) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 123 | llvm::errs() << "error: no input files\n"; |
| 124 | return true; |
| 125 | } |
| 126 | |
Argyrios Kyrtzidis | eaed19e | 2011-06-16 00:53:46 +0000 | [diff] [blame] | 127 | if (!CI.getLangOpts().ObjC1) |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 128 | return false; |
| 129 | |
Argyrios Kyrtzidis | e665d69 | 2011-06-18 00:53:41 +0000 | [diff] [blame] | 130 | arcmt::checkForManualIssues(CI, |
| 131 | CI.getFrontendOpts().Inputs[0].second, |
| 132 | CI.getFrontendOpts().Inputs[0].first, |
| 133 | Diags->getClient()); |
| 134 | return Diags->getClient()->getNumErrors() > 0; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 137 | static void printResult(FileRemapper &remapper, raw_ostream &OS) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 138 | CompilerInvocation CI; |
| 139 | remapper.applyMappings(CI); |
| 140 | PreprocessorOptions &PPOpts = CI.getPreprocessorOpts(); |
| 141 | // The changed files will be in memory buffers, print them. |
| 142 | for (unsigned i = 0, e = PPOpts.RemappedFileBuffers.size(); i != e; ++i) { |
| 143 | const llvm::MemoryBuffer *mem = PPOpts.RemappedFileBuffers[i].second; |
| 144 | OS << mem->getBuffer(); |
| 145 | } |
| 146 | } |
| 147 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 148 | static bool performTransformations(StringRef resourcesPath, |
Chris Lattner | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 149 | ArrayRef<const char *> Args) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 150 | // Check first. |
| 151 | if (checkForMigration(resourcesPath, Args)) |
| 152 | return true; |
| 153 | |
| 154 | DiagnosticClient *DiagClient = |
| 155 | new TextDiagnosticPrinter(llvm::errs(), DiagnosticOptions()); |
| 156 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 157 | llvm::IntrusiveRefCntPtr<Diagnostic> TopDiags(new Diagnostic(DiagID, DiagClient)); |
| 158 | |
Argyrios Kyrtzidis | eaed19e | 2011-06-16 00:53:46 +0000 | [diff] [blame] | 159 | CompilerInvocation origCI; |
| 160 | CompilerInvocation::CreateFromArgs(origCI, Args.begin(), Args.end(), |
| 161 | *TopDiags); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 162 | |
Argyrios Kyrtzidis | eaed19e | 2011-06-16 00:53:46 +0000 | [diff] [blame] | 163 | if (origCI.getFrontendOpts().Inputs.empty()) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 164 | llvm::errs() << "error: no input files\n"; |
| 165 | return true; |
| 166 | } |
| 167 | |
Argyrios Kyrtzidis | eaed19e | 2011-06-16 00:53:46 +0000 | [diff] [blame] | 168 | if (!origCI.getLangOpts().ObjC1) |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 169 | return false; |
| 170 | |
Argyrios Kyrtzidis | eaed19e | 2011-06-16 00:53:46 +0000 | [diff] [blame] | 171 | MigrationProcess migration(origCI, DiagClient); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 172 | |
| 173 | std::vector<TransformFn> transforms = arcmt::getAllTransformations(); |
| 174 | assert(!transforms.empty()); |
| 175 | |
| 176 | llvm::OwningPtr<PrintTransforms> transformPrinter; |
| 177 | if (OutputTransformations) |
| 178 | transformPrinter.reset(new PrintTransforms(llvm::outs())); |
| 179 | |
| 180 | for (unsigned i=0, e = transforms.size(); i != e; ++i) { |
| 181 | bool err = migration.applyTransform(transforms[i], transformPrinter.get()); |
| 182 | if (err) return true; |
| 183 | |
| 184 | if (VerboseOpt) { |
| 185 | if (i == e-1) |
| 186 | llvm::errs() << "\n##### FINAL RESULT #####\n"; |
| 187 | else |
| 188 | llvm::errs() << "\n##### OUTPUT AFTER "<< i+1 <<". TRANSFORMATION #####\n"; |
| 189 | printResult(migration.getRemapper(), llvm::errs()); |
| 190 | llvm::errs() << "\n##########################\n\n"; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | if (!OutputTransformations) |
| 195 | printResult(migration.getRemapper(), llvm::outs()); |
| 196 | |
| 197 | // FIXME: TestResultForARC |
| 198 | |
| 199 | return false; |
| 200 | } |
| 201 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 202 | static bool filesCompareEqual(StringRef fname1, StringRef fname2) { |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 203 | using namespace llvm; |
| 204 | |
| 205 | OwningPtr<MemoryBuffer> file1; |
| 206 | MemoryBuffer::getFile(fname1, file1); |
| 207 | if (!file1) |
| 208 | return false; |
| 209 | |
| 210 | OwningPtr<MemoryBuffer> file2; |
| 211 | MemoryBuffer::getFile(fname2, file2); |
| 212 | if (!file2) |
| 213 | return false; |
| 214 | |
| 215 | return file1->getBuffer() == file2->getBuffer(); |
| 216 | } |
| 217 | |
Chris Lattner | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 218 | static bool verifyTransformedFiles(ArrayRef<std::string> resultFiles) { |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 219 | using namespace llvm; |
| 220 | |
| 221 | assert(!resultFiles.empty()); |
| 222 | |
| 223 | std::map<StringRef, StringRef> resultMap; |
| 224 | |
| 225 | for (ArrayRef<std::string>::iterator |
| 226 | I = resultFiles.begin(), E = resultFiles.end(); I != E; ++I) { |
| 227 | StringRef fname(*I); |
| 228 | if (!fname.endswith(".result")) { |
| 229 | errs() << "error: filename '" << fname |
| 230 | << "' does not have '.result' extension\n"; |
| 231 | return true; |
| 232 | } |
| 233 | resultMap[sys::path::stem(fname)] = fname; |
| 234 | } |
| 235 | |
| 236 | OwningPtr<MemoryBuffer> inputBuf; |
| 237 | if (RemappingsFile.empty()) |
| 238 | MemoryBuffer::getSTDIN(inputBuf); |
| 239 | else |
| 240 | MemoryBuffer::getFile(RemappingsFile, inputBuf); |
| 241 | if (!inputBuf) { |
| 242 | errs() << "error: could not read remappings input\n"; |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | SmallVector<StringRef, 8> strs; |
| 247 | inputBuf->getBuffer().split(strs, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false); |
| 248 | |
| 249 | if (strs.empty()) { |
| 250 | errs() << "error: no files to verify from stdin\n"; |
| 251 | return true; |
| 252 | } |
| 253 | if (strs.size() % 2 != 0) { |
| 254 | errs() << "error: files to verify are not original/result pairs\n"; |
| 255 | return true; |
| 256 | } |
| 257 | |
| 258 | for (unsigned i = 0, e = strs.size(); i != e; i += 2) { |
| 259 | StringRef inputOrigFname = strs[i]; |
| 260 | StringRef inputResultFname = strs[i+1]; |
| 261 | |
| 262 | std::map<StringRef, StringRef>::iterator It; |
| 263 | It = resultMap.find(sys::path::filename(inputOrigFname)); |
| 264 | if (It == resultMap.end()) { |
| 265 | errs() << "error: '" << inputOrigFname << "' is not in the list of " |
| 266 | << "transformed files to verify\n"; |
| 267 | return true; |
| 268 | } |
| 269 | |
| 270 | bool exists = false; |
| 271 | sys::fs::exists(It->second, exists); |
| 272 | if (!exists) { |
| 273 | errs() << "error: '" << It->second << "' does not exist\n"; |
| 274 | return true; |
| 275 | } |
| 276 | sys::fs::exists(inputResultFname, exists); |
| 277 | if (!exists) { |
| 278 | errs() << "error: '" << inputResultFname << "' does not exist\n"; |
| 279 | return true; |
| 280 | } |
| 281 | |
| 282 | if (!filesCompareEqual(It->second, inputResultFname)) { |
| 283 | errs() << "error: '" << It->second << "' is different than " |
| 284 | << "'" << inputResultFname << "'\n"; |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | resultMap.erase(It); |
| 289 | } |
| 290 | |
| 291 | if (!resultMap.empty()) { |
| 292 | for (std::map<StringRef, StringRef>::iterator |
| 293 | I = resultMap.begin(), E = resultMap.end(); I != E; ++I) |
| 294 | errs() << "error: '" << I->second << "' was not verified!\n"; |
| 295 | return true; |
| 296 | } |
| 297 | |
| 298 | return false; |
| 299 | } |
| 300 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 301 | //===----------------------------------------------------------------------===// |
| 302 | // Misc. functions. |
| 303 | //===----------------------------------------------------------------------===// |
| 304 | |
| 305 | static void printSourceLocation(SourceLocation loc, ASTContext &Ctx, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 306 | raw_ostream &OS) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 307 | SourceManager &SM = Ctx.getSourceManager(); |
| 308 | PresumedLoc PL = SM.getPresumedLoc(loc); |
| 309 | |
| 310 | OS << llvm::sys::path::filename(PL.getFilename()); |
| 311 | OS << ":" << PL.getLine() << ":" |
| 312 | << PL.getColumn(); |
| 313 | } |
| 314 | |
| 315 | static void printSourceRange(CharSourceRange range, ASTContext &Ctx, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 316 | raw_ostream &OS) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 317 | SourceManager &SM = Ctx.getSourceManager(); |
| 318 | const LangOptions &langOpts = Ctx.getLangOptions(); |
| 319 | |
| 320 | PresumedLoc PL = SM.getPresumedLoc(range.getBegin()); |
| 321 | |
| 322 | OS << llvm::sys::path::filename(PL.getFilename()); |
| 323 | OS << " [" << PL.getLine() << ":" |
| 324 | << PL.getColumn(); |
| 325 | OS << " - "; |
| 326 | |
| 327 | SourceLocation end = range.getEnd(); |
| 328 | PL = SM.getPresumedLoc(end); |
| 329 | |
| 330 | unsigned endCol = PL.getColumn() - 1; |
| 331 | if (!range.isTokenRange()) |
| 332 | endCol += Lexer::MeasureTokenLength(end, SM, langOpts); |
| 333 | OS << PL.getLine() << ":" << endCol << "]"; |
| 334 | } |
| 335 | |
| 336 | //===----------------------------------------------------------------------===// |
| 337 | // Command line processing. |
| 338 | //===----------------------------------------------------------------------===// |
| 339 | |
| 340 | int main(int argc, const char **argv) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 341 | void *MainAddr = (void*) (intptr_t) GetExecutablePath; |
| 342 | llvm::sys::PrintStackTraceOnErrorSignal(); |
| 343 | |
| 344 | std::string |
| 345 | resourcesPath = CompilerInvocation::GetResourcesPath(argv[0], MainAddr); |
| 346 | |
| 347 | int optargc = 0; |
| 348 | for (; optargc != argc; ++optargc) { |
| 349 | if (StringRef(argv[optargc]) == "--args") |
| 350 | break; |
| 351 | } |
| 352 | llvm::cl::ParseCommandLineOptions(optargc, const_cast<char **>(argv), "arcmt-test"); |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 353 | |
| 354 | if (VerifyTransformedFiles) { |
| 355 | if (ResultFiles.empty()) { |
| 356 | llvm::cl::PrintHelpMessage(); |
| 357 | return 1; |
| 358 | } |
| 359 | return verifyTransformedFiles(ResultFiles); |
| 360 | } |
| 361 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 362 | if (optargc == argc) { |
| 363 | llvm::cl::PrintHelpMessage(); |
| 364 | return 1; |
| 365 | } |
| 366 | |
Chris Lattner | 2d3ba4f | 2011-07-23 17:14:25 +0000 | [diff] [blame] | 367 | ArrayRef<const char*> Args(argv+optargc+1, argc-optargc-1); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 368 | |
| 369 | if (CheckOnly) |
| 370 | return checkForMigration(resourcesPath, Args); |
| 371 | |
| 372 | return performTransformations(resourcesPath, Args); |
| 373 | } |