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
21namespace clang {
22namespace tooling {
23namespace {
24
25class ExpandResponseFilesDatabase : public CompilationDatabase {
26public:
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(Cmds: Base->getCompileCommands(FilePath));
44 }
45
46 std::vector<CompileCommand> getAllCompileCommands() const override {
47 return expand(Cmds: Base->getAllCompileCommands());
48 }
49
50private:
51 std::vector<CompileCommand> expand(std::vector<CompileCommand> Cmds) const {
52 for (auto &Cmd : Cmds)
53 tooling::addExpandedResponseFiles(CommandLine&: Cmd.CommandLine, WorkingDir: Cmd.Directory,
54 Tokenizer, FS&: *FS);
55 return Cmds;
56 }
57
58private:
59 std::unique_ptr<CompilationDatabase> Base;
60 llvm::cl::TokenizerCallback Tokenizer;
61 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
62};
63
64} // namespace
65
66std::unique_ptr<CompilationDatabase>
67expandResponseFiles(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 args: std::move(Base), args&: Tokenizer, args: std::move(FS));
74}
75
76} // namespace tooling
77} // namespace clang
78