From 46eaf7789a1059a7b42b7dbd183150c72df5738f Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Mon, 10 Oct 2011 22:36:31 +0000 Subject: [PATCH] [analyzer] Teach the static analyzer about CXXForRangeStmt. Patch by Jim Goodnow II! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@141587 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/StaticAnalyzer/Core/CoreEngine.cpp | 5 +++++ lib/StaticAnalyzer/Core/ExprEngine.cpp | 2 +- test/Analysis/misc-ps-cxx0x.cpp | 23 +++++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/StaticAnalyzer/Core/CoreEngine.cpp b/lib/StaticAnalyzer/Core/CoreEngine.cpp index c675f608b5..0587977f2c 100644 --- a/lib/StaticAnalyzer/Core/CoreEngine.cpp +++ b/lib/StaticAnalyzer/Core/CoreEngine.cpp @@ -17,6 +17,7 @@ #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" #include "clang/Index/TranslationUnit.h" #include "clang/AST/Expr.h" +#include "clang/AST/StmtCXX.h" #include "llvm/Support/Casting.h" #include "llvm/ADT/DenseMap.h" using namespace clang; @@ -349,6 +350,10 @@ void CoreEngine::HandleBlockExit(const CFGBlock * B, ExplodedNode *Pred) { HandleBranch(cast(Term)->getCond(), Term, B, Pred); return; + case Stmt::CXXForRangeStmtClass: + HandleBranch(cast(Term)->getCond(), Term, B, Pred); + return; + case Stmt::ForStmtClass: HandleBranch(cast(Term)->getCond(), Term, B, Pred); return; diff --git a/lib/StaticAnalyzer/Core/ExprEngine.cpp b/lib/StaticAnalyzer/Core/ExprEngine.cpp index 11719c370e..11be71a968 100644 --- a/lib/StaticAnalyzer/Core/ExprEngine.cpp +++ b/lib/StaticAnalyzer/Core/ExprEngine.cpp @@ -453,7 +453,6 @@ void ExprEngine::Visit(const Stmt *S, ExplodedNode *Pred, case Stmt::CXXBindTemporaryExprClass: case Stmt::CXXCatchStmtClass: case Stmt::CXXDependentScopeMemberExprClass: - case Stmt::CXXForRangeStmtClass: case Stmt::CXXPseudoDestructorExprClass: case Stmt::CXXThrowExprClass: case Stmt::CXXTryStmtClass: @@ -500,6 +499,7 @@ void ExprEngine::Visit(const Stmt *S, ExplodedNode *Pred, case Stmt::CaseStmtClass: case Stmt::CompoundStmtClass: case Stmt::ContinueStmtClass: + case Stmt::CXXForRangeStmtClass: case Stmt::DefaultStmtClass: case Stmt::DoStmtClass: case Stmt::ForStmtClass: diff --git a/test/Analysis/misc-ps-cxx0x.cpp b/test/Analysis/misc-ps-cxx0x.cpp index e840bb01d2..b00164ca47 100644 --- a/test/Analysis/misc-ps-cxx0x.cpp +++ b/test/Analysis/misc-ps-cxx0x.cpp @@ -45,3 +45,26 @@ int tempobj2() return j; // no-warning } + + +// Test for correct handling of C++ ForRange statement. +void test1() { + int array[2] = { 1, 2 }; + int j = 0; + for ( int i : array ) + j += i; + int *p = 0; + *p = 0xDEADBEEF; // expected-warning {{null}} +} + +void test2() { + int array[2] = { 1, 2 }; + int j = 0; + for (int i : array) + j += i; + if (j == 3) + return; + int *p = 0; + *p = 0xDEADBEEF; // no-warning +} + -- 2.40.0