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