https://github.com/mattst88 updated https://github.com/llvm/llvm-project/pull/217816
>From fc2f516c759e4a3880adba639dec58a104da3e30 Mon Sep 17 00:00:00 2001 From: Matt Turner <[email protected]> Date: Fri, 10 Jul 2026 03:15:12 -0400 Subject: [PATCH] [clang] Suppress -Wuninitialized for unevaluated builtin args CFGBuilder leaves the arguments of __builtin_object_size and __builtin_dynamic_object_size out of the CFG because they are never evaluated. Use CallExpr::isUnevaluatedBuiltinCall instead, which checks the UnevaluatedArguments attribute, so the same applies to every builtin declared that way. None of them uses its arguments' values. __builtin_constant_p loads its argument for llvm.is.constant, but that folds to false for an undefined value, so an uninitialized argument cannot change the result. This fixes spurious -Wuninitialized warnings for __builtin_classify_type, __builtin_constant_p, __builtin_infer_alloc_token and __builtin_os_log_format_buffer_size. For the last one, the paired __builtin_os_log_format call evaluates the arguments. Assisted-by: Claude Code --- clang/docs/ReleaseNotes.md | 7 +++++++ clang/lib/Analysis/CFG.cpp | 3 +-- clang/test/Analysis/cfg.cpp | 17 +++++++++++++++++ clang/test/Sema/uninit-variables.c | 19 +++++++++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index aab88904167abc..9e043481cbf4d0 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -581,6 +581,13 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and `-pedantic` or when that group is enabled explicitly, matching how the `_BitInt` type itself is already handled. +- `-Wuninitialized` no longer warns about an uninitialized variable passed to a + builtin that does not evaluate its arguments. It already made this exception + for `__builtin_object_size` and `__builtin_dynamic_object_size`; it now + applies to every builtin declared with unevaluated arguments, including + `__builtin_classify_type`, `__builtin_constant_p` and + `__builtin_os_log_format_buffer_size`. + - Fixed bug in `-Wdocumentation` so that it correctly handles explicit function template instantiations (#64087). diff --git a/clang/lib/Analysis/CFG.cpp b/clang/lib/Analysis/CFG.cpp index 1c69f5d71583f5..1c44ffa0e21017 100644 --- a/clang/lib/Analysis/CFG.cpp +++ b/clang/lib/Analysis/CFG.cpp @@ -2918,8 +2918,7 @@ CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) { if (FD->hasAttr<NoThrowAttr>()) AddEHEdge = false; if (isBuiltinAssumeWithSideEffects(FD->getASTContext(), C) || - FD->getBuiltinID() == Builtin::BI__builtin_object_size || - FD->getBuiltinID() == Builtin::BI__builtin_dynamic_object_size) + C->isUnevaluatedBuiltinCall(*Context)) OmitArguments = true; } diff --git a/clang/test/Analysis/cfg.cpp b/clang/test/Analysis/cfg.cpp index 478e457d5dcd40..b9f897536d6527 100644 --- a/clang/test/Analysis/cfg.cpp +++ b/clang/test/Analysis/cfg.cpp @@ -82,6 +82,23 @@ void testBuiltinSize() { (void)__builtin_object_size(dummy(), 0); } +// CHECK-LABEL: void testBuiltinConstantP() +// CHECK: ENTRY +// CHECK-NEXT: Succs (1): B1 +// CHECK: [B1] +// CHECK-NEXT: 1: __builtin_constant_p +// CHECK-NEXT: 2: [B1.1] (ImplicitCastExpr, BuiltinFnToFnPtr, int (*)(...) noexcept) +// CHECK-NEXT: 3: [B1.2](dummy()) +// CHECK-NEXT: 4: (void)[B1.3] (CStyleCastExpr, ToVoid, void) +// CHECK-NEXT: Preds (1): B2 +// CHECK-NEXT: Succs (1): B0 +// CHECK: [B0 (EXIT)] +// CHECK-NEXT: Preds (1): B1 +void testBuiltinConstantP() { + extern int *dummy(); + (void)__builtin_constant_p(dummy()); +} + class A { public: A() {} diff --git a/clang/test/Sema/uninit-variables.c b/clang/test/Sema/uninit-variables.c index 17e83de5f489ab..d9833d63504bd1 100644 --- a/clang/test/Sema/uninit-variables.c +++ b/clang/test/Sema/uninit-variables.c @@ -583,3 +583,22 @@ void aggregate() { (void)sizeof({ struct with_explicit_field a; a; }); // no warning -- unevaluated operand } + +// These builtins never read their arguments' values. Each uses its own +// variable because only the first use of a variable is diagnosed. +int unevaluated_builtin_args(void) { + int a, b, c; + char *p, *q; + int classify = __builtin_classify_type(a); // no-warning + int constant = __builtin_constant_p(b); // no-warning + unsigned long size = __builtin_object_size(p, 0); // no-warning + unsigned long dsize = __builtin_dynamic_object_size(q, 0); // no-warning + unsigned long token = __builtin_infer_alloc_token(sizeof(int) * c); // no-warning + return classify + constant + (int)size + (int)dsize + (int)token; +} + +// __builtin_os_log_format, not this size query, evaluates the arguments. +unsigned long unevaluated_os_log_buffer_size(void) { + int x; + return __builtin_os_log_format_buffer_size("%d", x); // no-warning +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
