• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- Tooling.cpp - Running clang standalone tools ---------------------===//
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 //  This file implements functions to run clang tools standalone instead
11 //  of running them as a plugin.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "clang/Tooling/ArgumentsAdjusters.h"
16 #include "clang/Tooling/Tooling.h"
17 #include "clang/Tooling/CompilationDatabase.h"
18 #include "clang/Driver/Compilation.h"
19 #include "clang/Driver/Driver.h"
20 #include "clang/Driver/Tool.h"
21 #include "clang/Frontend/CompilerInstance.h"
22 #include "clang/Frontend/FrontendDiagnostic.h"
23 #include "clang/Frontend/TextDiagnosticPrinter.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/Host.h"
27 #include "llvm/Support/raw_ostream.h"
28 
29 // For chdir, see the comment in ClangTool::run for more information.
30 #ifdef _WIN32
31 #  include <direct.h>
32 #else
33 #  include <unistd.h>
34 #endif
35 
36 namespace clang {
37 namespace tooling {
38 
~FrontendActionFactory()39 FrontendActionFactory::~FrontendActionFactory() {}
40 
41 // FIXME: This file contains structural duplication with other parts of the
42 // code that sets up a compiler to run tools on it, and we should refactor
43 // it to be based on the same framework.
44 
45 /// \brief Builds a clang driver initialized for running clang tools.
newDriver(clang::DiagnosticsEngine * Diagnostics,const char * BinaryName)46 static clang::driver::Driver *newDriver(clang::DiagnosticsEngine *Diagnostics,
47                                         const char *BinaryName) {
48   const std::string DefaultOutputName = "a.out";
49   clang::driver::Driver *CompilerDriver = new clang::driver::Driver(
50     BinaryName, llvm::sys::getDefaultTargetTriple(),
51     DefaultOutputName, false, *Diagnostics);
52   CompilerDriver->setTitle("clang_based_tool");
53   return CompilerDriver;
54 }
55 
56 /// \brief Retrieves the clang CC1 specific flags out of the compilation's jobs.
57 ///
58 /// Returns NULL on error.
getCC1Arguments(clang::DiagnosticsEngine * Diagnostics,clang::driver::Compilation * Compilation)59 static const clang::driver::ArgStringList *getCC1Arguments(
60     clang::DiagnosticsEngine *Diagnostics,
61     clang::driver::Compilation *Compilation) {
62   // We expect to get back exactly one Command job, if we didn't something
63   // failed. Extract that job from the Compilation.
64   const clang::driver::JobList &Jobs = Compilation->getJobs();
65   if (Jobs.size() != 1 || !isa<clang::driver::Command>(*Jobs.begin())) {
66     llvm::SmallString<256> error_msg;
67     llvm::raw_svector_ostream error_stream(error_msg);
68     Compilation->PrintJob(error_stream, Compilation->getJobs(), "; ", true);
69     Diagnostics->Report(clang::diag::err_fe_expected_compiler_job)
70         << error_stream.str();
71     return NULL;
72   }
73 
74   // The one job we find should be to invoke clang again.
75   const clang::driver::Command *Cmd =
76       cast<clang::driver::Command>(*Jobs.begin());
77   if (StringRef(Cmd->getCreator().getName()) != "clang") {
78     Diagnostics->Report(clang::diag::err_fe_expected_clang_command);
79     return NULL;
80   }
81 
82   return &Cmd->getArguments();
83 }
84 
85 /// \brief Returns a clang build invocation initialized from the CC1 flags.
newInvocation(clang::DiagnosticsEngine * Diagnostics,const clang::driver::ArgStringList & CC1Args)86 static clang::CompilerInvocation *newInvocation(
87     clang::DiagnosticsEngine *Diagnostics,
88     const clang::driver::ArgStringList &CC1Args) {
89   assert(!CC1Args.empty() && "Must at least contain the program name!");
90   clang::CompilerInvocation *Invocation = new clang::CompilerInvocation;
91   clang::CompilerInvocation::CreateFromArgs(
92       *Invocation, CC1Args.data() + 1, CC1Args.data() + CC1Args.size(),
93       *Diagnostics);
94   Invocation->getFrontendOpts().DisableFree = false;
95   return Invocation;
96 }
97 
runToolOnCode(clang::FrontendAction * ToolAction,const Twine & Code,const Twine & FileName)98 bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code,
99                    const Twine &FileName) {
100   return runToolOnCodeWithArgs(
101       ToolAction, Code, std::vector<std::string>(), FileName);
102 }
103 
runToolOnCodeWithArgs(clang::FrontendAction * ToolAction,const Twine & Code,const std::vector<std::string> & Args,const Twine & FileName)104 bool runToolOnCodeWithArgs(clang::FrontendAction *ToolAction, const Twine &Code,
105                            const std::vector<std::string> &Args,
106                            const Twine &FileName) {
107   SmallString<16> FileNameStorage;
108   StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage);
109   std::vector<std::string> Commands;
110   Commands.push_back("clang-tool");
111   Commands.push_back("-fsyntax-only");
112   Commands.insert(Commands.end(), Args.begin(), Args.end());
113   Commands.push_back(FileNameRef.data());
114   FileManager Files((FileSystemOptions()));
115   ToolInvocation Invocation(Commands, ToolAction, &Files);
116 
117   SmallString<1024> CodeStorage;
118   Invocation.mapVirtualFile(FileNameRef,
119                             Code.toNullTerminatedStringRef(CodeStorage));
120   return Invocation.run();
121 }
122 
getAbsolutePath(StringRef File)123 std::string getAbsolutePath(StringRef File) {
124   llvm::SmallString<1024> BaseDirectory;
125   if (const char *PWD = ::getenv("PWD"))
126     BaseDirectory = PWD;
127   else
128     llvm::sys::fs::current_path(BaseDirectory);
129   SmallString<1024> PathStorage;
130   if (llvm::sys::path::is_absolute(File)) {
131     llvm::sys::path::native(File, PathStorage);
132     return PathStorage.str();
133   }
134   StringRef RelativePath(File);
135   // FIXME: Should '.\\' be accepted on Win32?
136   if (RelativePath.startswith("./")) {
137     RelativePath = RelativePath.substr(strlen("./"));
138   }
139   llvm::SmallString<1024> AbsolutePath(BaseDirectory);
140   llvm::sys::path::append(AbsolutePath, RelativePath);
141   llvm::sys::path::native(Twine(AbsolutePath), PathStorage);
142   return PathStorage.str();
143 }
144 
ToolInvocation(ArrayRef<std::string> CommandLine,FrontendAction * ToolAction,FileManager * Files)145 ToolInvocation::ToolInvocation(
146     ArrayRef<std::string> CommandLine, FrontendAction *ToolAction,
147     FileManager *Files)
148     : CommandLine(CommandLine.vec()), ToolAction(ToolAction), Files(Files) {
149 }
150 
mapVirtualFile(StringRef FilePath,StringRef Content)151 void ToolInvocation::mapVirtualFile(StringRef FilePath, StringRef Content) {
152   SmallString<1024> PathStorage;
153   llvm::sys::path::native(FilePath, PathStorage);
154   MappedFileContents[PathStorage] = Content;
155 }
156 
run()157 bool ToolInvocation::run() {
158   std::vector<const char*> Argv;
159   for (int I = 0, E = CommandLine.size(); I != E; ++I)
160     Argv.push_back(CommandLine[I].c_str());
161   const char *const BinaryName = Argv[0];
162   DiagnosticOptions DefaultDiagnosticOptions;
163   TextDiagnosticPrinter DiagnosticPrinter(
164       llvm::errs(), DefaultDiagnosticOptions);
165   DiagnosticsEngine Diagnostics(llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs>(
166       new DiagnosticIDs()), &DiagnosticPrinter, false);
167 
168   const llvm::OwningPtr<clang::driver::Driver> Driver(
169       newDriver(&Diagnostics, BinaryName));
170   // Since the input might only be virtual, don't check whether it exists.
171   Driver->setCheckInputsExist(false);
172   const llvm::OwningPtr<clang::driver::Compilation> Compilation(
173       Driver->BuildCompilation(llvm::makeArrayRef(Argv)));
174   const clang::driver::ArgStringList *const CC1Args = getCC1Arguments(
175       &Diagnostics, Compilation.get());
176   if (CC1Args == NULL) {
177     return false;
178   }
179   llvm::OwningPtr<clang::CompilerInvocation> Invocation(
180       newInvocation(&Diagnostics, *CC1Args));
181   return runInvocation(BinaryName, Compilation.get(), Invocation.take(),
182                        *CC1Args);
183 }
184 
runInvocation(const char * BinaryName,clang::driver::Compilation * Compilation,clang::CompilerInvocation * Invocation,const clang::driver::ArgStringList & CC1Args)185 bool ToolInvocation::runInvocation(
186     const char *BinaryName,
187     clang::driver::Compilation *Compilation,
188     clang::CompilerInvocation *Invocation,
189     const clang::driver::ArgStringList &CC1Args) {
190   // Show the invocation, with -v.
191   if (Invocation->getHeaderSearchOpts().Verbose) {
192     llvm::errs() << "clang Invocation:\n";
193     Compilation->PrintJob(llvm::errs(), Compilation->getJobs(), "\n", true);
194     llvm::errs() << "\n";
195   }
196 
197   // Create a compiler instance to handle the actual work.
198   clang::CompilerInstance Compiler;
199   Compiler.setInvocation(Invocation);
200   Compiler.setFileManager(Files);
201   // FIXME: What about LangOpts?
202 
203   // ToolAction can have lifetime requirements for Compiler or its members, and
204   // we need to ensure it's deleted earlier than Compiler. So we pass it to an
205   // OwningPtr declared after the Compiler variable.
206   llvm::OwningPtr<FrontendAction> ScopedToolAction(ToolAction.take());
207 
208   // Create the compilers actual diagnostics engine.
209   Compiler.createDiagnostics(CC1Args.size(),
210                              const_cast<char**>(CC1Args.data()));
211   if (!Compiler.hasDiagnostics())
212     return false;
213 
214   Compiler.createSourceManager(*Files);
215   addFileMappingsTo(Compiler.getSourceManager());
216 
217   const bool Success = Compiler.ExecuteAction(*ScopedToolAction);
218 
219   Compiler.resetAndLeakFileManager();
220   Files->clearStatCaches();
221   return Success;
222 }
223 
addFileMappingsTo(SourceManager & Sources)224 void ToolInvocation::addFileMappingsTo(SourceManager &Sources) {
225   for (llvm::StringMap<StringRef>::const_iterator
226            It = MappedFileContents.begin(), End = MappedFileContents.end();
227        It != End; ++It) {
228     // Inject the code as the given file name into the preprocessor options.
229     const llvm::MemoryBuffer *Input =
230         llvm::MemoryBuffer::getMemBuffer(It->getValue());
231     // FIXME: figure out what '0' stands for.
232     const FileEntry *FromFile = Files->getVirtualFile(
233         It->getKey(), Input->getBufferSize(), 0);
234     Sources.overrideFileContents(FromFile, Input);
235   }
236 }
237 
ClangTool(const CompilationDatabase & Compilations,ArrayRef<std::string> SourcePaths)238 ClangTool::ClangTool(const CompilationDatabase &Compilations,
239                      ArrayRef<std::string> SourcePaths)
240     : Files((FileSystemOptions())),
241       ArgsAdjuster(new ClangSyntaxOnlyAdjuster()) {
242   for (unsigned I = 0, E = SourcePaths.size(); I != E; ++I) {
243     llvm::SmallString<1024> File(getAbsolutePath(SourcePaths[I]));
244 
245     std::vector<CompileCommand> CompileCommandsForFile =
246       Compilations.getCompileCommands(File.str());
247     if (!CompileCommandsForFile.empty()) {
248       for (int I = 0, E = CompileCommandsForFile.size(); I != E; ++I) {
249         CompileCommands.push_back(std::make_pair(File.str(),
250                                   CompileCommandsForFile[I]));
251       }
252     } else {
253       // FIXME: There are two use cases here: doing a fuzzy
254       // "find . -name '*.cc' |xargs tool" match, where as a user I don't care
255       // about the .cc files that were not found, and the use case where I
256       // specify all files I want to run over explicitly, where this should
257       // be an error. We'll want to add an option for this.
258       llvm::outs() << "Skipping " << File << ". Command line not found.\n";
259     }
260   }
261 }
262 
mapVirtualFile(StringRef FilePath,StringRef Content)263 void ClangTool::mapVirtualFile(StringRef FilePath, StringRef Content) {
264   MappedFileContents.push_back(std::make_pair(FilePath, Content));
265 }
266 
setArgumentsAdjuster(ArgumentsAdjuster * Adjuster)267 void ClangTool::setArgumentsAdjuster(ArgumentsAdjuster *Adjuster) {
268   ArgsAdjuster.reset(Adjuster);
269 }
270 
run(FrontendActionFactory * ActionFactory)271 int ClangTool::run(FrontendActionFactory *ActionFactory) {
272   // Exists solely for the purpose of lookup of the resource path.
273   // This just needs to be some symbol in the binary.
274   static int StaticSymbol;
275   // The driver detects the builtin header path based on the path of the
276   // executable.
277   // FIXME: On linux, GetMainExecutable is independent of the value of the
278   // first argument, thus allowing ClangTool and runToolOnCode to just
279   // pass in made-up names here. Make sure this works on other platforms.
280   std::string MainExecutable =
281     llvm::sys::Path::GetMainExecutable("clang_tool", &StaticSymbol).str();
282 
283   bool ProcessingFailed = false;
284   for (unsigned I = 0; I < CompileCommands.size(); ++I) {
285     std::string File = CompileCommands[I].first;
286     // FIXME: chdir is thread hostile; on the other hand, creating the same
287     // behavior as chdir is complex: chdir resolves the path once, thus
288     // guaranteeing that all subsequent relative path operations work
289     // on the same path the original chdir resulted in. This makes a difference
290     // for example on network filesystems, where symlinks might be switched
291     // during runtime of the tool. Fixing this depends on having a file system
292     // abstraction that allows openat() style interactions.
293     if (chdir(CompileCommands[I].second.Directory.c_str()))
294       llvm::report_fatal_error("Cannot chdir into \"" +
295                                CompileCommands[I].second.Directory + "\n!");
296     std::vector<std::string> CommandLine =
297       ArgsAdjuster->Adjust(CompileCommands[I].second.CommandLine);
298     assert(!CommandLine.empty());
299     CommandLine[0] = MainExecutable;
300     llvm::outs() << "Processing: " << File << ".\n";
301     ToolInvocation Invocation(CommandLine, ActionFactory->create(), &Files);
302     for (int I = 0, E = MappedFileContents.size(); I != E; ++I) {
303       Invocation.mapVirtualFile(MappedFileContents[I].first,
304                                 MappedFileContents[I].second);
305     }
306     if (!Invocation.run()) {
307       llvm::outs() << "Error while processing " << File << ".\n";
308       ProcessingFailed = true;
309     }
310   }
311   return ProcessingFailed ? 1 : 0;
312 }
313 
314 } // end namespace tooling
315 } // end namespace clang
316