Skip to content

Commit c3dbded

Browse files
committed
fix(ast): prevent TU parent from including unmatched globals
Parent-based include-symbol propagation walked up to the translation unit, causing global symbols to be extracted even when include-symbols were restricted to namespaces. fix #1121
1 parent 34ea190 commit c3dbded

File tree

6 files changed

+176
-0
lines changed

6 files changed

+176
-0
lines changed

src/lib/AST/ASTVisitor.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3201,6 +3201,26 @@ checkSymbolFilters(clang::Decl const* D, bool const AllowParent)
32013201
else if (AllowParent)
32023202
{
32033203
clang::Decl const* P = getParent(D);
3204+
while (P)
3205+
{
3206+
// Only propagate inclusion from meaningful scopes. Translation units
3207+
// (and other non-scope wrappers like linkage specs) should not cause
3208+
// otherwise-unmatched globals to be extracted.
3209+
if (isa<clang::NamespaceDecl>(P) ||
3210+
isa<clang::RecordDecl>(P) ||
3211+
isa<clang::EnumDecl>(P))
3212+
{
3213+
break;
3214+
}
3215+
3216+
if (isa<clang::TranslationUnitDecl>(P))
3217+
{
3218+
P = nullptr;
3219+
break;
3220+
}
3221+
3222+
P = getParent(P);
3223+
}
32043224
if (P)
32053225
{
32063226
// 4) Parent symbols imply this symbol should be included
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
= Reference
2+
:mrdocs:
3+
4+
[#index]
5+
== Global namespace
6+
7+
=== Namespaces
8+
9+
[cols="1,4"]
10+
|===
11+
| Name| Description
12+
| link:#mrdocs[`mrdocs`]
13+
| Included namespace
14+
|===
15+
16+
[#mrdocs]
17+
== mrdocs
18+
19+
Included namespace
20+
21+
=== Types
22+
23+
[cols="1,4"]
24+
|===
25+
| Name| Description
26+
| link:#mrdocs-Foo[`Foo`]
27+
| Included record
28+
|===
29+
30+
[#mrdocs-Foo]
31+
== link:#mrdocs[mrdocs]::Foo
32+
33+
Included record
34+
35+
=== Synopsis
36+
37+
Declared in `&lt;global&hyphen;symbol&hyphen;excluded&period;cpp&gt;`
38+
39+
[source,cpp,subs="verbatim,replacements,macros,-callouts"]
40+
----
41+
struct Foo;
42+
----
43+
44+
45+
[.small]#Created with https://www.mrdocs.com[MrDocs]#
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Regression test for #1121: limiting include-symbols to a namespace should
2+
// not extract global symbols that do not match the pattern.
3+
struct Baz {};
4+
5+
/// Included namespace
6+
namespace mrdocs {
7+
/// Included record
8+
struct Foo {};
9+
} // namespace mrdocs
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<html lang="en">
2+
<head>
3+
<title>Reference</title>
4+
<meta charset="utf-8">
5+
</head>
6+
<body>
7+
<div>
8+
<h1>Reference</h1>
9+
<div>
10+
<div>
11+
<h2 id="index">
12+
Global Namespace<a class="mrdocs-anchor" href="#index" aria-label="Permalink">#</a>
13+
</h2>
14+
</div>
15+
<h2>
16+
Namespaces</h2>
17+
<table style="table-layout: fixed; width: 100%;">
18+
<thead>
19+
<tr>
20+
<th>Name</th><th>Description</th>
21+
</tr>
22+
</thead>
23+
<tbody>
24+
<tr>
25+
<td><a href="#mrdocs"><code>mrdocs</code></a> </td><td>Included namespace</td></tr>
26+
</tbody>
27+
</table>
28+
29+
</div>
30+
<div>
31+
<div>
32+
<h2 id="mrdocs">
33+
mrdocs<a class="mrdocs-anchor" href="#mrdocs" aria-label="Permalink">#</a>
34+
</h2>
35+
<div>
36+
<p>Included namespace</p>
37+
</div>
38+
</div>
39+
<h2>
40+
Types</h2>
41+
<table style="table-layout: fixed; width: 100%;">
42+
<thead>
43+
<tr>
44+
<th>Name</th><th>Description</th>
45+
</tr>
46+
</thead>
47+
<tbody>
48+
<tr>
49+
<td><a href="#mrdocs-Foo"><code>Foo</code></a> </td><td>Included record</td></tr>
50+
</tbody>
51+
</table>
52+
53+
</div>
54+
<div>
55+
<div>
56+
<h2 id="mrdocs-Foo">
57+
mrdocs::Foo<a class="mrdocs-anchor" href="#mrdocs-Foo" aria-label="Permalink">#</a>
58+
</h2>
59+
<div>
60+
<p>Included record</p>
61+
</div>
62+
</div>
63+
<div>
64+
<h3>
65+
Synopsis</h3>
66+
<div>
67+
Declared in <code>&lt;global-symbol-excluded.cpp&gt;</code></div>
68+
<pre><code class="source-code cpp">struct Foo;</code></pre>
69+
</div>
70+
71+
72+
</div>
73+
74+
</div>
75+
<footer class="mrdocs-footer">
76+
<span>Created with <a href="https://www.mrdocs.com">MrDocs</a></span>
77+
</footer>
78+
</body>
79+
</html>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<mrdocs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://github.com/cppalliance/mrdocs/raw/develop/mrdocs.rnc">
4+
<namespace id="//////////////////////////8=">
5+
<namespace name="mrdocs" id="rQr6X4pFwyRradNsE4fgTcl6Jmk=">
6+
<doc>
7+
<brief>
8+
<text>Included namespace</text>
9+
</brief>
10+
</doc>
11+
<struct name="Foo" id="yJQinsro3N5aSu4KK6p+ChnY5FM=">
12+
<file short-path="global-symbol-excluded.cpp" source-path="global-symbol-excluded.cpp" line="8" class="def"/>
13+
<doc>
14+
<brief>
15+
<text>Included record</text>
16+
</brief>
17+
</doc>
18+
</struct>
19+
</namespace>
20+
</namespace>
21+
</mrdocs>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include-symbols:
2+
- 'mrdocs::**'

0 commit comments

Comments
 (0)