diff --git a/docs/LibASTMatchersReference.html b/docs/LibASTMatchersReference.html index 88d84faf0d2dc2137f0999a43785b3ec9a9d1b77..b476065b8495559062921723189c4f7cf9fd982c 100644 --- a/docs/LibASTMatchersReference.html +++ b/docs/LibASTMatchersReference.html @@ -1840,6 +1840,21 @@ callExpr(on(hasType(asString("class Y *")))) </pre></td></tr> +<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1QualType.html">QualType</a>></td><td class="name" onclick="toggle('hasLocalQualifiers0')"><a name="hasLocalQualifiers0Anchor">hasLocalQualifiers</a></td><td></td></tr> +<tr><td colspan="4" class="doc" id="hasLocalQualifiers0"><pre>Matches QualType nodes that have local CV-qualifiers attached to +the node, not hidden within a typedef. + +Given + typedef const int const_int; + const_int i; + int *const j; + int *volatile k; + int m; +varDecl(hasType(hasLocalQualifiers())) matches only j and k. +i is const-qualified but the qualifier is not local. +</pre></td></tr> + + <tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1QualType.html">QualType</a>></td><td class="name" onclick="toggle('isConstQualified0')"><a name="isConstQualified0Anchor">isConstQualified</a></td><td></td></tr> <tr><td colspan="4" class="doc" id="isConstQualified0"><pre>Matches QualType nodes that are const-qualified, i.e., that include "top-level" const. diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h index c320209f8456c130d3df019c51660fd753d37c93..f10addcb7af6fc46d3ecac290778dd75f0fa6c8c 100644 --- a/include/clang/ASTMatchers/ASTMatchers.h +++ b/include/clang/ASTMatchers/ASTMatchers.h @@ -2566,6 +2566,23 @@ AST_MATCHER(QualType, isConstQualified) { return Node.isConstQualified(); } +/// \brief Matches QualType nodes that have local CV-qualifiers attached to +/// the node, not hidden within a typedef. +/// +/// Given +/// \code +/// typedef const int const_int; +/// const_int i; +/// int *const j; +/// int *volatile k; +/// int m; +/// \endcode +/// \c varDecl(hasType(hasLocalQualifiers())) matches only \c j and \c k. +/// \c i is const-qualified but the qualifier is not local. +AST_MATCHER(QualType, hasLocalQualifiers) { + return Node.hasLocalQualifiers(); +} + /// \brief Matches a member expression where the member is matched by a /// given matcher. /// diff --git a/unittests/ASTMatchers/ASTMatchersTest.cpp b/unittests/ASTMatchers/ASTMatchersTest.cpp index 14824d018ba76f08894c59054f2fa71eb5d13ca0..301b4f7c8a8a707fc2482c318a8584cb1f478c77 100644 --- a/unittests/ASTMatchers/ASTMatchersTest.cpp +++ b/unittests/ASTMatchers/ASTMatchersTest.cpp @@ -1356,6 +1356,17 @@ TEST(QualType, hasCanonicalType) { varDecl(hasType(qualType(hasCanonicalType(referenceType())))))); } +TEST(QualType, hasLocalQualifiers) { + EXPECT_TRUE(notMatches("typedef const int const_int; const_int i = 1;", + varDecl(hasType(hasLocalQualifiers())))); + EXPECT_TRUE(matches("int *const j = nullptr;", + varDecl(hasType(hasLocalQualifiers())))); + EXPECT_TRUE(matches("int *volatile k;", + varDecl(hasType(hasLocalQualifiers())))); + EXPECT_TRUE(notMatches("int m;", + varDecl(hasType(hasLocalQualifiers())))); +} + TEST(HasParameter, CallsInnerMatcher) { EXPECT_TRUE(matches("class X { void x(int) {} };", methodDecl(hasParameter(0, varDecl()))));