xref: /aosp_15_r20/external/clang/lib/Tooling/CompilationDatabase.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li //===--- CompilationDatabase.cpp - ----------------------------------------===//
2*67e74705SXin Li //
3*67e74705SXin Li //                     The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li //  This file contains implementations of the CompilationDatabase base class
11*67e74705SXin Li //  and the FixedCompilationDatabase.
12*67e74705SXin Li //
13*67e74705SXin Li //===----------------------------------------------------------------------===//
14*67e74705SXin Li 
15*67e74705SXin Li #include "clang/Tooling/CompilationDatabase.h"
16*67e74705SXin Li #include "clang/Basic/Diagnostic.h"
17*67e74705SXin Li #include "clang/Basic/DiagnosticOptions.h"
18*67e74705SXin Li #include "clang/Driver/Action.h"
19*67e74705SXin Li #include "clang/Driver/Compilation.h"
20*67e74705SXin Li #include "clang/Driver/Driver.h"
21*67e74705SXin Li #include "clang/Driver/DriverDiagnostic.h"
22*67e74705SXin Li #include "clang/Driver/Job.h"
23*67e74705SXin Li #include "clang/Frontend/TextDiagnosticPrinter.h"
24*67e74705SXin Li #include "clang/Tooling/CompilationDatabasePluginRegistry.h"
25*67e74705SXin Li #include "clang/Tooling/Tooling.h"
26*67e74705SXin Li #include "llvm/ADT/SmallString.h"
27*67e74705SXin Li #include "llvm/Option/Arg.h"
28*67e74705SXin Li #include "llvm/Support/Host.h"
29*67e74705SXin Li #include "llvm/Support/Path.h"
30*67e74705SXin Li #include <sstream>
31*67e74705SXin Li #include <system_error>
32*67e74705SXin Li using namespace clang;
33*67e74705SXin Li using namespace tooling;
34*67e74705SXin Li 
~CompilationDatabase()35*67e74705SXin Li CompilationDatabase::~CompilationDatabase() {}
36*67e74705SXin Li 
37*67e74705SXin Li std::unique_ptr<CompilationDatabase>
loadFromDirectory(StringRef BuildDirectory,std::string & ErrorMessage)38*67e74705SXin Li CompilationDatabase::loadFromDirectory(StringRef BuildDirectory,
39*67e74705SXin Li                                        std::string &ErrorMessage) {
40*67e74705SXin Li   std::stringstream ErrorStream;
41*67e74705SXin Li   for (CompilationDatabasePluginRegistry::iterator
42*67e74705SXin Li        It = CompilationDatabasePluginRegistry::begin(),
43*67e74705SXin Li        Ie = CompilationDatabasePluginRegistry::end();
44*67e74705SXin Li        It != Ie; ++It) {
45*67e74705SXin Li     std::string DatabaseErrorMessage;
46*67e74705SXin Li     std::unique_ptr<CompilationDatabasePlugin> Plugin(It->instantiate());
47*67e74705SXin Li     if (std::unique_ptr<CompilationDatabase> DB =
48*67e74705SXin Li             Plugin->loadFromDirectory(BuildDirectory, DatabaseErrorMessage))
49*67e74705SXin Li       return DB;
50*67e74705SXin Li     ErrorStream << It->getName() << ": " << DatabaseErrorMessage << "\n";
51*67e74705SXin Li   }
52*67e74705SXin Li   ErrorMessage = ErrorStream.str();
53*67e74705SXin Li   return nullptr;
54*67e74705SXin Li }
55*67e74705SXin Li 
56*67e74705SXin Li static std::unique_ptr<CompilationDatabase>
findCompilationDatabaseFromDirectory(StringRef Directory,std::string & ErrorMessage)57*67e74705SXin Li findCompilationDatabaseFromDirectory(StringRef Directory,
58*67e74705SXin Li                                      std::string &ErrorMessage) {
59*67e74705SXin Li   std::stringstream ErrorStream;
60*67e74705SXin Li   bool HasErrorMessage = false;
61*67e74705SXin Li   while (!Directory.empty()) {
62*67e74705SXin Li     std::string LoadErrorMessage;
63*67e74705SXin Li 
64*67e74705SXin Li     if (std::unique_ptr<CompilationDatabase> DB =
65*67e74705SXin Li             CompilationDatabase::loadFromDirectory(Directory, LoadErrorMessage))
66*67e74705SXin Li       return DB;
67*67e74705SXin Li 
68*67e74705SXin Li     if (!HasErrorMessage) {
69*67e74705SXin Li       ErrorStream << "No compilation database found in " << Directory.str()
70*67e74705SXin Li                   << " or any parent directory\n" << LoadErrorMessage;
71*67e74705SXin Li       HasErrorMessage = true;
72*67e74705SXin Li     }
73*67e74705SXin Li 
74*67e74705SXin Li     Directory = llvm::sys::path::parent_path(Directory);
75*67e74705SXin Li   }
76*67e74705SXin Li   ErrorMessage = ErrorStream.str();
77*67e74705SXin Li   return nullptr;
78*67e74705SXin Li }
79*67e74705SXin Li 
80*67e74705SXin Li std::unique_ptr<CompilationDatabase>
autoDetectFromSource(StringRef SourceFile,std::string & ErrorMessage)81*67e74705SXin Li CompilationDatabase::autoDetectFromSource(StringRef SourceFile,
82*67e74705SXin Li                                           std::string &ErrorMessage) {
83*67e74705SXin Li   SmallString<1024> AbsolutePath(getAbsolutePath(SourceFile));
84*67e74705SXin Li   StringRef Directory = llvm::sys::path::parent_path(AbsolutePath);
85*67e74705SXin Li 
86*67e74705SXin Li   std::unique_ptr<CompilationDatabase> DB =
87*67e74705SXin Li       findCompilationDatabaseFromDirectory(Directory, ErrorMessage);
88*67e74705SXin Li 
89*67e74705SXin Li   if (!DB)
90*67e74705SXin Li     ErrorMessage = ("Could not auto-detect compilation database for file \"" +
91*67e74705SXin Li                    SourceFile + "\"\n" + ErrorMessage).str();
92*67e74705SXin Li   return DB;
93*67e74705SXin Li }
94*67e74705SXin Li 
95*67e74705SXin Li std::unique_ptr<CompilationDatabase>
autoDetectFromDirectory(StringRef SourceDir,std::string & ErrorMessage)96*67e74705SXin Li CompilationDatabase::autoDetectFromDirectory(StringRef SourceDir,
97*67e74705SXin Li                                              std::string &ErrorMessage) {
98*67e74705SXin Li   SmallString<1024> AbsolutePath(getAbsolutePath(SourceDir));
99*67e74705SXin Li 
100*67e74705SXin Li   std::unique_ptr<CompilationDatabase> DB =
101*67e74705SXin Li       findCompilationDatabaseFromDirectory(AbsolutePath, ErrorMessage);
102*67e74705SXin Li 
103*67e74705SXin Li   if (!DB)
104*67e74705SXin Li     ErrorMessage = ("Could not auto-detect compilation database from directory \"" +
105*67e74705SXin Li                    SourceDir + "\"\n" + ErrorMessage).str();
106*67e74705SXin Li   return DB;
107*67e74705SXin Li }
108*67e74705SXin Li 
~CompilationDatabasePlugin()109*67e74705SXin Li CompilationDatabasePlugin::~CompilationDatabasePlugin() {}
110*67e74705SXin Li 
111*67e74705SXin Li namespace {
112*67e74705SXin Li // Helper for recursively searching through a chain of actions and collecting
113*67e74705SXin Li // all inputs, direct and indirect, of compile jobs.
114*67e74705SXin Li struct CompileJobAnalyzer {
run__anonaa085dd10111::CompileJobAnalyzer115*67e74705SXin Li   void run(const driver::Action *A) {
116*67e74705SXin Li     runImpl(A, false);
117*67e74705SXin Li   }
118*67e74705SXin Li 
119*67e74705SXin Li   SmallVector<std::string, 2> Inputs;
120*67e74705SXin Li 
121*67e74705SXin Li private:
122*67e74705SXin Li 
runImpl__anonaa085dd10111::CompileJobAnalyzer123*67e74705SXin Li   void runImpl(const driver::Action *A, bool Collect) {
124*67e74705SXin Li     bool CollectChildren = Collect;
125*67e74705SXin Li     switch (A->getKind()) {
126*67e74705SXin Li     case driver::Action::CompileJobClass:
127*67e74705SXin Li       CollectChildren = true;
128*67e74705SXin Li       break;
129*67e74705SXin Li 
130*67e74705SXin Li     case driver::Action::InputClass: {
131*67e74705SXin Li       if (Collect) {
132*67e74705SXin Li         const driver::InputAction *IA = cast<driver::InputAction>(A);
133*67e74705SXin Li         Inputs.push_back(IA->getInputArg().getSpelling());
134*67e74705SXin Li       }
135*67e74705SXin Li     } break;
136*67e74705SXin Li 
137*67e74705SXin Li     default:
138*67e74705SXin Li       // Don't care about others
139*67e74705SXin Li       ;
140*67e74705SXin Li     }
141*67e74705SXin Li 
142*67e74705SXin Li     for (const driver::Action *AI : A->inputs())
143*67e74705SXin Li       runImpl(AI, CollectChildren);
144*67e74705SXin Li   }
145*67e74705SXin Li };
146*67e74705SXin Li 
147*67e74705SXin Li // Special DiagnosticConsumer that looks for warn_drv_input_file_unused
148*67e74705SXin Li // diagnostics from the driver and collects the option strings for those unused
149*67e74705SXin Li // options.
150*67e74705SXin Li class UnusedInputDiagConsumer : public DiagnosticConsumer {
151*67e74705SXin Li public:
UnusedInputDiagConsumer()152*67e74705SXin Li   UnusedInputDiagConsumer() : Other(nullptr) {}
153*67e74705SXin Li 
154*67e74705SXin Li   // Useful for debugging, chain diagnostics to another consumer after
155*67e74705SXin Li   // recording for our own purposes.
UnusedInputDiagConsumer(DiagnosticConsumer * Other)156*67e74705SXin Li   UnusedInputDiagConsumer(DiagnosticConsumer *Other) : Other(Other) {}
157*67e74705SXin Li 
HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,const Diagnostic & Info)158*67e74705SXin Li   void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
159*67e74705SXin Li                         const Diagnostic &Info) override {
160*67e74705SXin Li     if (Info.getID() == clang::diag::warn_drv_input_file_unused) {
161*67e74705SXin Li       // Arg 1 for this diagnostic is the option that didn't get used.
162*67e74705SXin Li       UnusedInputs.push_back(Info.getArgStdStr(0));
163*67e74705SXin Li     }
164*67e74705SXin Li     if (Other)
165*67e74705SXin Li       Other->HandleDiagnostic(DiagLevel, Info);
166*67e74705SXin Li   }
167*67e74705SXin Li 
168*67e74705SXin Li   DiagnosticConsumer *Other;
169*67e74705SXin Li   SmallVector<std::string, 2> UnusedInputs;
170*67e74705SXin Li };
171*67e74705SXin Li 
172*67e74705SXin Li // Unary functor for asking "Given a StringRef S1, does there exist a string
173*67e74705SXin Li // S2 in Arr where S1 == S2?"
174*67e74705SXin Li struct MatchesAny {
MatchesAny__anonaa085dd10111::MatchesAny175*67e74705SXin Li   MatchesAny(ArrayRef<std::string> Arr) : Arr(Arr) {}
operator ()__anonaa085dd10111::MatchesAny176*67e74705SXin Li   bool operator() (StringRef S) {
177*67e74705SXin Li     for (const std::string *I = Arr.begin(), *E = Arr.end(); I != E; ++I)
178*67e74705SXin Li       if (*I == S)
179*67e74705SXin Li         return true;
180*67e74705SXin Li     return false;
181*67e74705SXin Li   }
182*67e74705SXin Li private:
183*67e74705SXin Li   ArrayRef<std::string> Arr;
184*67e74705SXin Li };
185*67e74705SXin Li } // namespace
186*67e74705SXin Li 
187*67e74705SXin Li /// \brief Strips any positional args and possible argv[0] from a command-line
188*67e74705SXin Li /// provided by the user to construct a FixedCompilationDatabase.
189*67e74705SXin Li ///
190*67e74705SXin Li /// FixedCompilationDatabase requires a command line to be in this format as it
191*67e74705SXin Li /// constructs the command line for each file by appending the name of the file
192*67e74705SXin Li /// to be compiled. FixedCompilationDatabase also adds its own argv[0] to the
193*67e74705SXin Li /// start of the command line although its value is not important as it's just
194*67e74705SXin Li /// ignored by the Driver invoked by the ClangTool using the
195*67e74705SXin Li /// FixedCompilationDatabase.
196*67e74705SXin Li ///
197*67e74705SXin Li /// FIXME: This functionality should probably be made available by
198*67e74705SXin Li /// clang::driver::Driver although what the interface should look like is not
199*67e74705SXin Li /// clear.
200*67e74705SXin Li ///
201*67e74705SXin Li /// \param[in] Args Args as provided by the user.
202*67e74705SXin Li /// \return Resulting stripped command line.
203*67e74705SXin Li ///          \li true if successful.
204*67e74705SXin Li ///          \li false if \c Args cannot be used for compilation jobs (e.g.
205*67e74705SXin Li ///          contains an option like -E or -version).
stripPositionalArgs(std::vector<const char * > Args,std::vector<std::string> & Result)206*67e74705SXin Li static bool stripPositionalArgs(std::vector<const char *> Args,
207*67e74705SXin Li                                 std::vector<std::string> &Result) {
208*67e74705SXin Li   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
209*67e74705SXin Li   UnusedInputDiagConsumer DiagClient;
210*67e74705SXin Li   DiagnosticsEngine Diagnostics(
211*67e74705SXin Li       IntrusiveRefCntPtr<clang::DiagnosticIDs>(new DiagnosticIDs()),
212*67e74705SXin Li       &*DiagOpts, &DiagClient, false);
213*67e74705SXin Li 
214*67e74705SXin Li   // The clang executable path isn't required since the jobs the driver builds
215*67e74705SXin Li   // will not be executed.
216*67e74705SXin Li   std::unique_ptr<driver::Driver> NewDriver(new driver::Driver(
217*67e74705SXin Li       /* ClangExecutable= */ "", llvm::sys::getDefaultTargetTriple(),
218*67e74705SXin Li       Diagnostics));
219*67e74705SXin Li   NewDriver->setCheckInputsExist(false);
220*67e74705SXin Li 
221*67e74705SXin Li   // This becomes the new argv[0]. The value is actually not important as it
222*67e74705SXin Li   // isn't used for invoking Tools.
223*67e74705SXin Li   Args.insert(Args.begin(), "clang-tool");
224*67e74705SXin Li 
225*67e74705SXin Li   // By adding -c, we force the driver to treat compilation as the last phase.
226*67e74705SXin Li   // It will then issue warnings via Diagnostics about un-used options that
227*67e74705SXin Li   // would have been used for linking. If the user provided a compiler name as
228*67e74705SXin Li   // the original argv[0], this will be treated as a linker input thanks to
229*67e74705SXin Li   // insertng a new argv[0] above. All un-used options get collected by
230*67e74705SXin Li   // UnusedInputdiagConsumer and get stripped out later.
231*67e74705SXin Li   Args.push_back("-c");
232*67e74705SXin Li 
233*67e74705SXin Li   // Put a dummy C++ file on to ensure there's at least one compile job for the
234*67e74705SXin Li   // driver to construct. If the user specified some other argument that
235*67e74705SXin Li   // prevents compilation, e.g. -E or something like -version, we may still end
236*67e74705SXin Li   // up with no jobs but then this is the user's fault.
237*67e74705SXin Li   Args.push_back("placeholder.cpp");
238*67e74705SXin Li 
239*67e74705SXin Li   // Remove -no-integrated-as; it's not used for syntax checking,
240*67e74705SXin Li   // and it confuses targets which don't support this option.
241*67e74705SXin Li   Args.erase(std::remove_if(Args.begin(), Args.end(),
242*67e74705SXin Li                             MatchesAny(std::string("-no-integrated-as"))),
243*67e74705SXin Li              Args.end());
244*67e74705SXin Li 
245*67e74705SXin Li   const std::unique_ptr<driver::Compilation> Compilation(
246*67e74705SXin Li       NewDriver->BuildCompilation(Args));
247*67e74705SXin Li 
248*67e74705SXin Li   const driver::JobList &Jobs = Compilation->getJobs();
249*67e74705SXin Li 
250*67e74705SXin Li   CompileJobAnalyzer CompileAnalyzer;
251*67e74705SXin Li 
252*67e74705SXin Li   for (const auto &Cmd : Jobs) {
253*67e74705SXin Li     // Collect only for Assemble jobs. If we do all jobs we get duplicates
254*67e74705SXin Li     // since Link jobs point to Assemble jobs as inputs.
255*67e74705SXin Li     if (Cmd.getSource().getKind() == driver::Action::AssembleJobClass)
256*67e74705SXin Li       CompileAnalyzer.run(&Cmd.getSource());
257*67e74705SXin Li   }
258*67e74705SXin Li 
259*67e74705SXin Li   if (CompileAnalyzer.Inputs.empty()) {
260*67e74705SXin Li     // No compile jobs found.
261*67e74705SXin Li     // FIXME: Emit a warning of some kind?
262*67e74705SXin Li     return false;
263*67e74705SXin Li   }
264*67e74705SXin Li 
265*67e74705SXin Li   // Remove all compilation input files from the command line. This is
266*67e74705SXin Li   // necessary so that getCompileCommands() can construct a command line for
267*67e74705SXin Li   // each file.
268*67e74705SXin Li   std::vector<const char *>::iterator End = std::remove_if(
269*67e74705SXin Li       Args.begin(), Args.end(), MatchesAny(CompileAnalyzer.Inputs));
270*67e74705SXin Li 
271*67e74705SXin Li   // Remove all inputs deemed unused for compilation.
272*67e74705SXin Li   End = std::remove_if(Args.begin(), End, MatchesAny(DiagClient.UnusedInputs));
273*67e74705SXin Li 
274*67e74705SXin Li   // Remove the -c add above as well. It will be at the end right now.
275*67e74705SXin Li   assert(strcmp(*(End - 1), "-c") == 0);
276*67e74705SXin Li   --End;
277*67e74705SXin Li 
278*67e74705SXin Li   Result = std::vector<std::string>(Args.begin() + 1, End);
279*67e74705SXin Li   return true;
280*67e74705SXin Li }
281*67e74705SXin Li 
loadFromCommandLine(int & Argc,const char * const * Argv,Twine Directory)282*67e74705SXin Li FixedCompilationDatabase *FixedCompilationDatabase::loadFromCommandLine(
283*67e74705SXin Li     int &Argc, const char *const *Argv, Twine Directory) {
284*67e74705SXin Li   const char *const *DoubleDash = std::find(Argv, Argv + Argc, StringRef("--"));
285*67e74705SXin Li   if (DoubleDash == Argv + Argc)
286*67e74705SXin Li     return nullptr;
287*67e74705SXin Li   std::vector<const char *> CommandLine(DoubleDash + 1, Argv + Argc);
288*67e74705SXin Li   Argc = DoubleDash - Argv;
289*67e74705SXin Li 
290*67e74705SXin Li   std::vector<std::string> StrippedArgs;
291*67e74705SXin Li   if (!stripPositionalArgs(CommandLine, StrippedArgs))
292*67e74705SXin Li     return nullptr;
293*67e74705SXin Li   return new FixedCompilationDatabase(Directory, StrippedArgs);
294*67e74705SXin Li }
295*67e74705SXin Li 
296*67e74705SXin Li FixedCompilationDatabase::
FixedCompilationDatabase(Twine Directory,ArrayRef<std::string> CommandLine)297*67e74705SXin Li FixedCompilationDatabase(Twine Directory, ArrayRef<std::string> CommandLine) {
298*67e74705SXin Li   std::vector<std::string> ToolCommandLine(1, "clang-tool");
299*67e74705SXin Li   ToolCommandLine.insert(ToolCommandLine.end(),
300*67e74705SXin Li                          CommandLine.begin(), CommandLine.end());
301*67e74705SXin Li   CompileCommands.emplace_back(Directory, StringRef(),
302*67e74705SXin Li                                std::move(ToolCommandLine));
303*67e74705SXin Li }
304*67e74705SXin Li 
305*67e74705SXin Li std::vector<CompileCommand>
getCompileCommands(StringRef FilePath) const306*67e74705SXin Li FixedCompilationDatabase::getCompileCommands(StringRef FilePath) const {
307*67e74705SXin Li   std::vector<CompileCommand> Result(CompileCommands);
308*67e74705SXin Li   Result[0].CommandLine.push_back(FilePath);
309*67e74705SXin Li   Result[0].Filename = FilePath;
310*67e74705SXin Li   return Result;
311*67e74705SXin Li }
312*67e74705SXin Li 
313*67e74705SXin Li std::vector<std::string>
getAllFiles() const314*67e74705SXin Li FixedCompilationDatabase::getAllFiles() const {
315*67e74705SXin Li   return std::vector<std::string>();
316*67e74705SXin Li }
317*67e74705SXin Li 
318*67e74705SXin Li std::vector<CompileCommand>
getAllCompileCommands() const319*67e74705SXin Li FixedCompilationDatabase::getAllCompileCommands() const {
320*67e74705SXin Li   return std::vector<CompileCommand>();
321*67e74705SXin Li }
322*67e74705SXin Li 
323*67e74705SXin Li namespace clang {
324*67e74705SXin Li namespace tooling {
325*67e74705SXin Li 
326*67e74705SXin Li // This anchor is used to force the linker to link in the generated object file
327*67e74705SXin Li // and thus register the JSONCompilationDatabasePlugin.
328*67e74705SXin Li extern volatile int JSONAnchorSource;
329*67e74705SXin Li static int LLVM_ATTRIBUTE_UNUSED JSONAnchorDest = JSONAnchorSource;
330*67e74705SXin Li 
331*67e74705SXin Li } // end namespace tooling
332*67e74705SXin Li } // end namespace clang
333