Skip to content

Commit 022cce3

Browse files
committed
do not traverse implicit instantiations
1 parent 1481de8 commit 022cce3

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

source/AST/ASTVisitor.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,19 @@ shouldExtract(
569569
return true;
570570
}
571571

572+
template<typename DeclTy>
573+
bool
574+
ASTVisitor::
575+
isImplicitInstantiation(
576+
const DeclTy* D)
577+
{
578+
if constexpr(requires { { D->getTemplateSpecializationKind() } ->
579+
std::same_as<TemplateSpecializationKind>; })
580+
return D->getTemplateSpecializationKind() ==
581+
TemplateSpecializationKind::TSK_ImplicitInstantiation;
582+
else
583+
return false;
584+
}
572585

573586
bool
574587
ASTVisitor::
@@ -1185,6 +1198,10 @@ Traverse(
11851198
AccessSpecifier A,
11861199
std::unique_ptr<TemplateInfo>&& Template = nullptr)
11871200
{
1201+
// implicit instantiations should *only* have their Info
1202+
// extracted via buildSpecialization calls from getParentNamespaces
1203+
if(isImplicitInstantiation(D))
1204+
return true;
11881205
if(! shouldExtract(D))
11891206
return true;
11901207

@@ -1241,6 +1258,13 @@ Traverse(
12411258
AccessSpecifier A,
12421259
std::unique_ptr<TemplateInfo>&& Template = nullptr)
12431260
{
1261+
// implicitly instantiated definitions of non-inline
1262+
// static data members of class templates are added to
1263+
// the end of the TU DeclContext. Decl::isImplicit returns
1264+
// false for these VarDecls, so we must check whether this
1265+
// variable is an implicit instantiation & skip such Decls
1266+
if(isImplicitInstantiation(D))
1267+
return true;
12441268
if(! shouldExtract(D))
12451269
return true;
12461270

@@ -1259,6 +1283,8 @@ Traverse(
12591283
AccessSpecifier A,
12601284
std::unique_ptr<TemplateInfo>&& Template = nullptr)
12611285
{
1286+
if(isImplicitInstantiation(D))
1287+
return true;
12621288
if(! shouldExtract(D))
12631289
return true;
12641290

@@ -1278,6 +1304,8 @@ Traverse(
12781304
AccessSpecifier A,
12791305
std::unique_ptr<TemplateInfo>&& Template = nullptr)
12801306
{
1307+
if(isImplicitInstantiation(D))
1308+
return true;
12811309
if(! shouldExtract(D))
12821310
return true;
12831311

@@ -1303,6 +1331,8 @@ Traverse(
13031331
if(DN)
13041332
s = DN.getAsString();
13051333
*/
1334+
if(isImplicitInstantiation(D))
1335+
return true;
13061336
if(! shouldExtract(D))
13071337
return true;
13081338

@@ -1321,6 +1351,8 @@ Traverse(
13211351
AccessSpecifier A,
13221352
std::unique_ptr<TemplateInfo>&& Template = nullptr)
13231353
{
1354+
if(isImplicitInstantiation(D))
1355+
return true;
13241356
if(! shouldExtract(D))
13251357
return true;
13261358

@@ -1339,6 +1371,8 @@ Traverse(
13391371
AccessSpecifier A,
13401372
std::unique_ptr<TemplateInfo>&& Template = nullptr)
13411373
{
1374+
if(isImplicitInstantiation(D))
1375+
return true;
13421376
if(! shouldExtract(D))
13431377
return true;
13441378

@@ -1353,6 +1387,8 @@ Traverse(
13531387
CXXDestructorDecl* D,
13541388
AccessSpecifier A)
13551389
{
1390+
if(isImplicitInstantiation(D))
1391+
return true;
13561392
if(! shouldExtract(D))
13571393
return true;
13581394

@@ -1379,6 +1415,8 @@ Traverse(
13791415
EnumDecl* D,
13801416
AccessSpecifier A)
13811417
{
1418+
if(isImplicitInstantiation(D))
1419+
return true;
13821420
if(! shouldExtract(D))
13831421
return true;
13841422

source/AST/ASTVisitor.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ class ASTVisitor
8585
shouldExtract(
8686
const Decl* D);
8787

88+
template<typename DeclTy>
89+
bool
90+
isImplicitInstantiation(
91+
const DeclTy* D);
92+
8893
bool
8994
extractInfo(
9095
Info& I,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
template<typename T>
2+
struct A
3+
{
4+
static int v0;
5+
static int v1;
6+
static const int v2;
7+
static const int v3;
8+
static const int v4;
9+
10+
static inline int v5 = 5;
11+
static inline const int v6 = 6;
12+
static constexpr int v7 = 7;
13+
};
14+
15+
template<typename T>
16+
int A<T>::v0 = 0;
17+
18+
template<typename T>
19+
inline int A<T>::v1 = 1;
20+
21+
template<typename T>
22+
constexpr int A<T>::v2 = 2;
23+
24+
template<typename T>
25+
inline const int A<T>::v3 = 3;
26+
27+
template<typename T>
28+
const int A<T>::v4 = 4;
29+
30+
auto f()
31+
{
32+
return
33+
A<int>::v0 +
34+
A<int>::v1 +
35+
A<int>::v2 +
36+
A<int>::v3 +
37+
A<int>::v4 +
38+
A<int>::v5 +
39+
A<int>::v6 +
40+
A<int>::v7;
41+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<mrdox xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://github.com/cppalliance/mrdox/raw/develop/mrdox.rnc">
4+
<namespace name="">
5+
<template>
6+
<tparam name="T" class="type"/>
7+
<struct name="A" id="5tUSuMtQqtYE49jBjSYSWp0DJAM=">
8+
<file path="static-data-def.cpp" line="2" class="def"/>
9+
<variable name="v0" id="rW6h+mCfZLROHlYzwXFIUPN6oSE=">
10+
<file path="static-data-def.cpp" line="15" class="def"/>
11+
<file path="static-data-def.cpp" line="4"/>
12+
<attr id="storage-class" name="static" value="2"/>
13+
<type name="int"/>
14+
</variable>
15+
<variable name="v1" id="nkB0lMmDvMsu9GDrKr3Mzwi5g6I=">
16+
<file path="static-data-def.cpp" line="18" class="def"/>
17+
<file path="static-data-def.cpp" line="5"/>
18+
<attr id="storage-class" name="static" value="2"/>
19+
<type name="int"/>
20+
</variable>
21+
<variable name="v2" id="e48c97dC3AIWJHWE5T0yWVxezgg=">
22+
<file path="static-data-def.cpp" line="21" class="def"/>
23+
<file path="static-data-def.cpp" line="6"/>
24+
<attr id="storage-class" name="static" value="2"/>
25+
<type name="const int"/>
26+
</variable>
27+
<variable name="v3" id="Wx5S5oWv5o4nV7I+zFEKTvWl40g=">
28+
<file path="static-data-def.cpp" line="24" class="def"/>
29+
<file path="static-data-def.cpp" line="7"/>
30+
<attr id="storage-class" name="static" value="2"/>
31+
<type name="const int"/>
32+
</variable>
33+
<variable name="v4" id="6a2wBN4mHiuAAqV0gYgYQeqFNVM=">
34+
<file path="static-data-def.cpp" line="27" class="def"/>
35+
<file path="static-data-def.cpp" line="8"/>
36+
<attr id="storage-class" name="static" value="2"/>
37+
<type name="const int"/>
38+
</variable>
39+
<variable name="v5" id="Q96okKllwtE2Tg+t7HAkms6flbk=">
40+
<file path="static-data-def.cpp" line="10" class="def"/>
41+
<attr id="storage-class" name="static" value="2"/>
42+
<type name="int"/>
43+
</variable>
44+
<variable name="v6" id="0Xh4ypD2+Eipbqc5Z8n4fEkXZ8c=">
45+
<file path="static-data-def.cpp" line="11" class="def"/>
46+
<attr id="storage-class" name="static" value="2"/>
47+
<type name="const int"/>
48+
</variable>
49+
<variable name="v7" id="VCnmz8Cp9RtJzQQQ7yw57uzbQAY=">
50+
<file path="static-data-def.cpp" line="12" class="def"/>
51+
<attr id="storage-class" name="static" value="2"/>
52+
<type name="const int"/>
53+
</variable>
54+
</struct>
55+
</template>
56+
<function name="f" id="s6nsa+zVhpzzrN+yUVPP5rvdXqs=">
57+
<file path="static-data-def.cpp" line="30" class="def"/>
58+
<return type="int"/>
59+
</function>
60+
</namespace>
61+
</mrdox>

0 commit comments

Comments
 (0)