14#include "llvm/ADT/STLExtras.h"
15#include "llvm/ADT/SmallVector.h"
16#include "llvm/ADT/StringMap.h"
17#include "llvm/Support/Debug.h"
18#include "llvm/Support/Path.h"
20#define DEBUG_TYPE "clang-include-fixer"
23namespace include_fixer {
25using find_all_symbols::SymbolInfo;
26using find_all_symbols::SymbolAndSignals;
31 llvm::StringRef Header) {
38 for (
auto FileI = llvm::sys::path::begin(
FileName),
39 FileE = llvm::sys::path::end(
FileName);
40 FileI != FileE; ++FileI) {
42 for (
auto HeaderI = llvm::sys::path::begin(Header),
43 HeaderE = llvm::sys::path::end(Header), I = FileI;
44 HeaderI != HeaderE && *I == *HeaderI && I != FileE; ++I, ++HeaderI) {
47 MaxSegments = std::max(Segments, MaxSegments);
52static void rank(std::vector<SymbolAndSignals> &Symbols,
54 llvm::StringMap<double>
Score;
55 for (
const auto &Symbol : Symbols) {
59 (1.0 + std::log2(1 + Symbol.Signals.Seen));
60 double &S =
Score[Symbol.Symbol.getFilePath()];
61 S = std::max(S, NewScore);
67 auto AS =
Score[A.Symbol.getFilePath()];
68 auto BS =
Score[B.Symbol.getFilePath()];
71 return A.Symbol.getFilePath() < B.Symbol.getFilePath();
75std::vector<find_all_symbols::SymbolInfo>
81 llvm::SmallVector<llvm::StringRef, 8> Names;
82 Identifier.split(Names,
"::");
84 bool IsFullyQualified =
false;
85 if (Identifier.starts_with(
"::")) {
86 Names.erase(Names.begin());
87 IsFullyQualified =
true;
94 bool TookPrefix =
false;
95 std::vector<SymbolAndSignals> MatchedSymbols;
97 std::vector<SymbolAndSignals> Symbols;
98 for (
const auto &DB : SymbolIndices) {
99 auto Res = DB.get()->search(Names.back());
100 Symbols.insert(Symbols.end(), Res.begin(), Res.end());
103 LLVM_DEBUG(llvm::dbgs() <<
"Searching " << Names.back() <<
"... got "
104 << Symbols.size() <<
" results...\n");
106 for (
auto &SymAndSig : Symbols) {
109 bool IsMatched =
true;
110 auto SymbolContext = Symbol.getContexts().begin();
111 auto IdentiferContext = Names.rbegin() + 1;
113 while (IdentiferContext != Names.rend() &&
114 SymbolContext != Symbol.getContexts().end()) {
115 if (SymbolContext->second == *IdentiferContext) {
118 }
else if (SymbolContext->first ==
130 if (IsFullyQualified)
131 IsMatched &= (SymbolContext == Symbol.getContexts().end());
135 if (IsMatched && IdentiferContext == Names.rend()) {
141 Symbol.getSymbolKind() ==
146 MatchedSymbols.push_back(std::move(SymAndSig));
151 }
while (MatchedSymbols.empty() && !Names.empty() && IsNestedSearch);
155 std::vector<SymbolInfo> Res;
156 Res.reserve(MatchedSymbols.size());
157 for (
auto &SymAndSig : MatchedSymbols)
158 Res.push_back(std::move(SymAndSig.Symbol));
std::optional< float > Score
Describes a named symbol from a header.
std::vector< find_all_symbols::SymbolInfo > search(llvm::StringRef Identifier, bool IsNestedSearch=true, llvm::StringRef FileName="") const
Search for header files to be included for an identifier.
static void rank(std::vector< SymbolAndSignals > &Symbols, llvm::StringRef FileName)
static double similarityScore(llvm::StringRef FileName, llvm::StringRef Header)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//