[PATCH] D122249: [Clang] Add a compatibiliy warning for non-literals in constexpr.

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 22 12:30:46 PDT 2022


aaron.ballman added inline comments.


================
Comment at: clang/include/clang/Basic/DiagnosticSemaKinds.td:2725
+def warn_cxx20_compat_constexpr_var : Warning<
+  "definition of a %select{static variable|thread_local variable|variable of non-literal type}1 "
   "in a constexpr %select{function|constructor}0 "
----------------
You should re-flow the whole diagnostic to the usual 80-col limit.


================
Comment at: clang/lib/Sema/SemaDeclCXX.cpp:1904-1912
+        if(SemaRef.LangOpts.CPlusPlus2b) {
+            if(!VD->getType()->isLiteralType(SemaRef.Context))
+                SemaRef.Diag(VD->getLocation(),diag::warn_cxx20_compat_constexpr_var)
+                        << isa<CXXConstructorDecl>(Dcl)
+                        << 2;
+        }
+        else if (CheckLiteralType(SemaRef, Kind, VD->getLocation(), VD->getType(),
----------------
Formatting looks off here (I was expecting the `else if` on the same line as the curly brace, but maybe clang-format is being weird?)


================
Comment at: clang/lib/Sema/SemaDeclCXX.cpp:1908
+                        << isa<CXXConstructorDecl>(Dcl)
+                        << 2;
+        }
----------------



================
Comment at: clang/test/SemaCXX/constant-expression-cxx2b.cpp:99
     return 0;
-  NonLiteral n;
+  NonLiteral n; // expected-warning {{definition of a variable of non-literal type in a constexpr function is incompatible with C++ standards before C++2b}}
 }
----------------
Shouldn't this be a `cxx2b-warning` instead?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122249/new/

https://reviews.llvm.org/D122249



More information about the cfe-commits mailing list