Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc] add basic wide char functions
This patch adds the wchar header, as well as the functions to convert to and from wide chars. The header also sets up the definitions for wint and wchar. Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D145995
- Loading branch information
1 parent
c600b99
commit 46b5087
Showing
25 changed files
with
354 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -166,3 +166,9 @@ add_header( | |
DEPENDS | ||
.linux.unistd_macros | ||
) | ||
|
||
add_header( | ||
wchar_macros | ||
HDR | ||
wchar-macros.h | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//===-- Macros defined in wchar.h header file -----------------------------===// | ||
// | ||
// 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_MACROS_WCHAR_MACROS_H | ||
#define __LLVM_LIBC_MACROS_WCHAR_MACROS_H | ||
|
||
#ifndef WEOF | ||
#define WEOF 0xffffffffu | ||
#endif | ||
|
||
#endif // __LLVM_LIBC_MACROS_WCHAR_MACROS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//===-- Definition of wchar_t types ---------------------------------------===// | ||
// | ||
// 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_TYPES_WCHAR_T_H__ | ||
#define __LLVM_LIBC_TYPES_WCHAR_T_H__ | ||
|
||
// Since __need_wchar_t is defined, we get the definition of wchar_t from the | ||
// standalone C header stddef.h. Also, because __need_wchar_t is defined, | ||
// including stddef.h will pull only the type wchar_t and nothing else. | ||
#define __need_wchar_t | ||
#include <stddef.h> | ||
#undef __need_wchar_t | ||
|
||
#endif // __LLVM_LIBC_TYPES_WCHAR_T_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//===-- Definition of wint_t types ----------------------------------------===// | ||
// | ||
// 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_TYPES_WINT_T_H__ | ||
#define __LLVM_LIBC_TYPES_WINT_T_H__ | ||
|
||
// Since __need_wint_t is defined, we get the definition of wint_t from the | ||
// standalone C header stddef.h. Also, because __need_wint_t is defined, | ||
// including stddef.h will pull only the type wint_t and nothing else. | ||
#define __need_wint_t | ||
#include <stddef.h> | ||
#undef __need_wint_t | ||
|
||
#endif // __LLVM_LIBC_TYPES_WINT_T_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//===-- C standard library header wchar.h ---------------------------------===// | ||
// | ||
// 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_WCHAR_H | ||
#define LLVM_LIBC_WCHAR_H | ||
|
||
#include <__llvm-libc-common.h> | ||
#include <llvm-libc-macros/wchar-macros.h> | ||
|
||
%%public_api() | ||
|
||
#endif // LLVM_LIBC_WCHAR_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//===-- Collection of utils for implementing wide char functions --*-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_SUPPORT_WCTYPE_UTILS_H | ||
#define LLVM_LIBC_SRC_SUPPORT_WCTYPE_UTILS_H | ||
|
||
#include "src/__support/CPP/optional.h" | ||
#include "src/__support/macros/attributes.h" // LIBC_INLINE | ||
|
||
#define __need_wint_t | ||
#define __need_wchar_t | ||
#include <stddef.h> // needed for wint_t and wchar_t | ||
|
||
namespace __llvm_libc { | ||
namespace internal { | ||
|
||
// ------------------------------------------------------ | ||
// Rationale: Since these classification functions are | ||
// called in other functions, we will avoid the overhead | ||
// of a function call by inlining them. | ||
// ------------------------------------------------------ | ||
|
||
LIBC_INLINE cpp::optional<int> wctob(wint_t c) { | ||
// This needs to be translated to EOF at the callsite. This is to avoid | ||
// including stdio.h in this file. | ||
if (c > 127 || c < 0) | ||
return cpp::nullopt; | ||
return static_cast<int>(c); | ||
} | ||
|
||
LIBC_INLINE cpp::optional<wint_t> btowc(int c) { | ||
if (c > 127 || c < 0) | ||
return cpp::nullopt; | ||
return static_cast<wint_t>(c); | ||
} | ||
|
||
} // namespace internal | ||
} // namespace __llvm_libc | ||
|
||
#endif // LLVM_LIBC_SRC_SUPPORT_WCTYPE_UTILS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
add_entrypoint_object( | ||
wctob | ||
SRCS | ||
wctob.cpp | ||
HDRS | ||
wctob.h | ||
DEPENDS | ||
libc.src.__support.wctype_utils | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//===-- Implementation of btowc -------------------------------------------===// | ||
// | ||
// 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/wchar/btowc.h" | ||
#include "src/__support/common.h" | ||
#include "src/__support/wctype_utils.h" | ||
|
||
#include <stdio.h> // for EOF. | ||
|
||
namespace __llvm_libc { | ||
|
||
LLVM_LIBC_FUNCTION(int, btowc, (wint_t c)) { | ||
auto result = internal::btowc(c); | ||
if (result.has_value()) { | ||
return result.value(); | ||
} else { | ||
return WEOF; | ||
} | ||
} | ||
|
||
} // namespace __llvm_libc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//===-- Implementation header for btowc -------------------------*- 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_WCHAR_BTOWC_H | ||
#define LLVM_LIBC_SRC_WCHAR_BTOWC_H | ||
|
||
#include <wchar.h> | ||
|
||
namespace __llvm_libc { | ||
|
||
wint_t btowc(int c); | ||
|
||
} // namespace __llvm_libc | ||
|
||
#endif // LLVM_LIBC_SRC_WCHAR_BTOWC_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//===-- Implementation of wctob -------------------------------------------===// | ||
// | ||
// 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/wchar/wctob.h" | ||
#include "src/__support/common.h" | ||
#include "src/__support/wctype_utils.h" | ||
|
||
#include <stdio.h> // for EOF. | ||
|
||
namespace __llvm_libc { | ||
|
||
LLVM_LIBC_FUNCTION(int, wctob, (wint_t c)) { | ||
auto result = internal::wctob(c); | ||
if (result.has_value()) { | ||
return result.value(); | ||
} else { | ||
return EOF; | ||
} | ||
} | ||
|
||
} // namespace __llvm_libc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//===-- Implementation header for wctob -------------------------*- 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_WCHAR_WCTOB_H | ||
#define LLVM_LIBC_SRC_WCHAR_WCTOB_H | ||
|
||
#include <wchar.h> | ||
|
||
namespace __llvm_libc { | ||
|
||
int wctob(wint_t c); | ||
|
||
} // namespace __llvm_libc | ||
|
||
#endif // LLVM_LIBC_SRC_WCHAR_WCTOB_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.