[PATCH] D17619: [MSVC Compat] Don't evaluate member base expressions w/o side effects

David Majnemer via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 25 11:37:31 PST 2016


majnemer updated this revision to Diff 49103.
majnemer added a comment.

- Add support for static data members too.


http://reviews.llvm.org/D17619

Files:
  lib/AST/ExprConstant.cpp
  test/SemaCXX/ms-const-member-expr.cpp

Index: test/SemaCXX/ms-const-member-expr.cpp
===================================================================
--- /dev/null
+++ test/SemaCXX/ms-const-member-expr.cpp
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 %s -std=c++11 -fms-compatibility -fsyntax-only -verify
+
+struct S {
+  enum { E = 1 };
+  static const int sdm = 1;
+};
+
+void f(S *s) {
+  char array[s->E] = { 0 };
+}
+
+extern S *s;
+constexpr int e1 = s->E;
+
+S *side_effect();  // expected-note{{declared here}}
+constexpr int e2 = // expected-error{{must be initialized by a constant expression}}
+    side_effect()->E; // expected-note{{cannot be used in a constant expression}}
+
+constexpr int e4 = s->sdm;
Index: lib/AST/ExprConstant.cpp
===================================================================
--- lib/AST/ExprConstant.cpp
+++ lib/AST/ExprConstant.cpp
@@ -4748,14 +4748,20 @@
 }
 
 bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) {
+  ValueDecl *MemberDecl = E->getMemberDecl();
+  ASTContext &Ctx = MemberDecl->getASTContext();
+
   // Handle static data members.
-  if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) {
-    VisitIgnoredValue(E->getBase());
+  if (const auto *VD = dyn_cast<VarDecl>(MemberDecl)) {
+    // While MSVC doesn't evaluate the base expression, it does diagnose the
+    // presence of side-effecting behavior.
+    if (!Info.getLangOpts().MSVCCompat || E->getBase()->HasSideEffects(Ctx))
+      VisitIgnoredValue(E->getBase());
     return VisitVarDecl(E, VD);
   }
 
   // Handle static member functions.
-  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) {
+  if (const auto *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) {
     if (MD->isStatic()) {
       VisitIgnoredValue(E->getBase());
       return Success(MD);
@@ -6077,8 +6083,14 @@
     return ExprEvaluatorBaseTy::VisitDeclRefExpr(E);
   }
   bool VisitMemberExpr(const MemberExpr *E) {
-    if (CheckReferencedDecl(E, E->getMemberDecl())) {
-      VisitIgnoredValue(E->getBase());
+    ValueDecl *MemberDecl = E->getMemberDecl();
+    ASTContext &Ctx = MemberDecl->getASTContext();
+    if (CheckReferencedDecl(E, MemberDecl)) {
+      // While MSVC doesn't evaluate the base expression, it does diagnose the
+      // presence of side-effecting behavior.
+      if (!Info.getLangOpts().MSVCCompat || E->getBase()->HasSideEffects(Ctx)) {
+        VisitIgnoredValue(E->getBase());
+      }
       return true;
     }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D17619.49103.patch
Type: text/x-patch
Size: 2440 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160225/a21f1776/attachment.bin>


More information about the cfe-commits mailing list