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 "llvm/ADT/StringRef.h" 11 #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 19 namespace clang { 20 namespace tooling { 21 namespace { 22 23 class ExpandResponseFilesDatabase : public CompilationDatabase { 24 public: 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 48 private: 49 std::vector<CompileCommand> expand(std::vector<CompileCommand> Cmds) const { 50 for (auto &Cmd : Cmds) { 51 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); 62 llvm::cl::ExpandResponseFiles(Saver, Tokenizer, Argv, false, false, *FS, 63 llvm::StringRef(Cmd.Directory)); 64 Cmd.CommandLine.assign(Argv.begin(), Argv.end()); 65 } 66 return Cmds; 67 } 68 69 private: 70 std::unique_ptr<CompilationDatabase> Base; 71 llvm::cl::TokenizerCallback Tokenizer; 72 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS; 73 }; 74 75 } // namespace 76 77 std::unique_ptr<CompilationDatabase> 78 expandResponseFiles(std::unique_ptr<CompilationDatabase> Base, 79 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS) { 80 auto Tokenizer = llvm::Triple(llvm::sys::getProcessTriple()).isOSWindows() 81 ? llvm::cl::TokenizeWindowsCommandLine 82 : llvm::cl::TokenizeGNUCommandLine; 83 return std::make_unique<ExpandResponseFilesDatabase>( 84 std::move(Base), Tokenizer, std::move(FS)); 85 } 86 87 } // namespace tooling 88 } // namespace clang 89