[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:20:14 PST 2016


majnemer created this revision.
majnemer added reviewers: rsmith, andreybokhanko.
majnemer added a subscriber: cfe-commits.

A member expression's base doesn't always have an impact on what the
member decl would evaluate to.  In such a case, the base is used as a
poor man's scope qualifier.

This fixes PR26738.

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,16 @@
+// RUN: %clang_cc1 %s -std=c++11 -fms-compatibility -fsyntax-only -verify
+
+struct S {
+  enum { E = 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}}
Index: lib/AST/ExprConstant.cpp
===================================================================
--- lib/AST/ExprConstant.cpp
+++ lib/AST/ExprConstant.cpp
@@ -6077,8 +6077,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.49101.patch
Type: text/x-patch
Size: 1434 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160225/c5df4e12/attachment.bin>


More information about the cfe-commits mailing list