[libc] Implement alloca.h header (#210150)

Implemented the alloca.h header for LLVM libc.

* libc/include/llvm-libc-macros/alloca-macros.h: Defined alloca macro
using __builtin_alloca.
* libc/include/alloca.yaml: Added header specification.
* libc/include/CMakeLists.txt,
libc/include/llvm-libc-macros/CMakeLists.txt: Registered the new header
and macro targets.
* libc/config/linux/*/headers.txt: Enabled the header for Linux targets
(x86_64, aarch64, arm, i386, riscv).
* libc/test/include/alloca_test.cpp, libc/test/include/CMakeLists.txt:
Added and registered functional unit tests.

Tested by running the new unit and hermetic tests.

Assisted-by: Automated tooling, human reviewed.
This commit is contained in:
Jeff Bailey 2026-07-16 21:43:12 +01:00 committed by GitHub
parent d49307497b
commit fe616e4d87
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 98 additions and 0 deletions

View file

@ -1,4 +1,5 @@
set(TARGET_PUBLIC_HEADERS
libc.include.alloca
libc.include.arpa_inet
libc.include.assert
libc.include.byteswap

View file

@ -1,4 +1,5 @@
set(TARGET_PUBLIC_HEADERS
libc.include.alloca
libc.include.complex
libc.include.cpio
libc.include.ctype

View file

@ -1,4 +1,5 @@
set(TARGET_PUBLIC_HEADERS
libc.include.alloca
libc.include.assert
libc.include.cpio
)

View file

@ -1,4 +1,5 @@
set(TARGET_PUBLIC_HEADERS
libc.include.alloca
libc.include.arpa_inet
libc.include.assert
libc.include.byteswap

View file

@ -1,4 +1,5 @@
set(TARGET_PUBLIC_HEADERS
libc.include.alloca
libc.include.arpa_inet
libc.include.assert
libc.include.byteswap

View file

@ -211,6 +211,16 @@ add_header_macro(
.llvm-libc-macros.sysexits_macros
)
add_header_macro(
alloca
../libc/include/alloca.yaml
alloca.h
DEPENDS
.llvm_libc_common_h
.llvm-libc-macros.alloca_macros
.llvm-libc-types.size_t
)
add_header_macro(
assert
../libc/include/assert.yaml

11
libc/include/alloca.yaml Normal file
View file

@ -0,0 +1,11 @@
header: alloca.h
standards:
- gnu
macros:
- macro_name: alloca
macro_header: alloca-macros.h
types:
- type_name: size_t
enums: []
objects: []
functions: []

View file

@ -459,3 +459,9 @@ else()
_LIBC_MODULAR_FORMAT_PRINTF.h
)
endif()
add_macro_header(
alloca_macros
HDR
alloca-macros.h
)

View file

@ -0,0 +1,19 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
///
/// \file
/// Defines the alloca macro.
///
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_MACROS_ALLOCA_MACROS_H
#define LLVM_LIBC_MACROS_ALLOCA_MACROS_H
#define alloca(size) __builtin_alloca(size)
#endif // LLVM_LIBC_MACROS_ALLOCA_MACROS_H

View file

@ -1,6 +1,16 @@
add_custom_target(libc_include_tests)
add_dependencies(check-libc libc_include_tests-build)
add_libc_test(
alloca_test
SUITE
libc_include_tests
SRCS
alloca_test.cpp
DEPENDS
libc.include.llvm-libc-macros.alloca_macros
)
add_libc_test(
assert_test
SUITE

View file

@ -0,0 +1,37 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
///
/// \file
/// Unittests for alloca.
///
//===----------------------------------------------------------------------===//
#include "test/UnitTest/Test.h"
#include "include/llvm-libc-macros/alloca-macros.h"
TEST(LlvmLibcAllocaTest, Basic) {
// Ensure the macro is defined.
#ifndef alloca
FAIL() << "alloca macro is not defined";
#endif
// Allocate some memory on the stack.
void *ptr = alloca(10);
ASSERT_NE(ptr, static_cast<void *>(nullptr));
// Write to it to make sure it's valid memory.
char *char_ptr = static_cast<char *>(ptr);
for (int i = 0; i < 10; ++i) {
char_ptr[i] = static_cast<char>(i);
}
for (int i = 0; i < 10; ++i) {
EXPECT_EQ(char_ptr[i], static_cast<char>(i));
}
}