From 1bbd143945ee57c30870cd452e0755dad54ec525 Mon Sep 17 00:00:00 2001 From: Jordan Rose Date: Tue, 23 Oct 2012 23:59:08 +0000 Subject: [PATCH] [analyzer] Handle 'SomeVar.SomeEnumConstant', which is legal in C++. This caused assertion failures analyzing LLVM. llvm-svn: 166529 --- clang/lib/StaticAnalyzer/Core/ExprEngine.cpp | 14 ++++++------ clang/test/Analysis/member-expr.cpp | 23 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 clang/test/Analysis/member-expr.cpp diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp index 08f8405c4b2b..e3425139e80f 100644 --- a/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp +++ b/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp @@ -1512,13 +1512,13 @@ void ExprEngine::VisitMemberExpr(const MemberExpr *M, ExplodedNode *Pred, StmtNodeBuilder Bldr(Pred, TopDst, *currBldrCtx); ExplodedNodeSet Dst; - Decl *member = M->getMemberDecl(); + ValueDecl *Member = M->getMemberDecl(); - // Handle static member variables accessed via member syntax. - if (VarDecl *VD = dyn_cast(member)) { - assert(M->isGLValue()); + // Handle static member variables and enum constants accessed via + // member syntax. + if (isa(Member) || isa(Member)) { Bldr.takeNodes(Pred); - VisitCommonDeclRefExpr(M, VD, Pred, Dst); + VisitCommonDeclRefExpr(M, Member, Pred, Dst); Bldr.addNodes(Dst); return; } @@ -1528,7 +1528,7 @@ void ExprEngine::VisitMemberExpr(const MemberExpr *M, ExplodedNode *Pred, Expr *BaseExpr = M->getBase(); // Handle C++ method calls. - if (const CXXMethodDecl *MD = dyn_cast(member)) { + if (const CXXMethodDecl *MD = dyn_cast(Member)) { if (MD->isInstance()) state = createTemporaryRegionIfNeeded(state, LCtx, BaseExpr); @@ -1543,7 +1543,7 @@ void ExprEngine::VisitMemberExpr(const MemberExpr *M, ExplodedNode *Pred, state = createTemporaryRegionIfNeeded(state, LCtx, BaseExpr); SVal baseExprVal = state->getSVal(BaseExpr, LCtx); - FieldDecl *field = cast(member); + FieldDecl *field = cast(Member); SVal L = state->getLValue(field, baseExprVal); if (M->isGLValue()) { if (field->getType()->isReferenceType()) { diff --git a/clang/test/Analysis/member-expr.cpp b/clang/test/Analysis/member-expr.cpp new file mode 100644 index 000000000000..cf437387d6a6 --- /dev/null +++ b/clang/test/Analysis/member-expr.cpp @@ -0,0 +1,23 @@ +// RUN: %clang_cc1 -analyze -analyzer-checker=core,debug.ExprInspection %s -verify + +void clang_analyzer_eval(int); + +namespace EnumsViaMemberExpr { + struct Foo { + enum E { + Bar = 1 + }; + }; + + void testEnumVal(Foo Baz) { + clang_analyzer_eval(Baz.Bar == Foo::Bar); // expected-warning{{TRUE}} + } + + void testEnumRef(Foo &Baz) { + clang_analyzer_eval(Baz.Bar == Foo::Bar); // expected-warning{{TRUE}} + } + + void testEnumPtr(Foo *Baz) { + clang_analyzer_eval(Baz->Bar == Foo::Bar); // expected-warning{{TRUE}} + } +} \ No newline at end of file