[libc-commits] [libc] ee5749b - [libc] Provide compiler version properties (#73344)

via libc-commits libc-commits at lists.llvm.org
Fri Nov 24 08:02:16 PST 2023


Author: Guillaume Chatelet
Date: 2023-11-24T17:02:11+01:00
New Revision: ee5749bf785b66adfd9b4edc4d3a676691f28599

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

LOG: [libc] Provide compiler version properties (#73344)

This will be used to support conditional compilation based on compiler
version.
We adopt the same convention as
[libc++](https://github.com/llvm/llvm-project/blob/main/libcxx/include/__config)
- thx @legrosbuffle for the suggestion!
Usage:
```
#if defined(LIBC_COMPILER_CLANG_VER)
#  if LIBC_COMPILER_CLANG_VER < 1500
#    warning "Libc only supports Clang 15 and later"
#  endif
#elif defined(LIBC_COMPILER_GCC_VER)
#  if LIBC_COMPILER_GCC_VER < 1500
#    warning "Libc only supports GCC 15 and later"
#  endif
#elif defined(LIBC_COMPILER_MSC_VER)
#  if LIBC_COMPILER_MSC_VER < 1930
#    warning "Libc only supports Visual Studio 2022 RTW (17.0) and later"
#  endif
#endif
```

Added: 
    

Modified: 
    libc/src/__support/macros/properties/compiler.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/macros/properties/compiler.h b/libc/src/__support/macros/properties/compiler.h
index a7a2822bf6a14a6..b9ec0dd1defb9df 100644
--- a/libc/src/__support/macros/properties/compiler.h
+++ b/libc/src/__support/macros/properties/compiler.h
@@ -9,16 +9,35 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_COMPILER_H
 #define LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_COMPILER_H
 
+// Example usage of compiler version checks
+// #if defined(LIBC_COMPILER_CLANG_VER)
+// #  if LIBC_COMPILER_CLANG_VER < 1500
+// #    warning "Libc only supports Clang 15 and later"
+// #  endif
+// #elif defined(LIBC_COMPILER_GCC_VER)
+// #  if LIBC_COMPILER_GCC_VER < 1500
+// #    warning "Libc only supports GCC 15 and later"
+// #  endif
+// #elif defined(LIBC_COMPILER_MSC_VER)
+// #  if LIBC_COMPILER_MSC_VER < 1930
+// #    warning "Libc only supports Visual Studio 2022 RTW (17.0) and later"
+// #  endif
+// #endif
+
 #if defined(__clang__)
 #define LIBC_COMPILER_IS_CLANG
+#define LIBC_COMPILER_CLANG_VER (__clang_major__ * 100 + __clang_minor__)
 #endif
 
 #if defined(__GNUC__) && !defined(__clang__)
 #define LIBC_COMPILER_IS_GCC
+#define LIBC_COMPILER_GCC_VER (__GNUC__ * 100 + __GNUC_MINOR__)
 #endif
 
 #if defined(_MSC_VER)
 #define LIBC_COMPILER_IS_MSC
+// https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros
+#define LIBC_COMPILER_MSC_VER (_MSC_VER)
 #endif
 
 #endif // LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_COMPILER_H


        


More information about the libc-commits mailing list