ExpandResponseFilesCompilationDatabase.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #include "clang/Tooling/CompilationDatabase.h"
  9. #include "llvm/ADT/StringRef.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/Host.h"
  15. #include "llvm/Support/MemoryBuffer.h"
  16. #include "llvm/Support/Path.h"
  17. #include "llvm/Support/StringSaver.h"
  18. namespace clang {
  19. namespace tooling {
  20. namespace {
  21. class ExpandResponseFilesDatabase : public CompilationDatabase {
  22. public:
  23. ExpandResponseFilesDatabase(
  24. std::unique_ptr<CompilationDatabase> Base,
  25. llvm::cl::TokenizerCallback Tokenizer,
  26. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
  27. : Base(std::move(Base)), Tokenizer(Tokenizer), FS(std::move(FS)) {
  28. assert(this->Base != nullptr);
  29. assert(this->Tokenizer != nullptr);
  30. assert(this->FS != nullptr);
  31. }
  32. std::vector<std::string> getAllFiles() const override {
  33. return Base->getAllFiles();
  34. }
  35. std::vector<CompileCommand>
  36. getCompileCommands(StringRef FilePath) const override {
  37. return expand(Base->getCompileCommands(FilePath));
  38. }
  39. std::vector<CompileCommand> getAllCompileCommands() const override {
  40. return expand(Base->getAllCompileCommands());
  41. }
  42. private:
  43. std::vector<CompileCommand> expand(std::vector<CompileCommand> Cmds) const {
  44. for (auto &Cmd : Cmds) {
  45. bool SeenRSPFile = false;
  46. llvm::SmallVector<const char *, 20> Argv;
  47. Argv.reserve(Cmd.CommandLine.size());
  48. for (auto &Arg : Cmd.CommandLine) {
  49. Argv.push_back(Arg.c_str());
  50. if (!Arg.empty())
  51. SeenRSPFile |= Arg.front() == '@';
  52. }
  53. if (!SeenRSPFile)
  54. continue;
  55. llvm::BumpPtrAllocator Alloc;
  56. llvm::StringSaver Saver(Alloc);
  57. llvm::cl::ExpandResponseFiles(Saver, Tokenizer, Argv, false, false, false,
  58. llvm::StringRef(Cmd.Directory), *FS);
  59. // Don't assign directly, Argv aliases CommandLine.
  60. std::vector<std::string> ExpandedArgv(Argv.begin(), Argv.end());
  61. Cmd.CommandLine = std::move(ExpandedArgv);
  62. }
  63. return Cmds;
  64. }
  65. private:
  66. std::unique_ptr<CompilationDatabase> Base;
  67. llvm::cl::TokenizerCallback Tokenizer;
  68. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
  69. };
  70. } // namespace
  71. std::unique_ptr<CompilationDatabase>
  72. expandResponseFiles(std::unique_ptr<CompilationDatabase> Base,
  73. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS) {
  74. auto Tokenizer = llvm::Triple(llvm::sys::getProcessTriple()).isOSWindows()
  75. ? llvm::cl::TokenizeWindowsCommandLine
  76. : llvm::cl::TokenizeGNUCommandLine;
  77. return std::make_unique<ExpandResponseFilesDatabase>(
  78. std::move(Base), Tokenizer, std::move(FS));
  79. }
  80. } // namespace tooling
  81. } // namespace clang