[libc] add wctype.h header (#149202)

Add basic configurations to generate wctype.h header file. To begin with
this header file just exposes one function iswalpha.
This commit is contained in:
Prabhu Rajasekaran
2025-07-17 13:06:04 -07:00
committed by GitHub
parent 48cd22c566
commit e8182fb501
25 changed files with 166 additions and 0 deletions

View File

@@ -278,6 +278,9 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.wchar.wcslen
libc.src.wchar.wctob
# wctype.h entrypoints
libc.src.wctype.iswalpha
# internal entrypoints
libc.startup.baremetal.init
libc.startup.baremetal.fini

View File

@@ -23,4 +23,5 @@ set(TARGET_PUBLIC_HEADERS
libc.include.time
libc.include.uchar
libc.include.wchar
libc.include.wctype
)

View File

@@ -278,6 +278,9 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.wchar.wcslen
libc.src.wchar.wctob
# wctype.h entrypoints
libc.src.wctype.iswalpha
# internal entrypoints
libc.startup.baremetal.init
libc.startup.baremetal.fini

View File

@@ -23,4 +23,5 @@ set(TARGET_PUBLIC_HEADERS
libc.include.time
libc.include.uchar
libc.include.wchar
libc.include.wctype
)

View File

@@ -99,6 +99,9 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.calloc
libc.src.stdlib.realloc
libc.src.stdlib.free
# wctype.h entrypoints
libc.src.wctype.iswalpha
)
if(LLVM_LIBC_FULL_BUILD)

View File

@@ -11,4 +11,5 @@ set(TARGET_PUBLIC_HEADERS
libc.include.stdlib
libc.include.string
libc.include.strings
libc.include.wctype
)

View File

@@ -363,6 +363,9 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.wchar.wcslen
libc.src.wchar.wctob
# wctype.h entrypoints
libc.src.wctype.iswalpha
# sys/uio.h entrypoints
libc.src.sys.uio.writev
libc.src.sys.uio.readv

View File

@@ -57,4 +57,5 @@ set(TARGET_PUBLIC_HEADERS
libc.include.uchar
libc.include.unistd
libc.include.wchar
libc.include.wctype
)

View File

@@ -191,6 +191,9 @@ set(TARGET_LIBC_ENTRYPOINTS
# sys/time.h entrypoints
libc.src.sys.time.setitimer
libc.src.sys.time.getitimer
# wctype.h entrypoints
libc.src.wctype.iswalpha
)
if(LLVM_LIBC_FULL_BUILD)

View File

@@ -17,6 +17,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.strings
libc.include.uchar
libc.include.wchar
libc.include.wctype
# Disabled due to epoll_wait syscalls not being available on this platform.
# libc.include.sys_epoll

View File

@@ -368,6 +368,9 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.wchar.wcslen
libc.src.wchar.wctob
# wctype.h entrypoints
libc.src.wctype.iswalpha
# sys/uio.h entrypoints
libc.src.sys.uio.writev
libc.src.sys.uio.readv

View File

@@ -57,4 +57,5 @@ set(TARGET_PUBLIC_HEADERS
libc.include.uchar
libc.include.unistd
libc.include.wchar
libc.include.wctype
)

View File

@@ -396,6 +396,8 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.wchar.wcstoul
libc.src.wchar.wcstoull
# wctype.h entrypoints
libc.src.wctype.iswalpha
# sys/uio.h entrypoints
libc.src.sys.uio.writev

View File

@@ -57,4 +57,5 @@ set(TARGET_PUBLIC_HEADERS
libc.include.uchar
libc.include.unistd
libc.include.wchar
libc.include.wctype
)

View File

@@ -105,6 +105,9 @@ set(TARGET_LIBC_ENTRYPOINTS
# unistd.h entrypoints
libc.src.unistd.getentropy
# wctype.h entrypoints
libc.src.wctype.iswalpha
)
set(TARGET_LIBM_ENTRYPOINTS

View File

@@ -7,4 +7,5 @@ set(TARGET_PUBLIC_HEADERS
libc.include.fenv
libc.include.math
libc.include.unistd
libc.include.wctype
)

View File

@@ -720,6 +720,15 @@ add_header_macro(
.llvm-libc-types.wchar_t
)
add_header_macro(
wctype
../libc/include/wctype.yaml
wctype.h
DEPENDS
.llvm_libc_common_h
.llvm-libc-types.wint_t
)
add_header_macro(
locale
../libc/include/locale.yaml

10
libc/include/wctype.yaml Normal file
View File

@@ -0,0 +1,10 @@
header: wctype.h
types:
- type_name: wint_t
functions:
- name: iswalpha
standards:
- stdc
return_type: int
arguments:
- type: wint_t

View File

@@ -17,6 +17,7 @@ add_subdirectory(strings)
add_subdirectory(time)
add_subdirectory(unistd)
add_subdirectory(wchar)
add_subdirectory(wctype)
if(${LIBC_TARGET_OS} STREQUAL "linux")
add_subdirectory(dirent)

View File

@@ -0,0 +1,9 @@
add_entrypoint_object(
iswalpha
SRCS
iswalpha.cpp
HDRS
iswalpha.h
DEPENDS
libc.src.__support.wctype_utils
)

View File

@@ -0,0 +1,19 @@
//===-- Implementation of iswalpha ----------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "src/wctype/iswalpha.h"
#include "src/__support/common.h"
#include "src/__support/wctype_utils.h"
#include "hdr/types/wint_t.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(bool, iswalpha, (wint_t c)) { return internal::iswalpha(c); }
} // namespace LIBC_NAMESPACE_DECL

View File

@@ -0,0 +1,21 @@
//===-- Implementation header for iswalpha ----------------------*- 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
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC_WCTYPE_ISWALPHA_H
#define LLVM_LIBC_SRC_WCTYPE_ISWALPHA_H
#include "hdr/types/wint_t.h"
#include "src/__support/common.h"
namespace LIBC_NAMESPACE_DECL {
bool iswalpha(wint_t c);
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC_WCTYPE_ISWALPHA_H

View File

@@ -70,6 +70,7 @@ add_subdirectory(stdlib)
add_subdirectory(string)
add_subdirectory(strings)
add_subdirectory(wchar)
add_subdirectory(wctype)
add_subdirectory(time)
add_subdirectory(unistd)

View File

@@ -0,0 +1,11 @@
add_custom_target(libc_wctype_unittests)
add_libc_test(
iswalpha_test
SUITE
libc_wctype_unittests
SRCS
iswalpha_test.cpp
DEPENDS
libc.src.wctype.iswalpha
)

View File

@@ -0,0 +1,54 @@
//===-- Unittests for iswalpha --------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "src/__support/CPP/span.h"
#include "src/wctype/iswalpha.h"
#include "test/UnitTest/LibcTest.h"
#include "test/UnitTest/Test.h"
namespace {
// TODO: Merge the wctype tests using this framework.
constexpr char WALPHA_ARRAY[] = {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
};
bool in_span(int ch, LIBC_NAMESPACE::cpp::span<const char> arr) {
for (size_t i = 0; i < arr.size(); ++i)
if (static_cast<int>(arr[i]) == ch)
return true;
return false;
}
} // namespace
TEST(LlvmLibciswalpha, SimpleTest) {
EXPECT_TRUE(LIBC_NAMESPACE::iswalpha('a'));
EXPECT_TRUE(LIBC_NAMESPACE::iswalpha('B'));
EXPECT_FALSE(LIBC_NAMESPACE::iswalpha('3'));
EXPECT_FALSE(LIBC_NAMESPACE::iswalpha(' '));
EXPECT_FALSE(LIBC_NAMESPACE::iswalpha('?'));
EXPECT_FALSE(LIBC_NAMESPACE::iswalpha('\0'));
EXPECT_FALSE(LIBC_NAMESPACE::iswalpha(-1));
}
TEST(LlvmLibciswalpha, DefaultLocale) {
// Loops through all characters, verifying that letters return
// true and everything else returns false.
for (int ch = -255; ch < 255; ++ch) {
if (in_span(ch, WALPHA_ARRAY))
EXPECT_TRUE(LIBC_NAMESPACE::iswalpha(ch));
else
EXPECT_FALSE(LIBC_NAMESPACE::iswalpha(ch));
}
}