-
Notifications
You must be signed in to change notification settings - Fork 12.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Clang][ASTMatcher] Add dependentNameType
Matcher
#121263
[Clang][ASTMatcher] Add dependentNameType
Matcher
#121263
Conversation
@llvm/pr-subscribers-clang Author: Amr Hesham (AmrDeveloper) ChangesAdd AST Matcher for Fixes: #121240 Full diff: https://github.com/llvm/llvm-project/pull/121263.diff 7 Files Affected:
diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html
index ddc99020604c94..69fd43b2114723 100644
--- a/clang/docs/LibASTMatchersReference.html
+++ b/clang/docs/LibASTMatchersReference.html
@@ -2536,6 +2536,15 @@ <h2 id="decl-matchers">Node Matchers</h2>
matches "decltype(i + j)"
</pre></td></tr>
+<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1Stmt.html">Stmt</a>></td><td class="name" onclick="toggle('dependentNameType0')"><a name="dependentNameType0Anchor">dependentNameType</a></td><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1DependentNameType.html">DependentNameType</a>>...</td></tr>
+<tr><td colspan="4" class="doc" id="dependentNameType0"><pre>Matches dependent name type.
+
+Example matches T::type
+
+ template <typename T> struct declToImport {
+ typedef typename T::type dependent_name;
+ };
+</pre></td></tr>
<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1Type.html">Type</a>></td><td class="name" onclick="toggle('deducedTemplateSpecializationType0')"><a name="deducedTemplateSpecializationType0Anchor">deducedTemplateSpecializationType</a></td><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1DeducedTemplateSpecializationType.html">DeducedTemplateSpecializationType</a>>...</td></tr>
<tr><td colspan="4" class="doc" id="deducedTemplateSpecializationType0"><pre>Matches C++17 deduced template specialization types, e.g. deduced class
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 983c1da20ed4c8..7446aaf57a02dc 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -1110,6 +1110,8 @@ AST Matchers
- Add ``dependentScopeDeclRefExpr`` matcher to match expressions that refer to dependent scope declarations.
+- Add ``dependentNameType`` matcher to match dependent name type.
+
clang-format
------------
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index 22e2546ab81e0a..b27914306b8270 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -7711,6 +7711,16 @@ AST_MATCHER_P(DecayedType, hasDecayedType, internal::Matcher<QualType>,
return InnerType.matches(Node.getDecayedType(), Finder, Builder);
}
+/// Matches dependent name type
+///
+/// Example matches T::type
+/// \code
+/// template <typename T> struct declToImport {
+/// typedef typename T::type dependent_name;
+/// };
+/// \endcode
+extern const AstTypeMatcher<DependentNameType> dependentNameType;
+
/// Matches declarations whose declaration context, interpreted as a
/// Decl, matches \c InnerMatcher.
///
diff --git a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
index 8c744eebbdfb50..a47633bf4bae24 100644
--- a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
+++ b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
@@ -1108,6 +1108,7 @@ const AstTypeMatcher<SubstTemplateTypeParmType> substTemplateTypeParmType;
const AstTypeMatcher<TemplateTypeParmType> templateTypeParmType;
const AstTypeMatcher<InjectedClassNameType> injectedClassNameType;
const AstTypeMatcher<DecayedType> decayedType;
+const AstTypeMatcher<DependentNameType> dependentNameType;
AST_TYPELOC_TRAVERSE_MATCHER_DEF(hasElementType,
AST_POLYMORPHIC_SUPPORTED_TYPES(ArrayType,
ComplexType));
diff --git a/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
index 685d626d2978bf..674129aee59241 100644
--- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -218,6 +218,7 @@ RegistryMaps::RegistryMaps() {
REGISTER_MATCHER(cxxTryStmt);
REGISTER_MATCHER(cxxUnresolvedConstructExpr);
REGISTER_MATCHER(decayedType);
+ REGISTER_MATCHER(dependentNameType);
REGISTER_MATCHER(decl);
REGISTER_MATCHER(decompositionDecl);
REGISTER_MATCHER(declCountIs);
diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index ec062a5cc953b8..ee1d896f1ca6dc 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -3196,9 +3196,6 @@ TEST_P(ImportExpr, DependentScopeDeclRefExpr) {
has(callExpr(has(dependentScopeDeclRefExpr())))))))));
}
-const internal::VariadicDynCastAllOfMatcher<Type, DependentNameType>
- dependentNameType;
-
TEST_P(ImportExpr, DependentNameType) {
MatchVerifier<Decl> Verifier;
testImport("template <typename T> struct declToImport {"
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
index a3baad367a27b1..0a5ecd29432c5f 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
@@ -1912,6 +1912,21 @@ TEST_P(ASTMatchersTest, DeducedTemplateSpecializationType) {
deducedTemplateSpecializationType()));
}
+TEST_P(ASTMatchersTest, DependentNameType) {
+ if (!GetParam().isCXX()) {
+ // FIXME: Add a test for `dependentNameType()` that does not depend on C++.
+ return;
+ }
+
+ EXPECT_TRUE(matches(
+ R"(
+ template <typename T> struct declToImport {
+ typedef typename T::type dependent_name;
+ };
+ )",
+ dependentNameType()));
+}
+
TEST_P(ASTMatchersTest, RecordType) {
EXPECT_TRUE(matches("struct S {}; struct S s;",
recordType(hasDeclaration(recordDecl(hasName("S"))))));
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
21873d0
to
bdbd11d
Compare
@@ -218,6 +218,7 @@ RegistryMaps::RegistryMaps() { | |||
REGISTER_MATCHER(cxxTryStmt); | |||
REGISTER_MATCHER(cxxUnresolvedConstructExpr); | |||
REGISTER_MATCHER(decayedType); | |||
REGISTER_MATCHER(dependentNameType); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please sort this in alphabetically
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thank you
@@ -7711,6 +7711,16 @@ AST_MATCHER_P(DecayedType, hasDecayedType, internal::Matcher<QualType>, | |||
return InnerType.matches(Node.getDecayedType(), Finder, Builder); | |||
} | |||
|
|||
/// Matches dependent name type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: a dependent name type
(+1 in release notes)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@@ -1912,6 +1912,21 @@ TEST_P(ASTMatchersTest, DeducedTemplateSpecializationType) { | |||
deducedTemplateSpecializationType())); | |||
} | |||
|
|||
TEST_P(ASTMatchersTest, DependentNameType) { | |||
if (!GetParam().isCXX()) { | |||
// FIXME: Add a test for `dependentNameType()` that does not depend on C++. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to DependentScopeDeclRefExpr
, DependentNameType
only exists in C++, so this FIXME comment can be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@@ -2536,6 +2536,15 @@ <h2 id="decl-matchers">Node Matchers</h2> | |||
matches "decltype(i + j)" | |||
</pre></td></tr> | |||
|
|||
<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1Stmt.html">Stmt</a>></td><td class="name" onclick="toggle('dependentNameType0')"><a name="dependentNameType0Anchor">dependentNameType</a></td><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1DependentNameType.html">DependentNameType</a>>...</td></tr> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first column here should be Matcher<Type>
, not Matcher<Stmt>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
(Update: that one's been taken by another contributor.) |
@HighCommander4 Thank you, if you find another one, I will be interested to work on it, you can assign me directly, even if it is not good first issue, I am interested to work on bigger ast matcher issues :D |
Add AST Matcher for
DependentNameType
Fixes: #121240