[clang] a5c7b46 - Fix checking for C++98 ICEs in C++11-and-later mode to not consider use

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 26 17:00:03 PDT 2020


Author: Richard Smith
Date: 2020-10-26T16:59:48-07:00
New Revision: a5c7b46862ec0531964eb52329cdf009862abecf

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

LOG: Fix checking for C++98 ICEs in C++11-and-later mode to not consider use
of a reference to be acceptable.

Added: 
    

Modified: 
    clang/lib/AST/ExprConstant.cpp
    clang/test/SemaCXX/MicrosoftCompatibility.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index fa4026b865fb..0b1293b3a2c7 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -15173,8 +15173,12 @@ static ICEDiag CheckICE(const Expr* E, const ASTContext &Ctx) {
     // C++ 7.1.5.1p2
     //   A variable of non-volatile const-qualified integral or enumeration
     //   type initialized by an ICE can be used in ICEs.
+    //
+    // We sometimes use CheckICE to check the C++98 rules in C++11 mode. In
+    // that mode, use of reference variables should not be allowed.
     const VarDecl *VD = dyn_cast<VarDecl>(D);
-    if (VD && VD->isUsableInConstantExpressions(Ctx))
+    if (VD && VD->isUsableInConstantExpressions(Ctx) &&
+        !VD->getType()->isReferenceType())
       return NoDiag();
 
     return ICEDiag(IK_NotICE, E->getBeginLoc());

diff  --git a/clang/test/SemaCXX/MicrosoftCompatibility.cpp b/clang/test/SemaCXX/MicrosoftCompatibility.cpp
index 453f78ad43df..8c0a000da07c 100644
--- a/clang/test/SemaCXX/MicrosoftCompatibility.cpp
+++ b/clang/test/SemaCXX/MicrosoftCompatibility.cpp
@@ -275,6 +275,17 @@ namespace IntToNullPtrConv {
 
   template<int N> int *get_n() { return N; }   // expected-warning {{expression which evaluates to zero treated as a null pointer constant}}
   int *g_nullptr = get_n<0>();  // expected-note {{in instantiation of function template specialization}}
+
+  // FIXME: MSVC accepts this.
+  constexpr float k = 0;
+  int *p1 = (int)k; // expected-error {{cannot initialize}}
+
+  constexpr int n = 0;
+  const int &r = n;
+  int *p2 = (int)r; // expected-error {{cannot initialize}}
+
+  constexpr int f() { return 0; }
+  int *p = f(); // expected-error {{cannot initialize}}
 }
 
 namespace signed_hex_i64 {


        


More information about the cfe-commits mailing list