[libc-commits] [libc] 9b8a64b - [libc] add noexcept to external function headers

Michael Jones via libc-commits libc-commits at lists.llvm.org
Fri Jan 6 11:43:49 PST 2023


Author: Michael Jones
Date: 2023-01-06T11:43:43-08:00
New Revision: 9b8a64b88dbcb3e881b5b659e91ba6b99245744d

URL: https://github.com/llvm/llvm-project/commit/9b8a64b88dbcb3e881b5b659e91ba6b99245744d
DIFF: https://github.com/llvm/llvm-project/commit/9b8a64b88dbcb3e881b5b659e91ba6b99245744d.diff

LOG: [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

Added: 
    

Modified: 
    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

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/api.td b/libc/config/linux/api.td
index 95542ef129bcb..4f51ac7d19120 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 ce289e55b13dc..6b883ee21a8c4 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 b7b2707849b90..6069533f9278e 100644
--- a/libc/test/src/time/gmtime_test.cpp
+++ b/libc/test/src/time/gmtime_test.cpp
@@ -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;

diff  --git a/libc/test/src/time/mktime_test.cpp b/libc/test/src/time/mktime_test.cpp
index 16b814f1d4255..e046fdcaac273 100644
--- a/libc/test/src/time/mktime_test.cpp
+++ b/libc/test/src/time/mktime_test.cpp
@@ -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;

diff  --git a/libc/utils/HdrGen/PrototypeTestGen/PrototypeTestGen.cpp b/libc/utils/HdrGen/PrototypeTestGen/PrototypeTestGen.cpp
index 06f621052f154..016826faf77ee 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 e1e8f4ee42b9e..b1c7a072658ff 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.


        


More information about the libc-commits mailing list