[Index] Return SourceLocation to consumers, not FileID/Offset pair.

Summary:
The FileID/Offset conversion is lossy. The code takes the fileLoc, which loses
e.g. the spelling location in some macro cases.
Instead, pass the original SourceLocation which preserves all information, and
update consumers to match current behavior.

This allows us to fix two bugs in clangd that need the spelling location.

Reviewers: akyrtzi, arphaman

Subscribers: ilya-biryukov, ioeric, cfe-commits

Differential Revision: https://reviews.llvm.org/D45014

llvm-svn: 329570
This commit is contained in:
Sam McCall 2018-04-09 14:12:51 +00:00
parent 6400c03e6a
commit cc026ebf32
6 changed files with 31 additions and 46 deletions

View File

@ -42,18 +42,16 @@ public:
/// \returns true to continue indexing, or false to abort. /// \returns true to continue indexing, or false to abort.
virtual bool handleDeclOccurence(const Decl *D, SymbolRoleSet Roles, virtual bool handleDeclOccurence(const Decl *D, SymbolRoleSet Roles,
ArrayRef<SymbolRelation> Relations, ArrayRef<SymbolRelation> Relations,
FileID FID, unsigned Offset, SourceLocation Loc, ASTNodeInfo ASTNode);
ASTNodeInfo ASTNode);
/// \returns true to continue indexing, or false to abort. /// \returns true to continue indexing, or false to abort.
virtual bool handleMacroOccurence(const IdentifierInfo *Name, virtual bool handleMacroOccurence(const IdentifierInfo *Name,
const MacroInfo *MI, SymbolRoleSet Roles, const MacroInfo *MI, SymbolRoleSet Roles,
FileID FID, unsigned Offset); SourceLocation Loc);
/// \returns true to continue indexing, or false to abort. /// \returns true to continue indexing, or false to abort.
virtual bool handleModuleOccurence(const ImportDecl *ImportD, virtual bool handleModuleOccurence(const ImportDecl *ImportD,
SymbolRoleSet Roles, SymbolRoleSet Roles, SourceLocation Loc);
FileID FID, unsigned Offset);
virtual void finish() {} virtual void finish() {}

View File

@ -23,20 +23,21 @@ void IndexDataConsumer::_anchor() {}
bool IndexDataConsumer::handleDeclOccurence(const Decl *D, SymbolRoleSet Roles, bool IndexDataConsumer::handleDeclOccurence(const Decl *D, SymbolRoleSet Roles,
ArrayRef<SymbolRelation> Relations, ArrayRef<SymbolRelation> Relations,
FileID FID, unsigned Offset, SourceLocation Loc,
ASTNodeInfo ASTNode) { ASTNodeInfo ASTNode) {
return true; return true;
} }
bool IndexDataConsumer::handleMacroOccurence(const IdentifierInfo *Name, bool IndexDataConsumer::handleMacroOccurence(const IdentifierInfo *Name,
const MacroInfo *MI, SymbolRoleSet Roles, const MacroInfo *MI,
FileID FID, unsigned Offset) { SymbolRoleSet Roles,
SourceLocation Loc) {
return true; return true;
} }
bool IndexDataConsumer::handleModuleOccurence(const ImportDecl *ImportD, bool IndexDataConsumer::handleModuleOccurence(const ImportDecl *ImportD,
SymbolRoleSet Roles, SymbolRoleSet Roles,
FileID FID, unsigned Offset) { SourceLocation Loc) {
return true; return true;
} }

View File

@ -82,14 +82,9 @@ bool IndexingContext::importedModule(const ImportDecl *ImportD) {
Loc = IdLocs.front(); Loc = IdLocs.front();
else else
Loc = ImportD->getLocation(); Loc = ImportD->getLocation();
SourceManager &SM = Ctx->getSourceManager();
Loc = SM.getFileLoc(Loc);
if (Loc.isInvalid())
return true;
FileID FID; SourceManager &SM = Ctx->getSourceManager();
unsigned Offset; FileID FID = SM.getFileID(SM.getFileLoc(Loc));
std::tie(FID, Offset) = SM.getDecomposedLoc(Loc);
if (FID.isInvalid()) if (FID.isInvalid())
return true; return true;
@ -112,7 +107,7 @@ bool IndexingContext::importedModule(const ImportDecl *ImportD) {
if (ImportD->isImplicit()) if (ImportD->isImplicit())
Roles |= (unsigned)SymbolRole::Implicit; Roles |= (unsigned)SymbolRole::Implicit;
return DataConsumer.handleModuleOccurence(ImportD, Roles, FID, Offset); return DataConsumer.handleModuleOccurence(ImportD, Roles, Loc);
} }
bool IndexingContext::isTemplateImplicitInstantiation(const Decl *D) { bool IndexingContext::isTemplateImplicitInstantiation(const Decl *D) {
@ -327,13 +322,7 @@ bool IndexingContext::handleDeclOccurrence(const Decl *D, SourceLocation Loc,
return true; return true;
SourceManager &SM = Ctx->getSourceManager(); SourceManager &SM = Ctx->getSourceManager();
Loc = SM.getFileLoc(Loc); FileID FID = SM.getFileID(SM.getFileLoc(Loc));
if (Loc.isInvalid())
return true;
FileID FID;
unsigned Offset;
std::tie(FID, Offset) = SM.getDecomposedLoc(Loc);
if (FID.isInvalid()) if (FID.isInvalid())
return true; return true;
@ -414,7 +403,6 @@ bool IndexingContext::handleDeclOccurrence(const Decl *D, SourceLocation Loc,
Rel.RelatedSymbol->getCanonicalDecl())); Rel.RelatedSymbol->getCanonicalDecl()));
} }
IndexDataConsumer::ASTNodeInfo Node{ OrigE, OrigD, Parent, ContainerDC }; IndexDataConsumer::ASTNodeInfo Node{OrigE, OrigD, Parent, ContainerDC};
return DataConsumer.handleDeclOccurence(D, Roles, FinalRelations, FID, Offset, return DataConsumer.handleDeclOccurence(D, Roles, FinalRelations, Loc, Node);
Node);
} }

View File

@ -88,13 +88,14 @@ public:
bool handleDeclOccurence(const Decl *D, SymbolRoleSet Roles, bool handleDeclOccurence(const Decl *D, SymbolRoleSet Roles,
ArrayRef<SymbolRelation> Relations, ArrayRef<SymbolRelation> Relations,
FileID FID, unsigned Offset, SourceLocation Loc, ASTNodeInfo ASTNode) override {
ASTNodeInfo ASTNode) override {
ASTContext &Ctx = D->getASTContext(); ASTContext &Ctx = D->getASTContext();
SourceManager &SM = Ctx.getSourceManager(); SourceManager &SM = Ctx.getSourceManager();
unsigned Line = SM.getLineNumber(FID, Offset); Loc = SM.getFileLoc(Loc);
unsigned Col = SM.getColumnNumber(FID, Offset); FileID FID = SM.getFileID(Loc);
unsigned Line = SM.getLineNumber(FID, SM.getFileOffset(Loc));
unsigned Col = SM.getColumnNumber(FID, SM.getFileOffset(Loc));
OS << Line << ':' << Col << " | "; OS << Line << ':' << Col << " | ";
printSymbolInfo(getSymbolInfo(D), OS); printSymbolInfo(getSymbolInfo(D), OS);
@ -124,12 +125,14 @@ public:
} }
bool handleModuleOccurence(const ImportDecl *ImportD, SymbolRoleSet Roles, bool handleModuleOccurence(const ImportDecl *ImportD, SymbolRoleSet Roles,
FileID FID, unsigned Offset) override { SourceLocation Loc) override {
ASTContext &Ctx = ImportD->getASTContext(); ASTContext &Ctx = ImportD->getASTContext();
SourceManager &SM = Ctx.getSourceManager(); SourceManager &SM = Ctx.getSourceManager();
unsigned Line = SM.getLineNumber(FID, Offset); Loc = SM.getFileLoc(Loc);
unsigned Col = SM.getColumnNumber(FID, Offset); FileID FID = SM.getFileID(Loc);
unsigned Line = SM.getLineNumber(FID, SM.getFileOffset(Loc));
unsigned Col = SM.getColumnNumber(FID, SM.getFileOffset(Loc));
OS << Line << ':' << Col << " | "; OS << Line << ':' << Col << " | ";
printSymbolInfo(getSymbolInfo(ImportD), OS); printSymbolInfo(getSymbolInfo(ImportD), OS);

View File

@ -155,13 +155,10 @@ CXSymbolRole getSymbolRole(SymbolRoleSet Role) {
} }
} }
bool CXIndexDataConsumer::handleDeclOccurence(const Decl *D, bool CXIndexDataConsumer::handleDeclOccurence(
SymbolRoleSet Roles, const Decl *D, SymbolRoleSet Roles, ArrayRef<SymbolRelation> Relations,
ArrayRef<SymbolRelation> Relations, SourceLocation Loc, ASTNodeInfo ASTNode) {
FileID FID, unsigned Offset, Loc = getASTContext().getSourceManager().getFileLoc(Loc);
ASTNodeInfo ASTNode) {
SourceLocation Loc = getASTContext().getSourceManager()
.getLocForStartOfFile(FID).getLocWithOffset(Offset);
if (Roles & (unsigned)SymbolRole::Reference) { if (Roles & (unsigned)SymbolRole::Reference) {
const NamedDecl *ND = dyn_cast<NamedDecl>(D); const NamedDecl *ND = dyn_cast<NamedDecl>(D);
@ -226,8 +223,7 @@ bool CXIndexDataConsumer::handleDeclOccurence(const Decl *D,
bool CXIndexDataConsumer::handleModuleOccurence(const ImportDecl *ImportD, bool CXIndexDataConsumer::handleModuleOccurence(const ImportDecl *ImportD,
SymbolRoleSet Roles, SymbolRoleSet Roles,
FileID FID, SourceLocation Loc) {
unsigned Offset) {
IndexingDeclVisitor(*this, SourceLocation(), nullptr).Visit(ImportD); IndexingDeclVisitor(*this, SourceLocation(), nullptr).Visit(ImportD);
return !shouldAbort(); return !shouldAbort();
} }

View File

@ -465,12 +465,11 @@ public:
private: private:
bool handleDeclOccurence(const Decl *D, index::SymbolRoleSet Roles, bool handleDeclOccurence(const Decl *D, index::SymbolRoleSet Roles,
ArrayRef<index::SymbolRelation> Relations, ArrayRef<index::SymbolRelation> Relations,
FileID FID, unsigned Offset, SourceLocation Loc, ASTNodeInfo ASTNode) override;
ASTNodeInfo ASTNode) override;
bool handleModuleOccurence(const ImportDecl *ImportD, bool handleModuleOccurence(const ImportDecl *ImportD,
index::SymbolRoleSet Roles, index::SymbolRoleSet Roles,
FileID FID, unsigned Offset) override; SourceLocation Loc) override;
void finish() override; void finish() override;