[clang] [clang][bytecode][NFC] Only collect non-null args if we have to (PR #152074)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 4 22:01:13 PDT 2025
https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/152074
>From 3bcb6a3d737dfcfe083286be3d664baf7c254ac0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 5 Aug 2025 06:25:55 +0200
Subject: [PATCH] [clang][bytecode][NFC] Only collect non-null args if we have
to
Only do this if the function really has a NonNullArg.
---
clang/lib/AST/ByteCode/Compiler.cpp | 9 +++++++--
clang/lib/AST/ByteCode/InterpShared.cpp | 18 +++++++++---------
clang/test/AST/ByteCode/nullable.cpp | 4 ++--
3 files changed, 18 insertions(+), 13 deletions(-)
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 7a5bd4dfc0bed..2436c844b169d 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