ExpandResponseFilesCompilationDatabase.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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::cl::ExpansionContext ECtx(Alloc, Tokenizer);
  57. llvm::Error Err = ECtx.setVFS(FS.get())
  58. .setCurrentDir(Cmd.Directory)
  59. .expandResponseFiles(Argv);
  60. if (Err)
  61. llvm::errs() << Err;
  62. // Don't assign directly, Argv aliases CommandLine.
  63. std::vector<std::string> ExpandedArgv(Argv.begin(), Argv.end());
  64. Cmd.CommandLine = std::move(ExpandedArgv);
  65. }
  66. return Cmds;
  67. }
  68. private:
  69. std::unique_ptr<CompilationDatabase> Base;
  70. llvm::cl::TokenizerCallback Tokenizer;
  71. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
  72. };
  73. } // namespace
  74. std::unique_ptr<CompilationDatabase>
  75. expandResponseFiles(std::unique_ptr<CompilationDatabase> Base,
  76. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS) {
  77. auto Tokenizer = llvm::Triple(llvm::sys::getProcessTriple()).isOSWindows()
  78. ? llvm::cl::TokenizeWindowsCommandLine
  79. : llvm::cl::TokenizeGNUCommandLine;
  80. return std::make_unique<ExpandResponseFilesDatabase>(
  81. std::move(Base), Tokenizer, std::move(FS));
  82. }
  83. } // namespace tooling
  84. } // namespace clang