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/Triple.h" 11 #include "llvm/Support/CommandLine.h" 12 #include "llvm/Support/ConvertUTF.h" 13 #include "llvm/Support/ErrorOr.h" 14 #include "llvm/Support/MemoryBuffer.h" 15 #include "llvm/Support/Path.h" 16 #include "llvm/Support/StringSaver.h" 17 18 namespace clang { 19 namespace tooling { 20 namespace { 21 22 class ExpandResponseFilesDatabase : public CompilationDatabase { 23 public: 24 ExpandResponseFilesDatabase( 25 std::unique_ptr<CompilationDatabase> Base, 26 llvm::cl::TokenizerCallback Tokenizer, 27 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS) 28 : Base(std::move(Base)), Tokenizer(Tokenizer), FS(std::move(FS)) { 29 assert(this->Base != nullptr); 30 assert(this->Tokenizer != nullptr); 31 assert(this->FS != nullptr); 32 } 33 34 std::vector<std::string> getAllFiles() const override { 35 return Base->getAllFiles(); 36 } 37 38 std::vector<CompileCommand> 39 getCompileCommands(StringRef FilePath) const override { 40 return expand(Base->getCompileCommands(FilePath)); 41 } 42 43 std::vector<CompileCommand> getAllCompileCommands() const override { 44 return expand(Base->getAllCompileCommands()); 45 } 46 47 private: 48 std::vector<CompileCommand> expand(std::vector<CompileCommand> Cmds) const { 49 for (auto &Cmd : Cmds) { 50 // FIXME: we should rather propagate the current directory into 51 // ExpandResponseFiles as well in addition to FS. 52 if (std::error_code EC = FS->setCurrentWorkingDirectory(Cmd.Directory)) { 53 llvm::consumeError(llvm::errorCodeToError(EC)); 54 continue; 55 } 56 bool SeenRSPFile = false; 57 llvm::SmallVector<const char *, 20> Argv; 58 Argv.reserve(Cmd.CommandLine.size()); 59 for (auto &Arg : Cmd.CommandLine) { 60 Argv.push_back(Arg.c_str()); 61 SeenRSPFile |= Arg.front() == '@'; 62 } 63 if (!SeenRSPFile) 64 continue; 65 llvm::BumpPtrAllocator Alloc; 66 llvm::StringSaver Saver(Alloc); 67 llvm::cl::ExpandResponseFiles(Saver, Tokenizer, Argv, false, false, *FS); 68 Cmd.CommandLine.assign(Argv.begin(), Argv.end()); 69 } 70 return Cmds; 71 } 72 73 private: 74 std::unique_ptr<CompilationDatabase> Base; 75 llvm::cl::TokenizerCallback Tokenizer; 76 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS; 77 }; 78 79 } // namespace 80 81 std::unique_ptr<CompilationDatabase> 82 expandResponseFiles(std::unique_ptr<CompilationDatabase> Base, 83 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS) { 84 auto Tokenizer = llvm::Triple(llvm::sys::getProcessTriple()).isOSWindows() 85 ? llvm::cl::TokenizeWindowsCommandLine 86 : llvm::cl::TokenizeGNUCommandLine; 87 return std::make_unique<ExpandResponseFilesDatabase>( 88 std::move(Base), Tokenizer, std::move(FS)); 89 } 90 91 } // namespace tooling 92 } // namespace clang 93