blob: 99298316718bc03c371dac5f2501d868da88ebd4 [file] [log] [blame]
Kadir Cetinkaya45ef0552019-11-29 12:14:25 +01001//===- 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"
Kadir Cetinkayab3b37782019-12-04 16:34:56 +010010#include "llvm/ADT/StringRef.h"
Kadir Cetinkaya45ef0552019-11-29 12:14:25 +010011#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
19namespace clang {
20namespace tooling {
21namespace {
22
23class ExpandResponseFilesDatabase : public CompilationDatabase {
24public:
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
35 std::vector<std::string> getAllFiles() const override {
36 return Base->getAllFiles();
37 }
38
39 std::vector<CompileCommand>
40 getCompileCommands(StringRef FilePath) const override {
41 return expand(Base->getCompileCommands(FilePath));
42 }
43
44 std::vector<CompileCommand> getAllCompileCommands() const override {
45 return expand(Base->getAllCompileCommands());
46 }
47
48private:
49 std::vector<CompileCommand> expand(std::vector<CompileCommand> Cmds) const {
50 for (auto &Cmd : Cmds) {
Kadir Cetinkaya45ef0552019-11-29 12:14:25 +010051 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);
Kadir Cetinkayab3b37782019-12-04 16:34:56 +010062 llvm::cl::ExpandResponseFiles(Saver, Tokenizer, Argv, false, false, *FS,
63 llvm::StringRef(Cmd.Directory));
Sam McCall966fac12019-12-09 12:04:05 +010064 // Don't assign directly, Argv aliases CommandLine.
65 std::vector<std::string> ExpandedArgv(Argv.begin(), Argv.end());
66 Cmd.CommandLine = std::move(ExpandedArgv);
Kadir Cetinkaya45ef0552019-11-29 12:14:25 +010067 }
68 return Cmds;
69 }
70
71private:
72 std::unique_ptr<CompilationDatabase> Base;
73 llvm::cl::TokenizerCallback Tokenizer;
74 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
75};
76
77} // namespace
78
79std::unique_ptr<CompilationDatabase>
80expandResponseFiles(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