xref: /freebsd/contrib/llvm-project/clang/lib/Tooling/ExpandResponseFilesCompilationDatabase.cpp (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 //===- ExpandResponseFileCompilationDataBase.cpp --------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "clang/Tooling/CompilationDatabase.h"
10 #include "clang/Tooling/Tooling.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/Support/CommandLine.h"
13 #include "llvm/Support/ConvertUTF.h"
14 #include "llvm/Support/ErrorOr.h"
15 #include "llvm/Support/MemoryBuffer.h"
16 #include "llvm/Support/Path.h"
17 #include "llvm/Support/StringSaver.h"
18 #include "llvm/TargetParser/Host.h"
19 #include "llvm/TargetParser/Triple.h"
20 
21 namespace clang {
22 namespace tooling {
23 namespace {
24 
25 class ExpandResponseFilesDatabase : public CompilationDatabase {
26 public:
27   ExpandResponseFilesDatabase(
28       std::unique_ptr<CompilationDatabase> Base,
29       llvm::cl::TokenizerCallback Tokenizer,
30       llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
31       : Base(std::move(Base)), Tokenizer(Tokenizer), FS(std::move(FS)) {
32     assert(this->Base != nullptr);
33     assert(this->Tokenizer != nullptr);
34     assert(this->FS != nullptr);
35   }
36 
37   std::vector<std::string> getAllFiles() const override {
38     return Base->getAllFiles();
39   }
40 
41   std::vector<CompileCommand>
42   getCompileCommands(StringRef FilePath) const override {
43     return expand(Base->getCompileCommands(FilePath));
44   }
45 
46   std::vector<CompileCommand> getAllCompileCommands() const override {
47     return expand(Base->getAllCompileCommands());
48   }
49 
50 private:
51   std::vector<CompileCommand> expand(std::vector<CompileCommand> Cmds) const {
52     for (auto &Cmd : Cmds)
53       tooling::addExpandedResponseFiles(Cmd.CommandLine, Cmd.Directory,
54                                         Tokenizer, *FS);
55     return Cmds;
56   }
57 
58 private:
59   std::unique_ptr<CompilationDatabase> Base;
60   llvm::cl::TokenizerCallback Tokenizer;
61   llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
62 };
63 
64 } // namespace
65 
66 std::unique_ptr<CompilationDatabase>
67 expandResponseFiles(std::unique_ptr<CompilationDatabase> Base,
68                     llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS) {
69   auto Tokenizer = llvm::Triple(llvm::sys::getProcessTriple()).isOSWindows()
70                        ? llvm::cl::TokenizeWindowsCommandLine
71                        : llvm::cl::TokenizeGNUCommandLine;
72   return std::make_unique<ExpandResponseFilesDatabase>(
73       std::move(Base), Tokenizer, std::move(FS));
74 }
75 
76 } // namespace tooling
77 } // namespace clang
78