Skip to content

Conversation

@frobtech
Copy link
Contributor

@frobtech frobtech commented Jan 7, 2025

C89 doesn't have the restrict keyword. When in -std=c89
mode, GNU-compatible compilers require the __restrict spelling.

C89 doesn't have the `restrict` keyword.  When in `-std=c89`
mode, GNU-compatible compilers require the `__restrict` spelling.
@frobtech frobtech marked this pull request as ready for review January 7, 2025 19:08
@llvmbot llvmbot added the libc label Jan 7, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 7, 2025

@llvm/pr-subscribers-libc

Author: Roland McGrath (frobtech)

Changes

C89 doesn't have the restrict keyword. When in -std=c89
mode, GNU-compatible compilers require the __restrict spelling.


Full diff: https://github.com/llvm/llvm-project/pull/121981.diff

1 Files Affected:

  • (modified) libc/include/__llvm-libc-common.h (+8-1)
diff --git a/libc/include/__llvm-libc-common.h b/libc/include/__llvm-libc-common.h
index d9d70aff771c08..c63eb134a5e5d7 100644
--- a/libc/include/__llvm-libc-common.h
+++ b/libc/include/__llvm-libc-common.h
@@ -50,7 +50,14 @@
 #define __END_C_DECLS
 
 #undef __restrict
-#define __restrict restrict // C99 and above support the restrict keyword.
+#if __STDC_VERSION__ >= 199901L
+// C99 and above support the restrict keyword.
+#define __restrict restrict
+#elif !defined(__GNUC__)
+// GNU-compatible compilers accept the __ spelling in all modes.
+// Otherwise, omit the qualifier for pure C89 compatibility.
+#define __restrict
+#endif
 
 #undef _Noreturn
 #if __STDC_VERSION__ >= 201112L

Copy link
Contributor

@jhuber6 jhuber6 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to do this for __inline__ as well? I forget if that shows up anywhere.

@frobtech
Copy link
Contributor Author

frobtech commented Jan 7, 2025

Do we need to do this for __inline__ as well? I forget if that shows up anywhere.

We don't have any use of inline (by any spelling) in the headers today, except for one set that's #ifdef __cplusplus.

@frobtech frobtech merged commit ce33a48 into llvm:main Jan 7, 2025
9 of 10 checks passed
@frobtech frobtech deleted the p/libc-restrict-macro branch January 7, 2025 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants