InterpolatingCompilationDatabase.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. //===- InterpolatingCompilationDatabase.cpp ---------------------*- C++ -*-===//
  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. // InterpolatingCompilationDatabase wraps another CompilationDatabase and
  10. // attempts to heuristically determine appropriate compile commands for files
  11. // that are not included, such as headers or newly created files.
  12. //
  13. // Motivating cases include:
  14. // Header files that live next to their implementation files. These typically
  15. // share a base filename. (libclang/CXString.h, libclang/CXString.cpp).
  16. // Some projects separate headers from includes. Filenames still typically
  17. // match, maybe other path segments too. (include/llvm/IR/Use.h, lib/IR/Use.cc).
  18. // Matches are sometimes only approximate (Sema.h, SemaDecl.cpp). This goes
  19. // for directories too (Support/Unix/Process.inc, lib/Support/Process.cpp).
  20. // Even if we can't find a "right" compile command, even a random one from
  21. // the project will tend to get important flags like -I and -x right.
  22. //
  23. // We "borrow" the compile command for the closest available file:
  24. // - points are awarded if the filename matches (ignoring extension)
  25. // - points are awarded if the directory structure matches
  26. // - ties are broken by length of path prefix match
  27. //
  28. // The compile command is adjusted, replacing the filename and removing output
  29. // file arguments. The -x and -std flags may be affected too.
  30. //
  31. // Source language is a tricky issue: is it OK to use a .c file's command
  32. // for building a .cc file? What language is a .h file in?
  33. // - We only consider compile commands for c-family languages as candidates.
  34. // - For files whose language is implied by the filename (e.g. .m, .hpp)
  35. // we prefer candidates from the same language.
  36. // If we must cross languages, we drop any -x and -std flags.
  37. // - For .h files, candidates from any c-family language are acceptable.
  38. // We use the candidate's language, inserting e.g. -x c++-header.
  39. //
  40. // This class is only useful when wrapping databases that can enumerate all
  41. // their compile commands. If getAllFilenames() is empty, no inference occurs.
  42. //
  43. //===----------------------------------------------------------------------===//
  44. #include "clang/Basic/LangStandard.h"
  45. #include "clang/Driver/Options.h"
  46. #include "clang/Driver/Types.h"
  47. #include "clang/Tooling/CompilationDatabase.h"
  48. #include "llvm/ADT/DenseMap.h"
  49. #include "llvm/ADT/Optional.h"
  50. #include "llvm/ADT/StringExtras.h"
  51. #include "llvm/ADT/StringSwitch.h"
  52. #include "llvm/Option/ArgList.h"
  53. #include "llvm/Option/OptTable.h"
  54. #include "llvm/Support/Debug.h"
  55. #include "llvm/Support/Path.h"
  56. #include "llvm/Support/StringSaver.h"
  57. #include "llvm/Support/raw_ostream.h"
  58. #include <memory>
  59. namespace clang {
  60. namespace tooling {
  61. namespace {
  62. using namespace llvm;
  63. namespace types = clang::driver::types;
  64. namespace path = llvm::sys::path;
  65. // The length of the prefix these two strings have in common.
  66. size_t matchingPrefix(StringRef L, StringRef R) {
  67. size_t Limit = std::min(L.size(), R.size());
  68. for (size_t I = 0; I < Limit; ++I)
  69. if (L[I] != R[I])
  70. return I;
  71. return Limit;
  72. }
  73. // A comparator for searching SubstringWithIndexes with std::equal_range etc.
  74. // Optionaly prefix semantics: compares equal if the key is a prefix.
  75. template <bool Prefix> struct Less {
  76. bool operator()(StringRef Key, std::pair<StringRef, size_t> Value) const {
  77. StringRef V = Prefix ? Value.first.substr(0, Key.size()) : Value.first;
  78. return Key < V;
  79. }
  80. bool operator()(std::pair<StringRef, size_t> Value, StringRef Key) const {
  81. StringRef V = Prefix ? Value.first.substr(0, Key.size()) : Value.first;
  82. return V < Key;
  83. }
  84. };
  85. // Infer type from filename. If we might have gotten it wrong, set *Certain.
  86. // *.h will be inferred as a C header, but not certain.
  87. types::ID guessType(StringRef Filename, bool *Certain = nullptr) {
  88. // path::extension is ".cpp", lookupTypeForExtension wants "cpp".
  89. auto Lang =
  90. types::lookupTypeForExtension(path::extension(Filename).substr(1));
  91. if (Certain)
  92. *Certain = Lang != types::TY_CHeader && Lang != types::TY_INVALID;
  93. return Lang;
  94. }
  95. // Return Lang as one of the canonical supported types.
  96. // e.g. c-header --> c; fortran --> TY_INVALID
  97. static types::ID foldType(types::ID Lang) {
  98. switch (Lang) {
  99. case types::TY_C:
  100. case types::TY_CHeader:
  101. return types::TY_C;
  102. case types::TY_ObjC:
  103. case types::TY_ObjCHeader:
  104. return types::TY_ObjC;
  105. case types::TY_CXX:
  106. case types::TY_CXXHeader:
  107. return types::TY_CXX;
  108. case types::TY_ObjCXX:
  109. case types::TY_ObjCXXHeader:
  110. return types::TY_ObjCXX;
  111. default:
  112. return types::TY_INVALID;
  113. }
  114. }
  115. // A CompileCommand that can be applied to another file.
  116. struct TransferableCommand {
  117. // Flags that should not apply to all files are stripped from CommandLine.
  118. CompileCommand Cmd;
  119. // Language detected from -x or the filename. Never TY_INVALID.
  120. Optional<types::ID> Type;
  121. // Standard specified by -std.
  122. LangStandard::Kind Std = LangStandard::lang_unspecified;
  123. // Whether the command line is for the cl-compatible driver.
  124. bool ClangCLMode;
  125. TransferableCommand(CompileCommand C)
  126. : Cmd(std::move(C)), Type(guessType(Cmd.Filename)),
  127. ClangCLMode(checkIsCLMode(Cmd.CommandLine)) {
  128. std::vector<std::string> OldArgs = std::move(Cmd.CommandLine);
  129. Cmd.CommandLine.clear();
  130. // Wrap the old arguments in an InputArgList.
  131. llvm::opt::InputArgList ArgList;
  132. {
  133. SmallVector<const char *, 16> TmpArgv;
  134. for (const std::string &S : OldArgs)
  135. TmpArgv.push_back(S.c_str());
  136. ArgList = {TmpArgv.begin(), TmpArgv.end()};
  137. }
  138. // Parse the old args in order to strip out and record unwanted flags.
  139. // We parse each argument individually so that we can retain the exact
  140. // spelling of each argument; re-rendering is lossy for aliased flags.
  141. // E.g. in CL mode, /W4 maps to -Wall.
  142. auto &OptTable = clang::driver::getDriverOptTable();
  143. if (!OldArgs.empty())
  144. Cmd.CommandLine.emplace_back(OldArgs.front());
  145. for (unsigned Pos = 1; Pos < OldArgs.size();) {
  146. using namespace driver::options;
  147. const unsigned OldPos = Pos;
  148. std::unique_ptr<llvm::opt::Arg> Arg(OptTable.ParseOneArg(
  149. ArgList, Pos,
  150. /* Include */ ClangCLMode ? CoreOption | CLOption : 0,
  151. /* Exclude */ ClangCLMode ? 0 : CLOption));
  152. if (!Arg)
  153. continue;
  154. const llvm::opt::Option &Opt = Arg->getOption();
  155. // Strip input and output files.
  156. if (Opt.matches(OPT_INPUT) || Opt.matches(OPT_o) ||
  157. (ClangCLMode && (Opt.matches(OPT__SLASH_Fa) ||
  158. Opt.matches(OPT__SLASH_Fe) ||
  159. Opt.matches(OPT__SLASH_Fi) ||
  160. Opt.matches(OPT__SLASH_Fo))))
  161. continue;
  162. // Strip -x, but record the overridden language.
  163. if (const auto GivenType = tryParseTypeArg(*Arg)) {
  164. Type = *GivenType;
  165. continue;
  166. }
  167. // Strip -std, but record the value.
  168. if (const auto GivenStd = tryParseStdArg(*Arg)) {
  169. if (*GivenStd != LangStandard::lang_unspecified)
  170. Std = *GivenStd;
  171. continue;
  172. }
  173. Cmd.CommandLine.insert(Cmd.CommandLine.end(),
  174. OldArgs.data() + OldPos, OldArgs.data() + Pos);
  175. }
  176. if (Std != LangStandard::lang_unspecified) // -std take precedence over -x
  177. Type = toType(LangStandard::getLangStandardForKind(Std).getLanguage());
  178. Type = foldType(*Type);
  179. // The contract is to store None instead of TY_INVALID.
  180. if (Type == types::TY_INVALID)
  181. Type = llvm::None;
  182. }
  183. // Produce a CompileCommand for \p filename, based on this one.
  184. CompileCommand transferTo(StringRef Filename) const {
  185. CompileCommand Result = Cmd;
  186. Result.Filename = Filename;
  187. bool TypeCertain;
  188. auto TargetType = guessType(Filename, &TypeCertain);
  189. // If the filename doesn't determine the language (.h), transfer with -x.
  190. if ((!TargetType || !TypeCertain) && Type) {
  191. // Use *Type, or its header variant if the file is a header.
  192. // Treat no/invalid extension as header (e.g. C++ standard library).
  193. TargetType =
  194. (!TargetType || types::onlyPrecompileType(TargetType)) // header?
  195. ? types::lookupHeaderTypeForSourceType(*Type)
  196. : *Type;
  197. if (ClangCLMode) {
  198. const StringRef Flag = toCLFlag(TargetType);
  199. if (!Flag.empty())
  200. Result.CommandLine.push_back(Flag);
  201. } else {
  202. Result.CommandLine.push_back("-x");
  203. Result.CommandLine.push_back(types::getTypeName(TargetType));
  204. }
  205. }
  206. // --std flag may only be transferred if the language is the same.
  207. // We may consider "translating" these, e.g. c++11 -> c11.
  208. if (Std != LangStandard::lang_unspecified && foldType(TargetType) == Type) {
  209. Result.CommandLine.emplace_back((
  210. llvm::Twine(ClangCLMode ? "/std:" : "-std=") +
  211. LangStandard::getLangStandardForKind(Std).getName()).str());
  212. }
  213. Result.CommandLine.push_back(Filename);
  214. Result.Heuristic = "inferred from " + Cmd.Filename;
  215. return Result;
  216. }
  217. private:
  218. // Determine whether the given command line is intended for the CL driver.
  219. static bool checkIsCLMode(ArrayRef<std::string> CmdLine) {
  220. // First look for --driver-mode.
  221. for (StringRef S : llvm::reverse(CmdLine)) {
  222. if (S.consume_front("--driver-mode="))
  223. return S == "cl";
  224. }
  225. // Otherwise just check the clang executable file name.
  226. return !CmdLine.empty() &&
  227. llvm::sys::path::stem(CmdLine.front()).endswith_lower("cl");
  228. }
  229. // Map the language from the --std flag to that of the -x flag.
  230. static types::ID toType(Language Lang) {
  231. switch (Lang) {
  232. case Language::C:
  233. return types::TY_C;
  234. case Language::CXX:
  235. return types::TY_CXX;
  236. case Language::ObjC:
  237. return types::TY_ObjC;
  238. case Language::ObjCXX:
  239. return types::TY_ObjCXX;
  240. default:
  241. return types::TY_INVALID;
  242. }
  243. }
  244. // Convert a file type to the matching CL-style type flag.
  245. static StringRef toCLFlag(types::ID Type) {
  246. switch (Type) {
  247. case types::TY_C:
  248. case types::TY_CHeader:
  249. return "/TC";
  250. case types::TY_CXX:
  251. case types::TY_CXXHeader:
  252. return "/TP";
  253. default:
  254. return StringRef();
  255. }
  256. }
  257. // Try to interpret the argument as a type specifier, e.g. '-x'.
  258. Optional<types::ID> tryParseTypeArg(const llvm::opt::Arg &Arg) {
  259. const llvm::opt::Option &Opt = Arg.getOption();
  260. using namespace driver::options;
  261. if (ClangCLMode) {
  262. if (Opt.matches(OPT__SLASH_TC) || Opt.matches(OPT__SLASH_Tc))
  263. return types::TY_C;
  264. if (Opt.matches(OPT__SLASH_TP) || Opt.matches(OPT__SLASH_Tp))
  265. return types::TY_CXX;
  266. } else {
  267. if (Opt.matches(driver::options::OPT_x))
  268. return types::lookupTypeForTypeSpecifier(Arg.getValue());
  269. }
  270. return None;
  271. }
  272. // Try to interpret the argument as '-std='.
  273. Optional<LangStandard::Kind> tryParseStdArg(const llvm::opt::Arg &Arg) {
  274. using namespace driver::options;
  275. if (Arg.getOption().matches(ClangCLMode ? OPT__SLASH_std : OPT_std_EQ))
  276. return LangStandard::getLangKind(Arg.getValue());
  277. return None;
  278. }
  279. };
  280. // Given a filename, FileIndex picks the best matching file from the underlying
  281. // DB. This is the proxy file whose CompileCommand will be reused. The
  282. // heuristics incorporate file name, extension, and directory structure.
  283. // Strategy:
  284. // - Build indexes of each of the substrings we want to look up by.
  285. // These indexes are just sorted lists of the substrings.
  286. // - Each criterion corresponds to a range lookup into the index, so we only
  287. // need O(log N) string comparisons to determine scores.
  288. //
  289. // Apart from path proximity signals, also takes file extensions into account
  290. // when scoring the candidates.
  291. class FileIndex {
  292. public:
  293. FileIndex(std::vector<std::string> Files)
  294. : OriginalPaths(std::move(Files)), Strings(Arena) {
  295. // Sort commands by filename for determinism (index is a tiebreaker later).
  296. llvm::sort(OriginalPaths);
  297. Paths.reserve(OriginalPaths.size());
  298. Types.reserve(OriginalPaths.size());
  299. Stems.reserve(OriginalPaths.size());
  300. for (size_t I = 0; I < OriginalPaths.size(); ++I) {
  301. StringRef Path = Strings.save(StringRef(OriginalPaths[I]).lower());
  302. Paths.emplace_back(Path, I);
  303. Types.push_back(foldType(guessType(Path)));
  304. Stems.emplace_back(sys::path::stem(Path), I);
  305. auto Dir = ++sys::path::rbegin(Path), DirEnd = sys::path::rend(Path);
  306. for (int J = 0; J < DirectorySegmentsIndexed && Dir != DirEnd; ++J, ++Dir)
  307. if (Dir->size() > ShortDirectorySegment) // not trivial ones
  308. Components.emplace_back(*Dir, I);
  309. }
  310. llvm::sort(Paths);
  311. llvm::sort(Stems);
  312. llvm::sort(Components);
  313. }
  314. bool empty() const { return Paths.empty(); }
  315. // Returns the path for the file that best fits OriginalFilename.
  316. // Candidates with extensions matching PreferLanguage will be chosen over
  317. // others (unless it's TY_INVALID, or all candidates are bad).
  318. StringRef chooseProxy(StringRef OriginalFilename,
  319. types::ID PreferLanguage) const {
  320. assert(!empty() && "need at least one candidate!");
  321. std::string Filename = OriginalFilename.lower();
  322. auto Candidates = scoreCandidates(Filename);
  323. std::pair<size_t, int> Best =
  324. pickWinner(Candidates, Filename, PreferLanguage);
  325. DEBUG_WITH_TYPE(
  326. "interpolate",
  327. llvm::dbgs() << "interpolate: chose " << OriginalPaths[Best.first]
  328. << " as proxy for " << OriginalFilename << " preferring "
  329. << (PreferLanguage == types::TY_INVALID
  330. ? "none"
  331. : types::getTypeName(PreferLanguage))
  332. << " score=" << Best.second << "\n");
  333. return OriginalPaths[Best.first];
  334. }
  335. private:
  336. using SubstringAndIndex = std::pair<StringRef, size_t>;
  337. // Directory matching parameters: we look at the last two segments of the
  338. // parent directory (usually the semantically significant ones in practice).
  339. // We search only the last four of each candidate (for efficiency).
  340. constexpr static int DirectorySegmentsIndexed = 4;
  341. constexpr static int DirectorySegmentsQueried = 2;
  342. constexpr static int ShortDirectorySegment = 1; // Only look at longer names.
  343. // Award points to candidate entries that should be considered for the file.
  344. // Returned keys are indexes into paths, and the values are (nonzero) scores.
  345. DenseMap<size_t, int> scoreCandidates(StringRef Filename) const {
  346. // Decompose Filename into the parts we care about.
  347. // /some/path/complicated/project/Interesting.h
  348. // [-prefix--][---dir---] [-dir-] [--stem---]
  349. StringRef Stem = sys::path::stem(Filename);
  350. llvm::SmallVector<StringRef, DirectorySegmentsQueried> Dirs;
  351. llvm::StringRef Prefix;
  352. auto Dir = ++sys::path::rbegin(Filename),
  353. DirEnd = sys::path::rend(Filename);
  354. for (int I = 0; I < DirectorySegmentsQueried && Dir != DirEnd; ++I, ++Dir) {
  355. if (Dir->size() > ShortDirectorySegment)
  356. Dirs.push_back(*Dir);
  357. Prefix = Filename.substr(0, Dir - DirEnd);
  358. }
  359. // Now award points based on lookups into our various indexes.
  360. DenseMap<size_t, int> Candidates; // Index -> score.
  361. auto Award = [&](int Points, ArrayRef<SubstringAndIndex> Range) {
  362. for (const auto &Entry : Range)
  363. Candidates[Entry.second] += Points;
  364. };
  365. // Award one point if the file's basename is a prefix of the candidate,
  366. // and another if it's an exact match (so exact matches get two points).
  367. Award(1, indexLookup</*Prefix=*/true>(Stem, Stems));
  368. Award(1, indexLookup</*Prefix=*/false>(Stem, Stems));
  369. // For each of the last few directories in the Filename, award a point
  370. // if it's present in the candidate.
  371. for (StringRef Dir : Dirs)
  372. Award(1, indexLookup</*Prefix=*/false>(Dir, Components));
  373. // Award one more point if the whole rest of the path matches.
  374. if (sys::path::root_directory(Prefix) != Prefix)
  375. Award(1, indexLookup</*Prefix=*/true>(Prefix, Paths));
  376. return Candidates;
  377. }
  378. // Pick a single winner from the set of scored candidates.
  379. // Returns (index, score).
  380. std::pair<size_t, int> pickWinner(const DenseMap<size_t, int> &Candidates,
  381. StringRef Filename,
  382. types::ID PreferredLanguage) const {
  383. struct ScoredCandidate {
  384. size_t Index;
  385. bool Preferred;
  386. int Points;
  387. size_t PrefixLength;
  388. };
  389. // Choose the best candidate by (preferred, points, prefix length, alpha).
  390. ScoredCandidate Best = {size_t(-1), false, 0, 0};
  391. for (const auto &Candidate : Candidates) {
  392. ScoredCandidate S;
  393. S.Index = Candidate.first;
  394. S.Preferred = PreferredLanguage == types::TY_INVALID ||
  395. PreferredLanguage == Types[S.Index];
  396. S.Points = Candidate.second;
  397. if (!S.Preferred && Best.Preferred)
  398. continue;
  399. if (S.Preferred == Best.Preferred) {
  400. if (S.Points < Best.Points)
  401. continue;
  402. if (S.Points == Best.Points) {
  403. S.PrefixLength = matchingPrefix(Filename, Paths[S.Index].first);
  404. if (S.PrefixLength < Best.PrefixLength)
  405. continue;
  406. // hidden heuristics should at least be deterministic!
  407. if (S.PrefixLength == Best.PrefixLength)
  408. if (S.Index > Best.Index)
  409. continue;
  410. }
  411. }
  412. // PrefixLength was only set above if actually needed for a tiebreak.
  413. // But it definitely needs to be set to break ties in the future.
  414. S.PrefixLength = matchingPrefix(Filename, Paths[S.Index].first);
  415. Best = S;
  416. }
  417. // Edge case: no candidate got any points.
  418. // We ignore PreferredLanguage at this point (not ideal).
  419. if (Best.Index == size_t(-1))
  420. return {longestMatch(Filename, Paths).second, 0};
  421. return {Best.Index, Best.Points};
  422. }
  423. // Returns the range within a sorted index that compares equal to Key.
  424. // If Prefix is true, it's instead the range starting with Key.
  425. template <bool Prefix>
  426. ArrayRef<SubstringAndIndex>
  427. indexLookup(StringRef Key, ArrayRef<SubstringAndIndex> Idx) const {
  428. // Use pointers as iteratiors to ease conversion of result to ArrayRef.
  429. auto Range = std::equal_range(Idx.data(), Idx.data() + Idx.size(), Key,
  430. Less<Prefix>());
  431. return {Range.first, Range.second};
  432. }
  433. // Performs a point lookup into a nonempty index, returning a longest match.
  434. SubstringAndIndex longestMatch(StringRef Key,
  435. ArrayRef<SubstringAndIndex> Idx) const {
  436. assert(!Idx.empty());
  437. // Longest substring match will be adjacent to a direct lookup.
  438. auto It = llvm::lower_bound(Idx, SubstringAndIndex{Key, 0});
  439. if (It == Idx.begin())
  440. return *It;
  441. if (It == Idx.end())
  442. return *--It;
  443. // Have to choose between It and It-1
  444. size_t Prefix = matchingPrefix(Key, It->first);
  445. size_t PrevPrefix = matchingPrefix(Key, (It - 1)->first);
  446. return Prefix > PrevPrefix ? *It : *--It;
  447. }
  448. // Original paths, everything else is in lowercase.
  449. std::vector<std::string> OriginalPaths;
  450. BumpPtrAllocator Arena;
  451. StringSaver Strings;
  452. // Indexes of candidates by certain substrings.
  453. // String is lowercase and sorted, index points into OriginalPaths.
  454. std::vector<SubstringAndIndex> Paths; // Full path.
  455. // Lang types obtained by guessing on the corresponding path. I-th element is
  456. // a type for the I-th path.
  457. std::vector<types::ID> Types;
  458. std::vector<SubstringAndIndex> Stems; // Basename, without extension.
  459. std::vector<SubstringAndIndex> Components; // Last path components.
  460. };
  461. // The actual CompilationDatabase wrapper delegates to its inner database.
  462. // If no match, looks up a proxy file in FileIndex and transfers its
  463. // command to the requested file.
  464. class InterpolatingCompilationDatabase : public CompilationDatabase {
  465. public:
  466. InterpolatingCompilationDatabase(std::unique_ptr<CompilationDatabase> Inner)
  467. : Inner(std::move(Inner)), Index(this->Inner->getAllFiles()) {}
  468. std::vector<CompileCommand>
  469. getCompileCommands(StringRef Filename) const override {
  470. auto Known = Inner->getCompileCommands(Filename);
  471. if (Index.empty() || !Known.empty())
  472. return Known;
  473. bool TypeCertain;
  474. auto Lang = guessType(Filename, &TypeCertain);
  475. if (!TypeCertain)
  476. Lang = types::TY_INVALID;
  477. auto ProxyCommands =
  478. Inner->getCompileCommands(Index.chooseProxy(Filename, foldType(Lang)));
  479. if (ProxyCommands.empty())
  480. return {};
  481. return {TransferableCommand(ProxyCommands[0]).transferTo(Filename)};
  482. }
  483. std::vector<std::string> getAllFiles() const override {
  484. return Inner->getAllFiles();
  485. }
  486. std::vector<CompileCommand> getAllCompileCommands() const override {
  487. return Inner->getAllCompileCommands();
  488. }
  489. private:
  490. std::unique_ptr<CompilationDatabase> Inner;
  491. FileIndex Index;
  492. };
  493. } // namespace
  494. std::unique_ptr<CompilationDatabase>
  495. inferMissingCompileCommands(std::unique_ptr<CompilationDatabase> Inner) {
  496. return std::make_unique<InterpolatingCompilationDatabase>(std::move(Inner));
  497. }
  498. } // namespace tooling
  499. } // namespace clang