Skip to content

Commit c34d898

Browse files
committed
[ASTMatchers] Expand isInline matcher to VarDecl
Add support to the `isInline` matcher for C++17's inline variables. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D118900
1 parent f85a6a8 commit c34d898

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

clang/docs/LibASTMatchersReference.html

+23-2
Original file line numberDiff line numberDiff line change
@@ -4322,7 +4322,7 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
43224322

43234323

43244324
<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html">FunctionDecl</a>&gt;</td><td class="name" onclick="toggle('isInline1')"><a name="isInline1Anchor">isInline</a></td><td></td></tr>
4325-
<tr><td colspan="4" class="doc" id="isInline1"><pre>Matches function and namespace declarations that are marked with
4325+
<tr><td colspan="4" class="doc" id="isInline1"><pre>Matches functions, variables and namespace declarations that are marked with
43264326
the inline keyword.
43274327

43284328
Given
@@ -4331,8 +4331,10 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
43314331
namespace n {
43324332
inline namespace m {}
43334333
}
4334+
inline int Foo = 5;
43344335
functionDecl(isInline()) will match ::f().
43354336
namespaceDecl(isInline()) will match n::m.
4337+
varDecl(isInline()) will match Foo;
43364338
</pre></td></tr>
43374339

43384340

@@ -4697,7 +4699,7 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
46974699

46984700

46994701
<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1NamespaceDecl.html">NamespaceDecl</a>&gt;</td><td class="name" onclick="toggle('isInline0')"><a name="isInline0Anchor">isInline</a></td><td></td></tr>
4700-
<tr><td colspan="4" class="doc" id="isInline0"><pre>Matches function and namespace declarations that are marked with
4702+
<tr><td colspan="4" class="doc" id="isInline0"><pre>Matches functions, variables and namespace declarations that are marked with
47014703
the inline keyword.
47024704

47034705
Given
@@ -4706,8 +4708,10 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
47064708
namespace n {
47074709
inline namespace m {}
47084710
}
4711+
inline int Foo = 5;
47094712
functionDecl(isInline()) will match ::f().
47104713
namespaceDecl(isInline()) will match n::m.
4714+
varDecl(isInline()) will match Foo;
47114715
</pre></td></tr>
47124716

47134717

@@ -5728,6 +5732,23 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
57285732
</pre></td></tr>
57295733

57305734

5735+
<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1VarDecl.html">VarDecl</a>&gt;</td><td class="name" onclick="toggle('isInline2')"><a name="isInline2Anchor">isInline</a></td><td></td></tr>
5736+
<tr><td colspan="4" class="doc" id="isInline2"><pre>Matches functions, variables and namespace declarations that are marked with
5737+
the inline keyword.
5738+
5739+
Given
5740+
inline void f();
5741+
void g();
5742+
namespace n {
5743+
inline namespace m {}
5744+
}
5745+
inline int Foo = 5;
5746+
functionDecl(isInline()) will match ::f().
5747+
namespaceDecl(isInline()) will match n::m.
5748+
varDecl(isInline()) will match Foo;
5749+
</pre></td></tr>
5750+
5751+
57315752
<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1VarDecl.html">VarDecl</a>&gt;</td><td class="name" onclick="toggle('isStaticLocal0')"><a name="isStaticLocal0Anchor">isStaticLocal</a></td><td></td></tr>
57325753
<tr><td colspan="4" class="doc" id="isStaticLocal0"><pre>Matches a static variable with local scope.
57335754

clang/docs/ReleaseNotes.rst

+2
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ Build System Changes
186186
AST Matchers
187187
------------
188188

189+
- Expanded ``isInline`` narrowing matcher to support c++17 inline variables.
190+
189191
clang-format
190192
------------
191193

clang/include/clang/ASTMatchers/ASTMatchers.h

+9-5
Original file line numberDiff line numberDiff line change
@@ -7673,7 +7673,7 @@ AST_MATCHER_P(FunctionDecl, hasExplicitSpecifier, internal::Matcher<Expr>,
76737673
return InnerMatcher.matches(*ES.getExpr(), Finder, Builder);
76747674
}
76757675

7676-
/// Matches function and namespace declarations that are marked with
7676+
/// Matches functions, variables and namespace declarations that are marked with
76777677
/// the inline keyword.
76787678
///
76797679
/// Given
@@ -7683,18 +7683,22 @@ AST_MATCHER_P(FunctionDecl, hasExplicitSpecifier, internal::Matcher<Expr>,
76837683
/// namespace n {
76847684
/// inline namespace m {}
76857685
/// }
7686+
/// inline int Foo = 5;
76867687
/// \endcode
76877688
/// functionDecl(isInline()) will match ::f().
76887689
/// namespaceDecl(isInline()) will match n::m.
7689-
AST_POLYMORPHIC_MATCHER(isInline,
7690-
AST_POLYMORPHIC_SUPPORTED_TYPES(NamespaceDecl,
7691-
FunctionDecl)) {
7690+
/// varDecl(isInline()) will match Foo;
7691+
AST_POLYMORPHIC_MATCHER(isInline, AST_POLYMORPHIC_SUPPORTED_TYPES(NamespaceDecl,
7692+
FunctionDecl,
7693+
VarDecl)) {
76927694
// This is required because the spelling of the function used to determine
76937695
// whether inline is specified or not differs between the polymorphic types.
76947696
if (const auto *FD = dyn_cast<FunctionDecl>(&Node))
76957697
return FD->isInlineSpecified();
7696-
else if (const auto *NSD = dyn_cast<NamespaceDecl>(&Node))
7698+
if (const auto *NSD = dyn_cast<NamespaceDecl>(&Node))
76977699
return NSD->isInline();
7700+
if (const auto *VD = dyn_cast<VarDecl>(&Node))
7701+
return VD->isInline();
76987702
llvm_unreachable("Not a valid polymorphic type");
76997703
}
77007704

clang/unittests/ASTMatchers/ASTMatchersInternalTest.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ TEST(IsInlineMatcher, IsInline) {
197197
functionDecl(isInline(), hasName("f"))));
198198
EXPECT_TRUE(matches("namespace n { inline namespace m {} }",
199199
namespaceDecl(isInline(), hasName("m"))));
200+
EXPECT_TRUE(matches("inline int Foo = 5;",
201+
varDecl(isInline(), hasName("Foo")), {Lang_CXX17}));
200202
}
201203

202204
// FIXME: Figure out how to specify paths so the following tests pass on

0 commit comments

Comments
 (0)