xref: /src/contrib/llvm-project/clang/lib/Tooling/ExpandResponseFilesCompilationDatabase.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1706b4fc4SDimitry Andric //===- ExpandResponseFileCompilationDataBase.cpp --------------------------===//
2706b4fc4SDimitry Andric //
3706b4fc4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4706b4fc4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5706b4fc4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6706b4fc4SDimitry Andric //
7706b4fc4SDimitry Andric //===----------------------------------------------------------------------===//
8706b4fc4SDimitry Andric 
9706b4fc4SDimitry Andric #include "clang/Tooling/CompilationDatabase.h"
107fa27ce4SDimitry Andric #include "clang/Tooling/Tooling.h"
11706b4fc4SDimitry Andric #include "llvm/ADT/StringRef.h"
12706b4fc4SDimitry Andric #include "llvm/Support/CommandLine.h"
13706b4fc4SDimitry Andric #include "llvm/Support/ConvertUTF.h"
14706b4fc4SDimitry Andric #include "llvm/Support/ErrorOr.h"
15706b4fc4SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
16706b4fc4SDimitry Andric #include "llvm/Support/Path.h"
17706b4fc4SDimitry Andric #include "llvm/Support/StringSaver.h"
187fa27ce4SDimitry Andric #include "llvm/TargetParser/Host.h"
197fa27ce4SDimitry Andric #include "llvm/TargetParser/Triple.h"
20706b4fc4SDimitry Andric 
21706b4fc4SDimitry Andric namespace clang {
22706b4fc4SDimitry Andric namespace tooling {
23706b4fc4SDimitry Andric namespace {
24706b4fc4SDimitry Andric 
25706b4fc4SDimitry Andric class ExpandResponseFilesDatabase : public CompilationDatabase {
26706b4fc4SDimitry Andric public:
ExpandResponseFilesDatabase(std::unique_ptr<CompilationDatabase> Base,llvm::cl::TokenizerCallback Tokenizer,llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)27706b4fc4SDimitry Andric   ExpandResponseFilesDatabase(
28706b4fc4SDimitry Andric       std::unique_ptr<CompilationDatabase> Base,
29706b4fc4SDimitry Andric       llvm::cl::TokenizerCallback Tokenizer,
30706b4fc4SDimitry Andric       llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
31706b4fc4SDimitry Andric       : Base(std::move(Base)), Tokenizer(Tokenizer), FS(std::move(FS)) {
32706b4fc4SDimitry Andric     assert(this->Base != nullptr);
33706b4fc4SDimitry Andric     assert(this->Tokenizer != nullptr);
34706b4fc4SDimitry Andric     assert(this->FS != nullptr);
35706b4fc4SDimitry Andric   }
36706b4fc4SDimitry Andric 
getAllFiles() const37706b4fc4SDimitry Andric   std::vector<std::string> getAllFiles() const override {
38706b4fc4SDimitry Andric     return Base->getAllFiles();
39706b4fc4SDimitry Andric   }
40706b4fc4SDimitry Andric 
41706b4fc4SDimitry Andric   std::vector<CompileCommand>
getCompileCommands(StringRef FilePath) const42706b4fc4SDimitry Andric   getCompileCommands(StringRef FilePath) const override {
43706b4fc4SDimitry Andric     return expand(Base->getCompileCommands(FilePath));
44706b4fc4SDimitry Andric   }
45706b4fc4SDimitry Andric 
getAllCompileCommands() const46706b4fc4SDimitry Andric   std::vector<CompileCommand> getAllCompileCommands() const override {
47706b4fc4SDimitry Andric     return expand(Base->getAllCompileCommands());
48706b4fc4SDimitry Andric   }
49706b4fc4SDimitry Andric 
50706b4fc4SDimitry Andric private:
expand(std::vector<CompileCommand> Cmds) const51706b4fc4SDimitry Andric   std::vector<CompileCommand> expand(std::vector<CompileCommand> Cmds) const {
527fa27ce4SDimitry Andric     for (auto &Cmd : Cmds)
537fa27ce4SDimitry Andric       tooling::addExpandedResponseFiles(Cmd.CommandLine, Cmd.Directory,
547fa27ce4SDimitry Andric                                         Tokenizer, *FS);
55706b4fc4SDimitry Andric     return Cmds;
56706b4fc4SDimitry Andric   }
57706b4fc4SDimitry Andric 
58706b4fc4SDimitry Andric private:
59706b4fc4SDimitry Andric   std::unique_ptr<CompilationDatabase> Base;
60706b4fc4SDimitry Andric   llvm::cl::TokenizerCallback Tokenizer;
61706b4fc4SDimitry Andric   llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
62706b4fc4SDimitry Andric };
63706b4fc4SDimitry Andric 
64706b4fc4SDimitry Andric } // namespace
65706b4fc4SDimitry Andric 
66706b4fc4SDimitry Andric std::unique_ptr<CompilationDatabase>
expandResponseFiles(std::unique_ptr<CompilationDatabase> Base,llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)67706b4fc4SDimitry Andric expandResponseFiles(std::unique_ptr<CompilationDatabase> Base,
68706b4fc4SDimitry Andric                     llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS) {
69706b4fc4SDimitry Andric   auto Tokenizer = llvm::Triple(llvm::sys::getProcessTriple()).isOSWindows()
70706b4fc4SDimitry Andric                        ? llvm::cl::TokenizeWindowsCommandLine
71706b4fc4SDimitry Andric                        : llvm::cl::TokenizeGNUCommandLine;
72706b4fc4SDimitry Andric   return std::make_unique<ExpandResponseFilesDatabase>(
73706b4fc4SDimitry Andric       std::move(Base), Tokenizer, std::move(FS));
74706b4fc4SDimitry Andric }
75706b4fc4SDimitry Andric 
76706b4fc4SDimitry Andric } // namespace tooling
77706b4fc4SDimitry Andric } // namespace clang
78