Skip to content

Commit

Permalink
[ASTMatchers] Expand isInline matcher to VarDecl
Browse files Browse the repository at this point in the history
Add support to the `isInline` matcher for C++17's inline variables.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D118900
  • Loading branch information
njames93 committed Feb 23, 2022
1 parent f85a6a8 commit c34d898
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
25 changes: 23 additions & 2 deletions clang/docs/LibASTMatchersReference.html
Expand Up @@ -4322,7 +4322,7 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>


<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>
<tr><td colspan="4" class="doc" id="isInline1"><pre>Matches function and namespace declarations that are marked with
<tr><td colspan="4" class="doc" id="isInline1"><pre>Matches functions, variables and namespace declarations that are marked with
the inline keyword.

Given
Expand All @@ -4331,8 +4331,10 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
namespace n {
inline namespace m {}
}
inline int Foo = 5;
functionDecl(isInline()) will match ::f().
namespaceDecl(isInline()) will match n::m.
varDecl(isInline()) will match Foo;
</pre></td></tr>


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


<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>
<tr><td colspan="4" class="doc" id="isInline0"><pre>Matches function and namespace declarations that are marked with
<tr><td colspan="4" class="doc" id="isInline0"><pre>Matches functions, variables and namespace declarations that are marked with
the inline keyword.

Given
Expand All @@ -4706,8 +4708,10 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
namespace n {
inline namespace m {}
}
inline int Foo = 5;
functionDecl(isInline()) will match ::f().
namespaceDecl(isInline()) will match n::m.
varDecl(isInline()) will match Foo;
</pre></td></tr>


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


<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>
<tr><td colspan="4" class="doc" id="isInline2"><pre>Matches functions, variables and namespace declarations that are marked with
the inline keyword.

Given
inline void f();
void g();
namespace n {
inline namespace m {}
}
inline int Foo = 5;
functionDecl(isInline()) will match ::f().
namespaceDecl(isInline()) will match n::m.
varDecl(isInline()) will match Foo;
</pre></td></tr>


<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>
<tr><td colspan="4" class="doc" id="isStaticLocal0"><pre>Matches a static variable with local scope.

Expand Down
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Expand Up @@ -186,6 +186,8 @@ Build System Changes
AST Matchers
------------

- Expanded ``isInline`` narrowing matcher to support c++17 inline variables.

clang-format
------------

Expand Down
14 changes: 9 additions & 5 deletions clang/include/clang/ASTMatchers/ASTMatchers.h
Expand Up @@ -7673,7 +7673,7 @@ AST_MATCHER_P(FunctionDecl, hasExplicitSpecifier, internal::Matcher<Expr>,
return InnerMatcher.matches(*ES.getExpr(), Finder, Builder);
}

/// Matches function and namespace declarations that are marked with
/// Matches functions, variables and namespace declarations that are marked with
/// the inline keyword.
///
/// Given
Expand All @@ -7683,18 +7683,22 @@ AST_MATCHER_P(FunctionDecl, hasExplicitSpecifier, internal::Matcher<Expr>,
/// namespace n {
/// inline namespace m {}
/// }
/// inline int Foo = 5;
/// \endcode
/// functionDecl(isInline()) will match ::f().
/// namespaceDecl(isInline()) will match n::m.
AST_POLYMORPHIC_MATCHER(isInline,
AST_POLYMORPHIC_SUPPORTED_TYPES(NamespaceDecl,
FunctionDecl)) {
/// varDecl(isInline()) will match Foo;
AST_POLYMORPHIC_MATCHER(isInline, AST_POLYMORPHIC_SUPPORTED_TYPES(NamespaceDecl,
FunctionDecl,
VarDecl)) {
// This is required because the spelling of the function used to determine
// whether inline is specified or not differs between the polymorphic types.
if (const auto *FD = dyn_cast<FunctionDecl>(&Node))
return FD->isInlineSpecified();
else if (const auto *NSD = dyn_cast<NamespaceDecl>(&Node))
if (const auto *NSD = dyn_cast<NamespaceDecl>(&Node))
return NSD->isInline();
if (const auto *VD = dyn_cast<VarDecl>(&Node))
return VD->isInline();
llvm_unreachable("Not a valid polymorphic type");
}

Expand Down
2 changes: 2 additions & 0 deletions clang/unittests/ASTMatchers/ASTMatchersInternalTest.cpp
Expand Up @@ -197,6 +197,8 @@ TEST(IsInlineMatcher, IsInline) {
functionDecl(isInline(), hasName("f"))));
EXPECT_TRUE(matches("namespace n { inline namespace m {} }",
namespaceDecl(isInline(), hasName("m"))));
EXPECT_TRUE(matches("inline int Foo = 5;",
varDecl(isInline(), hasName("Foo")), {Lang_CXX17}));
}

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

0 comments on commit c34d898

Please sign in to comment.