llvm-c166/libc/test/include/alloca_test.cpp
Jeff Bailey fe616e4d87
[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.
2026-07-16 21:43:12 +01:00

37 lines
1.1 KiB
C++

//===----------------------------------------------------------------------===//
//
// 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));
}
}