https://github.com/nico updated https://github.com/llvm/llvm-project/pull/226605

>From 01de8d37e0bcc845894fa18d4737647570eab6b5 Mon Sep 17 00:00:00 2001
From: Nico Weber <[email protected]>
Date: Sun, 20 Sep 2026 17:54:39 -0400
Subject: [PATCH 1/2] [-Wunsafe-buffer-usage] Speed up warning a bit

Checking a type is faster than calling ignoreUnsafeBufferIn...().
The latter has to get the Stmt's begin loc, look up diagnostic
state and suppression, etc. And the type check lets us skip
most Stmts cheaply, so do that first.

In a sampling profile of 48 Chromium TUs (which are built with
-Wunsafe-buffer-usage), UnsafeBufferUsageReporter's
ignoreUnsafeBufferInLibcCall() and ignoreUnsafeBufferInContainer() were
1.35% and 1.29% of the total compile time, that is 3.75% for
checkUnsafeBufferUsage() as a whole.

For 60 random Chromium TUs drawn with probability proportional
to their compile time, sum over all TUs:

    CPU time: 242.9 s => 237.5 s, -2.2%
    instructions: 2452.6e9 => 2371.8e9, -3.3%

No behavior change.
---
 clang/lib/Analysis/UnsafeBufferUsage.cpp | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp 
b/clang/lib/Analysis/UnsafeBufferUsage.cpp
index 32d1a0d34842c..7c510f7a74181 100644
--- a/clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -1894,7 +1894,7 @@ class SpanTwoParamConstructorGadget : public 
WarningGadget {
   static bool matches(const Stmt *S, ASTContext &Ctx,
                       const UnsafeBufferUsageHandler *Handler,
                       MatchResult &Result) {
-    if (ignoreUnsafeBufferInContainer(*S, Handler))
+    if (!isa<CXXConstructExpr>(S) || ignoreUnsafeBufferInContainer(*S, 
Handler))
       return false;
     return matches(S, Ctx, Result);
   }
@@ -1957,7 +1957,7 @@ class StringViewTwoParamConstructorGadget : public 
WarningGadget {
   static bool matches(const Stmt *S, ASTContext &Ctx,
                       const UnsafeBufferUsageHandler *Handler,
                       MatchResult &Result) {
-    if (ignoreUnsafeBufferInContainer(*S, Handler))
+    if (!isa<CXXConstructExpr>(S) || ignoreUnsafeBufferInContainer(*S, 
Handler))
       return false;
     return matches(S, Ctx, Result);
   }
@@ -2370,14 +2370,14 @@ class UnsafeLibcFunctionCallGadget : public 
WarningGadget {
   static bool matches(const Stmt *S, ASTContext &Ctx,
                       const UnsafeBufferUsageHandler *Handler,
                       MatchResult &Result) {
-    if (ignoreUnsafeLibcCall(Ctx, *S, Handler))
-      return false;
     const auto *CE = dyn_cast<CallExpr>(S);
     if (!CE)
       return false;
     const auto *FD = CE->getDirectCallee();
     if (!FD)
       return false;
+    if (ignoreUnsafeLibcCall(Ctx, *S, Handler))
+      return false;
 
     const bool IsGlobalAndNotInAnyNamespace =
         FD->isGlobal() && !FD->getEnclosingNamespaceContext()->isNamespace();
@@ -2461,14 +2461,14 @@ class UnsafeFormatAttributedFunctionCallGadget : public 
WarningGadget {
   static bool matches(const Stmt *S, ASTContext &Ctx,
                       const UnsafeBufferUsageHandler *Handler,
                       MatchResult &Result) {
-    if (ignoreUnsafeLibcCall(Ctx, *S, Handler))
-      return false;
     auto *CE = dyn_cast<CallExpr>(S);
     if (!CE || !CE->getDirectCallee())
       return false;
     const FunctionDecl *FD = CE->getDirectCallee();
     if (!FD)
       return false;
+    if (ignoreUnsafeLibcCall(Ctx, *S, Handler))
+      return false;
 
     const FormatAttr *Attr = nullptr;
     bool IsPrintf = false;

>From 7c548bc8b8ccf1eccbc81d8fd0519e7e9e65cd15 Mon Sep 17 00:00:00 2001
From: Nico Weber <[email protected]>
Date: Mon, 28 Sep 2026 08:55:48 -0700
Subject: [PATCH 2/2] comments

---
 clang/lib/Analysis/UnsafeBufferUsage.cpp | 26 +++++++++++++-----------
 1 file changed, 14 insertions(+), 12 deletions(-)

diff --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp 
b/clang/lib/Analysis/UnsafeBufferUsage.cpp
index 7c510f7a74181..9a4269acb1cdb 100644
--- a/clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -1876,10 +1876,8 @@ class SpanTwoParamConstructorGadget : public 
WarningGadget {
     return G->getKind() == Kind::SpanTwoParamConstructor;
   }
 
-  static bool matches(const Stmt *S, ASTContext &Ctx, MatchResult &Result) {
-    const auto *CE = dyn_cast<CXXConstructExpr>(S);
-    if (!CE)
-      return false;
+  static bool matches(const CXXConstructExpr *CE, ASTContext &Ctx,
+                      MatchResult &Result) {
     const auto *CDecl = CE->getConstructor();
     const auto *CRecordDecl = CDecl->getParent();
     auto HasTwoParamSpanCtorDecl =
@@ -1894,9 +1892,12 @@ class SpanTwoParamConstructorGadget : public 
WarningGadget {
   static bool matches(const Stmt *S, ASTContext &Ctx,
                       const UnsafeBufferUsageHandler *Handler,
                       MatchResult &Result) {
-    if (!isa<CXXConstructExpr>(S) || ignoreUnsafeBufferInContainer(*S, 
Handler))
+    const auto *CE = dyn_cast<CXXConstructExpr>(S);
+    if (!CE)
+      return false;
+    if (ignoreUnsafeBufferInContainer(*S, Handler))
       return false;
-    return matches(S, Ctx, Result);
+    return matches(CE, Ctx, Result);
   }
 
   void handleUnsafeOperation(UnsafeBufferUsageHandler &Handler,
@@ -1934,10 +1935,8 @@ class StringViewTwoParamConstructorGadget : public 
WarningGadget {
     return G->getKind() == Kind::StringViewTwoParamConstructor;
   }
 
-  static bool matches(const Stmt *S, ASTContext &Ctx, MatchResult &Result) {
-    const auto *CE = dyn_cast<CXXConstructExpr>(S);
-    if (!CE)
-      return false;
+  static bool matches(const CXXConstructExpr *CE, ASTContext &Ctx,
+                      MatchResult &Result) {
     const auto *CDecl = CE->getConstructor();
     const auto *CRecordDecl = CDecl->getParent();
 
@@ -1957,9 +1956,12 @@ class StringViewTwoParamConstructorGadget : public 
WarningGadget {
   static bool matches(const Stmt *S, ASTContext &Ctx,
                       const UnsafeBufferUsageHandler *Handler,
                       MatchResult &Result) {
-    if (!isa<CXXConstructExpr>(S) || ignoreUnsafeBufferInContainer(*S, 
Handler))
+    const auto *CE = dyn_cast<CXXConstructExpr>(S);
+    if (!CE)
+      return false;
+    if (ignoreUnsafeBufferInContainer(*S, Handler))
       return false;
-    return matches(S, Ctx, Result);
+    return matches(CE, Ctx, Result);
   }
 
   void handleUnsafeOperation(UnsafeBufferUsageHandler &Handler,

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to