[clang] df3f629 - [clang][bytecode][NFC] Only collect non-null args if we have to (#152074)

via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 4 22:54:28 PDT 2025


Author: Timm Baeder
Date: 2025-08-05T07:54:25+02:00
New Revision: df3f6297354b3ea5fa53cc6a9d5464b54aae6f15

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

LOG: [clang][bytecode][NFC] Only collect non-null args if we have to (#152074)

Only do this if the function really has a NonNullArg.

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Compiler.cpp
    clang/lib/AST/ByteCode/InterpShared.cpp
    clang/test/AST/ByteCode/nullable.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 5bcac390feeae..6b74b5ea1c973 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -2038,7 +2038,12 @@ bool Compiler<Emitter>::visitCallArgs(ArrayRef<const Expr *> Args,
                                       const FunctionDecl *FuncDecl,
                                       bool Activate) {
   assert(VarScope->getKind() == ScopeKind::Call);
-  llvm::BitVector NonNullArgs = collectNonNullArgs(FuncDecl, Args);
+  bool HasNonNullAttr = false;
+  llvm::BitVector NonNullArgs;
+  if (FuncDecl && FuncDecl->hasAttr<NonNullAttr>()) {
+    HasNonNullAttr = true;
+    NonNullArgs = collectNonNullArgs(FuncDecl, Args);
+  }
 
   unsigned ArgIndex = 0;
   for (const Expr *Arg : Args) {
@@ -2064,7 +2069,7 @@ bool Compiler<Emitter>::visitCallArgs(ArrayRef<const Expr *> Args,
         return false;
     }
 
-    if (FuncDecl && NonNullArgs[ArgIndex]) {
+    if (HasNonNullAttr && NonNullArgs[ArgIndex]) {
       PrimType ArgT = classify(Arg).value_or(PT_Ptr);
       if (ArgT == PT_Ptr) {
         if (!this->emitCheckNonNullArg(ArgT, Arg))

diff  --git a/clang/lib/AST/ByteCode/InterpShared.cpp b/clang/lib/AST/ByteCode/InterpShared.cpp
index 1e94dc19d03c1..e01b32bc63a2a 100644
--- a/clang/lib/AST/ByteCode/InterpShared.cpp
+++ b/clang/lib/AST/ByteCode/InterpShared.cpp
@@ -16,23 +16,23 @@ namespace interp {
 llvm::BitVector collectNonNullArgs(const FunctionDecl *F,
                                    ArrayRef<const Expr *> Args) {
   llvm::BitVector NonNullArgs;
-  if (!F)
-    return NonNullArgs;
 
   assert(F);
+  assert(F->hasAttr<NonNullAttr>());
   NonNullArgs.resize(Args.size());
 
   for (const auto *Attr : F->specific_attrs<NonNullAttr>()) {
     if (!Attr->args_size()) {
       NonNullArgs.set();
       break;
-    } else
-      for (auto Idx : Attr->args()) {
-        unsigned ASTIdx = Idx.getASTIndex();
-        if (ASTIdx >= Args.size())
-          continue;
-        NonNullArgs[ASTIdx] = true;
-      }
+    }
+
+    for (auto Idx : Attr->args()) {
+      unsigned ASTIdx = Idx.getASTIndex();
+      if (ASTIdx >= Args.size())
+        continue;
+      NonNullArgs[ASTIdx] = true;
+    }
   }
 
   return NonNullArgs;

diff  --git a/clang/test/AST/ByteCode/nullable.cpp b/clang/test/AST/ByteCode/nullable.cpp
index 3bc2595fb8f00..9e79fc7dd5a39 100644
--- a/clang/test/AST/ByteCode/nullable.cpp
+++ b/clang/test/AST/ByteCode/nullable.cpp
@@ -1,5 +1,5 @@
-// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=expected,both %s
-// RUN: %clang_cc1 -verify=ref,both %s
+// RUN: %clang_cc1 -verify=expected,both %s -fexperimental-new-constant-interpreter
+// RUN: %clang_cc1 -verify=ref,both      %s
 
 
 constexpr int dummy = 1;


        


More information about the cfe-commits mailing list