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 "llvm/ADT/StringRef.h"
11 #include "llvm/ADT/Triple.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 
19 namespace clang {
20 namespace tooling {
21 namespace {
22 
23 class ExpandResponseFilesDatabase : public CompilationDatabase {
24 public:
ExpandResponseFilesDatabase(std::unique_ptr<CompilationDatabase> Base,llvm::cl::TokenizerCallback Tokenizer,llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)25   ExpandResponseFilesDatabase(
26       std::unique_ptr<CompilationDatabase> Base,
27       llvm::cl::TokenizerCallback Tokenizer,
28       llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
29       : Base(std::move(Base)), Tokenizer(Tokenizer), FS(std::move(FS)) {
30     assert(this->Base != nullptr);
31     assert(this->Tokenizer != nullptr);
32     assert(this->FS != nullptr);
33   }
34 
getAllFiles() const35   std::vector<std::string> getAllFiles() const override {
36     return Base->getAllFiles();
37   }
38 
39   std::vector<CompileCommand>
getCompileCommands(StringRef FilePath) const40   getCompileCommands(StringRef FilePath) const override {
41     return expand(Base->getCompileCommands(FilePath));
42   }
43 
getAllCompileCommands() const44   std::vector<CompileCommand> getAllCompileCommands() const override {
45     return expand(Base->getAllCompileCommands());
46   }
47 
48 private:
expand(std::vector<CompileCommand> Cmds) const49   std::vector<CompileCommand> expand(std::vector<CompileCommand> Cmds) const {
50     for (auto &Cmd : Cmds) {
51       bool SeenRSPFile = false;
52       llvm::SmallVector<const char *, 20> Argv;
53       Argv.reserve(Cmd.CommandLine.size());
54       for (auto &Arg : Cmd.CommandLine) {
55         Argv.push_back(Arg.c_str());
56         SeenRSPFile |= Arg.front() == '@';
57       }
58       if (!SeenRSPFile)
59         continue;
60       llvm::BumpPtrAllocator Alloc;
61       llvm::StringSaver Saver(Alloc);
62       llvm::cl::ExpandResponseFiles(Saver, Tokenizer, Argv, false, false, *FS,
63                                     llvm::StringRef(Cmd.Directory));
64       // Don't assign directly, Argv aliases CommandLine.
65       std::vector<std::string> ExpandedArgv(Argv.begin(), Argv.end());
66       Cmd.CommandLine = std::move(ExpandedArgv);
67     }
68     return Cmds;
69   }
70 
71 private:
72   std::unique_ptr<CompilationDatabase> Base;
73   llvm::cl::TokenizerCallback Tokenizer;
74   llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
75 };
76 
77 } // namespace
78 
79 std::unique_ptr<CompilationDatabase>
expandResponseFiles(std::unique_ptr<CompilationDatabase> Base,llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)80 expandResponseFiles(std::unique_ptr<CompilationDatabase> Base,
81                     llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS) {
82   auto Tokenizer = llvm::Triple(llvm::sys::getProcessTriple()).isOSWindows()
83                        ? llvm::cl::TokenizeWindowsCommandLine
84                        : llvm::cl::TokenizeGNUCommandLine;
85   return std::make_unique<ExpandResponseFilesDatabase>(
86       std::move(Base), Tokenizer, std::move(FS));
87 }
88 
89 } // namespace tooling
90 } // namespace clang
91