Skip to content
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

Traverse attributes in AttributedStmt. #117692

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

16bit-ykiko
Copy link
Contributor

Fix #117687.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 26, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 26, 2024

@llvm/pr-subscribers-clang

Author: ykiko (16bit-ykiko)

Changes

Fix #117687.


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

1 Files Affected:

  • (modified) clang/include/clang/AST/RecursiveASTVisitor.h (+4-1)
diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h
index b2dd51319ba9ea..6ab9d0e71d71aa 100644
--- a/clang/include/clang/AST/RecursiveASTVisitor.h
+++ b/clang/include/clang/AST/RecursiveASTVisitor.h
@@ -2442,7 +2442,10 @@ DEF_TRAVERSE_STMT(GotoStmt, {})
 DEF_TRAVERSE_STMT(IfStmt, {})
 DEF_TRAVERSE_STMT(IndirectGotoStmt, {})
 DEF_TRAVERSE_STMT(LabelStmt, {})
-DEF_TRAVERSE_STMT(AttributedStmt, {})
+DEF_TRAVERSE_STMT(AttributedStmt, {
+  for (auto *I : S->getAttrs())
+    TRY_TO(getDerived().TraverseAttr(const_cast<clang::Attr *>(I)));
+})
 DEF_TRAVERSE_STMT(NullStmt, {})
 DEF_TRAVERSE_STMT(ObjCAtCatchStmt, {})
 DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, {})

@16bit-ykiko
Copy link
Contributor Author

16bit-ykiko commented Nov 26, 2024

A test failed after the modification, but it really confused me

// Contexts where there is no function call, no diagnostic.
bool bad();

template <bool>
requires requires { bad(); }
void g() [[clang::nonblocking]] {}

void g() [[clang::nonblocking]] {
    decltype(bad()) a; // doesn't generate a call so, OK
    [[maybe_unused]] auto b = noexcept(bad());
    [[maybe_unused]] auto c = sizeof(bad());
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wassume"
    [[assume(bad())]]; // never evaluated, but maybe still semantically questionable?
#pragma clang diagnostic pop
}
  File /home/ykiko/C++/clice2/deps/llvm/clang/test/Sema/attr-nonblocking-constraints.cpp Line 396: function with 'nonblocking' attribute must not call non-'nonblocking' function 'bad'
error: 'expected-note' diagnostics seen but not expected: 
  File /home/ykiko/C++/clice2/deps/llvm/clang/test/Sema/attr-nonblocking-constraints.cpp Line 384: declaration cannot be inferred 'nonblocking' because it has no definition in this translation unit

I don't know what happened here. They seem to have no connection at all.

@Sirraide
Copy link
Member

Sirraide commented Nov 26, 2024

I don't know what happened here. They seem to have no connection at all.

What seems to be happening here is that we’re now traversing the argument of the [[assume]] attribute, which causes us to warn about the call to bad(). This wasn’t a problem before because TraverseAttributedStmt wouldn’t traverse the attributes. I think you need to override TraverseAttr() in the FunctionBodyASTVisitor in SemaFunctionEffects.cpp to do nothing, as there currently are no statement attributes that we’d want to traverse in the first place afaik.

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

Thanks for the fix! Please be sure to also add some test coverage for the changes to clang/unittests/AST/RecursiveASTVisitorTest.cpp.

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.

[[clang]] Forget to traverse attributes when traverse AttributedStmt
4 participants