[clangd] Simplify code using findName. NFC

`findName` was always used in conjuction with `spellingLocIfSpelled`.
This patch replaces patterns of the form:
  spellingLocIfSpelled(findName(&ND), SM)

With a new helper function:
  nameLocation(ND, SM)

And removes `spellingLocIfSpelled` and `findName`. Both are never used
anywhere else and the latter is an equivalent of `Decl::getLocation` if
we ever need it again.
This commit is contained in:
Ilya Biryukov 2019-12-10 10:08:39 +01:00
parent dbf520f617
commit b63c35ebf7
7 changed files with 17 additions and 28 deletions

View File

@ -156,7 +156,12 @@ bool isImplementationDetail(const Decl *D) {
D->getASTContext().getSourceManager());
}
SourceLocation findName(const clang::Decl *D) { return D->getLocation(); }
SourceLocation nameLocation(const clang::Decl &D, const SourceManager &SM) {
auto L = D.getLocation();
if (isSpelledInSource(L, SM))
return SM.getSpellingLoc(L);
return SM.getExpansionLoc(L);
}
std::string printQualifiedName(const NamedDecl &ND) {
std::string QName;

View File

@ -34,11 +34,11 @@ namespace clangd {
/// in code is considered implementation detail.
bool isImplementationDetail(const Decl *D);
/// Find the identifier source location of the given D.
///
/// The returned location is usually the spelling location where the name of the
/// decl occurs in the code.
SourceLocation findName(const clang::Decl *D);
/// Find the source location of the identifier for \p D.
/// Transforms macro locations to locations spelled inside files. All code
/// that needs locations of declaration names (e.g. the index) should go through
/// this function.
SourceLocation nameLocation(const clang::Decl &D, const SourceManager &SM);
/// Returns the qualified name of ND. The scope doesn't contain unwritten scopes
/// like inline namespaces.

View File

@ -131,7 +131,7 @@ namespace {
llvm::Optional<DocumentSymbol> declToSym(ASTContext &Ctx, const NamedDecl &ND) {
auto &SM = Ctx.getSourceManager();
SourceLocation NameLoc = spellingLocIfSpelled(findName(&ND), SM);
SourceLocation NameLoc = nameLocation(ND, SM);
// getFileLoc is a good choice for us, but we also need to make sure
// sourceLocToPosition won't switch files, so we call getSpellingLoc on top of
// that to make sure it does not switch files.

View File

@ -224,14 +224,6 @@ bool isSpelledInSource(SourceLocation Loc, const SourceManager &SM) {
return true;
}
SourceLocation spellingLocIfSpelled(SourceLocation Loc,
const SourceManager &SM) {
if (!isSpelledInSource(Loc, SM))
// Use the expansion location as spelling location is not interesting.
return SM.getExpansionRange(Loc).getBegin();
return SM.getSpellingLoc(Loc);
}
llvm::Optional<Range> getTokenRange(const SourceManager &SM,
const LangOptions &LangOpts,
SourceLocation TokLoc) {

View File

@ -107,12 +107,6 @@ SourceLocation includeHashLoc(FileID IncludedFile, const SourceManager &SM);
/// `-DName=foo`, the spelling location will be "<command line>".
bool isSpelledInSource(SourceLocation Loc, const SourceManager &SM);
/// Returns the spelling location of the token at Loc if isSpelledInSource,
/// otherwise its expansion location.
/// FIXME: Most callers likely want some variant of "file location" instead.
SourceLocation spellingLocIfSpelled(SourceLocation Loc,
const SourceManager &SM);
/// Turns a token range into a half-open range and checks its correctness.
/// The resulting range will have only valid source location on both sides, both
/// of which are file locations.

View File

@ -246,8 +246,7 @@ std::vector<LocatedSymbol> locateSymbolAt(ParsedAST &AST, Position Pos,
}
}
auto Loc = makeLocation(AST.getASTContext(),
spellingLocIfSpelled(findName(Preferred), SM),
auto Loc = makeLocation(AST.getASTContext(), nameLocation(*Preferred, SM),
*MainFilePath);
if (!Loc)
continue;
@ -535,8 +534,7 @@ static llvm::Optional<TypeHierarchyItem>
declToTypeHierarchyItem(ASTContext &Ctx, const NamedDecl &ND) {
auto &SM = Ctx.getSourceManager();
SourceLocation NameLoc =
spellingLocIfSpelled(findName(&ND), Ctx.getSourceManager());
SourceLocation NameLoc = nameLocation(ND, Ctx.getSourceManager());
// getFileLoc is a good choice for us, but we also need to make sure
// sourceLocToPosition won't switch files, so we call getSpellingLoc on top of
// that to make sure it does not switch files.

View File

@ -82,7 +82,7 @@ static const char *PROTO_HEADER_COMMENT =
// filters.
bool isPrivateProtoDecl(const NamedDecl &ND) {
const auto &SM = ND.getASTContext().getSourceManager();
auto Loc = spellingLocIfSpelled(findName(&ND), SM);
auto Loc = nameLocation(ND, SM);
auto FileName = SM.getFilename(Loc);
if (!FileName.endswith(".proto.h") && !FileName.endswith(".pb.h"))
return false;
@ -595,7 +595,7 @@ const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND, SymbolID ID,
S.Flags |= Symbol::VisibleOutsideFile;
S.SymInfo = index::getSymbolInfo(&ND);
std::string FileURI;
auto Loc = spellingLocIfSpelled(findName(&ND), SM);
auto Loc = nameLocation(ND, SM);
assert(Loc.isValid() && "Invalid source location for NamedDecl");
// FIXME: use the result to filter out symbols.
shouldIndexFile(SM.getFileID(Loc));
@ -656,7 +656,7 @@ void SymbolCollector::addDefinition(const NamedDecl &ND,
Symbol S = DeclSym;
std::string FileURI;
const auto &SM = ND.getASTContext().getSourceManager();
auto Loc = spellingLocIfSpelled(findName(&ND), SM);
auto Loc = nameLocation(ND, SM);
// FIXME: use the result to filter out symbols.
shouldIndexFile(SM.getFileID(Loc));
if (auto DefLoc =