Skip to content

Commit e9aa5d2

Browse files
fix: Emit references for non-TagType specifiers (#317)
1 parent ad425a3 commit e9aa5d2

File tree

8 files changed

+107
-3
lines changed

8 files changed

+107
-3
lines changed

indexer/Indexer.cc

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,31 @@ void TuIndexer::saveNamespaceDecl(const clang::NamespaceDecl &namespaceDecl) {
530530
this->saveDefinition(symbol, startLoc, std::move(symbolInfo));
531531
}
532532

533+
void TuIndexer::trySaveTypeReference(const clang::Type *type,
534+
clang::SourceLocation loc) {
535+
if (!type) {
536+
return;
537+
}
538+
const clang::NamedDecl *namedDecl = nullptr;
539+
if (auto *typedefType = llvm::dyn_cast<clang::TypedefType>(type)) {
540+
namedDecl = typedefType->getDecl();
541+
} else if (auto *tagDecl = type->getAsTagDecl()) {
542+
namedDecl = tagDecl;
543+
} else if (auto *templateSpecializationType =
544+
llvm::dyn_cast<clang::TemplateSpecializationType>(type)) {
545+
if (auto *templateDecl =
546+
templateSpecializationType->getTemplateName().getAsTemplateDecl()) {
547+
namedDecl = templateDecl->getTemplatedDecl();
548+
}
549+
}
550+
if (!namedDecl) {
551+
return;
552+
}
553+
if (auto optSymbol = this->symbolFormatter.getNamedDeclSymbol(*namedDecl)) {
554+
this->saveReference(*optSymbol, loc);
555+
}
556+
}
557+
533558
void TuIndexer::saveNestedNameSpecifierLoc(
534559
const clang::NestedNameSpecifierLoc &argNameSpecLoc) {
535560
clang::NestedNameSpecifierLoc nameSpecLoc = argNameSpecLoc;
@@ -554,9 +579,7 @@ void TuIndexer::saveNestedNameSpecifierLoc(
554579
}
555580
case Kind::TypeSpec: {
556581
auto *type = nameSpec->getAsType();
557-
if (auto *tagDecl = type->getAsTagDecl()) {
558-
tryEmit(nameSpecLoc, *tagDecl);
559-
}
582+
this->trySaveTypeReference(type, nameSpecLoc.getLocalBeginLoc());
560583
break;
561584
}
562585
// FIXME(issue: https://github.com/sourcegraph/scip-clang/issues/109)

indexer/Indexer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class SourceManager;
4545
class TagDecl;
4646
class TagTypeLoc;
4747
class Token;
48+
class Type;
4849
} // namespace clang
4950

5051
namespace scip {
@@ -265,6 +266,9 @@ class TuIndexer final {
265266
void saveTagDecl(const clang::TagDecl &);
266267
void saveTagTypeLoc(const clang::TagTypeLoc &);
267268

269+
// Save a reference to a possibly-sugared type, without canonicalization.
270+
void trySaveTypeReference(const clang::Type *, clang::SourceLocation);
271+
268272
#define SAVE_EXPR(ExprName) \
269273
void save##ExprName##Expr(const clang::ExprName##Expr &);
270274
FOR_EACH_EXPR_TO_BE_INDEXED(SAVE_EXPR)

test/index/functions/ctors_dtors.snapshot.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
template <typename T>
2323
// ^ definition local 3
2424
typename remove_ref<T>::type&& move(T&& arg) {
25+
// ^^^^^^^^^^ reference [..] remove_ref#
2526
// ^^^^ definition [..] move(721d19cf58c53974).
2627
// ^ reference local 3
2728
// ^^^ definition local 4
2829
return static_cast<typename remove_ref<T>::type&&>(arg);
30+
// ^^^^^^^^^^ reference [..] remove_ref#
2931
// ^^^ reference local 4
3032
}
3133

test/index/types/bad_tagdecl.snapshot.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
// ^ definition local 4
3030
using enable_if_t = typename enable_if<B,T>::type;
3131
// ^^^^^^^^^^^ definition [..] enable_if_t#
32+
// ^^^^^^^^^ reference [..] enable_if#
3233

3334
template <typename T, typename Enable = void> struct MyTemplate { };
3435
// ^ definition local 5

test/index/types/templates.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,14 @@ void use_empty() {
4949
Empty<PointerType<void *>> z;
5050
RefPtr<int> w;
5151
}
52+
53+
template <typename T>
54+
struct M0 {
55+
using A = int;
56+
};
57+
58+
template <typename T>
59+
struct M1: M0<T> {
60+
using B = M0<T>;
61+
using B::A;
62+
};

test/index/types/templates.snapshot.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
// ^ definition local 13
6969
using RefPtr = typename PointerType<T &>::type;
7070
// ^^^^^^ definition [..] RefPtr#
71+
// ^^^^^^^^^^^ reference [..] PointerType#
7172

7273
using IntRefPtr = RefPtr<int>;
7374
// ^^^^^^^^^ definition [..] IntRefPtr#
@@ -77,10 +78,12 @@
7778
// ^ definition local 14
7879
using PtrPtr = typename PointerType<T *>::type;
7980
// ^^^^^^ definition [..] PtrPtr#
81+
// ^^^^^^^^^^^ reference [..] PointerType#
8082

8183
template <typename T, typename S = typename PointerType<T>::type>
8284
// ^ definition local 15
8385
// ^ definition local 16
86+
// ^^^^^^^^^^^ reference [..] PointerType#
8487
void specialized(T) {}
8588
// ^^^^^^^^^^^ definition [..] specialized(9b289cee16747614).
8689
// ^ reference local 15
@@ -111,3 +114,25 @@
111114
// ^^^^^^ reference [..] RefPtr#
112115
// ^ definition local 21
113116
}
117+
118+
template <typename T>
119+
// ^ definition local 22
120+
struct M0 {
121+
// ^^ definition [..] M0#
122+
using A = int;
123+
// ^ definition [..] M0#A#
124+
};
125+
126+
template <typename T>
127+
// ^ definition local 23
128+
struct M1: M0<T> {
129+
// ^^ definition [..] M1#
130+
// ^^ reference [..] M0#
131+
// ^ reference local 23
132+
using B = M0<T>;
133+
// ^ definition [..] M1#B#
134+
// ^^ reference [..] M0#
135+
// ^ reference local 23
136+
using B::A;
137+
// ^ reference [..] M1#B#
138+
};

test/index/types/types.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,12 @@ auto trailing_return_type() -> L {
124124
};
125125
return ignore_first("", L{});
126126
}
127+
128+
struct M0 {
129+
using A = int;
130+
};
131+
132+
struct M1: M0 {
133+
using B = M0;
134+
using B::A;
135+
};

test/index/types/types.snapshot.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,32 @@
344344
// ^ reference [..] trailing_return_type(693bfa61ed1914d5).$anonymous_type_4#`operator()`(dc97d1a1ce4cdab3).
345345
// ^ reference [..] L#
346346
}
347+
348+
struct M0 {
349+
// ^^ definition [..] M0#
350+
// documentation
351+
// | No documentation available.
352+
using A = int;
353+
// ^ definition [..] M0#A#
354+
// documentation
355+
// | No documentation available.
356+
};
357+
358+
struct M1: M0 {
359+
// ^^ definition [..] M1#
360+
// documentation
361+
// | No documentation available.
362+
// relation implementation [..] M0#
363+
// ^^ reference [..] M0#
364+
using B = M0;
365+
// ^ definition [..] M1#B#
366+
// documentation
367+
// | No documentation available.
368+
// ^^ reference [..] M0#
369+
using B::A;
370+
// ^ reference [..] M1#B#
371+
// ^ reference [..] M0#A#
372+
// ^ definition [..] M1#A#
373+
// documentation
374+
// | No documentation available.
375+
};

0 commit comments

Comments
 (0)