[libc] add noexcept to external function headers
authorMichael Jones <michaelrj@google.com>
Wed, 4 Jan 2023 18:37:51 +0000 (10:37 -0800)
committerMichael Jones <michaelrj@google.com>
Fri, 6 Jan 2023 19:43:43 +0000 (11:43 -0800)
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
libc/include/__llvm-libc-common.h
libc/test/src/time/gmtime_test.cpp
libc/test/src/time/mktime_test.cpp
libc/utils/HdrGen/PrototypeTestGen/PrototypeTestGen.cpp
libc/utils/HdrGen/PublicAPICommand.cpp

index 95542ef..4f51ac7 100644 (file)
@@ -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__))
index ce289e5..6b883ee 100644 (file)
@@ -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
index b7b2707..6069533 100644 (file)
@@ -14,7 +14,6 @@
 
 #include <errno.h>
 #include <limits.h>
-#include <string.h>
 
 using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
 using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
index 16b814f..e046fdc 100644 (file)
@@ -15,7 +15,6 @@
 
 #include <errno.h>
 #include <limits.h>
-#include <string.h>
 
 using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
 using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
index 06f6210..016826f 100644 (file)
@@ -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;
 }
index e1e8f4e..b1c7a07 100644 (file)
@@ -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.