blob: f1ab2aed54c025d1cf532818c79f0bdebccc8765 [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"
Reid Kleckner213aea42020-03-11 15:39:28 -070015#include "llvm/Support/Host.h"
Kadir Cetinkaya45ef0552019-11-29 12:14:25 +010016#include "llvm/Support/MemoryBuffer.h"
17#include "llvm/Support/Path.h"
18#include "llvm/Support/StringSaver.h"
19
20namespace clang {
21namespace tooling {
22namespace {
23
24class ExpandResponseFilesDatabase : public CompilationDatabase {
25public:
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
36 std::vector<std::string> getAllFiles() const override {
37 return Base->getAllFiles();
38 }
39
40 std::vector<CompileCommand>
41 getCompileCommands(StringRef FilePath) const override {
42 return expand(Base->getCompileCommands(FilePath));
43 }
44
45 std::vector<CompileCommand> getAllCompileCommands() const override {
46 return expand(Base->getAllCompileCommands());
47 }
48
49private:
50 std::vector<CompileCommand> expand(std::vector<CompileCommand> Cmds) const {
51 for (auto &Cmd : Cmds) {
Kadir Cetinkaya45ef0552019-11-29 12:14:25 +010052 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 SeenRSPFile |= Arg.front() == '@';
58 }
59 if (!SeenRSPFile)
60 continue;
61 llvm::BumpPtrAllocator Alloc;
62 llvm::StringSaver Saver(Alloc);
Kadir Cetinkayab3b37782019-12-04 16:34:56 +010063 llvm::cl::ExpandResponseFiles(Saver, Tokenizer, Argv, false, false, *FS,
64 llvm::StringRef(Cmd.Directory));
Sam McCall966fac12019-12-09 12:04:05 +010065 // Don't assign directly, Argv aliases CommandLine.
66 std::vector<std::string> ExpandedArgv(Argv.begin(), Argv.end());
67 Cmd.CommandLine = std::move(ExpandedArgv);
Kadir Cetinkaya45ef0552019-11-29 12:14:25 +010068 }
69 return Cmds;
70 }
71
72private:
73 std::unique_ptr<CompilationDatabase> Base;
74 llvm::cl::TokenizerCallback Tokenizer;
75 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
76};
77
78} // namespace
79
80std::unique_ptr<CompilationDatabase>
81expandResponseFiles(std::unique_ptr<CompilationDatabase> Base,
82 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS) {
83 auto Tokenizer = llvm::Triple(llvm::sys::getProcessTriple()).isOSWindows()
84 ? llvm::cl::TokenizeWindowsCommandLine
85 : llvm::cl::TokenizeGNUCommandLine;
86 return std::make_unique<ExpandResponseFilesDatabase>(
87 std::move(Base), Tokenizer, std::move(FS));
88}
89
90} // namespace tooling
91} // namespace clang