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/TargetParser/Host.h"
15 #include "llvm/TargetParser/Triple.h"
16
17 namespace clang {
18 namespace tooling {
19 namespace {
20
21 class ExpandResponseFilesDatabase : public CompilationDatabase {
22 public:
ExpandResponseFilesDatabase(std::unique_ptr<CompilationDatabase> Base,llvm::cl::TokenizerCallback Tokenizer,llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)23 ExpandResponseFilesDatabase(
24 std::unique_ptr<CompilationDatabase> Base,
25 llvm::cl::TokenizerCallback Tokenizer,
26 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
27 : Base(std::move(Base)), Tokenizer(Tokenizer), FS(std::move(FS)) {
28 assert(this->Base != nullptr);
29 assert(this->Tokenizer != nullptr);
30 assert(this->FS != nullptr);
31 }
32
getAllFiles() const33 std::vector<std::string> getAllFiles() const override {
34 return Base->getAllFiles();
35 }
36
37 std::vector<CompileCommand>
getCompileCommands(StringRef FilePath) const38 getCompileCommands(StringRef FilePath) const override {
39 return expand(Base->getCompileCommands(FilePath));
40 }
41
getAllCompileCommands() const42 std::vector<CompileCommand> getAllCompileCommands() const override {
43 return expand(Base->getAllCompileCommands());
44 }
45
46 private:
expand(std::vector<CompileCommand> Cmds) const47 std::vector<CompileCommand> expand(std::vector<CompileCommand> Cmds) const {
48 for (auto &Cmd : Cmds)
49 tooling::addExpandedResponseFiles(Cmd.CommandLine, Cmd.Directory,
50 Tokenizer, *FS);
51 return Cmds;
52 }
53
54 private:
55 std::unique_ptr<CompilationDatabase> Base;
56 llvm::cl::TokenizerCallback Tokenizer;
57 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
58 };
59
60 } // namespace
61
62 std::unique_ptr<CompilationDatabase>
expandResponseFiles(std::unique_ptr<CompilationDatabase> Base,llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)63 expandResponseFiles(std::unique_ptr<CompilationDatabase> Base,
64 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS) {
65 auto Tokenizer = llvm::Triple(llvm::sys::getProcessTriple()).isOSWindows()
66 ? llvm::cl::TokenizeWindowsCommandLine
67 : llvm::cl::TokenizeGNUCommandLine;
68 return std::make_unique<ExpandResponseFilesDatabase>(
69 std::move(Base), Tokenizer, std::move(FS));
70 }
71
72 } // namespace tooling
73 } // namespace clang
74