blob: baa3f568c0142d2f6dc078e91e9d1efa3de5c383 [file] [log] [blame]
John McCall8f0e8d22011-06-15 23:25:17 +00001//===-- 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"
17#include "llvm/Support/Signals.h"
18
19using namespace clang;
20using namespace arcmt;
21
22static llvm::cl::opt<bool>
23CheckOnly("check-only",
24 llvm::cl::desc("Just check for issues that need to be handled manually"));
25
26//static llvm::cl::opt<bool>
27//TestResultForARC("test-result",
28//llvm::cl::desc("Test the result of transformations by parsing it in ARC mode"));
29
30static llvm::cl::opt<bool>
31OutputTransformations("output-transformations",
32 llvm::cl::desc("Print the source transformations"));
33
34static llvm::cl::opt<bool>
35VerifyDiags("verify",llvm::cl::desc("Verify emitted diagnostics and warnings"));
36
37static llvm::cl::opt<bool>
38VerboseOpt("v", llvm::cl::desc("Enable verbose output"));
39
40static llvm::cl::extrahelp extraHelp(
41 "\nusage with compiler args: arcmt-test [options] --args [compiler flags]\n");
42
43// This function isn't referenced outside its translation unit, but it
44// can't use the "static" keyword because its address is used for
45// GetMainExecutable (since some platforms don't support taking the
46// address of main, and some platforms can't implement GetMainExecutable
47// without being given the address of a function in the main executable).
48llvm::sys::Path GetExecutablePath(const char *Argv0) {
49 // This just needs to be some symbol in the binary; C++ doesn't
50 // allow taking the address of ::main however.
51 void *MainAddr = (void*) (intptr_t) GetExecutablePath;
52 return llvm::sys::Path::GetMainExecutable(Argv0, MainAddr);
53}
54
55static void printSourceLocation(SourceLocation loc, ASTContext &Ctx,
56 llvm::raw_ostream &OS);
57static void printSourceRange(CharSourceRange range, ASTContext &Ctx,
58 llvm::raw_ostream &OS);
59
60namespace {
61
62class PrintTransforms : public MigrationProcess::RewriteListener {
63 ASTContext *Ctx;
64 llvm::raw_ostream &OS;
65
66public:
67 PrintTransforms(llvm::raw_ostream &OS)
68 : Ctx(0), OS(OS) { }
69
70 virtual void start(ASTContext &ctx) { Ctx = &ctx; }
71 virtual void finish() { Ctx = 0; }
72
73 virtual void insert(SourceLocation loc, llvm::StringRef text) {
74 assert(Ctx);
75 OS << "Insert: ";
76 printSourceLocation(loc, *Ctx, OS);
77 OS << " \"" << text << "\"\n";
78 }
79
80 virtual void remove(CharSourceRange range) {
81 assert(Ctx);
82 OS << "Remove: ";
83 printSourceRange(range, *Ctx, OS);
84 OS << '\n';
85 }
86};
87
88} // anonymous namespace
89
90static bool checkForMigration(llvm::StringRef resourcesPath,
91 llvm::ArrayRef<const char *> Args) {
92 DiagnosticClient *DiagClient =
93 new TextDiagnosticPrinter(llvm::errs(), DiagnosticOptions());
94 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
95 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(new Diagnostic(DiagID, DiagClient));
96 // Chain in -verify checker, if requested.
97 VerifyDiagnosticsClient *verifyDiag = 0;
98 if (VerifyDiags) {
99 verifyDiag = new VerifyDiagnosticsClient(*Diags, Diags->takeClient());
100 Diags->setClient(verifyDiag);
101 }
102
Argyrios Kyrtzidiseaed19e2011-06-16 00:53:46 +0000103 CompilerInvocation CI;
104 CompilerInvocation::CreateFromArgs(CI, Args.begin(), Args.end(), *Diags);
John McCall8f0e8d22011-06-15 23:25:17 +0000105
Argyrios Kyrtzidiseaed19e2011-06-16 00:53:46 +0000106 if (CI.getFrontendOpts().Inputs.empty()) {
John McCall8f0e8d22011-06-15 23:25:17 +0000107 llvm::errs() << "error: no input files\n";
108 return true;
109 }
110
Argyrios Kyrtzidiseaed19e2011-06-16 00:53:46 +0000111 if (!CI.getLangOpts().ObjC1)
John McCall8f0e8d22011-06-15 23:25:17 +0000112 return false;
113
Argyrios Kyrtzidiseaed19e2011-06-16 00:53:46 +0000114 return arcmt::checkForManualIssues(CI,
115 CI.getFrontendOpts().Inputs[0].second,
116 CI.getFrontendOpts().Inputs[0].first,
John McCall8f0e8d22011-06-15 23:25:17 +0000117 Diags->getClient());
118}
119
120static void printResult(FileRemapper &remapper, llvm::raw_ostream &OS) {
121 CompilerInvocation CI;
122 remapper.applyMappings(CI);
123 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
124 // The changed files will be in memory buffers, print them.
125 for (unsigned i = 0, e = PPOpts.RemappedFileBuffers.size(); i != e; ++i) {
126 const llvm::MemoryBuffer *mem = PPOpts.RemappedFileBuffers[i].second;
127 OS << mem->getBuffer();
128 }
129}
130
131static bool performTransformations(llvm::StringRef resourcesPath,
132 llvm::ArrayRef<const char *> Args) {
133 // Check first.
134 if (checkForMigration(resourcesPath, Args))
135 return true;
136
137 DiagnosticClient *DiagClient =
138 new TextDiagnosticPrinter(llvm::errs(), DiagnosticOptions());
139 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
140 llvm::IntrusiveRefCntPtr<Diagnostic> TopDiags(new Diagnostic(DiagID, DiagClient));
141
Argyrios Kyrtzidiseaed19e2011-06-16 00:53:46 +0000142 CompilerInvocation origCI;
143 CompilerInvocation::CreateFromArgs(origCI, Args.begin(), Args.end(),
144 *TopDiags);
John McCall8f0e8d22011-06-15 23:25:17 +0000145
Argyrios Kyrtzidiseaed19e2011-06-16 00:53:46 +0000146 if (origCI.getFrontendOpts().Inputs.empty()) {
John McCall8f0e8d22011-06-15 23:25:17 +0000147 llvm::errs() << "error: no input files\n";
148 return true;
149 }
150
Argyrios Kyrtzidiseaed19e2011-06-16 00:53:46 +0000151 if (!origCI.getLangOpts().ObjC1)
John McCall8f0e8d22011-06-15 23:25:17 +0000152 return false;
153
Argyrios Kyrtzidiseaed19e2011-06-16 00:53:46 +0000154 MigrationProcess migration(origCI, DiagClient);
John McCall8f0e8d22011-06-15 23:25:17 +0000155
156 std::vector<TransformFn> transforms = arcmt::getAllTransformations();
157 assert(!transforms.empty());
158
159 llvm::OwningPtr<PrintTransforms> transformPrinter;
160 if (OutputTransformations)
161 transformPrinter.reset(new PrintTransforms(llvm::outs()));
162
163 for (unsigned i=0, e = transforms.size(); i != e; ++i) {
164 bool err = migration.applyTransform(transforms[i], transformPrinter.get());
165 if (err) return true;
166
167 if (VerboseOpt) {
168 if (i == e-1)
169 llvm::errs() << "\n##### FINAL RESULT #####\n";
170 else
171 llvm::errs() << "\n##### OUTPUT AFTER "<< i+1 <<". TRANSFORMATION #####\n";
172 printResult(migration.getRemapper(), llvm::errs());
173 llvm::errs() << "\n##########################\n\n";
174 }
175 }
176
177 if (!OutputTransformations)
178 printResult(migration.getRemapper(), llvm::outs());
179
180 // FIXME: TestResultForARC
181
182 return false;
183}
184
185//===----------------------------------------------------------------------===//
186// Misc. functions.
187//===----------------------------------------------------------------------===//
188
189static void printSourceLocation(SourceLocation loc, ASTContext &Ctx,
190 llvm::raw_ostream &OS) {
191 SourceManager &SM = Ctx.getSourceManager();
192 PresumedLoc PL = SM.getPresumedLoc(loc);
193
194 OS << llvm::sys::path::filename(PL.getFilename());
195 OS << ":" << PL.getLine() << ":"
196 << PL.getColumn();
197}
198
199static void printSourceRange(CharSourceRange range, ASTContext &Ctx,
200 llvm::raw_ostream &OS) {
201 SourceManager &SM = Ctx.getSourceManager();
202 const LangOptions &langOpts = Ctx.getLangOptions();
203
204 PresumedLoc PL = SM.getPresumedLoc(range.getBegin());
205
206 OS << llvm::sys::path::filename(PL.getFilename());
207 OS << " [" << PL.getLine() << ":"
208 << PL.getColumn();
209 OS << " - ";
210
211 SourceLocation end = range.getEnd();
212 PL = SM.getPresumedLoc(end);
213
214 unsigned endCol = PL.getColumn() - 1;
215 if (!range.isTokenRange())
216 endCol += Lexer::MeasureTokenLength(end, SM, langOpts);
217 OS << PL.getLine() << ":" << endCol << "]";
218}
219
220//===----------------------------------------------------------------------===//
221// Command line processing.
222//===----------------------------------------------------------------------===//
223
224int main(int argc, const char **argv) {
225 using llvm::StringRef;
226 void *MainAddr = (void*) (intptr_t) GetExecutablePath;
227 llvm::sys::PrintStackTraceOnErrorSignal();
228
229 std::string
230 resourcesPath = CompilerInvocation::GetResourcesPath(argv[0], MainAddr);
231
232 int optargc = 0;
233 for (; optargc != argc; ++optargc) {
234 if (StringRef(argv[optargc]) == "--args")
235 break;
236 }
237 llvm::cl::ParseCommandLineOptions(optargc, const_cast<char **>(argv), "arcmt-test");
238
239 if (optargc == argc) {
240 llvm::cl::PrintHelpMessage();
241 return 1;
242 }
243
244 llvm::ArrayRef<const char*> Args(argv+optargc+1, argc-optargc-1);
245
246 if (CheckOnly)
247 return checkForMigration(resourcesPath, Args);
248
249 return performTransformations(resourcesPath, Args);
250}