[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
//===--- FindTarget.cpp - What does an AST node refer to? -----------------===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "FindTarget.h"
|
|
|
|
#include "AST.h"
|
2021-01-18 15:58:43 +08:00
|
|
|
#include "HeuristicResolver.h"
|
[clangd] Move non-clang base pieces into separate support/ lib. NFCI
Summary:
This enforces layering, reduces a sprawling clangd/ directory, and makes life
easier for embedders.
Reviewers: kbobyrev
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D79014
2020-04-28 23:49:17 +08:00
|
|
|
#include "support/Logger.h"
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
#include "clang/AST/ASTTypeTraits.h"
|
2019-09-25 20:40:22 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2021-02-28 05:08:07 +08:00
|
|
|
#include "clang/AST/DeclBase.h"
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
|
|
|
#include "clang/AST/DeclTemplate.h"
|
|
|
|
#include "clang/AST/DeclVisitor.h"
|
|
|
|
#include "clang/AST/DeclarationName.h"
|
2019-09-25 20:40:22 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
2019-09-27 17:39:10 +08:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
2020-01-22 05:55:43 +08:00
|
|
|
#include "clang/AST/ExprConcepts.h"
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
#include "clang/AST/ExprObjC.h"
|
2019-09-25 20:40:22 +08:00
|
|
|
#include "clang/AST/NestedNameSpecifier.h"
|
|
|
|
#include "clang/AST/PrettyPrinter.h"
|
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2019-10-01 18:02:23 +08:00
|
|
|
#include "clang/AST/TemplateBase.h"
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
#include "clang/AST/Type.h"
|
2019-09-25 20:40:22 +08:00
|
|
|
#include "clang/AST/TypeLoc.h"
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
#include "clang/AST/TypeLocVisitor.h"
|
2020-01-16 22:57:28 +08:00
|
|
|
#include "clang/AST/TypeVisitor.h"
|
2019-09-25 20:40:22 +08:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
2019-12-16 09:42:25 +08:00
|
|
|
#include "clang/Basic/OperatorKinds.h"
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
#include "clang/Basic/SourceLocation.h"
|
2020-02-20 22:22:07 +08:00
|
|
|
#include "clang/Basic/Specifiers.h"
|
2019-09-25 20:40:22 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2020-03-13 06:22:44 +08:00
|
|
|
#include <iterator>
|
2019-09-25 20:40:22 +08:00
|
|
|
#include <utility>
|
2020-01-16 18:37:58 +08:00
|
|
|
#include <vector>
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
namespace {
|
|
|
|
|
2020-12-11 07:52:35 +08:00
|
|
|
LLVM_ATTRIBUTE_UNUSED std::string nodeToString(const DynTypedNode &N) {
|
2020-01-29 03:23:46 +08:00
|
|
|
std::string S = std::string(N.getNodeKind().asStringRef());
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
{
|
|
|
|
llvm::raw_string_ostream OS(S);
|
|
|
|
OS << ": ";
|
|
|
|
N.print(OS, PrintingPolicy(LangOptions()));
|
|
|
|
}
|
|
|
|
std::replace(S.begin(), S.end(), '\n', ' ');
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2020-01-16 18:37:58 +08:00
|
|
|
const NamedDecl *getTemplatePattern(const NamedDecl *D) {
|
|
|
|
if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) {
|
2020-02-20 22:22:07 +08:00
|
|
|
if (const auto *Result = CRD->getTemplateInstantiationPattern())
|
|
|
|
return Result;
|
|
|
|
// getTemplateInstantiationPattern returns null if the Specialization is
|
|
|
|
// incomplete (e.g. the type didn't need to be complete), fall back to the
|
|
|
|
// primary template.
|
|
|
|
if (CRD->getTemplateSpecializationKind() == TSK_Undeclared)
|
|
|
|
if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(CRD))
|
|
|
|
return Spec->getSpecializedTemplate()->getTemplatedDecl();
|
2020-01-16 18:37:58 +08:00
|
|
|
} else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
return FD->getTemplateInstantiationPattern();
|
|
|
|
} else if (auto *VD = dyn_cast<VarDecl>(D)) {
|
|
|
|
// Hmm: getTIP returns its arg if it's not an instantiation?!
|
|
|
|
VarDecl *T = VD->getTemplateInstantiationPattern();
|
|
|
|
return (T == D) ? nullptr : T;
|
|
|
|
} else if (const auto *ED = dyn_cast<EnumDecl>(D)) {
|
|
|
|
return ED->getInstantiatedFromMemberEnum();
|
|
|
|
} else if (isa<FieldDecl>(D) || isa<TypedefNameDecl>(D)) {
|
|
|
|
if (const auto *Parent = llvm::dyn_cast<NamedDecl>(D->getDeclContext()))
|
|
|
|
if (const DeclContext *ParentPat =
|
|
|
|
dyn_cast_or_null<DeclContext>(getTemplatePattern(Parent)))
|
|
|
|
for (const NamedDecl *BaseND : ParentPat->lookup(D->getDeclName()))
|
|
|
|
if (!BaseND->isImplicit() && BaseND->getKind() == D->getKind())
|
|
|
|
return BaseND;
|
|
|
|
} else if (const auto *ECD = dyn_cast<EnumConstantDecl>(D)) {
|
|
|
|
if (const auto *ED = dyn_cast<EnumDecl>(ECD->getDeclContext())) {
|
|
|
|
if (const EnumDecl *Pattern = ED->getInstantiatedFromMemberEnum()) {
|
|
|
|
for (const NamedDecl *BaseECD : Pattern->lookup(ECD->getDeclName()))
|
|
|
|
return BaseECD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
// TargetFinder locates the entities that an AST node refers to.
|
|
|
|
//
|
|
|
|
// Typically this is (possibly) one declaration and (possibly) one type, but
|
|
|
|
// may be more:
|
|
|
|
// - for ambiguous nodes like OverloadExpr
|
|
|
|
// - if we want to include e.g. both typedefs and the underlying type
|
|
|
|
//
|
|
|
|
// This is organized as a set of mutually recursive helpers for particular node
|
|
|
|
// types, but for most nodes this is a short walk rather than a deep traversal.
|
|
|
|
//
|
|
|
|
// It's tempting to do e.g. typedef resolution as a second normalization step,
|
|
|
|
// after finding the 'primary' decl etc. But we do this monolithically instead
|
|
|
|
// because:
|
|
|
|
// - normalization may require these traversals again (e.g. unwrapping a
|
|
|
|
// typedef reveals a decltype which must be traversed)
|
|
|
|
// - it doesn't simplify that much, e.g. the first stage must still be able
|
|
|
|
// to yield multiple decls to handle OverloadExpr
|
|
|
|
// - there are cases where it's required for correctness. e.g:
|
|
|
|
// template<class X> using pvec = vector<x*>; pvec<int> x;
|
|
|
|
// There's no Decl `pvec<int>`, we must choose `pvec<X>` or `vector<int*>`
|
|
|
|
// and both are lossy. We must know upfront what the caller ultimately wants.
|
|
|
|
//
|
|
|
|
// FIXME: improve common dependent scope using name lookup in primary templates.
|
2020-09-29 15:19:59 +08:00
|
|
|
// We currently handle several dependent constructs, but some others remain to
|
|
|
|
// be handled:
|
2019-12-06 07:29:32 +08:00
|
|
|
// - UnresolvedUsingTypenameDecl
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
struct TargetFinder {
|
|
|
|
using RelSet = DeclRelationSet;
|
|
|
|
using Rel = DeclRelation;
|
|
|
|
|
2020-01-16 18:37:58 +08:00
|
|
|
private:
|
2021-01-18 15:58:43 +08:00
|
|
|
const HeuristicResolver *Resolver;
|
2020-01-16 18:37:58 +08:00
|
|
|
llvm::SmallDenseMap<const NamedDecl *,
|
|
|
|
std::pair<RelSet, /*InsertionOrder*/ size_t>>
|
|
|
|
Decls;
|
2021-01-11 09:41:50 +08:00
|
|
|
llvm::SmallDenseMap<const Decl *, RelSet> Seen;
|
2020-01-16 18:37:58 +08:00
|
|
|
RelSet Flags;
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
|
|
|
|
template <typename T> void debug(T &Node, RelSet Flags) {
|
2020-12-11 07:52:35 +08:00
|
|
|
dlog("visit [{0}] {1}", Flags, nodeToString(DynTypedNode::create(Node)));
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
}
|
|
|
|
|
[clangd] targetDecl() returns only NamedDecls.
Summary:
While it's perfectly reasonable for non-named decls such as
static_assert to resolve to themselves:
- nothing else ever resolves to them
- features based on references (hover, highlight, find refs etc) tend
to be uninteresting where only trivial references are possible
- returning NamedDecl is a more convenient API (we cast to it in many places)
- this aligns closer to findExplicitReferences/explicitReferenceTargets
This fixes a crash in explicitReferenceTargets: if the target is a
non-named decl then there's an invalid unchecked cast to NamedDecl.
In practice this means when hovering over e.g. a static_assert:
- before ac3f9e4842, we would show a (boring) hover card
- after ac3f9e4842, we would crash
- after this patch, we will show nothing
Reviewers: kadircet, ilya-biryukov
Subscribers: MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D72163
2020-01-04 00:26:33 +08:00
|
|
|
void report(const NamedDecl *D, RelSet Flags) {
|
2020-12-11 07:52:35 +08:00
|
|
|
dlog("--> [{0}] {1}", Flags, nodeToString(DynTypedNode::create(*D)));
|
2020-01-16 18:37:58 +08:00
|
|
|
auto It = Decls.try_emplace(D, std::make_pair(Flags, Decls.size()));
|
|
|
|
// If already exists, update the flags.
|
|
|
|
if (!It.second)
|
|
|
|
It.first->second.first |= Flags;
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2021-01-18 15:58:43 +08:00
|
|
|
TargetFinder(const HeuristicResolver *Resolver) : Resolver(Resolver) {}
|
|
|
|
|
2020-01-16 18:37:58 +08:00
|
|
|
llvm::SmallVector<std::pair<const NamedDecl *, RelSet>, 1> takeDecls() const {
|
|
|
|
using ValTy = std::pair<const NamedDecl *, RelSet>;
|
|
|
|
llvm::SmallVector<ValTy, 1> Result;
|
|
|
|
Result.resize(Decls.size());
|
|
|
|
for (const auto &Elem : Decls)
|
|
|
|
Result[Elem.second.second] = {Elem.first, Elem.second.first};
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
[clangd] targetDecl() returns only NamedDecls.
Summary:
While it's perfectly reasonable for non-named decls such as
static_assert to resolve to themselves:
- nothing else ever resolves to them
- features based on references (hover, highlight, find refs etc) tend
to be uninteresting where only trivial references are possible
- returning NamedDecl is a more convenient API (we cast to it in many places)
- this aligns closer to findExplicitReferences/explicitReferenceTargets
This fixes a crash in explicitReferenceTargets: if the target is a
non-named decl then there's an invalid unchecked cast to NamedDecl.
In practice this means when hovering over e.g. a static_assert:
- before ac3f9e4842, we would show a (boring) hover card
- after ac3f9e4842, we would crash
- after this patch, we will show nothing
Reviewers: kadircet, ilya-biryukov
Subscribers: MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D72163
2020-01-04 00:26:33 +08:00
|
|
|
void add(const Decl *Dcl, RelSet Flags) {
|
2020-01-29 09:01:42 +08:00
|
|
|
const NamedDecl *D = llvm::dyn_cast_or_null<NamedDecl>(Dcl);
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
if (!D)
|
|
|
|
return;
|
|
|
|
debug(*D, Flags);
|
2021-01-11 09:41:50 +08:00
|
|
|
|
|
|
|
// Avoid recursion (which can arise in the presence of heuristic
|
|
|
|
// resolution of dependent names) by exiting early if we have
|
|
|
|
// already seen this decl with all flags in Flags.
|
|
|
|
auto Res = Seen.try_emplace(D);
|
|
|
|
if (!Res.second && Res.first->second.contains(Flags))
|
|
|
|
return;
|
|
|
|
Res.first->second |= Flags;
|
|
|
|
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
if (const UsingDirectiveDecl *UDD = llvm::dyn_cast<UsingDirectiveDecl>(D))
|
|
|
|
D = UDD->getNominatedNamespaceAsWritten();
|
|
|
|
|
|
|
|
if (const TypedefNameDecl *TND = dyn_cast<TypedefNameDecl>(D)) {
|
|
|
|
add(TND->getUnderlyingType(), Flags | Rel::Underlying);
|
|
|
|
Flags |= Rel::Alias; // continue with the alias.
|
|
|
|
} else if (const UsingDecl *UD = dyn_cast<UsingDecl>(D)) {
|
2020-10-07 16:01:04 +08:00
|
|
|
// no Underlying as this is a non-renaming alias.
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
for (const UsingShadowDecl *S : UD->shadows())
|
2020-10-07 16:01:04 +08:00
|
|
|
add(S->getUnderlyingDecl(), Flags);
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
Flags |= Rel::Alias; // continue with the alias.
|
|
|
|
} else if (const auto *NAD = dyn_cast<NamespaceAliasDecl>(D)) {
|
|
|
|
add(NAD->getUnderlyingDecl(), Flags | Rel::Underlying);
|
|
|
|
Flags |= Rel::Alias; // continue with the alias
|
2020-03-13 07:27:18 +08:00
|
|
|
} else if (const UnresolvedUsingValueDecl *UUVD =
|
|
|
|
dyn_cast<UnresolvedUsingValueDecl>(D)) {
|
2021-01-18 15:58:43 +08:00
|
|
|
if (Resolver) {
|
|
|
|
for (const NamedDecl *Target : Resolver->resolveUsingValueDecl(UUVD)) {
|
|
|
|
add(Target, Flags); // no Underlying as this is a non-renaming alias
|
|
|
|
}
|
2020-03-13 07:27:18 +08:00
|
|
|
}
|
|
|
|
Flags |= Rel::Alias; // continue with the alias
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
} else if (const UsingShadowDecl *USD = dyn_cast<UsingShadowDecl>(D)) {
|
|
|
|
// Include the using decl, but don't traverse it. This may end up
|
|
|
|
// including *all* shadows, which we don't want.
|
|
|
|
report(USD->getUsingDecl(), Flags | Rel::Alias);
|
|
|
|
// Shadow decls are synthetic and not themselves interesting.
|
|
|
|
// Record the underlying decl instead, if allowed.
|
|
|
|
D = USD->getTargetDecl();
|
2020-07-19 14:26:49 +08:00
|
|
|
} else if (const auto *DG = dyn_cast<CXXDeductionGuideDecl>(D)) {
|
|
|
|
D = DG->getDeducedTemplate();
|
2020-07-10 02:29:15 +08:00
|
|
|
} else if (const ObjCImplementationDecl *IID =
|
|
|
|
dyn_cast<ObjCImplementationDecl>(D)) {
|
|
|
|
// Treat ObjC{Interface,Implementation}Decl as if they were a decl/def
|
|
|
|
// pair as long as the interface isn't implicit.
|
|
|
|
if (const auto *CID = IID->getClassInterface())
|
|
|
|
if (const auto *DD = CID->getDefinition())
|
|
|
|
if (!DD->isImplicitInterfaceDecl())
|
|
|
|
D = DD;
|
|
|
|
} else if (const ObjCCategoryImplDecl *CID =
|
|
|
|
dyn_cast<ObjCCategoryImplDecl>(D)) {
|
|
|
|
// Treat ObjC{Category,CategoryImpl}Decl as if they were a decl/def pair.
|
|
|
|
D = CID->getCategoryDecl();
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
}
|
2020-07-10 02:29:15 +08:00
|
|
|
if (!D)
|
|
|
|
return;
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
|
|
|
|
if (const Decl *Pat = getTemplatePattern(D)) {
|
|
|
|
assert(Pat != D);
|
|
|
|
add(Pat, Flags | Rel::TemplatePattern);
|
|
|
|
// Now continue with the instantiation.
|
|
|
|
Flags |= Rel::TemplateInstantiation;
|
|
|
|
}
|
|
|
|
|
|
|
|
report(D, Flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
void add(const Stmt *S, RelSet Flags) {
|
|
|
|
if (!S)
|
|
|
|
return;
|
|
|
|
debug(*S, Flags);
|
|
|
|
struct Visitor : public ConstStmtVisitor<Visitor> {
|
|
|
|
TargetFinder &Outer;
|
|
|
|
RelSet Flags;
|
|
|
|
Visitor(TargetFinder &Outer, RelSet Flags) : Outer(Outer), Flags(Flags) {}
|
|
|
|
|
2019-11-27 23:22:16 +08:00
|
|
|
void VisitCallExpr(const CallExpr *CE) {
|
|
|
|
Outer.add(CE->getCalleeDecl(), Flags);
|
|
|
|
}
|
2020-01-22 05:55:43 +08:00
|
|
|
void VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E) {
|
|
|
|
Outer.add(E->getNamedConcept(), Flags);
|
|
|
|
}
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
void VisitDeclRefExpr(const DeclRefExpr *DRE) {
|
|
|
|
const Decl *D = DRE->getDecl();
|
|
|
|
// UsingShadowDecl allows us to record the UsingDecl.
|
|
|
|
// getFoundDecl() returns the wrong thing in other cases (templates).
|
|
|
|
if (auto *USD = llvm::dyn_cast<UsingShadowDecl>(DRE->getFoundDecl()))
|
|
|
|
D = USD;
|
|
|
|
Outer.add(D, Flags);
|
|
|
|
}
|
|
|
|
void VisitMemberExpr(const MemberExpr *ME) {
|
|
|
|
const Decl *D = ME->getMemberDecl();
|
|
|
|
if (auto *USD =
|
|
|
|
llvm::dyn_cast<UsingShadowDecl>(ME->getFoundDecl().getDecl()))
|
|
|
|
D = USD;
|
|
|
|
Outer.add(D, Flags);
|
|
|
|
}
|
2019-10-01 15:27:55 +08:00
|
|
|
void VisitOverloadExpr(const OverloadExpr *OE) {
|
|
|
|
for (auto *D : OE->decls())
|
|
|
|
Outer.add(D, Flags);
|
|
|
|
}
|
2019-12-06 03:27:23 +08:00
|
|
|
void VisitSizeOfPackExpr(const SizeOfPackExpr *SE) {
|
|
|
|
Outer.add(SE->getPack(), Flags);
|
|
|
|
}
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
void VisitCXXConstructExpr(const CXXConstructExpr *CCE) {
|
|
|
|
Outer.add(CCE->getConstructor(), Flags);
|
|
|
|
}
|
|
|
|
void VisitDesignatedInitExpr(const DesignatedInitExpr *DIE) {
|
|
|
|
for (const DesignatedInitExpr::Designator &D :
|
|
|
|
llvm::reverse(DIE->designators()))
|
|
|
|
if (D.isFieldDesignator()) {
|
|
|
|
Outer.add(D.getField(), Flags);
|
|
|
|
// We don't know which designator was intended, we assume the outer.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-04-08 17:03:50 +08:00
|
|
|
void VisitGotoStmt(const GotoStmt *Goto) {
|
|
|
|
if (auto *LabelDecl = Goto->getLabel())
|
|
|
|
Outer.add(LabelDecl, Flags);
|
|
|
|
}
|
|
|
|
void VisitLabelStmt(const LabelStmt *Label) {
|
|
|
|
if (auto *LabelDecl = Label->getDecl())
|
|
|
|
Outer.add(LabelDecl, Flags);
|
|
|
|
}
|
2019-12-06 07:29:32 +08:00
|
|
|
void
|
|
|
|
VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) {
|
2021-01-18 15:58:43 +08:00
|
|
|
if (Outer.Resolver) {
|
|
|
|
for (const NamedDecl *D : Outer.Resolver->resolveMemberExpr(E)) {
|
|
|
|
Outer.add(D, Flags);
|
|
|
|
}
|
2019-12-06 07:29:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E) {
|
2021-01-18 15:58:43 +08:00
|
|
|
if (Outer.Resolver) {
|
|
|
|
for (const NamedDecl *D : Outer.Resolver->resolveDeclRefExpr(E)) {
|
|
|
|
Outer.add(D, Flags);
|
|
|
|
}
|
2019-12-06 07:29:32 +08:00
|
|
|
}
|
|
|
|
}
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
void VisitObjCIvarRefExpr(const ObjCIvarRefExpr *OIRE) {
|
|
|
|
Outer.add(OIRE->getDecl(), Flags);
|
|
|
|
}
|
|
|
|
void VisitObjCMessageExpr(const ObjCMessageExpr *OME) {
|
|
|
|
Outer.add(OME->getMethodDecl(), Flags);
|
|
|
|
}
|
|
|
|
void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *OPRE) {
|
|
|
|
if (OPRE->isExplicitProperty())
|
|
|
|
Outer.add(OPRE->getExplicitProperty(), Flags);
|
|
|
|
else {
|
|
|
|
if (OPRE->isMessagingGetter())
|
|
|
|
Outer.add(OPRE->getImplicitPropertyGetter(), Flags);
|
|
|
|
if (OPRE->isMessagingSetter())
|
|
|
|
Outer.add(OPRE->getImplicitPropertySetter(), Flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void VisitObjCProtocolExpr(const ObjCProtocolExpr *OPE) {
|
|
|
|
Outer.add(OPE->getProtocol(), Flags);
|
|
|
|
}
|
2020-01-10 17:40:30 +08:00
|
|
|
void VisitOpaqueValueExpr(const OpaqueValueExpr *OVE) {
|
|
|
|
Outer.add(OVE->getSourceExpr(), Flags);
|
|
|
|
}
|
|
|
|
void VisitPseudoObjectExpr(const PseudoObjectExpr *POE) {
|
|
|
|
Outer.add(POE->getSyntacticForm(), Flags);
|
|
|
|
}
|
2020-07-26 03:52:33 +08:00
|
|
|
void VisitCXXNewExpr(const CXXNewExpr *CNE) {
|
|
|
|
Outer.add(CNE->getOperatorNew(), Flags);
|
|
|
|
}
|
|
|
|
void VisitCXXDeleteExpr(const CXXDeleteExpr *CDE) {
|
|
|
|
Outer.add(CDE->getOperatorDelete(), Flags);
|
|
|
|
}
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
};
|
|
|
|
Visitor(*this, Flags).Visit(S);
|
|
|
|
}
|
|
|
|
|
|
|
|
void add(QualType T, RelSet Flags) {
|
|
|
|
if (T.isNull())
|
|
|
|
return;
|
|
|
|
debug(T, Flags);
|
|
|
|
struct Visitor : public TypeVisitor<Visitor> {
|
|
|
|
TargetFinder &Outer;
|
|
|
|
RelSet Flags;
|
|
|
|
Visitor(TargetFinder &Outer, RelSet Flags) : Outer(Outer), Flags(Flags) {}
|
|
|
|
|
|
|
|
void VisitTagType(const TagType *TT) {
|
|
|
|
Outer.add(TT->getAsTagDecl(), Flags);
|
|
|
|
}
|
2020-01-21 22:28:06 +08:00
|
|
|
|
2020-02-05 16:56:13 +08:00
|
|
|
void VisitElaboratedType(const ElaboratedType *ET) {
|
|
|
|
Outer.add(ET->desugar(), Flags);
|
|
|
|
}
|
|
|
|
|
2020-01-21 22:28:06 +08:00
|
|
|
void VisitInjectedClassNameType(const InjectedClassNameType *ICNT) {
|
|
|
|
Outer.add(ICNT->getDecl(), Flags);
|
|
|
|
}
|
|
|
|
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
void VisitDecltypeType(const DecltypeType *DTT) {
|
|
|
|
Outer.add(DTT->getUnderlyingType(), Flags | Rel::Underlying);
|
|
|
|
}
|
|
|
|
void VisitDeducedType(const DeducedType *DT) {
|
|
|
|
// FIXME: In practice this doesn't work: the AutoType you find inside
|
|
|
|
// TypeLoc never has a deduced type. https://llvm.org/PR42914
|
|
|
|
Outer.add(DT->getDeducedType(), Flags | Rel::Underlying);
|
|
|
|
}
|
2020-01-02 13:45:01 +08:00
|
|
|
void VisitDeducedTemplateSpecializationType(
|
|
|
|
const DeducedTemplateSpecializationType *DTST) {
|
|
|
|
// FIXME: This is a workaround for https://llvm.org/PR42914,
|
|
|
|
// which is causing DTST->getDeducedType() to be empty. We
|
|
|
|
// fall back to the template pattern and miss the instantiation
|
|
|
|
// even when it's known in principle. Once that bug is fixed,
|
|
|
|
// this method can be removed (the existing handling in
|
|
|
|
// VisitDeducedType() is sufficient).
|
|
|
|
if (auto *TD = DTST->getTemplateName().getAsTemplateDecl())
|
|
|
|
Outer.add(TD->getTemplatedDecl(), Flags | Rel::TemplatePattern);
|
|
|
|
}
|
2020-09-29 15:19:59 +08:00
|
|
|
void VisitDependentNameType(const DependentNameType *DNT) {
|
2021-01-18 15:58:43 +08:00
|
|
|
if (Outer.Resolver) {
|
|
|
|
for (const NamedDecl *ND :
|
|
|
|
Outer.Resolver->resolveDependentNameType(DNT)) {
|
|
|
|
Outer.add(ND, Flags);
|
|
|
|
}
|
2020-09-29 15:19:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void VisitDependentTemplateSpecializationType(
|
|
|
|
const DependentTemplateSpecializationType *DTST) {
|
2021-01-18 15:58:43 +08:00
|
|
|
if (Outer.Resolver) {
|
|
|
|
for (const NamedDecl *ND :
|
|
|
|
Outer.Resolver->resolveTemplateSpecializationType(DTST)) {
|
|
|
|
Outer.add(ND, Flags);
|
|
|
|
}
|
2020-09-29 15:19:59 +08:00
|
|
|
}
|
|
|
|
}
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
void VisitTypedefType(const TypedefType *TT) {
|
|
|
|
Outer.add(TT->getDecl(), Flags);
|
|
|
|
}
|
|
|
|
void
|
|
|
|
VisitTemplateSpecializationType(const TemplateSpecializationType *TST) {
|
|
|
|
// Have to handle these case-by-case.
|
|
|
|
|
|
|
|
// templated type aliases: there's no specialized/instantiated using
|
|
|
|
// decl to point to. So try to find a decl for the underlying type
|
|
|
|
// (after substitution), and failing that point to the (templated) using
|
|
|
|
// decl.
|
|
|
|
if (TST->isTypeAlias()) {
|
|
|
|
Outer.add(TST->getAliasedType(), Flags | Rel::Underlying);
|
|
|
|
// Don't *traverse* the alias, which would result in traversing the
|
|
|
|
// template of the underlying type.
|
|
|
|
Outer.report(
|
|
|
|
TST->getTemplateName().getAsTemplateDecl()->getTemplatedDecl(),
|
|
|
|
Flags | Rel::Alias | Rel::TemplatePattern);
|
|
|
|
}
|
|
|
|
// specializations of template template parameters aren't instantiated
|
|
|
|
// into decls, so they must refer to the parameter itself.
|
|
|
|
else if (const auto *Parm =
|
|
|
|
llvm::dyn_cast_or_null<TemplateTemplateParmDecl>(
|
|
|
|
TST->getTemplateName().getAsTemplateDecl()))
|
|
|
|
Outer.add(Parm, Flags);
|
|
|
|
// class template specializations have a (specialized) CXXRecordDecl.
|
|
|
|
else if (const CXXRecordDecl *RD = TST->getAsCXXRecordDecl())
|
|
|
|
Outer.add(RD, Flags); // add(Decl) will despecialize if needed.
|
|
|
|
else {
|
|
|
|
// fallback: the (un-specialized) declaration from primary template.
|
|
|
|
if (auto *TD = TST->getTemplateName().getAsTemplateDecl())
|
|
|
|
Outer.add(TD->getTemplatedDecl(), Flags | Rel::TemplatePattern);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void VisitTemplateTypeParmType(const TemplateTypeParmType *TTPT) {
|
|
|
|
Outer.add(TTPT->getDecl(), Flags);
|
|
|
|
}
|
|
|
|
void VisitObjCInterfaceType(const ObjCInterfaceType *OIT) {
|
|
|
|
Outer.add(OIT->getDecl(), Flags);
|
|
|
|
}
|
|
|
|
void VisitObjCObjectType(const ObjCObjectType *OOT) {
|
|
|
|
// FIXME: ObjCObjectTypeLoc has no children for the protocol list, so
|
|
|
|
// there is no node in id<Foo> that refers to ObjCProtocolDecl Foo.
|
|
|
|
if (OOT->isObjCQualifiedId() && OOT->getNumProtocols() == 1)
|
|
|
|
Outer.add(OOT->getProtocol(0), Flags);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Visitor(*this, Flags).Visit(T.getTypePtr());
|
|
|
|
}
|
|
|
|
|
|
|
|
void add(const NestedNameSpecifier *NNS, RelSet Flags) {
|
|
|
|
if (!NNS)
|
|
|
|
return;
|
|
|
|
debug(*NNS, Flags);
|
|
|
|
switch (NNS->getKind()) {
|
|
|
|
case NestedNameSpecifier::Namespace:
|
|
|
|
add(NNS->getAsNamespace(), Flags);
|
|
|
|
return;
|
|
|
|
case NestedNameSpecifier::NamespaceAlias:
|
|
|
|
add(NNS->getAsNamespaceAlias(), Flags);
|
|
|
|
return;
|
2020-09-29 15:19:59 +08:00
|
|
|
case NestedNameSpecifier::Identifier:
|
2021-01-18 15:58:43 +08:00
|
|
|
if (Resolver) {
|
|
|
|
add(QualType(Resolver->resolveNestedNameSpecifierToType(NNS), 0),
|
|
|
|
Flags);
|
|
|
|
}
|
|
|
|
return;
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
case NestedNameSpecifier::TypeSpec:
|
|
|
|
case NestedNameSpecifier::TypeSpecWithTemplate:
|
2021-01-18 15:58:43 +08:00
|
|
|
add(QualType(NNS->getAsType(), 0), Flags);
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
return;
|
|
|
|
case NestedNameSpecifier::Global:
|
|
|
|
// This should be TUDecl, but we can't get a pointer to it!
|
|
|
|
return;
|
|
|
|
case NestedNameSpecifier::Super:
|
|
|
|
add(NNS->getAsRecordDecl(), Flags);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
llvm_unreachable("unhandled NestedNameSpecifier::SpecifierKind");
|
|
|
|
}
|
|
|
|
|
|
|
|
void add(const CXXCtorInitializer *CCI, RelSet Flags) {
|
|
|
|
if (!CCI)
|
|
|
|
return;
|
|
|
|
debug(*CCI, Flags);
|
|
|
|
|
|
|
|
if (CCI->isAnyMemberInitializer())
|
|
|
|
add(CCI->getAnyMember(), Flags);
|
|
|
|
// Constructor calls contain a TypeLoc node, so we don't handle them here.
|
|
|
|
}
|
2020-08-10 08:37:43 +08:00
|
|
|
|
|
|
|
void add(const TemplateArgument &Arg, RelSet Flags) {
|
|
|
|
// Only used for template template arguments.
|
|
|
|
// For type and non-type template arguments, SelectionTree
|
|
|
|
// will hit a more specific node (e.g. a TypeLoc or a
|
|
|
|
// DeclRefExpr).
|
|
|
|
if (Arg.getKind() == TemplateArgument::Template ||
|
|
|
|
Arg.getKind() == TemplateArgument::TemplateExpansion) {
|
|
|
|
if (TemplateDecl *TD = Arg.getAsTemplate().getAsTemplateDecl()) {
|
|
|
|
report(TD, Flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
[clangd] targetDecl() returns only NamedDecls.
Summary:
While it's perfectly reasonable for non-named decls such as
static_assert to resolve to themselves:
- nothing else ever resolves to them
- features based on references (hover, highlight, find refs etc) tend
to be uninteresting where only trivial references are possible
- returning NamedDecl is a more convenient API (we cast to it in many places)
- this aligns closer to findExplicitReferences/explicitReferenceTargets
This fixes a crash in explicitReferenceTargets: if the target is a
non-named decl then there's an invalid unchecked cast to NamedDecl.
In practice this means when hovering over e.g. a static_assert:
- before ac3f9e4842, we would show a (boring) hover card
- after ac3f9e4842, we would crash
- after this patch, we will show nothing
Reviewers: kadircet, ilya-biryukov
Subscribers: MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D72163
2020-01-04 00:26:33 +08:00
|
|
|
llvm::SmallVector<std::pair<const NamedDecl *, DeclRelationSet>, 1>
|
2021-01-18 15:58:43 +08:00
|
|
|
allTargetDecls(const DynTypedNode &N, const HeuristicResolver *Resolver) {
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
dlog("allTargetDecls({0})", nodeToString(N));
|
2021-01-18 15:58:43 +08:00
|
|
|
TargetFinder Finder(Resolver);
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
DeclRelationSet Flags;
|
|
|
|
if (const Decl *D = N.get<Decl>())
|
|
|
|
Finder.add(D, Flags);
|
|
|
|
else if (const Stmt *S = N.get<Stmt>())
|
|
|
|
Finder.add(S, Flags);
|
|
|
|
else if (const NestedNameSpecifierLoc *NNSL = N.get<NestedNameSpecifierLoc>())
|
|
|
|
Finder.add(NNSL->getNestedNameSpecifier(), Flags);
|
|
|
|
else if (const NestedNameSpecifier *NNS = N.get<NestedNameSpecifier>())
|
|
|
|
Finder.add(NNS, Flags);
|
|
|
|
else if (const TypeLoc *TL = N.get<TypeLoc>())
|
|
|
|
Finder.add(TL->getType(), Flags);
|
|
|
|
else if (const QualType *QT = N.get<QualType>())
|
|
|
|
Finder.add(*QT, Flags);
|
|
|
|
else if (const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>())
|
|
|
|
Finder.add(CCI, Flags);
|
2020-08-10 08:37:43 +08:00
|
|
|
else if (const TemplateArgumentLoc *TAL = N.get<TemplateArgumentLoc>())
|
|
|
|
Finder.add(TAL->getArgument(), Flags);
|
2021-01-27 02:59:29 +08:00
|
|
|
else if (const CXXBaseSpecifier *CBS = N.get<CXXBaseSpecifier>())
|
|
|
|
Finder.add(CBS->getTypeSourceInfo()->getType(), Flags);
|
2020-01-16 18:37:58 +08:00
|
|
|
return Finder.takeDecls();
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
}
|
|
|
|
|
2021-01-18 15:58:43 +08:00
|
|
|
llvm::SmallVector<const NamedDecl *, 1>
|
|
|
|
targetDecl(const DynTypedNode &N, DeclRelationSet Mask,
|
|
|
|
const HeuristicResolver *Resolver) {
|
[clangd] targetDecl() returns only NamedDecls.
Summary:
While it's perfectly reasonable for non-named decls such as
static_assert to resolve to themselves:
- nothing else ever resolves to them
- features based on references (hover, highlight, find refs etc) tend
to be uninteresting where only trivial references are possible
- returning NamedDecl is a more convenient API (we cast to it in many places)
- this aligns closer to findExplicitReferences/explicitReferenceTargets
This fixes a crash in explicitReferenceTargets: if the target is a
non-named decl then there's an invalid unchecked cast to NamedDecl.
In practice this means when hovering over e.g. a static_assert:
- before ac3f9e4842, we would show a (boring) hover card
- after ac3f9e4842, we would crash
- after this patch, we will show nothing
Reviewers: kadircet, ilya-biryukov
Subscribers: MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D72163
2020-01-04 00:26:33 +08:00
|
|
|
llvm::SmallVector<const NamedDecl *, 1> Result;
|
2021-01-18 15:58:43 +08:00
|
|
|
for (const auto &Entry : allTargetDecls(N, Resolver)) {
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
if (!(Entry.second & ~Mask))
|
|
|
|
Result.push_back(Entry.first);
|
2019-09-25 20:40:22 +08:00
|
|
|
}
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2019-09-25 20:40:22 +08:00
|
|
|
llvm::SmallVector<const NamedDecl *, 1>
|
2021-01-18 15:58:43 +08:00
|
|
|
explicitReferenceTargets(DynTypedNode N, DeclRelationSet Mask,
|
|
|
|
const HeuristicResolver *Resolver) {
|
2019-09-25 20:40:22 +08:00
|
|
|
assert(!(Mask & (DeclRelation::TemplatePattern |
|
|
|
|
DeclRelation::TemplateInstantiation)) &&
|
2020-04-05 14:28:11 +08:00
|
|
|
"explicitReferenceTargets handles templates on its own");
|
2021-01-18 15:58:43 +08:00
|
|
|
auto Decls = allTargetDecls(N, Resolver);
|
2019-09-25 20:40:22 +08:00
|
|
|
|
|
|
|
// We prefer to return template instantiation, but fallback to template
|
|
|
|
// pattern if instantiation is not available.
|
|
|
|
Mask |= DeclRelation::TemplatePattern | DeclRelation::TemplateInstantiation;
|
|
|
|
|
|
|
|
llvm::SmallVector<const NamedDecl *, 1> TemplatePatterns;
|
|
|
|
llvm::SmallVector<const NamedDecl *, 1> Targets;
|
|
|
|
bool SeenTemplateInstantiations = false;
|
|
|
|
for (auto &D : Decls) {
|
|
|
|
if (D.second & ~Mask)
|
|
|
|
continue;
|
|
|
|
if (D.second & DeclRelation::TemplatePattern) {
|
[clangd] targetDecl() returns only NamedDecls.
Summary:
While it's perfectly reasonable for non-named decls such as
static_assert to resolve to themselves:
- nothing else ever resolves to them
- features based on references (hover, highlight, find refs etc) tend
to be uninteresting where only trivial references are possible
- returning NamedDecl is a more convenient API (we cast to it in many places)
- this aligns closer to findExplicitReferences/explicitReferenceTargets
This fixes a crash in explicitReferenceTargets: if the target is a
non-named decl then there's an invalid unchecked cast to NamedDecl.
In practice this means when hovering over e.g. a static_assert:
- before ac3f9e4842, we would show a (boring) hover card
- after ac3f9e4842, we would crash
- after this patch, we will show nothing
Reviewers: kadircet, ilya-biryukov
Subscribers: MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D72163
2020-01-04 00:26:33 +08:00
|
|
|
TemplatePatterns.push_back(D.first);
|
2019-09-25 20:40:22 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (D.second & DeclRelation::TemplateInstantiation)
|
|
|
|
SeenTemplateInstantiations = true;
|
[clangd] targetDecl() returns only NamedDecls.
Summary:
While it's perfectly reasonable for non-named decls such as
static_assert to resolve to themselves:
- nothing else ever resolves to them
- features based on references (hover, highlight, find refs etc) tend
to be uninteresting where only trivial references are possible
- returning NamedDecl is a more convenient API (we cast to it in many places)
- this aligns closer to findExplicitReferences/explicitReferenceTargets
This fixes a crash in explicitReferenceTargets: if the target is a
non-named decl then there's an invalid unchecked cast to NamedDecl.
In practice this means when hovering over e.g. a static_assert:
- before ac3f9e4842, we would show a (boring) hover card
- after ac3f9e4842, we would crash
- after this patch, we will show nothing
Reviewers: kadircet, ilya-biryukov
Subscribers: MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D72163
2020-01-04 00:26:33 +08:00
|
|
|
Targets.push_back(D.first);
|
2019-09-25 20:40:22 +08:00
|
|
|
}
|
|
|
|
if (!SeenTemplateInstantiations)
|
|
|
|
Targets.insert(Targets.end(), TemplatePatterns.begin(),
|
|
|
|
TemplatePatterns.end());
|
|
|
|
return Targets;
|
|
|
|
}
|
|
|
|
|
2019-12-17 19:13:28 +08:00
|
|
|
namespace {
|
2021-01-18 15:58:43 +08:00
|
|
|
llvm::SmallVector<ReferenceLoc> refInDecl(const Decl *D,
|
|
|
|
const HeuristicResolver *Resolver) {
|
2019-09-25 20:40:22 +08:00
|
|
|
struct Visitor : ConstDeclVisitor<Visitor> {
|
2021-01-18 15:58:43 +08:00
|
|
|
Visitor(const HeuristicResolver *Resolver) : Resolver(Resolver) {}
|
|
|
|
|
|
|
|
const HeuristicResolver *Resolver;
|
2020-12-10 20:36:35 +08:00
|
|
|
llvm::SmallVector<ReferenceLoc> Refs;
|
2019-09-25 20:40:22 +08:00
|
|
|
|
|
|
|
void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
|
2019-10-18 20:07:19 +08:00
|
|
|
// We want to keep it as non-declaration references, as the
|
|
|
|
// "using namespace" declaration doesn't have a name.
|
|
|
|
Refs.push_back(ReferenceLoc{D->getQualifierLoc(),
|
|
|
|
D->getIdentLocation(),
|
|
|
|
/*IsDecl=*/false,
|
|
|
|
{D->getNominatedNamespaceAsWritten()}});
|
2019-09-25 20:40:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisitUsingDecl(const UsingDecl *D) {
|
2020-01-04 23:28:41 +08:00
|
|
|
// "using ns::identifier;" is a non-declaration reference.
|
2021-01-18 15:58:43 +08:00
|
|
|
Refs.push_back(ReferenceLoc{
|
|
|
|
D->getQualifierLoc(), D->getLocation(), /*IsDecl=*/false,
|
|
|
|
explicitReferenceTargets(DynTypedNode::create(*D),
|
|
|
|
DeclRelation::Underlying, Resolver)});
|
2019-09-25 20:40:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
|
2019-10-18 20:07:19 +08:00
|
|
|
// For namespace alias, "namespace Foo = Target;", we add two references.
|
|
|
|
// Add a declaration reference for Foo.
|
|
|
|
VisitNamedDecl(D);
|
|
|
|
// Add a non-declaration reference for Target.
|
|
|
|
Refs.push_back(ReferenceLoc{D->getQualifierLoc(),
|
|
|
|
D->getTargetNameLoc(),
|
|
|
|
/*IsDecl=*/false,
|
|
|
|
{D->getAliasedNamespace()}});
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitNamedDecl(const NamedDecl *ND) {
|
2020-01-21 22:15:06 +08:00
|
|
|
// We choose to ignore {Class, Function, Var, TypeAlias}TemplateDecls. As
|
|
|
|
// as their underlying decls, covering the same range, will be visited.
|
|
|
|
if (llvm::isa<ClassTemplateDecl>(ND) ||
|
|
|
|
llvm::isa<FunctionTemplateDecl>(ND) ||
|
|
|
|
llvm::isa<VarTemplateDecl>(ND) ||
|
|
|
|
llvm::isa<TypeAliasTemplateDecl>(ND))
|
|
|
|
return;
|
2019-10-18 20:07:19 +08:00
|
|
|
// FIXME: decide on how to surface destructors when we need them.
|
|
|
|
if (llvm::isa<CXXDestructorDecl>(ND))
|
|
|
|
return;
|
2019-10-28 21:41:06 +08:00
|
|
|
// Filter anonymous decls, name location will point outside the name token
|
|
|
|
// and the clients are not prepared to handle that.
|
|
|
|
if (ND->getDeclName().isIdentifier() &&
|
|
|
|
!ND->getDeclName().getAsIdentifierInfo())
|
|
|
|
return;
|
|
|
|
Refs.push_back(ReferenceLoc{getQualifierLoc(*ND),
|
|
|
|
ND->getLocation(),
|
|
|
|
/*IsDecl=*/true,
|
|
|
|
{ND}});
|
2019-09-25 20:40:22 +08:00
|
|
|
}
|
2020-07-19 14:26:49 +08:00
|
|
|
|
|
|
|
void VisitCXXDeductionGuideDecl(const CXXDeductionGuideDecl *DG) {
|
|
|
|
// The class template name in a deduction guide targets the class
|
|
|
|
// template.
|
|
|
|
Refs.push_back(ReferenceLoc{DG->getQualifierLoc(),
|
|
|
|
DG->getNameInfo().getLoc(),
|
|
|
|
/*IsDecl=*/false,
|
|
|
|
{DG->getDeducedTemplate()}});
|
|
|
|
}
|
2021-02-28 05:08:07 +08:00
|
|
|
|
|
|
|
void VisitObjCMethodDecl(const ObjCMethodDecl *OMD) {
|
|
|
|
// The name may have several tokens, we can only report the first.
|
|
|
|
Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
|
|
|
|
OMD->getSelectorStartLoc(),
|
|
|
|
/*IsDecl=*/true,
|
|
|
|
{OMD}});
|
|
|
|
}
|
|
|
|
|
|
|
|
void visitProtocolList(
|
|
|
|
llvm::iterator_range<ObjCProtocolList::iterator> Protocols,
|
|
|
|
llvm::iterator_range<const SourceLocation *> Locations) {
|
|
|
|
for (const auto &P : llvm::zip(Protocols, Locations)) {
|
|
|
|
Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
|
|
|
|
std::get<1>(P),
|
|
|
|
/*IsDecl=*/false,
|
|
|
|
{std::get<0>(P)}});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitObjCInterfaceDecl(const ObjCInterfaceDecl *OID) {
|
|
|
|
if (OID->isThisDeclarationADefinition())
|
|
|
|
visitProtocolList(OID->protocols(), OID->protocol_locs());
|
|
|
|
Base::VisitObjCInterfaceDecl(OID); // Visit the interface's name.
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitObjCCategoryDecl(const ObjCCategoryDecl *OCD) {
|
|
|
|
visitProtocolList(OCD->protocols(), OCD->protocol_locs());
|
|
|
|
// getLocation is the extended class's location, not the category's.
|
|
|
|
Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
|
|
|
|
OCD->getLocation(),
|
|
|
|
/*IsDecl=*/false,
|
|
|
|
{OCD->getClassInterface()}});
|
|
|
|
Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
|
|
|
|
OCD->getCategoryNameLoc(),
|
|
|
|
/*IsDecl=*/true,
|
|
|
|
{OCD}});
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *OCID) {
|
|
|
|
Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
|
|
|
|
OCID->getLocation(),
|
|
|
|
/*IsDecl=*/false,
|
|
|
|
{OCID->getClassInterface()}});
|
|
|
|
Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
|
|
|
|
OCID->getCategoryNameLoc(),
|
|
|
|
/*IsDecl=*/true,
|
|
|
|
{OCID->getCategoryDecl()}});
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitObjCProtocolDecl(const ObjCProtocolDecl *OPD) {
|
|
|
|
if (OPD->isThisDeclarationADefinition())
|
|
|
|
visitProtocolList(OPD->protocols(), OPD->protocol_locs());
|
|
|
|
Base::VisitObjCProtocolDecl(OPD); // Visit the protocol's name.
|
|
|
|
}
|
2019-09-25 20:40:22 +08:00
|
|
|
};
|
|
|
|
|
2021-01-18 15:58:43 +08:00
|
|
|
Visitor V{Resolver};
|
2019-09-25 20:40:22 +08:00
|
|
|
V.Visit(D);
|
2019-10-18 20:07:19 +08:00
|
|
|
return V.Refs;
|
2019-09-25 20:40:22 +08:00
|
|
|
}
|
|
|
|
|
2021-01-18 15:58:43 +08:00
|
|
|
llvm::SmallVector<ReferenceLoc> refInStmt(const Stmt *S,
|
|
|
|
const HeuristicResolver *Resolver) {
|
2019-09-25 20:40:22 +08:00
|
|
|
struct Visitor : ConstStmtVisitor<Visitor> {
|
2021-01-18 15:58:43 +08:00
|
|
|
Visitor(const HeuristicResolver *Resolver) : Resolver(Resolver) {}
|
|
|
|
|
|
|
|
const HeuristicResolver *Resolver;
|
2020-01-10 23:27:12 +08:00
|
|
|
// FIXME: handle more complicated cases: more ObjC, designated initializers.
|
2020-12-10 20:36:35 +08:00
|
|
|
llvm::SmallVector<ReferenceLoc> Refs;
|
2019-09-25 20:40:22 +08:00
|
|
|
|
2020-01-22 02:21:08 +08:00
|
|
|
void VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E) {
|
|
|
|
Refs.push_back(ReferenceLoc{E->getNestedNameSpecifierLoc(),
|
|
|
|
E->getConceptNameLoc(),
|
|
|
|
/*IsDecl=*/false,
|
|
|
|
{E->getNamedConcept()}});
|
|
|
|
}
|
2020-03-20 03:42:10 +08:00
|
|
|
|
2019-09-25 20:40:22 +08:00
|
|
|
void VisitDeclRefExpr(const DeclRefExpr *E) {
|
2019-10-18 20:07:19 +08:00
|
|
|
Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
|
|
|
|
E->getNameInfo().getLoc(),
|
|
|
|
/*IsDecl=*/false,
|
|
|
|
{E->getFoundDecl()}});
|
2019-09-25 20:40:22 +08:00
|
|
|
}
|
|
|
|
|
2020-03-20 03:42:10 +08:00
|
|
|
void VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E) {
|
|
|
|
Refs.push_back(ReferenceLoc{
|
|
|
|
E->getQualifierLoc(), E->getNameInfo().getLoc(), /*IsDecl=*/false,
|
2021-01-18 15:58:43 +08:00
|
|
|
explicitReferenceTargets(DynTypedNode::create(*E), {}, Resolver)});
|
2020-03-20 03:42:10 +08:00
|
|
|
}
|
|
|
|
|
2019-09-25 20:40:22 +08:00
|
|
|
void VisitMemberExpr(const MemberExpr *E) {
|
2020-01-21 12:33:39 +08:00
|
|
|
// Skip destructor calls to avoid duplication: TypeLoc within will be
|
|
|
|
// visited separately.
|
2021-01-03 03:56:27 +08:00
|
|
|
if (llvm::isa<CXXDestructorDecl>(E->getFoundDecl().getDecl()))
|
2020-01-21 12:33:39 +08:00
|
|
|
return;
|
2019-10-18 20:07:19 +08:00
|
|
|
Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
|
|
|
|
E->getMemberNameInfo().getLoc(),
|
|
|
|
/*IsDecl=*/false,
|
|
|
|
{E->getFoundDecl()}});
|
2019-09-25 20:40:22 +08:00
|
|
|
}
|
2019-09-27 17:39:10 +08:00
|
|
|
|
2020-03-20 03:42:10 +08:00
|
|
|
void
|
|
|
|
VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) {
|
2021-01-18 15:58:43 +08:00
|
|
|
Refs.push_back(ReferenceLoc{
|
|
|
|
E->getQualifierLoc(), E->getMemberNameInfo().getLoc(),
|
|
|
|
/*IsDecl=*/false,
|
|
|
|
explicitReferenceTargets(DynTypedNode::create(*E), {}, Resolver)});
|
2020-03-20 03:42:10 +08:00
|
|
|
}
|
|
|
|
|
2019-09-27 17:39:10 +08:00
|
|
|
void VisitOverloadExpr(const OverloadExpr *E) {
|
2019-10-18 20:07:19 +08:00
|
|
|
Refs.push_back(ReferenceLoc{E->getQualifierLoc(),
|
|
|
|
E->getNameInfo().getLoc(),
|
|
|
|
/*IsDecl=*/false,
|
|
|
|
llvm::SmallVector<const NamedDecl *, 1>(
|
|
|
|
E->decls().begin(), E->decls().end())});
|
2019-09-27 17:39:10 +08:00
|
|
|
}
|
2019-12-06 03:27:23 +08:00
|
|
|
|
|
|
|
void VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
|
|
|
|
Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
|
|
|
|
E->getPackLoc(),
|
|
|
|
/*IsDecl=*/false,
|
|
|
|
{E->getPack()}});
|
|
|
|
}
|
2020-01-10 23:27:12 +08:00
|
|
|
|
|
|
|
void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *E) {
|
|
|
|
Refs.push_back(ReferenceLoc{
|
|
|
|
NestedNameSpecifierLoc(), E->getLocation(),
|
|
|
|
/*IsDecl=*/false,
|
|
|
|
// Select the getter, setter, or @property depending on the call.
|
2021-01-18 15:58:43 +08:00
|
|
|
explicitReferenceTargets(DynTypedNode::create(*E), {}, Resolver)});
|
2020-01-10 23:27:12 +08:00
|
|
|
}
|
2020-02-10 18:53:17 +08:00
|
|
|
|
2021-02-28 05:08:07 +08:00
|
|
|
void VisitObjCMessageExpr(const ObjCMessageExpr *E) {
|
|
|
|
// The name may have several tokens, we can only report the first.
|
|
|
|
Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
|
|
|
|
E->getSelectorStartLoc(),
|
|
|
|
/*IsDecl=*/false,
|
|
|
|
{E->getMethodDecl()}});
|
|
|
|
}
|
|
|
|
|
2020-02-10 18:53:17 +08:00
|
|
|
void VisitDesignatedInitExpr(const DesignatedInitExpr *DIE) {
|
|
|
|
for (const DesignatedInitExpr::Designator &D : DIE->designators()) {
|
|
|
|
if (!D.isFieldDesignator())
|
|
|
|
continue;
|
2020-04-15 06:16:10 +08:00
|
|
|
|
2021-03-02 22:53:55 +08:00
|
|
|
Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
|
|
|
|
D.getFieldLoc(),
|
|
|
|
/*IsDecl=*/false,
|
|
|
|
{D.getField()}});
|
2020-02-10 18:53:17 +08:00
|
|
|
}
|
|
|
|
}
|
2020-05-19 06:45:27 +08:00
|
|
|
|
|
|
|
void VisitGotoStmt(const GotoStmt *GS) {
|
2021-03-02 22:53:55 +08:00
|
|
|
Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
|
|
|
|
GS->getLabelLoc(),
|
|
|
|
/*IsDecl=*/false,
|
|
|
|
{GS->getLabel()}});
|
2020-05-19 06:45:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisitLabelStmt(const LabelStmt *LS) {
|
|
|
|
Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
|
|
|
|
LS->getIdentLoc(),
|
|
|
|
/*IsDecl=*/true,
|
|
|
|
{LS->getDecl()}});
|
|
|
|
}
|
2019-09-25 20:40:22 +08:00
|
|
|
};
|
|
|
|
|
2021-01-18 15:58:43 +08:00
|
|
|
Visitor V{Resolver};
|
2020-05-19 06:45:27 +08:00
|
|
|
V.Visit(S);
|
2019-10-18 20:07:19 +08:00
|
|
|
return V.Refs;
|
2019-09-25 20:40:22 +08:00
|
|
|
}
|
|
|
|
|
2021-01-18 15:58:43 +08:00
|
|
|
llvm::SmallVector<ReferenceLoc>
|
|
|
|
refInTypeLoc(TypeLoc L, const HeuristicResolver *Resolver) {
|
2019-09-25 20:40:22 +08:00
|
|
|
struct Visitor : TypeLocVisitor<Visitor> {
|
2021-01-18 15:58:43 +08:00
|
|
|
Visitor(const HeuristicResolver *Resolver) : Resolver(Resolver) {}
|
|
|
|
|
|
|
|
const HeuristicResolver *Resolver;
|
2019-09-25 20:40:22 +08:00
|
|
|
llvm::Optional<ReferenceLoc> Ref;
|
|
|
|
|
|
|
|
void VisitElaboratedTypeLoc(ElaboratedTypeLoc L) {
|
|
|
|
// We only know about qualifier, rest if filled by inner locations.
|
|
|
|
Visit(L.getNamedTypeLoc().getUnqualifiedLoc());
|
|
|
|
// Fill in the qualifier.
|
|
|
|
if (!Ref)
|
|
|
|
return;
|
|
|
|
assert(!Ref->Qualifier.hasQualifier() && "qualifier already set");
|
|
|
|
Ref->Qualifier = L.getQualifierLoc();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitTagTypeLoc(TagTypeLoc L) {
|
2019-10-18 20:07:19 +08:00
|
|
|
Ref = ReferenceLoc{NestedNameSpecifierLoc(),
|
|
|
|
L.getNameLoc(),
|
|
|
|
/*IsDecl=*/false,
|
|
|
|
{L.getDecl()}};
|
2019-09-25 20:40:22 +08:00
|
|
|
}
|
|
|
|
|
2019-09-27 18:55:53 +08:00
|
|
|
void VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc L) {
|
2019-10-18 20:07:19 +08:00
|
|
|
Ref = ReferenceLoc{NestedNameSpecifierLoc(),
|
|
|
|
L.getNameLoc(),
|
|
|
|
/*IsDecl=*/false,
|
|
|
|
{L.getDecl()}};
|
2019-09-27 18:55:53 +08:00
|
|
|
}
|
|
|
|
|
2019-09-25 20:40:22 +08:00
|
|
|
void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc L) {
|
2019-09-28 01:55:46 +08:00
|
|
|
// We must ensure template type aliases are included in results if they
|
|
|
|
// were written in the source code, e.g. in
|
|
|
|
// template <class T> using valias = vector<T>;
|
|
|
|
// ^valias<int> x;
|
|
|
|
// 'explicitReferenceTargets' will return:
|
|
|
|
// 1. valias with mask 'Alias'.
|
|
|
|
// 2. 'vector<int>' with mask 'Underlying'.
|
|
|
|
// we want to return only #1 in this case.
|
2019-09-25 20:40:22 +08:00
|
|
|
Ref = ReferenceLoc{
|
2019-10-18 20:07:19 +08:00
|
|
|
NestedNameSpecifierLoc(), L.getTemplateNameLoc(), /*IsDecl=*/false,
|
2019-09-28 01:55:46 +08:00
|
|
|
explicitReferenceTargets(DynTypedNode::create(L.getType()),
|
2021-01-18 15:58:43 +08:00
|
|
|
DeclRelation::Alias, Resolver)};
|
2019-09-28 01:55:46 +08:00
|
|
|
}
|
|
|
|
void VisitDeducedTemplateSpecializationTypeLoc(
|
|
|
|
DeducedTemplateSpecializationTypeLoc L) {
|
|
|
|
Ref = ReferenceLoc{
|
2019-10-18 20:07:19 +08:00
|
|
|
NestedNameSpecifierLoc(), L.getNameLoc(), /*IsDecl=*/false,
|
2019-09-28 01:55:46 +08:00
|
|
|
explicitReferenceTargets(DynTypedNode::create(L.getType()),
|
2021-01-18 15:58:43 +08:00
|
|
|
DeclRelation::Alias, Resolver)};
|
2019-09-25 20:40:22 +08:00
|
|
|
}
|
|
|
|
|
2020-01-21 18:50:57 +08:00
|
|
|
void VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
|
|
|
|
Ref = ReferenceLoc{NestedNameSpecifierLoc(),
|
|
|
|
TL.getNameLoc(),
|
|
|
|
/*IsDecl=*/false,
|
|
|
|
{TL.getDecl()}};
|
|
|
|
}
|
|
|
|
|
2019-09-25 20:40:22 +08:00
|
|
|
void VisitDependentTemplateSpecializationTypeLoc(
|
|
|
|
DependentTemplateSpecializationTypeLoc L) {
|
2021-01-18 15:58:43 +08:00
|
|
|
Ref = ReferenceLoc{L.getQualifierLoc(), L.getTemplateNameLoc(),
|
|
|
|
/*IsDecl=*/false,
|
|
|
|
explicitReferenceTargets(
|
|
|
|
DynTypedNode::create(L.getType()), {}, Resolver)};
|
2019-09-25 20:40:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisitDependentNameTypeLoc(DependentNameTypeLoc L) {
|
2021-01-18 15:58:43 +08:00
|
|
|
Ref = ReferenceLoc{L.getQualifierLoc(), L.getNameLoc(), /*IsDecl=*/false,
|
|
|
|
explicitReferenceTargets(
|
|
|
|
DynTypedNode::create(L.getType()), {}, Resolver)};
|
2019-09-25 20:40:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisitTypedefTypeLoc(TypedefTypeLoc L) {
|
2019-10-18 20:07:19 +08:00
|
|
|
Ref = ReferenceLoc{NestedNameSpecifierLoc(),
|
|
|
|
L.getNameLoc(),
|
|
|
|
/*IsDecl=*/false,
|
|
|
|
{L.getTypedefNameDecl()}};
|
2019-09-25 20:40:22 +08:00
|
|
|
}
|
2021-02-28 05:08:07 +08:00
|
|
|
|
|
|
|
void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc L) {
|
|
|
|
Ref = ReferenceLoc{NestedNameSpecifierLoc(),
|
|
|
|
L.getNameLoc(),
|
|
|
|
/*IsDecl=*/false,
|
|
|
|
{L.getIFaceDecl()}};
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: add references to protocols in ObjCObjectTypeLoc and maybe
|
|
|
|
// ObjCObjectPointerTypeLoc.
|
2019-09-25 20:40:22 +08:00
|
|
|
};
|
|
|
|
|
2021-01-18 15:58:43 +08:00
|
|
|
Visitor V{Resolver};
|
2019-09-25 20:40:22 +08:00
|
|
|
V.Visit(L.getUnqualifiedLoc());
|
2019-10-18 20:07:19 +08:00
|
|
|
if (!V.Ref)
|
|
|
|
return {};
|
|
|
|
return {*V.Ref};
|
2019-09-25 20:40:22 +08:00
|
|
|
}
|
|
|
|
|
2019-12-16 17:33:56 +08:00
|
|
|
class ExplicitReferenceCollector
|
|
|
|
: public RecursiveASTVisitor<ExplicitReferenceCollector> {
|
2019-09-25 20:40:22 +08:00
|
|
|
public:
|
2021-01-18 15:58:43 +08:00
|
|
|
ExplicitReferenceCollector(llvm::function_ref<void(ReferenceLoc)> Out,
|
|
|
|
const HeuristicResolver *Resolver)
|
|
|
|
: Out(Out), Resolver(Resolver) {
|
2019-09-25 20:40:22 +08:00
|
|
|
assert(Out);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitTypeLoc(TypeLoc TTL) {
|
2020-10-20 23:21:38 +08:00
|
|
|
if (TypeLocsToSkip.count(TTL.getBeginLoc()))
|
2019-09-25 20:40:22 +08:00
|
|
|
return true;
|
|
|
|
visitNode(DynTypedNode::create(TTL));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TraverseElaboratedTypeLoc(ElaboratedTypeLoc L) {
|
|
|
|
// ElaboratedTypeLoc will reports information for its inner type loc.
|
|
|
|
// Otherwise we loose information about inner types loc's qualifier.
|
|
|
|
TypeLoc Inner = L.getNamedTypeLoc().getUnqualifiedLoc();
|
2020-10-20 23:21:38 +08:00
|
|
|
TypeLocsToSkip.insert(Inner.getBeginLoc());
|
2019-09-25 20:40:22 +08:00
|
|
|
return RecursiveASTVisitor::TraverseElaboratedTypeLoc(L);
|
|
|
|
}
|
|
|
|
|
2020-05-19 06:45:27 +08:00
|
|
|
bool VisitStmt(Stmt *S) {
|
|
|
|
visitNode(DynTypedNode::create(*S));
|
2019-09-25 20:40:22 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-01-10 23:27:12 +08:00
|
|
|
bool TraverseOpaqueValueExpr(OpaqueValueExpr *OVE) {
|
|
|
|
visitNode(DynTypedNode::create(*OVE));
|
|
|
|
// Not clear why the source expression is skipped by default...
|
|
|
|
// FIXME: can we just make RecursiveASTVisitor do this?
|
|
|
|
return RecursiveASTVisitor::TraverseStmt(OVE->getSourceExpr());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TraversePseudoObjectExpr(PseudoObjectExpr *POE) {
|
|
|
|
visitNode(DynTypedNode::create(*POE));
|
|
|
|
// Traverse only the syntactic form to find the *written* references.
|
|
|
|
// (The semantic form also contains lots of duplication)
|
|
|
|
return RecursiveASTVisitor::TraverseStmt(POE->getSyntacticForm());
|
|
|
|
}
|
|
|
|
|
2019-10-01 18:02:23 +08:00
|
|
|
// We re-define Traverse*, since there's no corresponding Visit*.
|
|
|
|
// TemplateArgumentLoc is the only way to get locations for references to
|
|
|
|
// template template parameters.
|
|
|
|
bool TraverseTemplateArgumentLoc(TemplateArgumentLoc A) {
|
|
|
|
switch (A.getArgument().getKind()) {
|
|
|
|
case TemplateArgument::Template:
|
|
|
|
case TemplateArgument::TemplateExpansion:
|
|
|
|
reportReference(ReferenceLoc{A.getTemplateQualifierLoc(),
|
|
|
|
A.getTemplateNameLoc(),
|
2021-03-02 22:53:55 +08:00
|
|
|
/*IsDecl=*/false,
|
|
|
|
{A.getArgument()
|
|
|
|
.getAsTemplateOrTemplatePattern()
|
|
|
|
.getAsTemplateDecl()}},
|
2019-10-01 18:02:23 +08:00
|
|
|
DynTypedNode::create(A.getArgument()));
|
|
|
|
break;
|
|
|
|
case TemplateArgument::Declaration:
|
|
|
|
break; // FIXME: can this actually happen in TemplateArgumentLoc?
|
|
|
|
case TemplateArgument::Integral:
|
|
|
|
case TemplateArgument::Null:
|
|
|
|
case TemplateArgument::NullPtr:
|
|
|
|
break; // no references.
|
|
|
|
case TemplateArgument::Pack:
|
|
|
|
case TemplateArgument::Type:
|
|
|
|
case TemplateArgument::Expression:
|
|
|
|
break; // Handled by VisitType and VisitExpression.
|
|
|
|
};
|
|
|
|
return RecursiveASTVisitor::TraverseTemplateArgumentLoc(A);
|
|
|
|
}
|
|
|
|
|
2019-09-25 20:40:22 +08:00
|
|
|
bool VisitDecl(Decl *D) {
|
|
|
|
visitNode(DynTypedNode::create(*D));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have to use Traverse* because there is no corresponding Visit*.
|
|
|
|
bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc L) {
|
|
|
|
if (!L.getNestedNameSpecifier())
|
|
|
|
return true;
|
|
|
|
visitNode(DynTypedNode::create(L));
|
|
|
|
// Inner type is missing information about its qualifier, skip it.
|
|
|
|
if (auto TL = L.getTypeLoc())
|
2020-10-20 23:21:38 +08:00
|
|
|
TypeLocsToSkip.insert(TL.getBeginLoc());
|
2019-09-25 20:40:22 +08:00
|
|
|
return RecursiveASTVisitor::TraverseNestedNameSpecifierLoc(L);
|
|
|
|
}
|
|
|
|
|
2019-10-21 16:11:30 +08:00
|
|
|
bool TraverseConstructorInitializer(CXXCtorInitializer *Init) {
|
|
|
|
visitNode(DynTypedNode::create(*Init));
|
|
|
|
return RecursiveASTVisitor::TraverseConstructorInitializer(Init);
|
|
|
|
}
|
|
|
|
|
2019-09-25 20:40:22 +08:00
|
|
|
private:
|
|
|
|
/// Obtain information about a reference directly defined in \p N. Does not
|
|
|
|
/// recurse into child nodes, e.g. do not expect references for constructor
|
|
|
|
/// initializers
|
|
|
|
///
|
|
|
|
/// Any of the fields in the returned structure can be empty, but not all of
|
|
|
|
/// them, e.g.
|
|
|
|
/// - for implicitly generated nodes (e.g. MemberExpr from range-based-for),
|
|
|
|
/// source location information may be missing,
|
|
|
|
/// - for dependent code, targets may be empty.
|
|
|
|
///
|
|
|
|
/// (!) For the purposes of this function declarations are not considered to
|
|
|
|
/// be references. However, declarations can have references inside them,
|
|
|
|
/// e.g. 'namespace foo = std' references namespace 'std' and this
|
|
|
|
/// function will return the corresponding reference.
|
2020-12-10 20:36:35 +08:00
|
|
|
llvm::SmallVector<ReferenceLoc> explicitReference(DynTypedNode N) {
|
2019-09-25 20:40:22 +08:00
|
|
|
if (auto *D = N.get<Decl>())
|
2021-01-18 15:58:43 +08:00
|
|
|
return refInDecl(D, Resolver);
|
2020-05-19 06:45:27 +08:00
|
|
|
if (auto *S = N.get<Stmt>())
|
2021-01-18 15:58:43 +08:00
|
|
|
return refInStmt(S, Resolver);
|
2019-10-31 18:58:57 +08:00
|
|
|
if (auto *NNSL = N.get<NestedNameSpecifierLoc>()) {
|
|
|
|
// (!) 'DeclRelation::Alias' ensures we do not loose namespace aliases.
|
|
|
|
return {ReferenceLoc{
|
|
|
|
NNSL->getPrefix(), NNSL->getLocalBeginLoc(), false,
|
|
|
|
explicitReferenceTargets(
|
|
|
|
DynTypedNode::create(*NNSL->getNestedNameSpecifier()),
|
2021-01-18 15:58:43 +08:00
|
|
|
DeclRelation::Alias, Resolver)}};
|
2019-10-31 18:58:57 +08:00
|
|
|
}
|
2019-09-25 20:40:22 +08:00
|
|
|
if (const TypeLoc *TL = N.get<TypeLoc>())
|
2021-01-18 15:58:43 +08:00
|
|
|
return refInTypeLoc(*TL, Resolver);
|
2019-09-25 20:40:22 +08:00
|
|
|
if (const CXXCtorInitializer *CCI = N.get<CXXCtorInitializer>()) {
|
2019-10-21 16:11:30 +08:00
|
|
|
// Other type initializers (e.g. base initializer) are handled by visiting
|
|
|
|
// the typeLoc.
|
|
|
|
if (CCI->isAnyMemberInitializer()) {
|
|
|
|
return {ReferenceLoc{NestedNameSpecifierLoc(),
|
|
|
|
CCI->getMemberLocation(),
|
|
|
|
/*IsDecl=*/false,
|
|
|
|
{CCI->getAnyMember()}}};
|
|
|
|
}
|
2019-09-25 20:40:22 +08:00
|
|
|
}
|
|
|
|
// We do not have location information for other nodes (QualType, etc)
|
2019-10-18 20:07:19 +08:00
|
|
|
return {};
|
2019-09-25 20:40:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void visitNode(DynTypedNode N) {
|
2021-03-02 22:53:55 +08:00
|
|
|
for (auto &R : explicitReference(N))
|
|
|
|
reportReference(std::move(R), N);
|
2019-10-01 18:02:23 +08:00
|
|
|
}
|
|
|
|
|
2021-03-02 22:53:55 +08:00
|
|
|
void reportReference(ReferenceLoc &&Ref, DynTypedNode N) {
|
|
|
|
// Strip null targets that can arise from invalid code.
|
|
|
|
// (This avoids having to check for null everywhere we insert)
|
|
|
|
llvm::erase_value(Ref.Targets, nullptr);
|
2019-09-25 20:40:22 +08:00
|
|
|
// Our promise is to return only references from the source code. If we lack
|
|
|
|
// location information, skip these nodes.
|
|
|
|
// Normally this should not happen in practice, unless there are bugs in the
|
|
|
|
// traversals or users started the traversal at an implicit node.
|
2019-10-01 18:02:23 +08:00
|
|
|
if (Ref.NameLoc.isInvalid()) {
|
2019-09-25 20:40:22 +08:00
|
|
|
dlog("invalid location at node {0}", nodeToString(N));
|
|
|
|
return;
|
|
|
|
}
|
2019-10-01 18:02:23 +08:00
|
|
|
Out(Ref);
|
2019-09-25 20:40:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::function_ref<void(ReferenceLoc)> Out;
|
2021-01-18 15:58:43 +08:00
|
|
|
const HeuristicResolver *Resolver;
|
2019-09-25 20:40:22 +08:00
|
|
|
/// TypeLocs starting at these locations must be skipped, see
|
|
|
|
/// TraverseElaboratedTypeSpecifierLoc for details.
|
2020-10-20 23:21:38 +08:00
|
|
|
llvm::DenseSet<SourceLocation> TypeLocsToSkip;
|
2019-09-25 20:40:22 +08:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2019-09-25 23:44:26 +08:00
|
|
|
void findExplicitReferences(const Stmt *S,
|
2021-01-18 15:58:43 +08:00
|
|
|
llvm::function_ref<void(ReferenceLoc)> Out,
|
|
|
|
const HeuristicResolver *Resolver) {
|
2019-09-25 20:40:22 +08:00
|
|
|
assert(S);
|
2021-01-18 15:58:43 +08:00
|
|
|
ExplicitReferenceCollector(Out, Resolver).TraverseStmt(const_cast<Stmt *>(S));
|
2019-09-25 20:40:22 +08:00
|
|
|
}
|
2019-09-25 23:44:26 +08:00
|
|
|
void findExplicitReferences(const Decl *D,
|
2021-01-18 15:58:43 +08:00
|
|
|
llvm::function_ref<void(ReferenceLoc)> Out,
|
|
|
|
const HeuristicResolver *Resolver) {
|
2019-09-25 20:40:22 +08:00
|
|
|
assert(D);
|
2021-01-18 15:58:43 +08:00
|
|
|
ExplicitReferenceCollector(Out, Resolver).TraverseDecl(const_cast<Decl *>(D));
|
2019-09-25 20:40:22 +08:00
|
|
|
}
|
[clangd] Implement semantic highlightings via findExplicitReferences
Summary:
To keep the logic of finding locations of interesting AST nodes in one
place.
The advantage is better coverage of various AST nodes, both now and in
the future: as new nodes get added to `findExplicitReferences`, semantic
highlighting will automatically pick them up.
The drawback of this change is that we have to traverse declarations
inside our file twice in order to highlight dependent names, 'auto'
and 'decltype'. Hopefully, this should not affect the actual latency
too much, most time should be spent in building the AST and not
traversing it.
Reviewers: hokein
Reviewed By: hokein
Subscribers: nridge, merge_guards_bot, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D69673
2019-11-06 02:06:12 +08:00
|
|
|
void findExplicitReferences(const ASTContext &AST,
|
2021-01-18 15:58:43 +08:00
|
|
|
llvm::function_ref<void(ReferenceLoc)> Out,
|
|
|
|
const HeuristicResolver *Resolver) {
|
|
|
|
ExplicitReferenceCollector(Out, Resolver)
|
|
|
|
.TraverseAST(const_cast<ASTContext &>(AST));
|
[clangd] Implement semantic highlightings via findExplicitReferences
Summary:
To keep the logic of finding locations of interesting AST nodes in one
place.
The advantage is better coverage of various AST nodes, both now and in
the future: as new nodes get added to `findExplicitReferences`, semantic
highlighting will automatically pick them up.
The drawback of this change is that we have to traverse declarations
inside our file twice in order to highlight dependent names, 'auto'
and 'decltype'. Hopefully, this should not affect the actual latency
too much, most time should be spent in building the AST and not
traversing it.
Reviewers: hokein
Reviewed By: hokein
Subscribers: nridge, merge_guards_bot, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D69673
2019-11-06 02:06:12 +08:00
|
|
|
}
|
2019-09-25 20:40:22 +08:00
|
|
|
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, DeclRelation R) {
|
|
|
|
switch (R) {
|
|
|
|
#define REL_CASE(X) \
|
|
|
|
case DeclRelation::X: \
|
|
|
|
return OS << #X;
|
|
|
|
REL_CASE(Alias);
|
|
|
|
REL_CASE(Underlying);
|
|
|
|
REL_CASE(TemplateInstantiation);
|
|
|
|
REL_CASE(TemplatePattern);
|
|
|
|
#undef REL_CASE
|
2019-09-03 23:02:46 +08:00
|
|
|
}
|
2019-09-03 21:05:13 +08:00
|
|
|
llvm_unreachable("Unhandled DeclRelation enum");
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
}
|
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, DeclRelationSet RS) {
|
|
|
|
const char *Sep = "";
|
|
|
|
for (unsigned I = 0; I < RS.S.size(); ++I) {
|
|
|
|
if (RS.S.test(I)) {
|
|
|
|
OS << Sep << static_cast<DeclRelation>(I);
|
|
|
|
Sep = "|";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2019-09-25 20:40:22 +08:00
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, ReferenceLoc R) {
|
|
|
|
// note we cannot print R.NameLoc without a source manager.
|
|
|
|
OS << "targets = {";
|
|
|
|
bool First = true;
|
|
|
|
for (const NamedDecl *T : R.Targets) {
|
|
|
|
if (!First)
|
|
|
|
OS << ", ";
|
|
|
|
else
|
|
|
|
First = false;
|
|
|
|
OS << printQualifiedName(*T) << printTemplateSpecializationArgs(*T);
|
|
|
|
}
|
|
|
|
OS << "}";
|
|
|
|
if (R.Qualifier) {
|
|
|
|
OS << ", qualifier = '";
|
|
|
|
R.Qualifier.getNestedNameSpecifier()->print(OS,
|
|
|
|
PrintingPolicy(LangOptions()));
|
|
|
|
OS << "'";
|
|
|
|
}
|
2019-10-18 20:07:19 +08:00
|
|
|
if (R.IsDecl)
|
|
|
|
OS << ", decl";
|
2019-09-25 20:40:22 +08:00
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
[clangd] Add targetDecl(), which determines what declaration an AST node refers to.
Summary:
This is the first part of an effort to "unbundle" our libIndex use into separate
concerns (AST traversal, token<->node mapping, node<->decl mapping,
decl<->decl relationshipes).
Currently, clangd relies on libIndex to associate tokens, AST nodes, and decls.
This leads to rather convoluted implementations of e.g. hover and
extract-function, which are not naturally thought of as indexing applications.
The idea is that by decoupling different concerns, we make them easier
to use, test, and combine, and more efficient when only one part is needed.
There are some synergies between e.g. traversal and finding
relationships between decls, hopefully the benefits outweight these.
Reviewers: kadircet, ilya-biryukov
Subscribers: mgorny, MaskRay, jkorous, arphaman, jfb, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66751
llvm-svn: 370746
2019-09-03 19:35:50 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|