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/Utils.h"
14 #include "clang/Frontend/VerifyDiagnosticConsumer.h"
15 #include "clang/Lex/Preprocessor.h"
16 #include "llvm/Support/FileSystem.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/Support/Signals.h"
19 #include <system_error>
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
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
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).
GetExecutablePath(const char * Argv0)64 std::string 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::fs::getMainExecutable(Argv0, MainAddr);
69 }
70
71 static void printSourceLocation(SourceLocation loc, ASTContext &Ctx,
72 raw_ostream &OS);
73 static void printSourceRange(CharSourceRange range, ASTContext &Ctx,
74 raw_ostream &OS);
75
76 namespace {
77
78 class PrintTransforms : public MigrationProcess::RewriteListener {
79 ASTContext *Ctx;
80 raw_ostream &OS;
81
82 public:
PrintTransforms(raw_ostream & OS)83 PrintTransforms(raw_ostream &OS)
84 : Ctx(nullptr), OS(OS) {}
85
start(ASTContext & ctx)86 void start(ASTContext &ctx) override { Ctx = &ctx; }
finish()87 void finish() override { Ctx = nullptr; }
88
insert(SourceLocation loc,StringRef text)89 void insert(SourceLocation loc, StringRef text) override {
90 assert(Ctx);
91 OS << "Insert: ";
92 printSourceLocation(loc, *Ctx, OS);
93 OS << " \"" << text << "\"\n";
94 }
95
remove(CharSourceRange range)96 void remove(CharSourceRange range) override {
97 assert(Ctx);
98 OS << "Remove: ";
99 printSourceRange(range, *Ctx, OS);
100 OS << '\n';
101 }
102 };
103
104 } // anonymous namespace
105
checkForMigration(StringRef resourcesPath,ArrayRef<const char * > Args)106 static bool checkForMigration(StringRef resourcesPath,
107 ArrayRef<const char *> Args) {
108 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
109 DiagnosticConsumer *DiagClient =
110 new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
111 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
112 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
113 new DiagnosticsEngine(DiagID, &*DiagOpts, DiagClient));
114 // Chain in -verify checker, if requested.
115 VerifyDiagnosticConsumer *verifyDiag = nullptr;
116 if (VerifyDiags) {
117 verifyDiag = new VerifyDiagnosticConsumer(*Diags);
118 Diags->setClient(verifyDiag);
119 }
120
121 CompilerInvocation CI;
122 if (!CompilerInvocation::CreateFromArgs(CI, Args.begin(), Args.end(), *Diags))
123 return true;
124
125 if (CI.getFrontendOpts().Inputs.empty()) {
126 llvm::errs() << "error: no input files\n";
127 return true;
128 }
129
130 if (!CI.getLangOpts()->ObjC1)
131 return false;
132
133 arcmt::checkForManualIssues(CI, CI.getFrontendOpts().Inputs[0],
134 Diags->getClient());
135 return Diags->getClient()->getNumErrors() > 0;
136 }
137
printResult(FileRemapper & remapper,raw_ostream & OS)138 static void printResult(FileRemapper &remapper, raw_ostream &OS) {
139 PreprocessorOptions PPOpts;
140 remapper.applyMappings(PPOpts);
141 // The changed files will be in memory buffers, print them.
142 for (const auto &RB : PPOpts.RemappedFileBuffers)
143 OS << RB.second->getBuffer();
144 }
145
performTransformations(StringRef resourcesPath,ArrayRef<const char * > Args)146 static bool performTransformations(StringRef resourcesPath,
147 ArrayRef<const char *> Args) {
148 // Check first.
149 if (checkForMigration(resourcesPath, Args))
150 return true;
151
152 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
153 DiagnosticConsumer *DiagClient =
154 new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
155 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
156 IntrusiveRefCntPtr<DiagnosticsEngine> TopDiags(
157 new DiagnosticsEngine(DiagID, &*DiagOpts, &*DiagClient));
158
159 CompilerInvocation origCI;
160 if (!CompilerInvocation::CreateFromArgs(origCI, Args.begin(), Args.end(),
161 *TopDiags))
162 return true;
163
164 if (origCI.getFrontendOpts().Inputs.empty()) {
165 llvm::errs() << "error: no input files\n";
166 return true;
167 }
168
169 if (!origCI.getLangOpts()->ObjC1)
170 return false;
171
172 MigrationProcess migration(origCI, DiagClient);
173
174 std::vector<TransformFn>
175 transforms = arcmt::getAllTransformations(origCI.getLangOpts()->getGC(),
176 origCI.getMigratorOpts().NoFinalizeRemoval);
177 assert(!transforms.empty());
178
179 std::unique_ptr<PrintTransforms> transformPrinter;
180 if (OutputTransformations)
181 transformPrinter.reset(new PrintTransforms(llvm::outs()));
182
183 for (unsigned i=0, e = transforms.size(); i != e; ++i) {
184 bool err = migration.applyTransform(transforms[i], transformPrinter.get());
185 if (err) return true;
186
187 if (VerboseOpt) {
188 if (i == e-1)
189 llvm::errs() << "\n##### FINAL RESULT #####\n";
190 else
191 llvm::errs() << "\n##### OUTPUT AFTER "<< i+1 <<". TRANSFORMATION #####\n";
192 printResult(migration.getRemapper(), llvm::errs());
193 llvm::errs() << "\n##########################\n\n";
194 }
195 }
196
197 if (!OutputTransformations)
198 printResult(migration.getRemapper(), llvm::outs());
199
200 // FIXME: TestResultForARC
201
202 return false;
203 }
204
filesCompareEqual(StringRef fname1,StringRef fname2)205 static bool filesCompareEqual(StringRef fname1, StringRef fname2) {
206 using namespace llvm;
207
208 ErrorOr<std::unique_ptr<MemoryBuffer>> file1 = MemoryBuffer::getFile(fname1);
209 if (!file1)
210 return false;
211
212 ErrorOr<std::unique_ptr<MemoryBuffer>> file2 = MemoryBuffer::getFile(fname2);
213 if (!file2)
214 return false;
215
216 return file1.get()->getBuffer() == file2.get()->getBuffer();
217 }
218
verifyTransformedFiles(ArrayRef<std::string> resultFiles)219 static bool verifyTransformedFiles(ArrayRef<std::string> resultFiles) {
220 using namespace llvm;
221
222 assert(!resultFiles.empty());
223
224 std::map<StringRef, StringRef> resultMap;
225
226 for (ArrayRef<std::string>::iterator
227 I = resultFiles.begin(), E = resultFiles.end(); I != E; ++I) {
228 StringRef fname(*I);
229 if (!fname.endswith(".result")) {
230 errs() << "error: filename '" << fname
231 << "' does not have '.result' extension\n";
232 return true;
233 }
234 resultMap[sys::path::stem(fname)] = fname;
235 }
236
237 ErrorOr<std::unique_ptr<MemoryBuffer>> inputBuf = std::error_code();
238 if (RemappingsFile.empty())
239 inputBuf = MemoryBuffer::getSTDIN();
240 else
241 inputBuf = MemoryBuffer::getFile(RemappingsFile);
242 if (!inputBuf) {
243 errs() << "error: could not read remappings input\n";
244 return true;
245 }
246
247 SmallVector<StringRef, 8> strs;
248 inputBuf.get()->getBuffer().split(strs, "\n", /*MaxSplit=*/-1,
249 /*KeepEmpty=*/false);
250
251 if (strs.empty()) {
252 errs() << "error: no files to verify from stdin\n";
253 return true;
254 }
255 if (strs.size() % 2 != 0) {
256 errs() << "error: files to verify are not original/result pairs\n";
257 return true;
258 }
259
260 for (unsigned i = 0, e = strs.size(); i != e; i += 2) {
261 StringRef inputOrigFname = strs[i];
262 StringRef inputResultFname = strs[i+1];
263
264 std::map<StringRef, StringRef>::iterator It;
265 It = resultMap.find(sys::path::filename(inputOrigFname));
266 if (It == resultMap.end()) {
267 errs() << "error: '" << inputOrigFname << "' is not in the list of "
268 << "transformed files to verify\n";
269 return true;
270 }
271
272 if (!sys::fs::exists(It->second)) {
273 errs() << "error: '" << It->second << "' does not exist\n";
274 return true;
275 }
276 if (!sys::fs::exists(inputResultFname)) {
277 errs() << "error: '" << inputResultFname << "' does not exist\n";
278 return true;
279 }
280
281 if (!filesCompareEqual(It->second, inputResultFname)) {
282 errs() << "error: '" << It->second << "' is different than "
283 << "'" << inputResultFname << "'\n";
284 return true;
285 }
286
287 resultMap.erase(It);
288 }
289
290 if (!resultMap.empty()) {
291 for (std::map<StringRef, StringRef>::iterator
292 I = resultMap.begin(), E = resultMap.end(); I != E; ++I)
293 errs() << "error: '" << I->second << "' was not verified!\n";
294 return true;
295 }
296
297 return false;
298 }
299
300 //===----------------------------------------------------------------------===//
301 // Misc. functions.
302 //===----------------------------------------------------------------------===//
303
printSourceLocation(SourceLocation loc,ASTContext & Ctx,raw_ostream & OS)304 static void printSourceLocation(SourceLocation loc, ASTContext &Ctx,
305 raw_ostream &OS) {
306 SourceManager &SM = Ctx.getSourceManager();
307 PresumedLoc PL = SM.getPresumedLoc(loc);
308
309 OS << llvm::sys::path::filename(PL.getFilename());
310 OS << ":" << PL.getLine() << ":"
311 << PL.getColumn();
312 }
313
printSourceRange(CharSourceRange range,ASTContext & Ctx,raw_ostream & OS)314 static void printSourceRange(CharSourceRange range, ASTContext &Ctx,
315 raw_ostream &OS) {
316 SourceManager &SM = Ctx.getSourceManager();
317 const LangOptions &langOpts = Ctx.getLangOpts();
318
319 PresumedLoc PL = SM.getPresumedLoc(range.getBegin());
320
321 OS << llvm::sys::path::filename(PL.getFilename());
322 OS << " [" << PL.getLine() << ":"
323 << PL.getColumn();
324 OS << " - ";
325
326 SourceLocation end = range.getEnd();
327 PL = SM.getPresumedLoc(end);
328
329 unsigned endCol = PL.getColumn() - 1;
330 if (!range.isTokenRange())
331 endCol += Lexer::MeasureTokenLength(end, SM, langOpts);
332 OS << PL.getLine() << ":" << endCol << "]";
333 }
334
335 //===----------------------------------------------------------------------===//
336 // Command line processing.
337 //===----------------------------------------------------------------------===//
338
main(int argc,const char ** argv)339 int main(int argc, const char **argv) {
340 void *MainAddr = (void*) (intptr_t) GetExecutablePath;
341 llvm::sys::PrintStackTraceOnErrorSignal();
342
343 std::string
344 resourcesPath = CompilerInvocation::GetResourcesPath(argv[0], MainAddr);
345
346 int optargc = 0;
347 for (; optargc != argc; ++optargc) {
348 if (StringRef(argv[optargc]) == "--args")
349 break;
350 }
351 llvm::cl::ParseCommandLineOptions(optargc, argv, "arcmt-test");
352
353 if (VerifyTransformedFiles) {
354 if (ResultFiles.empty()) {
355 llvm::cl::PrintHelpMessage();
356 return 1;
357 }
358 return verifyTransformedFiles(ResultFiles);
359 }
360
361 if (optargc == argc) {
362 llvm::cl::PrintHelpMessage();
363 return 1;
364 }
365
366 ArrayRef<const char*> Args(argv+optargc+1, argc-optargc-1);
367
368 if (CheckOnly)
369 return checkForMigration(resourcesPath, Args);
370
371 return performTransformations(resourcesPath, Args);
372 }
373