PR50641: Properly handle AttributedStmts when checking for a valid
authorRichard Smith <richard@metafoo.co.uk>
Wed, 6 Oct 2021 20:12:20 +0000 (13:12 -0700)
committerRichard Smith <richard@metafoo.co.uk>
Wed, 6 Oct 2021 22:13:05 +0000 (15:13 -0700)
constexpr function body.

clang/lib/Sema/SemaDeclCXX.cpp
clang/test/SemaCXX/attr-likelihood.cpp

index 88af1f7..b7f84ea 100644 (file)
@@ -2050,6 +2050,13 @@ CheckConstexprFunctionStmt(Sema &SemaRef, const FunctionDecl *Dcl, Stmt *S,
     ReturnStmts.push_back(S->getBeginLoc());
     return true;
 
+  case Stmt::AttributedStmtClass:
+    // Attributes on a statement don't affect its formal kind and hence don't
+    // affect its validity in a constexpr function.
+    return CheckConstexprFunctionStmt(SemaRef, Dcl,
+                                      cast<AttributedStmt>(S)->getSubStmt(),
+                                      ReturnStmts, Cxx1yLoc, Cxx2aLoc, Kind);
+
   case Stmt::CompoundStmtClass: {
     // C++1y allows compound-statements.
     if (!Cxx1yLoc.isValid())
@@ -2064,11 +2071,6 @@ CheckConstexprFunctionStmt(Sema &SemaRef, const FunctionDecl *Dcl, Stmt *S,
     return true;
   }
 
-  case Stmt::AttributedStmtClass:
-    if (!Cxx1yLoc.isValid())
-      Cxx1yLoc = S->getBeginLoc();
-    return true;
-
   case Stmt::IfStmtClass: {
     // C++1y allows if-statements.
     if (!Cxx1yLoc.isValid())
index 03ba301..f7503fe 100644 (file)
@@ -154,4 +154,9 @@ void o()
                                   // expected-note {{conflicting attribute is here}}
   }
 }
+
+constexpr int constexpr_function() {
+  [[likely]] return 0;
+}
+static_assert(constexpr_function() == 0);
 #endif