Improve extent of definition/declaration; uniquify typeDefinition

This commit is contained in:
Fangrui Song 2018-10-18 22:11:43 -07:00
parent 87ea7d244d
commit 119a05597d
5 changed files with 55 additions and 36 deletions

View File

@ -40,47 +40,56 @@ std::string PathFromFileEntry(const FileEntry &file) {
return ret;
}
static Position Decomposed2LineAndCol(const SourceManager &SM,
std::pair<FileID, unsigned> I) {
int l = SM.getLineNumber(I.first, I.second) - 1,
c = SM.getColumnNumber(I.first, I.second) - 1;
return {(int16_t)std::min(l, INT16_MAX), (int16_t)std::min(c, INT16_MAX)};
}
Range FromCharSourceRange(const SourceManager &SM, const LangOptions &LangOpts,
CharSourceRange R,
llvm::sys::fs::UniqueID *UniqueID) {
SourceLocation BLoc = R.getBegin(), ELoc = R.getEnd();
std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(BLoc);
std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(ELoc);
std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(BLoc),
EInfo = SM.getDecomposedLoc(ELoc);
if (R.isTokenRange())
EInfo.second += Lexer::MeasureTokenLength(ELoc, SM, LangOpts);
unsigned l0 = SM.getLineNumber(BInfo.first, BInfo.second) - 1,
c0 = SM.getColumnNumber(BInfo.first, BInfo.second) - 1,
l1 = SM.getLineNumber(EInfo.first, EInfo.second) - 1,
c1 = SM.getColumnNumber(EInfo.first, EInfo.second) - 1;
if (l0 > INT16_MAX)
l0 = 0;
if (c0 > INT16_MAX)
c0 = 0;
if (l1 > INT16_MAX)
l1 = 0;
if (c1 > INT16_MAX)
c1 = 0;
if (UniqueID) {
if (const FileEntry *F = SM.getFileEntryForID(BInfo.first))
*UniqueID = F->getUniqueID();
else
*UniqueID = llvm::sys::fs::UniqueID(0, 0);
}
return {{int16_t(l0), int16_t(c0)}, {int16_t(l1), int16_t(c1)}};
return {Decomposed2LineAndCol(SM, BInfo), Decomposed2LineAndCol(SM, EInfo)};
}
Range FromCharRange(const SourceManager &SM, const LangOptions &LangOpts,
Range FromCharRange(const SourceManager &SM, const LangOptions &Lang,
SourceRange R, llvm::sys::fs::UniqueID *UniqueID) {
return FromCharSourceRange(SM, LangOpts, CharSourceRange::getCharRange(R),
return FromCharSourceRange(SM, Lang, CharSourceRange::getCharRange(R),
UniqueID);
}
Range FromTokenRange(const SourceManager &SM, const LangOptions &LangOpts,
Range FromTokenRange(const SourceManager &SM, const LangOptions &Lang,
SourceRange R, llvm::sys::fs::UniqueID *UniqueID) {
return FromCharSourceRange(SM, LangOpts, CharSourceRange::getTokenRange(R),
return FromCharSourceRange(SM, Lang, CharSourceRange::getTokenRange(R),
UniqueID);
}
Range FromTokenRangeDefaulted(const SourceManager &SM, const LangOptions &Lang,
SourceRange R, const FileEntry *FE, Range range) {
auto I = SM.getDecomposedLoc(SM.getExpansionLoc(R.getBegin()));
if (SM.getFileEntryForID(I.first) == FE)
range.start = Decomposed2LineAndCol(SM, I);
SourceLocation L = SM.getExpansionLoc(R.getEnd());
I = SM.getDecomposedLoc(L);
if (SM.getFileEntryForID(I.first) == FE) {
I.second += Lexer::MeasureTokenLength(L, SM, Lang);
range.end = Decomposed2LineAndCol(SM, I);
}
return range;
}
std::unique_ptr<CompilerInvocation>
BuildCompilerInvocation(std::vector<const char *> args,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {

View File

@ -44,6 +44,11 @@ Range FromTokenRange(const clang::SourceManager &SM,
const clang::LangOptions &LangOpts, clang::SourceRange R,
llvm::sys::fs::UniqueID *UniqueID = nullptr);
Range FromTokenRangeDefaulted(const clang::SourceManager &SM,
const clang::LangOptions &Lang,
clang::SourceRange R, const clang::FileEntry *FE,
Range range);
std::unique_ptr<clang::CompilerInvocation>
BuildCompilerInvocation(std::vector<const char *> args,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS);

View File

@ -785,19 +785,18 @@ public:
Usr usr = GetUsr(D, &info);
auto do_def_decl = [&](auto *entity) {
Use use{{loc, role}, lid};
if (is_def) {
SourceRange R = OrigD->getSourceRange();
entity->def.spell = {
Use{{loc, role}, lid},
R.getBegin().isFileID() ? FromTokenRange(SM, Lang, R) : loc};
entity->def.spell = {use,
FromTokenRangeDefaulted(SM, Lang, R, FE, loc)};
GetSymbolKind(cast<Decl>(SemDC), entity->def.parent_kind);
} else if (is_decl) {
DeclRef &dr = entity->declarations.emplace_back();
static_cast<Use&>(dr) = {{loc, role}, lid};
SourceRange R = OrigD->getSourceRange();
dr.extent = R.getBegin().isFileID() ? FromTokenRange(SM, Lang, R) : loc;
entity->declarations.push_back(
{use, FromTokenRangeDefaulted(SM, Lang, R, FE, loc)});
} else {
entity->uses.push_back({{loc, role}, lid});
entity->uses.push_back(use);
return;
}
if (entity->def.comments[0] == '\0' && g_config->index.comments)

View File

@ -33,6 +33,18 @@ MAKE_REFLECT_STRUCT(In_CclsNavigate::Params, textDocument, position, direction);
MAKE_REFLECT_STRUCT(In_CclsNavigate, id, params);
REGISTER_IN_MESSAGE(In_CclsNavigate);
Maybe<Range> FindParent(QueryFile *file, Position pos) {
Maybe<Range> parent;
for (auto [sym, refcnt] : file->symbol2refcnt)
if (refcnt > 0 && sym.extent.Valid() && sym.extent.start <= pos &&
pos < sym.extent.end &&
(!parent || (parent->start == sym.extent.start
? parent->end < sym.extent.end
: parent->start < sym.extent.start)))
parent = sym.extent;
return parent;
}
struct Handler_CclsNavigate : BaseMessageHandler<In_CclsNavigate> {
MethodType GetMethodType() const override { return kMethodType; }
void Run(In_CclsNavigate *request) override {
@ -54,11 +66,7 @@ struct Handler_CclsNavigate : BaseMessageHandler<In_CclsNavigate> {
Maybe<Range> res;
switch (params.direction[0]) {
case 'D': {
Maybe<Range> parent;
for (auto [sym, refcnt] : file->symbol2refcnt)
if (refcnt > 0 && sym.extent.Valid() && sym.extent.start <= pos &&
pos < sym.extent.end && (!parent || parent->start < sym.extent.start))
parent = sym.extent;
Maybe<Range> parent = FindParent(file, pos);
for (auto [sym, refcnt] : file->symbol2refcnt)
if (refcnt > 0 && pos < sym.extent.start &&
(!parent || sym.extent.end <= parent->end) &&
@ -74,11 +82,7 @@ struct Handler_CclsNavigate : BaseMessageHandler<In_CclsNavigate> {
res = sym.extent;
break;
case 'R': {
Maybe<Range> parent;
for (auto [sym, refcnt] : file->symbol2refcnt)
if (refcnt > 0 && sym.extent.Valid() && sym.extent.start <= pos &&
pos < sym.extent.end && (!parent || parent->start < sym.extent.start))
parent = sym.extent;
Maybe<Range> parent = FindParent(file, pos);
if (parent && parent->start.line == pos.line && pos < parent->end) {
pos = parent->end;
if (pos.column)

View File

@ -74,6 +74,8 @@ struct Handler_TextDocumentTypeDefinition
}
}
std::sort(result.begin(), result.end());
result.erase(std::unique(result.begin(), result.end()), result.end());
pipeline::Reply(request->id, result);
}
};