From: David Majnemer Date: Fri, 26 Feb 2016 04:23:19 +0000 (+0000) Subject: [MSVC Compat] Don't evaluate member base expressions w/o side effects X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e466b31f6a9358b49b2836e319cdc1ad5317d24f;p=clang [MSVC Compat] Don't evaluate member base expressions w/o side effects 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. Differential Revision: http://reviews.llvm.org/D17619 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@261975 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp index 4089a20e58..89a21acc84 100644 --- a/lib/AST/ExprConstant.cpp +++ b/lib/AST/ExprConstant.cpp @@ -4427,6 +4427,15 @@ public: void VisitIgnoredValue(const Expr *E) { EvaluateIgnoredValue(Info, E); } + + /// Potentially visit a MemberExpr's base expression. + void VisitIgnoredBaseExpression(const Expr *E) { + // While MSVC doesn't evaluate the base expression, it does diagnose the + // presence of side-effecting behavior. + if (Info.getLangOpts().MSVCCompat && !E->HasSideEffects(Info.Ctx)) + return; + VisitIgnoredValue(E); + } }; } @@ -4750,14 +4759,14 @@ bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { // Handle static data members. if (const VarDecl *VD = dyn_cast(E->getMemberDecl())) { - VisitIgnoredValue(E->getBase()); + VisitIgnoredBaseExpression(E->getBase()); return VisitVarDecl(E, VD); } // Handle static member functions. if (const CXXMethodDecl *MD = dyn_cast(E->getMemberDecl())) { if (MD->isStatic()) { - VisitIgnoredValue(E->getBase()); + VisitIgnoredBaseExpression(E->getBase()); return Success(MD); } } @@ -6078,7 +6087,7 @@ public: } bool VisitMemberExpr(const MemberExpr *E) { if (CheckReferencedDecl(E, E->getMemberDecl())) { - VisitIgnoredValue(E->getBase()); + VisitIgnoredBaseExpression(E->getBase()); return true; } diff --git a/test/SemaCXX/ms-const-member-expr.cpp b/test/SemaCXX/ms-const-member-expr.cpp new file mode 100644 index 0000000000..72cfe76fbe --- /dev/null +++ b/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;