Skip to content

Commit da038da

Browse files
committed
extract-local-classes option
#feat
1 parent c69f121 commit da038da

14 files changed

+332
-6
lines changed

docs/mrdocs.schema.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@
101101
"title": "Extraction policy for anonymous namespaces",
102102
"type": "boolean"
103103
},
104+
"extract-local-classes": {
105+
"default": true,
106+
"description": "Determine whether records only defined locally in source files should be extracted.",
107+
"enum": [
108+
true,
109+
false
110+
],
111+
"title": "Extraction policy for records defined locally in source files",
112+
"type": "boolean"
113+
},
104114
"extract-private": {
105115
"default": false,
106116
"description": "Determine whether private class members should be extracted",

src/lib/AST/ASTVisitor.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2722,6 +2722,17 @@ checkTypeFilters(Decl const* D, AccessSpecifier const access)
27222722
{
27232723
MRDOCS_CHECK_OR(!isStaticFileLevelMember(D), false);
27242724
}
2725+
if (!config_->extractLocalClasses && isa<RecordDecl>(D))
2726+
{
2727+
if (auto const* FI = findFileInfo(D);
2728+
FI->full_path.ends_with(".cpp") ||
2729+
FI->full_path.ends_with(".cc") ||
2730+
FI->full_path.ends_with(".cxx") ||
2731+
FI->full_path.ends_with(".c"))
2732+
{
2733+
return false;
2734+
}
2735+
}
27252736

27262737
// Don't extract anonymous unions
27272738
auto const* RD = dyn_cast<RecordDecl>(D);
@@ -2737,12 +2748,7 @@ bool
27372748
ASTVisitor::
27382749
checkFileFilters(Decl const* D)
27392750
{
2740-
clang::SourceLocation Loc = D->getBeginLoc();
2741-
if (Loc.isInvalid())
2742-
{
2743-
Loc = D->getLocation();
2744-
}
2745-
FileInfo* fileInfo = findFileInfo(Loc);
2751+
FileInfo* fileInfo = findFileInfo(D);
27462752
MRDOCS_CHECK_OR(fileInfo, false);
27472753

27482754
// Check pre-processed file filters
@@ -3146,6 +3152,18 @@ findFileInfo(clang::SourceLocation const loc)
31463152
return std::addressof(it->second);
31473153
}
31483154

3155+
ASTVisitor::FileInfo*
3156+
ASTVisitor::
3157+
findFileInfo(Decl const* D)
3158+
{
3159+
clang::SourceLocation Loc = D->getBeginLoc();
3160+
if (Loc.isInvalid())
3161+
{
3162+
Loc = D->getLocation();
3163+
}
3164+
return findFileInfo(Loc);
3165+
}
3166+
31493167
std::optional<ASTVisitor::FileInfo>
31503168
ASTVisitor::
31513169
buildFileInfo(FileEntry const* entry)

src/lib/AST/ASTVisitor.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,9 @@ class ASTVisitor
11091109
FileInfo*
11101110
findFileInfo(clang::SourceLocation loc);
11111111

1112+
FileInfo*
1113+
findFileInfo(Decl const* D);
1114+
11121115
/* Build a FileInfo for a FileEntry
11131116
11141117
This function will build a FileInfo object for a

src/lib/Lib/ConfigOptions.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,13 @@
213213
"type": "bool",
214214
"default": false
215215
},
216+
{
217+
"name": "extract-local-classes",
218+
"brief": "Extraction policy for records defined locally in source files",
219+
"details": "Determine whether records only defined locally in source files should be extracted.",
220+
"type": "bool",
221+
"default": true
222+
},
216223
{
217224
"name": "extract-anonymous-namespaces",
218225
"brief": "Extraction policy for anonymous namespaces",
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
= Reference
2+
:mrdocs:
3+
4+
[#index]
5+
== Global namespace
6+
7+
8+
=== Types
9+
10+
[cols=1]
11+
|===
12+
| Name
13+
14+
| <<local_class,`local&lowbar;class`>>
15+
| <<local_struct,`local&lowbar;struct`>>
16+
|===
17+
=== Functions
18+
19+
[cols=1]
20+
|===
21+
| Name
22+
23+
| <<local_function,`local&lowbar;function`>>
24+
|===
25+
26+
[#local_class]
27+
== local&lowbar;class
28+
29+
30+
=== Synopsis
31+
32+
33+
Declared in `&lt;extract&hyphen;local&hyphen;classes&period;cpp&gt;`
34+
35+
[source,cpp,subs="verbatim,replacements,macros,-callouts"]
36+
----
37+
class local&lowbar;class;
38+
----
39+
40+
41+
42+
43+
[#local_struct]
44+
== local&lowbar;struct
45+
46+
47+
=== Synopsis
48+
49+
50+
Declared in `&lt;extract&hyphen;local&hyphen;classes&period;cpp&gt;`
51+
52+
[source,cpp,subs="verbatim,replacements,macros,-callouts"]
53+
----
54+
struct local&lowbar;struct;
55+
----
56+
57+
58+
59+
60+
[#local_function]
61+
== local&lowbar;function
62+
63+
64+
=== Synopsis
65+
66+
67+
Declared in `&lt;extract&hyphen;local&hyphen;classes&period;cpp&gt;`
68+
69+
[source,cpp,subs="verbatim,replacements,macros,-callouts"]
70+
----
71+
void
72+
local&lowbar;function();
73+
----
74+
75+
76+
77+
[.small]#Created with https://www.mrdocs.com[MrDocs]#
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Already a cpp file so this is a local class
2+
struct local_struct {};
3+
class local_class {};
4+
void local_function();
5+
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<html lang="en">
2+
<head>
3+
<title>Reference</title>
4+
</head>
5+
<body>
6+
<div>
7+
<h1>Reference</h1>
8+
<div>
9+
<div>
10+
<h2 id="index">Global namespace</h2>
11+
</div>
12+
<h2>Types</h2>
13+
<table style="table-layout: fixed; width: 100%;">
14+
<thead>
15+
<tr>
16+
<th>Name</th>
17+
</tr>
18+
</thead>
19+
<tbody>
20+
<tr>
21+
<td><a href="#local_class"><code>local_class</code></a> </td></tr><tr>
22+
<td><a href="#local_struct"><code>local_struct</code></a> </td></tr>
23+
</tbody>
24+
</table>
25+
<h2>Functions</h2>
26+
<table style="table-layout: fixed; width: 100%;">
27+
<thead>
28+
<tr>
29+
<th>Name</th>
30+
</tr>
31+
</thead>
32+
<tbody>
33+
<tr>
34+
<td><a href="#local_function"><code>local_function</code></a> </td></tr>
35+
</tbody>
36+
</table>
37+
</div>
38+
<div>
39+
<div>
40+
<h2 id="local_class">local_class</h2>
41+
</div>
42+
<div>
43+
<h3>Synopsis</h3>
44+
<div>
45+
Declared in <code>&lt;extract-local-classes.cpp&gt;</code></div>
46+
<pre>
47+
<code class="source-code cpp">
48+
class local_class;
49+
</code>
50+
</pre>
51+
</div>
52+
53+
54+
</div>
55+
<div>
56+
<div>
57+
<h2 id="local_struct">local_struct</h2>
58+
</div>
59+
<div>
60+
<h3>Synopsis</h3>
61+
<div>
62+
Declared in <code>&lt;extract-local-classes.cpp&gt;</code></div>
63+
<pre>
64+
<code class="source-code cpp">
65+
struct local_struct;
66+
</code>
67+
</pre>
68+
</div>
69+
70+
71+
</div>
72+
<div>
73+
<div>
74+
<h2 id="local_function">local_function</h2>
75+
</div>
76+
<div>
77+
<h3>Synopsis</h3>
78+
<div>
79+
Declared in <code>&lt;extract-local-classes.cpp&gt;</code></div>
80+
<pre>
81+
<code class="source-code cpp">
82+
void
83+
local_function();
84+
</code>
85+
</pre>
86+
</div>
87+
</div>
88+
89+
</div>
90+
<div>
91+
<h4>Created with <a href="https://www.mrdocs.com">MrDocs</a></h4>
92+
</div>
93+
</body>
94+
</html>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
<class name="local_class" id="qbmV5XYyPyjd0VRKzDp7fghXifw=">
6+
<file short-path="extract-local-classes.cpp" source-path="extract-local-classes.cpp" line="3" class="def"/>
7+
</class>
8+
<struct name="local_struct" id="WcflY1aoR4DWbE5trOYKvtGVEKw=">
9+
<file short-path="extract-local-classes.cpp" source-path="extract-local-classes.cpp" line="2" class="def"/>
10+
</struct>
11+
<function name="local_function" id="4N1JFtP3y7voqQo6aiZzzTWLj84=">
12+
<file short-path="extract-local-classes.cpp" source-path="extract-local-classes.cpp" line="4"/>
13+
</function>
14+
</namespace>
15+
</mrdocs>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extract-local-classes: true
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
= Reference
2+
:mrdocs:
3+
4+
[#index]
5+
== Global namespace
6+
7+
8+
=== Functions
9+
10+
[cols=1]
11+
|===
12+
| Name
13+
14+
| <<local_function,`local&lowbar;function`>>
15+
|===
16+
17+
[#local_function]
18+
== local&lowbar;function
19+
20+
21+
=== Synopsis
22+
23+
24+
Declared in `&lt;no&hyphen;extract&hyphen;local&hyphen;classes&period;cpp&gt;`
25+
26+
[source,cpp,subs="verbatim,replacements,macros,-callouts"]
27+
----
28+
void
29+
local&lowbar;function();
30+
----
31+
32+
33+
34+
[.small]#Created with https://www.mrdocs.com[MrDocs]#

0 commit comments

Comments
 (0)