blob: a5a7125f1ef5022521f94e85e0bf6812ac0d5961 [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
103 llvm::OwningPtr<CompilerInvocation> CI;
104 CI.reset(clang::createInvocationFromCommandLine(Args, Diags));
105 if (!CI)
106 return true;
107
108 if (CI->getFrontendOpts().Inputs.empty()) {
109 llvm::errs() << "error: no input files\n";
110 return true;
111 }
112
113 if (!CI->getLangOpts().ObjC1)
114 return false;
115
116 return arcmt::checkForManualIssues(*CI,
117 CI->getFrontendOpts().Inputs[0].second,
118 CI->getFrontendOpts().Inputs[0].first,
119 Diags->getClient());
120}
121
122static void printResult(FileRemapper &remapper, llvm::raw_ostream &OS) {
123 CompilerInvocation CI;
124 remapper.applyMappings(CI);
125 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
126 // The changed files will be in memory buffers, print them.
127 for (unsigned i = 0, e = PPOpts.RemappedFileBuffers.size(); i != e; ++i) {
128 const llvm::MemoryBuffer *mem = PPOpts.RemappedFileBuffers[i].second;
129 OS << mem->getBuffer();
130 }
131}
132
133static bool performTransformations(llvm::StringRef resourcesPath,
134 llvm::ArrayRef<const char *> Args) {
135 // Check first.
136 if (checkForMigration(resourcesPath, Args))
137 return true;
138
139 DiagnosticClient *DiagClient =
140 new TextDiagnosticPrinter(llvm::errs(), DiagnosticOptions());
141 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
142 llvm::IntrusiveRefCntPtr<Diagnostic> TopDiags(new Diagnostic(DiagID, DiagClient));
143
144 llvm::OwningPtr<CompilerInvocation> origCI;
145 origCI.reset(clang::createInvocationFromCommandLine(Args, TopDiags));
146 if (!origCI)
147 return true;
148
149 if (origCI->getFrontendOpts().Inputs.empty()) {
150 llvm::errs() << "error: no input files\n";
151 return true;
152 }
153
154 if (!origCI->getLangOpts().ObjC1)
155 return false;
156
157 MigrationProcess migration(*origCI, DiagClient);
158
159 std::vector<TransformFn> transforms = arcmt::getAllTransformations();
160 assert(!transforms.empty());
161
162 llvm::OwningPtr<PrintTransforms> transformPrinter;
163 if (OutputTransformations)
164 transformPrinter.reset(new PrintTransforms(llvm::outs()));
165
166 for (unsigned i=0, e = transforms.size(); i != e; ++i) {
167 bool err = migration.applyTransform(transforms[i], transformPrinter.get());
168 if (err) return true;
169
170 if (VerboseOpt) {
171 if (i == e-1)
172 llvm::errs() << "\n##### FINAL RESULT #####\n";
173 else
174 llvm::errs() << "\n##### OUTPUT AFTER "<< i+1 <<". TRANSFORMATION #####\n";
175 printResult(migration.getRemapper(), llvm::errs());
176 llvm::errs() << "\n##########################\n\n";
177 }
178 }
179
180 if (!OutputTransformations)
181 printResult(migration.getRemapper(), llvm::outs());
182
183 // FIXME: TestResultForARC
184
185 return false;
186}
187
188//===----------------------------------------------------------------------===//
189// Misc. functions.
190//===----------------------------------------------------------------------===//
191
192static void printSourceLocation(SourceLocation loc, ASTContext &Ctx,
193 llvm::raw_ostream &OS) {
194 SourceManager &SM = Ctx.getSourceManager();
195 PresumedLoc PL = SM.getPresumedLoc(loc);
196
197 OS << llvm::sys::path::filename(PL.getFilename());
198 OS << ":" << PL.getLine() << ":"
199 << PL.getColumn();
200}
201
202static void printSourceRange(CharSourceRange range, ASTContext &Ctx,
203 llvm::raw_ostream &OS) {
204 SourceManager &SM = Ctx.getSourceManager();
205 const LangOptions &langOpts = Ctx.getLangOptions();
206
207 PresumedLoc PL = SM.getPresumedLoc(range.getBegin());
208
209 OS << llvm::sys::path::filename(PL.getFilename());
210 OS << " [" << PL.getLine() << ":"
211 << PL.getColumn();
212 OS << " - ";
213
214 SourceLocation end = range.getEnd();
215 PL = SM.getPresumedLoc(end);
216
217 unsigned endCol = PL.getColumn() - 1;
218 if (!range.isTokenRange())
219 endCol += Lexer::MeasureTokenLength(end, SM, langOpts);
220 OS << PL.getLine() << ":" << endCol << "]";
221}
222
223//===----------------------------------------------------------------------===//
224// Command line processing.
225//===----------------------------------------------------------------------===//
226
227int main(int argc, const char **argv) {
228 using llvm::StringRef;
229 void *MainAddr = (void*) (intptr_t) GetExecutablePath;
230 llvm::sys::PrintStackTraceOnErrorSignal();
231
232 std::string
233 resourcesPath = CompilerInvocation::GetResourcesPath(argv[0], MainAddr);
234
235 int optargc = 0;
236 for (; optargc != argc; ++optargc) {
237 if (StringRef(argv[optargc]) == "--args")
238 break;
239 }
240 llvm::cl::ParseCommandLineOptions(optargc, const_cast<char **>(argv), "arcmt-test");
241
242 if (optargc == argc) {
243 llvm::cl::PrintHelpMessage();
244 return 1;
245 }
246
247 llvm::ArrayRef<const char*> Args(argv+optargc+1, argc-optargc-1);
248
249 if (CheckOnly)
250 return checkForMigration(resourcesPath, Args);
251
252 return performTransformations(resourcesPath, Args);
253}