[clang] 5b16893 - [libc] Fix modular printf attributes (#194003)
via cfe-commits
cfe-commits at lists.llvm.org
Wed May 20 05:00:18 PDT 2026
Author: Volodymyr Turanskyy
Date: 2026-05-20T13:00:11+01:00
New Revision: 5b168934d72d4f7ce6177c86bfea0961a0ccc4eb
URL: https://github.com/llvm/llvm-project/commit/5b168934d72d4f7ce6177c86bfea0961a0ccc4eb
DIFF: https://github.com/llvm/llvm-project/commit/5b168934d72d4f7ce6177c86bfea0961a0ccc4eb.diff
LOG: [libc] Fix modular printf attributes (#194003)
This fixes the validation error related to modular printf missing format attribute in C++ code by moving the validation after the implicit format attribute is added for builtins and known library functions.
This also adds a simple C++ test since the C code did compile successfully because the implicit attributes were added in time for the validation happening for C code.
Assisted-by: codex, reviewed and cross checked, also tested with ATfE,
by me. Modular printf reduced code size from ~37K to ~13K for int-only
printf sample.
Added:
clang/test/SemaCXX/attr-modular-format.cpp
Modified:
clang/lib/Sema/SemaDecl.cpp
libc/include/llvm-libc-macros/CMakeLists.txt
Removed:
################################################################################
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 68b64952639af..62cb9360d1322 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -7257,7 +7257,6 @@ static void checkAttributesAfterMerging(Sema &S, NamedDecl &ND) {
checkHybridPatchableAttr(S, ND);
checkInheritableAttr(S, ND);
checkLifetimeBoundAttr(S, ND);
- checkModularFormatAttr(S, ND);
}
static void checkDLLAttributeRedeclaration(Sema &S, NamedDecl *OldDecl,
@@ -11080,6 +11079,10 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC,
checkAttributesAfterMerging(*this, *NewFD);
AddKnownFunctionAttributes(NewFD);
+ // The above can add the format attribute for known builtin/library functions
+ // which is required by the modular_format attribute, thus
+ // validate modular_format now after those attributes have been added.
+ checkModularFormatAttr(*this, *NewFD);
if (NewFD->hasAttr<OverloadableAttr>() &&
!NewFD->getType()->getAs<FunctionProtoType>()) {
diff --git a/clang/test/SemaCXX/attr-modular-format.cpp b/clang/test/SemaCXX/attr-modular-format.cpp
new file mode 100644
index 0000000000000..3be3e7f6e250c
--- /dev/null
+++ b/clang/test/SemaCXX/attr-modular-format.cpp
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+// expected-no-diagnostics
+
+extern "C" int printf(const char *fmt, ...)
+ __attribute__((modular_format(__modular_printf, "__printf", "float")));
diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index 3225b5b1849a1..3472642941613 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -418,7 +418,7 @@ add_macro_header(
sysexits-macros.h
)
-if (LIBC_CONF_MODULAR_FORMAT)
+if (LIBC_CONF_PRINTF_MODULAR)
add_macro_header(
_LIBC_MODULAR_FORMAT_PRINTF
HDR
More information about the cfe-commits
mailing list