[clang] 3e30b36 - [Clang] SemaFunctionEffects: When verifying a function, ignore any conditional noexcept expression. (#115342)

via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 10 17:02:53 PST 2024


Author: Doug Wyatt
Date: 2024-11-10T17:02:50-08:00
New Revision: 3e30b365c1ec95f0cfb62c3cfdf4f6f1c824c0bd

URL: https://github.com/llvm/llvm-project/commit/3e30b365c1ec95f0cfb62c3cfdf4f6f1c824c0bd
DIFF: https://github.com/llvm/llvm-project/commit/3e30b365c1ec95f0cfb62c3cfdf4f6f1c824c0bd.diff

LOG: [Clang] SemaFunctionEffects: When verifying a function, ignore any conditional noexcept expression. (#115342)

---------

Co-authored-by: Doug Wyatt <dwyatt at apple.com>

Added: 
    

Modified: 
    clang/lib/Sema/SemaFunctionEffects.cpp
    clang/test/Sema/attr-nonblocking-constraints.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaFunctionEffects.cpp b/clang/lib/Sema/SemaFunctionEffects.cpp
index ab728f24d8a271..a76a0a41276896 100644
--- a/clang/lib/Sema/SemaFunctionEffects.cpp
+++ b/clang/lib/Sema/SemaFunctionEffects.cpp
@@ -972,6 +972,7 @@ class Analyzer {
     CallableInfo &CurrentCaller;
     ViolationSite VSite;
     const Expr *TrailingRequiresClause = nullptr;
+    const Expr *NoexceptExpr = nullptr;
 
     FunctionBodyASTVisitor(Analyzer &Outer,
                            PendingFunctionAnalysis &CurrentFunction,
@@ -986,9 +987,22 @@ class Analyzer {
       if (auto *Dtor = dyn_cast<CXXDestructorDecl>(CurrentCaller.CDecl))
         followDestructor(dyn_cast<CXXRecordDecl>(Dtor->getParent()), Dtor);
 
-      if (auto *FD = dyn_cast<FunctionDecl>(CurrentCaller.CDecl))
+      if (auto *FD = dyn_cast<FunctionDecl>(CurrentCaller.CDecl)) {
         TrailingRequiresClause = FD->getTrailingRequiresClause();
 
+        // Note that FD->getType->getAs<FunctionProtoType>() can yield a
+        // noexcept Expr which has been boiled down to a constant expression.
+        // Going through the TypeSourceInfo obtains the actual expression which
+        // will be traversed as part of the function -- unless we capture it
+        // here and have TraverseStmt skip it.
+        if (TypeSourceInfo *TSI = FD->getTypeSourceInfo()) {
+          if (FunctionProtoTypeLoc TL =
+                  TSI->getTypeLoc().getAs<FunctionProtoTypeLoc>())
+            if (const FunctionProtoType *FPT = TL.getTypePtr())
+              NoexceptExpr = FPT->getNoexceptExpr();
+        }
+      }
+
       // Do an AST traversal of the function/block body
       TraverseDecl(const_cast<Decl *>(CurrentCaller.CDecl));
     }
@@ -1269,7 +1283,8 @@ class Analyzer {
       // We skip the traversal of lambdas (beyond their captures, see
       // TraverseLambdaExpr below), so just caching this from our constructor
       // should suffice.
-      if (Statement != TrailingRequiresClause)
+      // The exact same is true for a conditional `noexcept()` clause.
+      if (Statement != TrailingRequiresClause && Statement != NoexceptExpr)
         return Base::TraverseStmt(Statement);
       return true;
     }

diff  --git a/clang/test/Sema/attr-nonblocking-constraints.cpp b/clang/test/Sema/attr-nonblocking-constraints.cpp
index 19a4c3b7942b12..cc9108c0a4fbd6 100644
--- a/clang/test/Sema/attr-nonblocking-constraints.cpp
+++ b/clang/test/Sema/attr-nonblocking-constraints.cpp
@@ -388,7 +388,7 @@ void nb26() [[clang::nonblocking]] {
 	abort_wrapper(); // no diagnostic
 }
 
-// --- Make sure we don't traverse a requires clause. ---
+// --- Make sure we don't traverse requires and noexcept clauses. ---
 
 // Apparently some requires clauses are able to be collapsed into a constant before the nonblocking
 // analysis sees any function calls. This example (extracted from a real-world case where
@@ -420,6 +420,7 @@ class expected {
   constexpr expected()
     {}
 
+  // This is a deliberate corruption of the real implementation for simplicity.
   constexpr expected(const expected&)
     requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err>)
   = default;
@@ -428,11 +429,20 @@ class expected {
 void test() [[clang::nonblocking]]
 {
 	expected<int, int> a;
-	auto b = a;
+	auto b = a;            // Copy constructor.
 }
 
 } // namespace ExpectedTest
 
+// Make sure a function call in a noexcept() clause is ignored.
+constexpr bool foo() [[clang::nonblocking(false)]] { return true; }
+void nb27() noexcept(foo()) [[clang::nonblocking]] {}
+
+// Make sure that simple type traits don't cause violations.
+void nb28() [[clang::nonblocking]] {
+	bool x = __is_constructible(int, const int&);
+}
+
 // --- nonblocking implies noexcept ---
 #pragma clang diagnostic warning "-Wperf-constraint-implies-noexcept"
 


        


More information about the cfe-commits mailing list