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