[libc-commits] [libc] [libc] Fix missing Annex K declarations caused by header include order (PR #246120)

via libc-commits libc-commits at lists.llvm.org
Wed Nov 25 10:52:47 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Vaibhav Agrawal (competitive-programmer04)

<details>
<summary>Changes</summary>

The file-level header guard in annex-k-macros.h prevented LIBC_HAS_ANNEX_K from being defined if a standard header was included before __STDC_WANT_LIB_EXT1__ was defined by the user.
This patch replaces the file-level guard with a state-level guard on LIBC_HAS_ANNEX_K. It applies the same state-level guard to errno_t.h, rsize_t.h, and constraint_handler_t.h to prevent lockout on subsequent inclusions.

Fixes #<!-- -->195276

---
Full diff: https://github.com/llvm/llvm-project/pull/246120.diff


6 Files Affected:

- (modified) libc/include/llvm-libc-macros/annex-k-macros.h (+3-4) 
- (modified) libc/include/llvm-libc-types/constraint_handler_t.h (+1-1) 
- (modified) libc/include/llvm-libc-types/errno_t.h (+1-1) 
- (modified) libc/include/llvm-libc-types/rsize_t.h (+1-1) 
- (modified) libc/test/include/CMakeLists.txt (+21) 
- (added) libc/test/include/annex_k_include_order_test.c (+36) 


``````````diff
diff --git a/libc/include/llvm-libc-macros/annex-k-macros.h b/libc/include/llvm-libc-macros/annex-k-macros.h
index 7cfb5c1d3060b2..f40e1fb6fa91fc 100644
--- a/libc/include/llvm-libc-macros/annex-k-macros.h
+++ b/libc/include/llvm-libc-macros/annex-k-macros.h
@@ -6,8 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_LIBC_INCLUDE_LLVM_LIBC_MACROS_ANNEX_K_MACROS_H
-#define LLVM_LIBC_INCLUDE_LLVM_LIBC_MACROS_ANNEX_K_MACROS_H
+#ifndef LIBC_HAS_ANNEX_K
 
 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) ||              \
     (defined(__cplusplus) && __cplusplus >= 201703L)
@@ -18,10 +17,10 @@
 
 #if defined(__STDC_WANT_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__ == 1
 
-#define LIBC_HAS_ANNEX_K
+#define LIBC_HAS_ANNEX_K 1
 
 #endif // defined(__STDC_WANT_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__ == 1
 
 #endif // (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) ||
        // (defined(__cplusplus) && __cplusplus >= 201703L)
-#endif // LLVM_LIBC_INCLUDE_LLVM_LIBC_MACROS_ANNEX_K_MACROS_H
+#endif // LIBC_HAS_ANNEX_K
diff --git a/libc/include/llvm-libc-types/constraint_handler_t.h b/libc/include/llvm-libc-types/constraint_handler_t.h
index 951e42f6c6116b..0f724260284fb0 100644
--- a/libc/include/llvm-libc-types/constraint_handler_t.h
+++ b/libc/include/llvm-libc-types/constraint_handler_t.h
@@ -7,12 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_CONSTRAINT_HANDLER_T_H
-#define LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_CONSTRAINT_HANDLER_T_H
 
 #include "../llvm-libc-macros/annex-k-macros.h"
 #include "errno_t.h"
 
 #ifdef LIBC_HAS_ANNEX_K
+#define LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_CONSTRAINT_HANDLER_T_H
 
 typedef void (*constraint_handler_t)(const char *__restrict, void *__restrict,
                                      errno_t);
diff --git a/libc/include/llvm-libc-types/errno_t.h b/libc/include/llvm-libc-types/errno_t.h
index 4ce694959ef2e5..a0c15788bdd6ef 100644
--- a/libc/include/llvm-libc-types/errno_t.h
+++ b/libc/include/llvm-libc-types/errno_t.h
@@ -7,7 +7,6 @@
 //===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_ERRNO_T_H
-#define LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_ERRNO_T_H
 
 #include "../llvm-libc-macros/annex-k-macros.h"
 
@@ -18,6 +17,7 @@
 // library unless Annex K is enabled, we must guard any code that uses it with
 // LIBC_HAS_ANNEX_K.
 #ifdef LIBC_HAS_ANNEX_K
+#define LLVM_LIBC_INCLUDE_LLVM_LIBC_TYPES_ERRNO_T_H
 
 typedef int errno_t;
 
diff --git a/libc/include/llvm-libc-types/rsize_t.h b/libc/include/llvm-libc-types/rsize_t.h
index 6e92bb99025f8e..2035bbd53ddbb1 100644
--- a/libc/include/llvm-libc-types/rsize_t.h
+++ b/libc/include/llvm-libc-types/rsize_t.h
@@ -7,11 +7,11 @@
 //===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_TYPES_RSIZE_T_H
-#define LLVM_LIBC_TYPES_RSIZE_T_H
 
 #include "../llvm-libc-macros/annex-k-macros.h"
 
 #ifdef LIBC_HAS_ANNEX_K
+#define LLVM_LIBC_TYPES_RSIZE_T_H
 
 #include "size_t.h"
 
diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index 0ea0ae28702a06..c408758e2fa3a7 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -127,6 +127,27 @@ add_libc_test(
     libc.src.sched.sched_getscheduler
 )
 
+if(LLVM_LIBC_ALL_HEADERS OR libc.src.string.strnlen_s IN_LIST TARGET_LLVMLIBC_ENTRYPOINTS)
+  add_libc_test(
+    annex_k_include_order_test
+    C_TEST
+    FULL_BUILD_ONLY
+    SUITE
+      libc_include_tests
+    SRCS
+      annex_k_include_order_test.c
+    COMPILE_OPTIONS
+      -std=c11
+      -Wall
+      -Wextra
+      -Werror
+    DEPENDS
+      libc.include.stdio
+      libc.include.string
+      libc.include.stdlib
+  )
+endif()
+
 add_libc_test(
   issubnormal_test
   SUITE
diff --git a/libc/test/include/annex_k_include_order_test.c b/libc/test/include/annex_k_include_order_test.c
new file mode 100644
index 00000000000000..55a7ee3524a6cd
--- /dev/null
+++ b/libc/test/include/annex_k_include_order_test.c
@@ -0,0 +1,36 @@
+//===-- Tests for Annex K feature selection -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// Regression test for https://github.com/llvm/llvm-project/issues/195276.
+#include <stdio.h>
+
+// Also exercise a type header that stdio.h does not include.
+#include "include/llvm-libc-types/constraint_handler_t.h"
+
+#ifdef LIBC_HAS_ANNEX_K
+#error                                                                         \
+    "LIBC_HAS_ANNEX_K should not be define without defining __STDC_WANT_LIB_EXT1__"
+#endif
+
+// defining LIBC_HAS_ANNEX_K after stdio.h includes annex-k-macros.h
+#define __STDC_WANT_LIB_EXT1__ 1
+
+// including strlen_s and rsize_t
+#include <string.h>
+
+// including constraint_handler_t and errno_t through a different header file
+#include <stdlib.h>
+
+int main(void) {
+  // Verify declaration avalibility without referencing strnlen_s at link time
+  (void)sizeof(strnlen_s("abc", 2));
+  (void)sizeof((errno_t)0);
+  (void)sizeof((rsize_t)0);
+  (void)sizeof((constraint_handler_t)0);
+  return 0;
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/246120


More information about the libc-commits mailing list