Author: Matt Turner
Date: 2026-11-27T13:06:15-05:00
New Revision: 379c7a0a0e9e816ca00175f90c955dd8ea04c3a3

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

LOG: Reapply "[clang] Suppress -Wuninitialized for unevaluated builtin args" 
(#245821)

This reapplies f19973fdd2fe (#217816), reverted in 550bc0246c54, with a
different implementation.

f19973fdd2fe left the arguments of every builtin with the
`UnevaluatedArguments` attribute out of the CFG, as CFGBuilder already
did for `__builtin_object_size` and `__builtin_dynamic_object_size`.
A warning queued with DiagRuntimeBehavior is kept when its expression
has no CFG block, so warnings in branches of those arguments that fold
away were no longer dropped. This broke -Werror builds of the Linux
kernel, where MAYBE_BUILD_BUG_ON wraps ilog2() in
`__builtin_constant_p`:

__builtin_constant_p(n) ? ... : (sizeof(n) <= 4) ? __ilog2_u32(n) : ...

Now only the two object size builtins leave their arguments out of the
CFG, as before f19973fdd2fe. Instead, the uninitialized values analysis
ignores references in the arguments of any call for which
`CallExpr::isUnevaluatedBuiltinCall` is true. As before, an assignment
in such an argument still counts as initializing the variable.

This fixes spurious -Wuninitialized warnings for
`__builtin_classify_type`, `__builtin_constant_p`,
`__builtin_infer_alloc_token` and `__builtin_os_log_format_buffer_size`.
None of them depends on its arguments' values.
`__builtin_os_log_format_buffer_size` only sizes the buffer; the paired
`__builtin_os_log_format` call evaluates the arguments. The attribute
also covers `__GetExceptionInfo`, `__builtin_amdgcn_processor_is` and
`__builtin_amdgcn_is_invocable`, which are not tested.

CodeGen does emit `__builtin_constant_p`'s argument for
`llvm.is.constant`, but that folds to false for an undefined value:
`int x; return __builtin_constant_p(x);` compiles to `ret i32 0`. GCC 15
warns on this case at -O2 (not at -O0), and does not warn for
`__builtin_classify_type`.

The kernel case is reduced in constant-conversion.c.

`clang/test` passes.

Assisted-by: Claude Code

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.md
    clang/lib/Analysis/CFG.cpp
    clang/lib/Analysis/UninitializedValues.cpp
    clang/test/Analysis/cfg.cpp
    clang/test/Sema/constant-conversion.c
    clang/test/Sema/uninit-variables.c

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index fe68c654f74a68..9199647b8e78d1 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -615,6 +615,11 @@ 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
+  `__builtin_classify_type`, `__builtin_constant_p`,
+  `__builtin_infer_alloc_token` or `__builtin_os_log_format_buffer_size`,
+  none of which depends on its arguments' values.
+
 - 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 6025cf202fb1f1..a259f5e9114fe2 100644
--- a/clang/lib/Analysis/CFG.cpp
+++ b/clang/lib/Analysis/CFG.cpp
@@ -2885,6 +2885,9 @@ CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, 
AddStmtChoice asc) {
       AnalyzerNoReturn = true;
     if (FD->hasAttr<NoThrowAttr>())
       AddEHEdge = false;
+    // Other unevaluated builtins, such as __builtin_constant_p, keep their
+    // arguments so that DiagRuntimeBehavior can drop warnings in branches of
+    // them that fold away.
     if (isBuiltinAssumeWithSideEffects(FD->getASTContext(), C) ||
         FD->getBuiltinID() == Builtin::BI__builtin_object_size ||
         FD->getBuiltinID() == Builtin::BI__builtin_dynamic_object_size)

diff  --git a/clang/lib/Analysis/UninitializedValues.cpp 
b/clang/lib/Analysis/UninitializedValues.cpp
index 94252c56bd5237..111f78f677423b 100644
--- a/clang/lib/Analysis/UninitializedValues.cpp
+++ b/clang/lib/Analysis/UninitializedValues.cpp
@@ -287,6 +287,7 @@ class ClassifyRefs : public ConstStmtVisitor<ClassifyRefs> {
   }
 
   void classify(const Expr *E, Class C);
+  void ignoreRefs(const Stmt *S);
 
 public:
   ClassifyRefs(AnalysisDeclContext &AC) : DC(cast<DeclContext>(AC.getDecl())) 
{}
@@ -423,7 +424,33 @@ static bool hasTrivialBody(const CallExpr *CE) {
   return false;
 }
 
+void ClassifyRefs::ignoreRefs(const Stmt *S) {
+  SmallVector<const Stmt *, 4> Worklist = {S};
+  while (!Worklist.empty()) {
+    const Stmt *Cur = Worklist.pop_back_val();
+    // Children can be null, e.g. the missing condition of a for statement in
+    // a statement expression.
+    if (!Cur)
+      continue;
+    if (const auto *DRE = dyn_cast<DeclRefExpr>(Cur)) {
+      Classification[DRE] = Ignore;
+      continue;
+    }
+    llvm::append_range(Worklist, Cur->children());
+  }
+}
+
 void ClassifyRefs::VisitCallExpr(const CallExpr *CE) {
+  // The result of a builtin such as __builtin_constant_p does not depend on
+  // its arguments' values, so a variable there is not a use. The arguments
+  // stay in the CFG so that DiagRuntimeBehavior still drops warnings in
+  // branches of them that fold away.
+  if (CE->isUnevaluatedBuiltinCall(DC->getParentASTContext())) {
+    for (const Expr *Argument : CE->arguments())
+      ignoreRefs(Argument);
+    return;
+  }
+
   // Classify arguments to std::move as used.
   if (CE->isCallToStdMove()) {
     // RecordTypes are handled in SemaDeclCXX.cpp.

diff  --git a/clang/test/Analysis/cfg.cpp b/clang/test/Analysis/cfg.cpp
index 478e457d5dcd40..d155d38050fc59 100644
--- a/clang/test/Analysis/cfg.cpp
+++ b/clang/test/Analysis/cfg.cpp
@@ -82,6 +82,29 @@ void testBuiltinSize() {
   (void)__builtin_object_size(dummy(), 0);
 }
 
+// Unlike __builtin_object_size, __builtin_constant_p keeps its argument in 
the CFG.
+// CHECK-LABEL: void testBuiltinConstantP()
+// CHECK-NEXT: [B2 (ENTRY)]
+// CHECK-NEXT:   Succs (1): B1
+// CHECK-EMPTY:
+// CHECK-NEXT: [B1]
+// CHECK-NEXT:   1: __builtin_constant_p
+// CHECK-NEXT:   2: [B1.1] (ImplicitCastExpr, BuiltinFnToFnPtr, int (*)(...) 
noexcept)
+// CHECK-NEXT:   3: dummy
+// CHECK-NEXT:   4: [B1.3] (ImplicitCastExpr, FunctionToPointerDecay, int 
*(*)(void))
+// CHECK-NEXT:   5: [B1.4]()
+// CHECK-NEXT:   6: [B1.2]([B1.5])
+// CHECK-NEXT:   7: (void)[B1.6] (CStyleCastExpr, ToVoid, void)
+// CHECK-NEXT:   Preds (1): B2
+// CHECK-NEXT:   Succs (1): B0
+// CHECK-EMPTY:
+// CHECK-NEXT: [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/constant-conversion.c 
b/clang/test/Sema/constant-conversion.c
index adba3315cc9b41..de3298dbf10e60 100644
--- a/clang/test/Sema/constant-conversion.c
+++ b/clang/test/Sema/constant-conversion.c
@@ -69,10 +69,17 @@ void test5(void) {
   a.b = 100;
 }
 
+int take_uchar(unsigned char);
+
 void test6(void) {
   // Test that unreachable code doesn't trigger the truncation warning.
   unsigned char x = 0 ? 65535 : 1; // no-warning
   unsigned char y = 1 ? 65535 : 1; // expected-warning {{changes value}}
+
+  // The same holds inside the argument of __builtin_constant_p, as in the
+  // Linux kernel's ilog2().
+  int z = __builtin_constant_p(0 ? take_uchar(65535) : 1); // no-warning
+  int w = __builtin_constant_p(1 ? take_uchar(65535) : 1); // expected-warning 
{{changes value}}
 }
 
 void test7(void) {

diff  --git a/clang/test/Sema/uninit-variables.c 
b/clang/test/Sema/uninit-variables.c
index 17e83de5f489ab..035e98e1342deb 100644
--- a/clang/test/Sema/uninit-variables.c
+++ b/clang/test/Sema/uninit-variables.c
@@ -583,3 +583,30 @@ 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, and
+// infer_alloc_token gets an expression because a bare variable is not read
+// there even when the argument is evaluated.
+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
+  __SIZE_TYPE__ size = __builtin_object_size(p, 0); // no-warning
+  __SIZE_TYPE__ dsize = __builtin_dynamic_object_size(q, 0); // no-warning
+  __SIZE_TYPE__ token = __builtin_infer_alloc_token(sizeof(int) * c); // 
no-warning
+  return classify + constant + (int)size + (int)dsize + (int)token;
+}
+
+// The for statement has null children.
+int unevaluated_builtin_null_children(void) {
+  int x;
+  return __builtin_constant_p(({ for (;;) break; x; })); // no-warning
+}
+
+// __builtin_os_log_format, not this size query, evaluates the arguments.
+__SIZE_TYPE__ 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

Reply via email to