From 9b8a64b88dbcb3e881b5b659e91ba6b99245744d Mon Sep 17 00:00:00 2001 From: Michael Jones Date: Wed, 4 Jan 2023 10:37:51 -0800 Subject: [PATCH] [libc] add noexcept to external function headers To improve code generation for C++ code that directly includes our headers, the external function definitions will now be marked noexcept. This may not be necessary for the internal definitions since we build with the -fno-exceptions flag. Reviewed By: sivachandra Differential Revision: https://reviews.llvm.org/D141095 --- libc/config/linux/api.td | 2 +- libc/include/__llvm-libc-common.h | 6 ++++++ libc/test/src/time/gmtime_test.cpp | 1 - libc/test/src/time/mktime_test.cpp | 1 - libc/utils/HdrGen/PrototypeTestGen/PrototypeTestGen.cpp | 8 ++++---- libc/utils/HdrGen/PublicAPICommand.cpp | 2 +- 6 files changed, 12 insertions(+), 8 deletions(-) diff --git a/libc/config/linux/api.td b/libc/config/linux/api.td index 95542ef129bc..4f51ac7d1912 100644 --- a/libc/config/linux/api.td +++ b/libc/config/linux/api.td @@ -18,7 +18,7 @@ def AssertMacro : MacroDef<"assert"> { #ifdef __cplusplus extern "C" #endif - _Noreturn void __assert_fail(const char *, const char *, unsigned, const char *); + _Noreturn void __assert_fail(const char *, const char *, unsigned, const char *) __NOEXCEPT; #define assert(e) \ ((e) ? (void)0 : __assert_fail(#e, __FILE__, __LINE__, __PRETTY_FUNCTION__)) diff --git a/libc/include/__llvm-libc-common.h b/libc/include/__llvm-libc-common.h index ce289e55b13d..6b883ee21a8c 100644 --- a/libc/include/__llvm-libc-common.h +++ b/libc/include/__llvm-libc-common.h @@ -32,6 +32,9 @@ #undef _Thread_local #define _Thread_local thread_local +#undef __NOEXCEPT +#define __NOEXCEPT noexcept + #else // not __cplusplus #undef __BEGIN_C_DECLS @@ -43,6 +46,9 @@ #undef __restrict #define __restrict restrict // C99 and above support the restrict keyword. +#undef __NOEXCEPT +#define __NOEXCEPT + #endif // __cplusplus #endif // LLVM_LIBC___COMMON_H diff --git a/libc/test/src/time/gmtime_test.cpp b/libc/test/src/time/gmtime_test.cpp index b7b2707849b9..6069533f9278 100644 --- a/libc/test/src/time/gmtime_test.cpp +++ b/libc/test/src/time/gmtime_test.cpp @@ -14,7 +14,6 @@ #include #include -#include using __llvm_libc::testing::ErrnoSetterMatcher::Fails; using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds; diff --git a/libc/test/src/time/mktime_test.cpp b/libc/test/src/time/mktime_test.cpp index 16b814f1d425..e046fdcaac27 100644 --- a/libc/test/src/time/mktime_test.cpp +++ b/libc/test/src/time/mktime_test.cpp @@ -15,7 +15,6 @@ #include #include -#include using __llvm_libc::testing::ErrnoSetterMatcher::Fails; using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds; diff --git a/libc/utils/HdrGen/PrototypeTestGen/PrototypeTestGen.cpp b/libc/utils/HdrGen/PrototypeTestGen/PrototypeTestGen.cpp index 06f621052f15..016826faf77e 100644 --- a/libc/utils/HdrGen/PrototypeTestGen/PrototypeTestGen.cpp +++ b/libc/utils/HdrGen/PrototypeTestGen/PrototypeTestGen.cpp @@ -81,7 +81,7 @@ bool TestGeneratorMain(llvm::raw_ostream &OS, llvm::RecordKeeper &records) { if (i < size - 1) OS << ", "; } - OS << "), decltype(" << entrypoint << ")>, "; + OS << ") __NOEXCEPT, decltype(" << entrypoint << ")>, "; OS << '"' << entrypoint << " prototype in TableGen does not match public header" << '"'; OS << ");\n"; @@ -93,9 +93,9 @@ bool TestGeneratorMain(llvm::raw_ostream &OS, llvm::RecordKeeper &records) { // We provide dummy malloc and free implementations to support the case // when LLVM libc does to include them. - OS << "void *malloc(size_t) { return nullptr; }\n"; - OS << "void *realloc(void *, size_t) { return nullptr; }\n"; - OS << "void free(void *) {}\n"; + OS << "void *malloc(size_t) __NOEXCEPT { return nullptr; }\n"; + OS << "void *realloc(void *, size_t) __NOEXCEPT { return nullptr; }\n"; + OS << "void free(void *) __NOEXCEPT {}\n"; return false; } diff --git a/libc/utils/HdrGen/PublicAPICommand.cpp b/libc/utils/HdrGen/PublicAPICommand.cpp index e1e8f4ee42b9..b1c7a072658f 100644 --- a/libc/utils/HdrGen/PublicAPICommand.cpp +++ b/libc/utils/HdrGen/PublicAPICommand.cpp @@ -112,7 +112,7 @@ void writeAPIFromIndex(APIIndexer &G, OS << ", "; } - OS << ");\n\n"; + OS << ") __NOEXCEPT;\n\n"; } // Make another pass over entrypoints to emit object declarations.