2016-05-13 17:27:54 +08:00
|
|
|
//===-- SymbolIndexManager.cpp - Managing multiple SymbolIndices-*- C++ -*-===//
|
2016-05-04 16:22:35 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-05-04 16:22:35 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-05-13 17:27:54 +08:00
|
|
|
#include "SymbolIndexManager.h"
|
2016-05-04 16:22:35 +08:00
|
|
|
#include "find-all-symbols/SymbolInfo.h"
|
2021-12-20 22:56:56 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-05-04 16:22:35 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2021-12-20 22:56:56 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2016-05-04 16:22:35 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2017-01-11 18:32:47 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2016-05-04 16:22:35 +08:00
|
|
|
|
2019-03-25 22:09:10 +08:00
|
|
|
#define DEBUG_TYPE "clang-include-fixer"
|
2016-05-04 16:22:35 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace include_fixer {
|
|
|
|
|
2017-02-28 16:13:15 +08:00
|
|
|
using find_all_symbols::SymbolInfo;
|
|
|
|
using find_all_symbols::SymbolAndSignals;
|
2016-05-31 22:33:28 +08:00
|
|
|
|
2017-01-11 18:32:47 +08:00
|
|
|
// Calculate a score based on whether we think the given header is closely
|
|
|
|
// related to the given source file.
|
|
|
|
static double similarityScore(llvm::StringRef FileName,
|
|
|
|
llvm::StringRef Header) {
|
2020-04-05 14:28:11 +08:00
|
|
|
// Compute the maximum number of common path segments between Header and
|
2017-01-11 18:32:47 +08:00
|
|
|
// a suffix of FileName.
|
|
|
|
// We do not do a full longest common substring computation, as Header
|
|
|
|
// specifies the path we would directly #include, so we assume it is rooted
|
|
|
|
// relatively to a subproject of the repository.
|
|
|
|
int MaxSegments = 1;
|
|
|
|
for (auto FileI = llvm::sys::path::begin(FileName),
|
|
|
|
FileE = llvm::sys::path::end(FileName);
|
|
|
|
FileI != FileE; ++FileI) {
|
|
|
|
int Segments = 0;
|
|
|
|
for (auto HeaderI = llvm::sys::path::begin(Header),
|
|
|
|
HeaderE = llvm::sys::path::end(Header), I = FileI;
|
|
|
|
HeaderI != HeaderE && *I == *HeaderI && I != FileE; ++I, ++HeaderI) {
|
|
|
|
++Segments;
|
|
|
|
}
|
|
|
|
MaxSegments = std::max(Segments, MaxSegments);
|
2016-05-31 22:33:28 +08:00
|
|
|
}
|
2017-01-11 18:32:47 +08:00
|
|
|
return MaxSegments;
|
|
|
|
}
|
2016-05-31 22:33:28 +08:00
|
|
|
|
2017-02-28 16:13:15 +08:00
|
|
|
static void rank(std::vector<SymbolAndSignals> &Symbols,
|
2017-01-11 18:32:47 +08:00
|
|
|
llvm::StringRef FileName) {
|
2021-12-20 22:56:56 +08:00
|
|
|
llvm::StringMap<double> Score;
|
2017-02-28 16:13:15 +08:00
|
|
|
for (const auto &Symbol : Symbols) {
|
2017-01-11 18:32:47 +08:00
|
|
|
// Calculate a score from the similarity of the header the symbol is in
|
|
|
|
// with the current file and the popularity of the symbol.
|
2017-02-28 16:13:15 +08:00
|
|
|
double NewScore = similarityScore(FileName, Symbol.Symbol.getFilePath()) *
|
|
|
|
(1.0 + std::log2(1 + Symbol.Signals.Seen));
|
|
|
|
double &S = Score[Symbol.Symbol.getFilePath()];
|
2017-01-11 18:32:47 +08:00
|
|
|
S = std::max(S, NewScore);
|
|
|
|
}
|
|
|
|
// Sort by the gathered scores. Use file name as a tie breaker so we can
|
2016-05-31 22:33:28 +08:00
|
|
|
// deduplicate.
|
2021-12-20 22:56:56 +08:00
|
|
|
llvm::sort(Symbols.begin(), Symbols.end(),
|
|
|
|
[&](const SymbolAndSignals &A, const SymbolAndSignals &B) {
|
|
|
|
auto AS = Score[A.Symbol.getFilePath()];
|
|
|
|
auto BS = Score[B.Symbol.getFilePath()];
|
|
|
|
if (AS != BS)
|
|
|
|
return AS > BS;
|
|
|
|
return A.Symbol.getFilePath() < B.Symbol.getFilePath();
|
|
|
|
});
|
2016-05-31 22:33:28 +08:00
|
|
|
}
|
|
|
|
|
2016-07-08 17:10:29 +08:00
|
|
|
std::vector<find_all_symbols::SymbolInfo>
|
2016-08-02 18:43:10 +08:00
|
|
|
SymbolIndexManager::search(llvm::StringRef Identifier,
|
2017-01-11 18:32:47 +08:00
|
|
|
bool IsNestedSearch,
|
|
|
|
llvm::StringRef FileName) const {
|
2016-05-04 16:22:35 +08:00
|
|
|
// The identifier may be fully qualified, so split it and get all the context
|
|
|
|
// names.
|
|
|
|
llvm::SmallVector<llvm::StringRef, 8> Names;
|
|
|
|
Identifier.split(Names, "::");
|
|
|
|
|
2016-05-19 20:41:56 +08:00
|
|
|
bool IsFullyQualified = false;
|
|
|
|
if (Identifier.startswith("::")) {
|
|
|
|
Names.erase(Names.begin()); // Drop first (empty) element.
|
|
|
|
IsFullyQualified = true;
|
|
|
|
}
|
|
|
|
|
2016-05-19 00:42:38 +08:00
|
|
|
// As long as we don't find a result keep stripping name parts from the end.
|
|
|
|
// This is to support nested classes which aren't recorded in the database.
|
|
|
|
// Eventually we will either hit a class (namespaces aren't in the database
|
|
|
|
// either) and can report that result.
|
2016-06-03 22:07:38 +08:00
|
|
|
bool TookPrefix = false;
|
2017-02-28 16:13:15 +08:00
|
|
|
std::vector<SymbolAndSignals> MatchedSymbols;
|
2016-08-02 18:43:10 +08:00
|
|
|
do {
|
2017-02-28 16:13:15 +08:00
|
|
|
std::vector<SymbolAndSignals> Symbols;
|
2016-05-19 00:42:38 +08:00
|
|
|
for (const auto &DB : SymbolIndices) {
|
2017-01-09 23:18:28 +08:00
|
|
|
auto Res = DB.get()->search(Names.back());
|
2016-05-19 00:42:38 +08:00
|
|
|
Symbols.insert(Symbols.end(), Res.begin(), Res.end());
|
|
|
|
}
|
2016-05-04 16:22:35 +08:00
|
|
|
|
2018-05-16 00:37:45 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "Searching " << Names.back() << "... got "
|
|
|
|
<< Symbols.size() << " results...\n");
|
2016-05-04 16:22:35 +08:00
|
|
|
|
2017-02-28 16:13:15 +08:00
|
|
|
for (auto &SymAndSig : Symbols) {
|
|
|
|
const SymbolInfo &Symbol = SymAndSig.Symbol;
|
2016-05-19 00:42:38 +08:00
|
|
|
// Match the identifier name without qualifier.
|
2017-03-13 23:55:59 +08:00
|
|
|
bool IsMatched = true;
|
|
|
|
auto SymbolContext = Symbol.getContexts().begin();
|
|
|
|
auto IdentiferContext = Names.rbegin() + 1; // Skip identifier name.
|
|
|
|
// Match the remaining context names.
|
|
|
|
while (IdentiferContext != Names.rend() &&
|
|
|
|
SymbolContext != Symbol.getContexts().end()) {
|
|
|
|
if (SymbolContext->second == *IdentiferContext) {
|
|
|
|
++IdentiferContext;
|
|
|
|
++SymbolContext;
|
|
|
|
} else if (SymbolContext->first ==
|
|
|
|
find_all_symbols::SymbolInfo::ContextType::EnumDecl) {
|
|
|
|
// Skip non-scoped enum context.
|
|
|
|
++SymbolContext;
|
|
|
|
} else {
|
|
|
|
IsMatched = false;
|
|
|
|
break;
|
2016-05-04 16:22:35 +08:00
|
|
|
}
|
2017-03-13 23:55:59 +08:00
|
|
|
}
|
2016-05-04 16:22:35 +08:00
|
|
|
|
2017-03-13 23:55:59 +08:00
|
|
|
// If the name was qualified we only want to add results if we evaluated
|
|
|
|
// all contexts.
|
|
|
|
if (IsFullyQualified)
|
|
|
|
IsMatched &= (SymbolContext == Symbol.getContexts().end());
|
|
|
|
|
|
|
|
// FIXME: Support full match. At this point, we only find symbols in
|
|
|
|
// database which end with the same contexts with the identifier.
|
|
|
|
if (IsMatched && IdentiferContext == Names.rend()) {
|
|
|
|
// If we're in a situation where we took a prefix but the thing we
|
|
|
|
// found couldn't possibly have a nested member ignore it.
|
|
|
|
if (TookPrefix &&
|
|
|
|
(Symbol.getSymbolKind() == SymbolInfo::SymbolKind::Function ||
|
|
|
|
Symbol.getSymbolKind() == SymbolInfo::SymbolKind::Variable ||
|
|
|
|
Symbol.getSymbolKind() ==
|
|
|
|
SymbolInfo::SymbolKind::EnumConstantDecl ||
|
|
|
|
Symbol.getSymbolKind() == SymbolInfo::SymbolKind::Macro))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
MatchedSymbols.push_back(std::move(SymAndSig));
|
2016-05-04 16:22:35 +08:00
|
|
|
}
|
|
|
|
}
|
2016-05-19 00:42:38 +08:00
|
|
|
Names.pop_back();
|
2016-06-03 22:07:38 +08:00
|
|
|
TookPrefix = true;
|
2016-08-02 18:43:10 +08:00
|
|
|
} while (MatchedSymbols.empty() && !Names.empty() && IsNestedSearch);
|
2016-05-19 00:42:38 +08:00
|
|
|
|
2017-01-11 18:32:47 +08:00
|
|
|
rank(MatchedSymbols, FileName);
|
2017-02-28 16:13:15 +08:00
|
|
|
// Strip signals, they are no longer needed.
|
|
|
|
std::vector<SymbolInfo> Res;
|
2021-02-03 15:30:45 +08:00
|
|
|
Res.reserve(MatchedSymbols.size());
|
2017-03-13 23:55:59 +08:00
|
|
|
for (auto &SymAndSig : MatchedSymbols)
|
2017-02-28 16:13:15 +08:00
|
|
|
Res.push_back(std::move(SymAndSig.Symbol));
|
|
|
|
return Res;
|
2016-05-04 16:22:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace include_fixer
|
|
|
|
} // namespace clang
|