Skip to content

[clang][Interp] Handle AttributedStmts #66495

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

Merged
merged 1 commit into from
Sep 15, 2023
Merged

[clang][Interp] Handle AttributedStmts #66495

merged 1 commit into from
Sep 15, 2023

Conversation

tbaederr
Copy link
Contributor

Just ignore the attributes.

Just ignore the attributes.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Sep 15, 2023
@llvmbot
Copy link
Member

llvmbot commented Sep 15, 2023

@llvm/pr-subscribers-clang

Changes Just ignore the attributes. -- Full diff: https://github.com//pull/66495.diff

3 Files Affected:

  • (modified) clang/lib/AST/Interp/ByteCodeStmtGen.cpp (+8)
  • (modified) clang/lib/AST/Interp/ByteCodeStmtGen.h (+1)
  • (modified) clang/test/AST/Interp/if.cpp (+7)
diff --git a/clang/lib/AST/Interp/ByteCodeStmtGen.cpp b/clang/lib/AST/Interp/ByteCodeStmtGen.cpp
index b7553d8963ff0ee..a0f50c3e69dd918 100644
--- a/clang/lib/AST/Interp/ByteCodeStmtGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeStmtGen.cpp
@@ -246,6 +246,8 @@ bool ByteCodeStmtGen<Emitter>::visitStmt(const Stmt *S) {
   case Stmt::GCCAsmStmtClass:
   case Stmt::MSAsmStmtClass:
     return visitAsmStmt(cast<AsmStmt>(S));
+  case Stmt::AttributedStmtClass:
+    return visitAttributedStmt(cast<AttributedStmt>(S));
   case Stmt::NullStmtClass:
     return true;
   default: {
@@ -628,6 +630,12 @@ bool ByteCodeStmtGen<Emitter>::visitAsmStmt(const AsmStmt *S) {
   return this->emitInvalid(S);
 }
 
+template <class Emitter>
+bool ByteCodeStmtGen<Emitter>::visitAttributedStmt(const AttributedStmt *S) {
+  // Ignore all attributes.
+  return this->visitStmt(S->getSubStmt());
+}
+
 namespace clang {
 namespace interp {
 
diff --git a/clang/lib/AST/Interp/ByteCodeStmtGen.h b/clang/lib/AST/Interp/ByteCodeStmtGen.h
index 278e804a803c951..3bdcdd78f397e5b 100644
--- a/clang/lib/AST/Interp/ByteCodeStmtGen.h
+++ b/clang/lib/AST/Interp/ByteCodeStmtGen.h
@@ -68,6 +68,7 @@ class ByteCodeStmtGen final : public ByteCodeExprGen<Emitter> {
   bool visitCaseStmt(const CaseStmt *S);
   bool visitDefaultStmt(const DefaultStmt *S);
   bool visitAsmStmt(const AsmStmt *S);
+  bool visitAttributedStmt(const AttributedStmt *S);
 
   bool emitLambdaStaticInvokerBody(const CXXMethodDecl *MD);
 
diff --git a/clang/test/AST/Interp/if.cpp b/clang/test/AST/Interp/if.cpp
index 2449ace4dd6c6b5..86ae8de6f73ebb7 100644
--- a/clang/test/AST/Interp/if.cpp
+++ b/clang/test/AST/Interp/if.cpp
@@ -43,4 +43,11 @@ namespace InitDecl {
     return false;
   }
   static_assert(!f2(), "");
+
+
+  constexpr int attrs() {
+    if (1) [[likely]] {}
+    return 1;
+  }
+  static_assert(attrs() == 1, "");
 };

@@ -628,6 +630,12 @@ bool ByteCodeStmtGen<Emitter>::visitAsmStmt(const AsmStmt *S) {
return this->emitInvalid(S);
}

template <class Emitter>
bool ByteCodeStmtGen<Emitter>::visitAttributedStmt(const AttributedStmt *S) {
// Ignore all attributes.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this what we want to do for [[assume]]? We can do it this way: https://eel.is/c++draft/expr.const#5.8 but we can also do it better: https://eel.is/c++draft/expr.const#5.33

CC @erichkeane @cor3ntin

(We don't support assumptions yet, so I think the code is fine as-is, but I'm wondering if we want to add a FIXME here about [[assume]]. We should also see if any of our vendor statement attributes have UB we need to consider as well.)

Copy link
Contributor

Choose a reason for hiding this comment

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

We can add a fix me but i wouldn't worry about it too much, until we have a clearer idea of what we do, which we really don't atm
And the wording is clear that this is unspecified either way.

And for vendor attributes, it would only make sense to check them if we want them to fail the evaluation, do we have such things?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this what we want to do for [[assume]]? We can do it this way: https://eel.is/c++draft/expr.const#5.8 but we can also do it better: https://eel.is/c++draft/expr.const#5.33

The second case would essentially work like an assertion? If this fails, the statement isn't a constant expression?

Copy link
Contributor

Choose a reason for hiding this comment

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

At compile time, i suppose so!

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm sitting in core, which is a good time to mention https://wg21.link/cwg2763 - [[assume]] is not the only UB standard attribute - but it requires a specific handling so it is also orthogonal to this patch

@tbaederr tbaederr merged commit d462bd5 into llvm:main Sep 15, 2023
ZijunZhaoCCK pushed a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Sep 19, 2023
zahiraam pushed a commit to tahonermann/llvm-project that referenced this pull request Oct 24, 2023
zahiraam pushed a commit to tahonermann/llvm-project that referenced this pull request Oct 24, 2023
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