Skip to content

[clang] AST Visitor: skip empty qualifiers in QualifiedTemplateName #93926

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

Conversation

mizvekov
Copy link
Contributor

@mizvekov mizvekov commented May 31, 2024

This change was missed in #93433.

This fixes a problem detected by UBSAN build bots.

@mizvekov mizvekov requested a review from pcc May 31, 2024 05:56
@mizvekov mizvekov self-assigned this May 31, 2024
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 31, 2024
@llvmbot
Copy link
Member

llvmbot commented May 31, 2024

@llvm/pr-subscribers-clang

Author: Matheus Izvekov (mizvekov)

Changes

This change was missed in #93433.


Full diff: https://github.com/llvm/llvm-project/pull/93926.diff

1 Files Affected:

  • (modified) clang/include/clang/AST/RecursiveASTVisitor.h (+6-3)
diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h
index 4bbb4380cdd7f..28a90dffcb8dc 100644
--- a/clang/include/clang/AST/RecursiveASTVisitor.h
+++ b/clang/include/clang/AST/RecursiveASTVisitor.h
@@ -855,10 +855,13 @@ bool RecursiveASTVisitor<Derived>::TraverseDeclarationNameInfo(
 
 template <typename Derived>
 bool RecursiveASTVisitor<Derived>::TraverseTemplateName(TemplateName Template) {
-  if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
+  if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
     TRY_TO(TraverseNestedNameSpecifier(DTN->getQualifier()));
-  else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
-    TRY_TO(TraverseNestedNameSpecifier(QTN->getQualifier()));
+  } else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) {
+    if (T->getQualifier()) {
+      TRY_TO(TraverseNestedNameSpecifier(QTN->getQualifier()));
+    }
+  }
 
   return true;
 }

Copy link

github-actions bot commented May 31, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@mizvekov mizvekov force-pushed the users/mizvekov/clang-fix-ast-visitor-qualified-templatename branch from dd8839b to db56ac3 Compare May 31, 2024 06:03
@mizvekov mizvekov force-pushed the users/mizvekov/clang-fix-ast-visitor-qualified-templatename branch from db56ac3 to 55d9466 Compare May 31, 2024 06:04
Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (modulo suggestion)

Comment on lines +860 to +865
} else if (QualifiedTemplateName *QTN =
Template.getAsQualifiedTemplateName()) {
if (QTN->getQualifier()) {
TRY_TO(TraverseNestedNameSpecifier(QTN->getQualifier()));
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else if (QualifiedTemplateName *QTN =
Template.getAsQualifiedTemplateName()) {
if (QTN->getQualifier()) {
TRY_TO(TraverseNestedNameSpecifier(QTN->getQualifier()));
}
}
} else if (QualifiedTemplateName *QTN =
Template.getAsQualifiedTemplateName(); QTN && QTN->getQualifier()) {
TRY_TO(TraverseNestedNameSpecifier(QTN->getQualifier()));
}

Copy link
Contributor Author

@mizvekov mizvekov May 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that looks cleaner at first glance, but the intention in the original code was for each arm to match each kind of TemplateName.

For example, this doesn't traverse OverloadTemplateName right now, but if we wanted to add it after this change, this would necessitate to refactor the if branches once again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am going to merge since this fixes broken build bot.

Let's continue in post.

@mizvekov mizvekov merged commit be566d2 into llvm:main May 31, 2024
7 checks passed
@mizvekov mizvekov deleted the users/mizvekov/clang-fix-ast-visitor-qualified-templatename branch May 31, 2024 07:31
@Sterling-Augustine
Copy link
Contributor

Even with this fix, I'm seeing a segfault in clang/lib/AST/QualTypeNames.cpp at line 216.

  switch(Scope->getKind()) {

Scope is nullptr. The call is from line 70:

  QualifiedTemplateName *QTName = TName.getAsQualifiedTemplateName();

  if (QTName && !QTName->hasTemplateKeyword()) {
    NNS = QTName->getQualifier();
    NestedNameSpecifier *QNNS = getFullyQualifiedNestedNameSpecifier(
        Ctx, NNS, WithGlobalNsPrefix);

which means that the QTName->getQualifier() is returning nullptr where it didn't before. Checking QTName->getQualifier() for nullptr in the conditional (so it can take the else side), everything works. But it seems to me that there is probably a better fix that someone familiar with the code would be able to figure out. I'm not even sure the original is doing the right thing.

I have a fix in testing that adds this check, but if you would double check it after it goes in, that would be helpful.

Unfortunately the failure is in an internal tool, against internal sources, which makes getting a reproducer a long process. I'm also working on that.

Sterling-Augustine added a commit that referenced this pull request Jun 1, 2024
Protect against nullptr after #93926
@mizvekov
Copy link
Contributor Author

mizvekov commented Jun 3, 2024

Even with this fix, I'm seeing a segfault in clang/lib/AST/QualTypeNames.cpp at line 216.
I have a fix in testing that adds this check, but if you would double check it after it goes in, that would be helpful.

Unfortunately the failure is in an internal tool, against internal sources, which makes getting a reproducer a long process. I'm also working on that.

The fix gets the intended behavior right.

There are unittests for QualTypeNames, a new test case could go in there.

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants