-
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
[StructuralEquivalence] improve NTTP and CXXDependentScopeMemberExpr comparison #95190
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
@llvm/pr-subscribers-clang Author: Qizhi Hu (jcsxky) Changesimprove
Full diff: https://github.com/llvm/llvm-project/pull/95190.diff 2 Files Affected:
diff --git a/clang/lib/AST/ASTStructuralEquivalence.cpp b/clang/lib/AST/ASTStructuralEquivalence.cpp
index d56bf21b459e0..37555c324282f 100644
--- a/clang/lib/AST/ASTStructuralEquivalence.cpp
+++ b/clang/lib/AST/ASTStructuralEquivalence.cpp
@@ -348,6 +348,15 @@ class StmtComparer {
return true;
}
+ bool IsStmtEquivalent(const CXXDependentScopeMemberExpr *E1,
+ const CXXDependentScopeMemberExpr *E2) {
+ if (!IsStructurallyEquivalent(Context, E1->getMember(), E2->getMember())) {
+ return false;
+ }
+ return IsStructurallyEquivalent(Context, E1->getBaseType(),
+ E2->getBaseType());
+ }
+
bool IsStmtEquivalent(const UnaryExprOrTypeTraitExpr *E1,
const UnaryExprOrTypeTraitExpr *E2) {
if (E1->getKind() != E2->getKind())
@@ -1997,7 +2006,10 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
}
return false;
}
-
+ if (!Context.IgnoreTemplateParmDepth && D1->getDepth() != D2->getDepth())
+ return false;
+ if (D1->getIndex() != D2->getIndex())
+ return false;
// Check types.
if (!IsStructurallyEquivalent(Context, D1->getType(), D2->getType())) {
if (Context.Complain) {
diff --git a/clang/unittests/AST/StructuralEquivalenceTest.cpp b/clang/unittests/AST/StructuralEquivalenceTest.cpp
index 91dd717d7b25e..55b2580bfce3b 100644
--- a/clang/unittests/AST/StructuralEquivalenceTest.cpp
+++ b/clang/unittests/AST/StructuralEquivalenceTest.cpp
@@ -1877,6 +1877,34 @@ TEST_F(StructuralEquivalenceCacheTest, VarDeclWithDifferentStorageClassNoEq) {
EXPECT_FALSE(Ctx.IsEquivalent(Var.first, Var.second));
}
+TEST_F(StructuralEquivalenceCacheTest,
+ NonTypeTemplateParmWithDifferentPositionNoEq) {
+ auto TU = makeTuDecls(
+ R"(
+ template<int T>
+ struct A {
+ template<int U>
+ void foo() {}
+ };
+ )",
+ R"(
+ template<int U>
+ struct A {
+ template<int V, int T>
+ void foo() {}
+ };
+ )",
+ Lang_CXX03);
+
+ StructuralEquivalenceContext Ctx(
+ get<0>(TU)->getASTContext(), get<1>(TU)->getASTContext(),
+ NonEquivalentDecls, StructuralEquivalenceKind::Default, false, false);
+
+ auto NTTP = findDeclPair<NonTypeTemplateParmDecl>(
+ TU, nonTypeTemplateParmDecl(hasName("T")));
+ EXPECT_FALSE(Ctx.IsEquivalent(NTTP.first, NTTP.second));
+}
+
TEST_F(StructuralEquivalenceCacheTest, VarDeclWithInitNoEq) {
auto TU = makeTuDecls(
R"(
@@ -2441,8 +2469,7 @@ TEST_F(StructuralEquivalenceStmtTest, NonTypeTemplateParm) {
void foo(A<T, y>);
)",
Lang_CXX11);
- // FIXME: These should not match,
- EXPECT_TRUE(testStructuralMatch(t));
+ EXPECT_FALSE(testStructuralMatch(t));
}
TEST_F(StructuralEquivalenceStmtTest, UnresolvedLookupDifferentName) {
@@ -2595,5 +2622,26 @@ TEST_F(StructuralEquivalenceStmtTest, DeclRefExpr) {
EXPECT_FALSE(testStructuralMatch(t));
}
+const internal::VariadicDynCastAllOfMatcher<Stmt, CXXDependentScopeMemberExpr>
+ cxxDependentScopeMemberExpr;
+
+TEST_F(StructuralEquivalenceCacheTest, CXXDependentScopeMemberExprNoEq) {
+ auto S = makeStmts(
+ R"(
+ template <class T>
+ void foo() {
+ T().x;
+ }
+ )",
+ R"(
+ template <class T>
+ void foo() {
+ T().y;
+ }
+ )",
+ Lang_CXX11, cxxDependentScopeMemberExpr());
+ EXPECT_FALSE(testStructuralMatch(S));
+}
+
} // end namespace ast_matchers
} // end namespace clang
|
mizvekov
approved these changes
Jun 12, 2024
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!
644c6e8 to
157f664
Compare
157f664 to
ba081d5
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
clang:frontend
Language frontend issues, e.g. anything involving "Sema"
clang
Clang issues not falling into any other category
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
improve
ASTStructuralEquivalenceTest:CXXDependentScopeMemberExprtoStmtCompare.