[PATCH] D127683: Adds a warning for aligned new in MSVC before C++17

Luke Nihlen via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 13 12:23:17 PDT 2022


luken-google created this revision.
Herald added a project: All.
luken-google requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

MSVC requires C++17 to support aligned new and defining the macro
__cpp_aligned_new. Clang does not, which could result in incompatibility
between clang-cl and msvc-built binaries. Issue a warning when this
use case is detected.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D127683

Files:
  clang/include/clang/Basic/DiagnosticFrontendKinds.td
  clang/lib/Frontend/InitPreprocessor.cpp


Index: clang/lib/Frontend/InitPreprocessor.cpp
===================================================================
--- clang/lib/Frontend/InitPreprocessor.cpp
+++ clang/lib/Frontend/InitPreprocessor.cpp
@@ -591,7 +591,8 @@
 /// Initialize the predefined C++ language feature test macros defined in
 /// ISO/IEC JTC1/SC22/WG21 (C++) SD-6: "SG10 Feature Test Recommendations".
 static void InitializeCPlusPlusFeatureTestMacros(const LangOptions &LangOpts,
-                                                 MacroBuilder &Builder) {
+                                                 MacroBuilder &Builder,
+                                                 DiagnosticsEngine &Diags) {
   // C++98 features.
   if (LangOpts.RTTI)
     Builder.defineMacro("__cpp_rtti", "199711L");
@@ -666,8 +667,13 @@
     Builder.defineMacro("__cpp_guaranteed_copy_elision", "201606L");
     Builder.defineMacro("__cpp_nontype_template_parameter_auto", "201606L");
   }
-  if (LangOpts.AlignedAllocation && !LangOpts.AlignedAllocationUnavailable)
+  if (LangOpts.AlignedAllocation && !LangOpts.AlignedAllocationUnavailable) {
+    // LLVM supports __cpp_aligned_new in C++ version older than C++17, but MSVC
+    // does not. Warn about the difference in support.
+    if (LangOpts.MSVCCompat && LangOpts.CPlusPlus && !LangOpts.CPlusPlus17)
+      Diags.Report(diag::warn_fe_msvc_aligned_new_before_cpp17);
     Builder.defineMacro("__cpp_aligned_new", "201606L");
+  }
   if (LangOpts.RelaxedTemplateTemplateArgs)
     Builder.defineMacro("__cpp_template_template_args", "201611L");
 
@@ -728,7 +734,8 @@
                                        const LangOptions &LangOpts,
                                        const FrontendOptions &FEOpts,
                                        const PreprocessorOptions &PPOpts,
-                                       MacroBuilder &Builder) {
+                                       MacroBuilder &Builder,
+                                       DiagnosticsEngine &Diags) {
   // Compiler version introspection macros.
   Builder.defineMacro("__llvm__");  // LLVM Backend
   Builder.defineMacro("__clang__"); // Clang Frontend
@@ -856,7 +863,7 @@
                       Twine(TI.useSignedCharForObjCBool() ? "0" : "1"));
 
   if (LangOpts.CPlusPlus)
-    InitializeCPlusPlusFeatureTestMacros(LangOpts, Builder);
+    InitializeCPlusPlusFeatureTestMacros(LangOpts, Builder, Diags);
 
   // darwin_constant_cfstrings controls this. This is also dependent
   // on other things like the runtime I believe.  This is set even for C code.
@@ -1326,10 +1333,10 @@
     if ((LangOpts.CUDA || LangOpts.OpenMPIsDevice || LangOpts.SYCLIsDevice) &&
         PP.getAuxTargetInfo())
       InitializePredefinedMacros(*PP.getAuxTargetInfo(), LangOpts, FEOpts,
-                                 PP.getPreprocessorOpts(), Builder);
+                                 PP.getPreprocessorOpts(), Builder, PP.getDiagnostics());
 
     InitializePredefinedMacros(PP.getTargetInfo(), LangOpts, FEOpts,
-                               PP.getPreprocessorOpts(), Builder);
+                               PP.getPreprocessorOpts(), Builder, PP.getDiagnostics());
 
     // Install definitions to make Objective-C++ ARC work well with various
     // C++ Standard Library implementations.
Index: clang/include/clang/Basic/DiagnosticFrontendKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -109,6 +109,9 @@
     "unable to open output file '%0': '%1'">;
 def warn_fe_macro_contains_embedded_newline : Warning<
     "macro '%0' contains embedded newline; text after the newline is ignored">;
+def warn_fe_msvc_aligned_new_before_cpp17 : Warning<
+    "possible incompatibility with MSVC, it does not support alignedNew before C++17">,
+    InGroup<MicrosoftCppMacro>;
 def warn_fe_cc_print_header_failure : Warning<
     "unable to open CC_PRINT_HEADERS file: %0 (using stderr)">;
 def warn_fe_cc_log_diagnostics_failure : Warning<


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127683.436513.patch
Type: text/x-patch
Size: 4068 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220613/ce4f44ef/attachment-0001.bin>


More information about the cfe-commits mailing list