Skip to content

Commit 710aa69

Browse files
committed
fix variable template specializations, misc ASTVisitor work
1 parent 0c92edd commit 710aa69

File tree

3 files changed

+78
-32
lines changed

3 files changed

+78
-32
lines changed

Diff for: source/AST/ASTVisitor.cpp

+56-23
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ ASTVisitor(
159159
bool
160160
ASTVisitor::
161161
extractSymbolID(
162-
SymbolID& id, const NamedDecl* D)
162+
const Decl* D,
163+
SymbolID& id)
163164
{
164165
usr_.clear();
165166
if(index::generateUSRForDecl(D, usr_))
@@ -169,6 +170,16 @@ extractSymbolID(
169170
return true;
170171
}
171172

173+
SymbolID
174+
ASTVisitor::
175+
extractSymbolID(
176+
const Decl* D)
177+
{
178+
SymbolID id = SymbolID::zero;
179+
extractSymbolID(D, id);
180+
return id;
181+
}
182+
172183
bool
173184
ASTVisitor::
174185
shouldSerializeInfo(
@@ -224,7 +235,7 @@ getParentNamespaces(
224235
{
225236
Namespace = N->getNameAsString();
226237
}
227-
extractSymbolID(id, N);
238+
extractSymbolID(N, id);
228239
Namespaces.emplace_back(
229240
id,
230241
Namespace,
@@ -259,15 +270,15 @@ getParentNamespaces(
259270
}
260271
Assert(TD);
261272

262-
extractSymbolID(id, TD);
273+
extractSymbolID(TD, id);
263274
Namespaces.emplace_back(
264275
id,
265276
N->getNameAsString(),
266277
InfoType::IT_record);
267278
}
268279
else if(const auto* N = dyn_cast<RecordDecl>(DC))
269280
{
270-
extractSymbolID(id, N);
281+
extractSymbolID(N, id);
271282
Namespaces.emplace_back(
272283
id,
273284
N->getNameAsString(),
@@ -283,7 +294,7 @@ getParentNamespaces(
283294
{
284295
N = S->getTemplateInstantiationPattern();
285296
}
286-
extractSymbolID(id, N);
297+
extractSymbolID(N, id);
287298
Namespaces.emplace_back(
288299
id,
289300
N->getNameAsString(),
@@ -292,15 +303,15 @@ getParentNamespaces(
292303
#endif
293304
else if(const auto* N = dyn_cast<FunctionDecl>(DC))
294305
{
295-
extractSymbolID(id, N);
306+
extractSymbolID(N, id);
296307
Namespaces.emplace_back(
297308
id,
298309
N->getNameAsString(),
299310
InfoType::IT_function);
300311
}
301312
else if(const auto* N = dyn_cast<EnumDecl>(DC))
302313
{
303-
extractSymbolID(id, N);
314+
extractSymbolID(N, id);
304315
Namespaces.emplace_back(
305316
id,
306317
N->getNameAsString(),
@@ -315,7 +326,7 @@ getParentNamespaces(
315326
// (because this means it's in the global namespace).
316327
// Also if its outermost namespace is a record because
317328
// that record matches the previous condition mentioned.
318-
if((Namespaces.empty() && isa<RecordDecl>(D)) ||
329+
if((Namespaces.empty() && isa<CXXRecordDecl>(D)) ||
319330
(!Namespaces.empty() && Namespaces.back().RefType == InfoType::IT_record))
320331
{
321332
Namespaces.emplace_back(
@@ -403,18 +414,17 @@ ASTVisitor::
403414
getTypeInfoForType(
404415
QualType T)
405416
{
406-
407417
SymbolID id = SymbolID::zero;
408418
if(const TagDecl* TD = getTagDeclForType(T))
409419
{
410420
InfoType IT;
411421
if(isa<EnumDecl>(TD))
412422
IT = InfoType::IT_enum;
413-
else if(isa<RecordDecl>(TD))
423+
else if(isa<CXXRecordDecl>(TD))
414424
IT = InfoType::IT_record;
415425
else
416426
IT = InfoType::IT_default;
417-
extractSymbolID(id, TD);
427+
extractSymbolID(TD, id);
418428
return TypeInfo(Reference(
419429
id, TD->getNameAsString(), IT));
420430
}
@@ -533,7 +543,7 @@ parseTemplateArgs(
533543
{
534544
if(auto* MT = primary->getInstantiatedFromMemberTemplate())
535545
primary = MT;
536-
extractSymbolID(I.Primary.emplace(), primary);
546+
extractSymbolID(primary, I.Primary.emplace());
537547
}
538548
// KRYSTIAN NOTE: when this is a partial specialization, we could use
539549
// ClassTemplatePartialSpecializationDecl::getTemplateArgsAsWritten
@@ -557,7 +567,11 @@ parseTemplateArgs(
557567
{
558568
if(auto* MT = primary->getInstantiatedFromMemberTemplate())
559569
primary = MT;
560-
extractSymbolID(I.Primary.emplace(), primary);
570+
// unlike function and class templates, the USR generated
571+
// for variable templates differs from that of the VarDecl
572+
// returned by getTemplatedDecl. this might be a clang bug.
573+
// the USR of the templated VarDecl seems to be the correct one.
574+
extractSymbolID(primary->getTemplatedDecl(), I.Primary.emplace());
561575
}
562576

563577
// spec->getTemplateArgsInfo()
@@ -588,7 +602,7 @@ parseTemplateArgs(
588602
{
589603
if(auto* MT = primary->getInstantiatedFromMemberTemplate())
590604
primary = MT;
591-
extractSymbolID(I.Primary.emplace(), primary);
605+
extractSymbolID(primary, I.Primary.emplace());
592606
}
593607
if(auto* args = spec->TemplateArguments)
594608
{
@@ -828,7 +842,7 @@ parseEnumerators(
828842
bool
829843
ASTVisitor::
830844
shouldExtract(
831-
Decl const* D)
845+
const Decl* D)
832846
{
833847
namespace path = llvm::sys::path;
834848

@@ -881,12 +895,12 @@ bool
881895
ASTVisitor::
882896
extractInfo(
883897
Info& I,
884-
NamedDecl const* D)
898+
const NamedDecl* D)
885899
{
886900
bool anonymous = getParentNamespaces(I.Namespace, D);
887901
if(! shouldSerializeInfo(PublicOnly, anonymous, D))
888902
return false;
889-
if(! extractSymbolID(I.id, D))
903+
if(! extractSymbolID(D, I.id))
890904
return false;
891905
I.Name = D->getNameAsString();
892906
parseRawComment(I.javadoc, D, R_);
@@ -915,16 +929,16 @@ extractBases(
915929
if(auto const* Ty = B.getType()->getAs<TemplateSpecializationType>())
916930
{
917931
TemplateDecl const* TD = Ty->getTemplateName().getAsTemplateDecl();
918-
extractSymbolID(id, TD);
932+
extractSymbolID(TD, id);
919933
I.Bases.emplace_back(
920934
id,
921935
getTypeAsString(B.getType()),
922936
getAccessFromSpecifier(B.getAccessSpecifier()),
923937
isVirtual);
924938
}
925-
else if(RecordDecl const* P = getCXXRecordDeclForType(B.getType()))
939+
else if(CXXRecordDecl const* P = getCXXRecordDeclForType(B.getType()))
926940
{
927-
extractSymbolID(id, P);
941+
extractSymbolID(P, id);
928942
I.Bases.emplace_back(
929943
id,
930944
P->getNameAsString(),
@@ -1157,10 +1171,10 @@ buildFriend(
11571171
getParent(id, D);
11581172
#else
11591173
const DeclContext* DC = D->getDeclContext();
1160-
const auto* RD = dyn_cast<RecordDecl>(DC);
1174+
const auto* RD = dyn_cast<CXXRecordDecl>(DC);
11611175
Assert(RD && "the semantic DeclContext of a FriendDecl must be a class");
11621176
RecordInfo P;
1163-
extractSymbolID(P.id, RD);
1177+
extractSymbolID(RD, P.id);
11641178

11651179
#if 0
11661180
SymbolID id_;
@@ -1677,7 +1691,26 @@ TraverseDecl(
16771691
#if 1
16781692
if(D->isImplicit())
16791693
return true;
1680-
#else
1694+
#elif 1
1695+
{
1696+
1697+
if(D->isImplicit())
1698+
{
1699+
if(! encountered_explicit_)
1700+
return true;
1701+
print_debug("{:<64}{:<32} ID = {}\n", std::format(
1702+
"{}(implicit) {}", indent(context_depth_), D->getDeclKindName()),
1703+
getDeclName(D), extractSymbolID(D));
1704+
return true;
1705+
}
1706+
encountered_explicit_ = true;
1707+
1708+
print_debug("{:<64}{:<32} ID = {}\n", std::format(
1709+
"{}{}", indent(context_depth_), D->getDeclKindName()),
1710+
getDeclName(D), extractSymbolID(D));
1711+
print_debug("{} {}\n", indent(context_depth_), usr_.c_str());
1712+
}
1713+
#elif 1
16811714
{
16821715
auto specs = getNumSpecializations(D);
16831716
std::string specs_str = specs.has_value() ?

Diff for: source/AST/ASTVisitor.hpp

+18-5
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ class ASTVisitor
6565
clang::SourceLocation::UIntTy,
6666
FileFilter> fileFilter_;
6767

68-
std::size_t context_depth_ = 0;
6968
Sema* sema_;
7069

70+
std::size_t context_depth_ = 0;
71+
bool encountered_explicit_ = false;
72+
7173
public:
7274
ASTVisitor(
7375
tooling::ExecutionContext& ex,
@@ -77,8 +79,12 @@ class ASTVisitor
7779

7880
bool
7981
extractSymbolID(
80-
SymbolID& id,
81-
const NamedDecl* D);
82+
const Decl* D,
83+
SymbolID& id);
84+
85+
SymbolID
86+
extractSymbolID(
87+
const Decl* D);
8288

8389
bool shouldSerializeInfo(
8490
bool PublicOnly,
@@ -90,6 +96,15 @@ class ASTVisitor
9096
llvm::SmallVector<Reference, 4>& Namespaces,
9197
const Decl* D);
9298

99+
bool
100+
shouldExtract(
101+
const Decl* D);
102+
103+
bool
104+
extractInfo(
105+
Info& I,
106+
const NamedDecl* D);
107+
93108
int
94109
getLine(
95110
const NamedDecl* D) const;
@@ -174,8 +189,6 @@ class ASTVisitor
174189
EnumInfo& I,
175190
const EnumDecl* D);
176191

177-
bool shouldExtract(Decl const* D);
178-
bool extractInfo(Info& I, NamedDecl const* D);
179192
void extractBases(RecordInfo& I, CXXRecordDecl* D);
180193

181194
template<class DeclTy>

Diff for: test-files/old-tests/var-template.xml

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
</variable>
1515
<variable name="C" id="Wtlw7VEK38lEEBeqNoDOjTkJE+U=">
1616
<file path="var-template.cpp" line="15" class="def"/>
17-
<template class="explicit" id="kMNXdSEDYBD+T4RYbkq3ekR+jUU=">
17+
<template class="explicit" id="629jUQifhGrj15iClsD5BexWpWQ=">
1818
<targ value="int"/>
1919
</template>
2020
<attr id="storage-class" name="static" value="2"/>
2121
<type name="unsigned int"/>
2222
</variable>
2323
<variable name="C" id="wvoZkG3eTMaiFAf971AFdvmHCDE=">
2424
<file path="var-template.cpp" line="18" class="def"/>
25-
<template class="partial" id="kMNXdSEDYBD+T4RYbkq3ekR+jUU=">
25+
<template class="partial" id="629jUQifhGrj15iClsD5BexWpWQ=">
2626
<tparam name="T" class="type"/>
2727
<targ value="T *"/>
2828
</template>
@@ -32,7 +32,7 @@
3232
</struct>
3333
<variable name="A" id="FsEwA9OFNGH4A/cxXIoOOa+ZQg8=">
3434
<file path="var-template.cpp" line="7" class="def"/>
35-
<template class="partial" id="z3eMoJQlHlg0acXVyn1pwg9Ktys=">
35+
<template class="partial" id="hGK5kng8FRozz2kCeW+NWHngZKE=">
3636
<tparam name="T" class="type"/>
3737
<targ value="T &amp;"/>
3838
</template>
@@ -47,7 +47,7 @@
4747
</variable>
4848
<variable name="A" id="jrW9aLj8cqTteNoZmH97vOW7csQ=">
4949
<file path="var-template.cpp" line="4" class="def"/>
50-
<template class="explicit" id="z3eMoJQlHlg0acXVyn1pwg9Ktys=">
50+
<template class="explicit" id="hGK5kng8FRozz2kCeW+NWHngZKE=">
5151
<targ value="void"/>
5252
</template>
5353
<type name="unsigned int"/>

0 commit comments

Comments
 (0)