[cfe-commits] r160846 - in /cfe/trunk: lib/StaticAnalyzer/Core/Environment.cpp lib/StaticAnalyzer/Core/ExprEngine.cpp test/Analysis/templates.cpp
Jordan Rose
jordan_rose at apple.com
Thu Jul 26 18:15:03 PDT 2012
Author: jrose
Date: Thu Jul 26 20:15:02 2012
New Revision: 160846
URL: http://llvm.org/viewvc/llvm-project?rev=160846&view=rev
Log:
[analyzer] Look through SubstNonTypeTemplateParmExprs.
We were treating this like a CXXDefaultArgExpr, but
SubstNonTypeTemplateParmExpr actually appears when a template is
instantiated, i.e. we have all the information necessary to evaluate it.
This allows us to inline functions like llvm::array_lengthof.
<rdar://problem/11949235>
Modified:
cfe/trunk/lib/StaticAnalyzer/Core/Environment.cpp
cfe/trunk/lib/StaticAnalyzer/Core/ExprEngine.cpp
cfe/trunk/test/Analysis/templates.cpp
Modified: cfe/trunk/lib/StaticAnalyzer/Core/Environment.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/Environment.cpp?rev=160846&r1=160845&r2=160846&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/Environment.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/Environment.cpp Thu Jul 26 20:15:02 2012
@@ -96,6 +96,9 @@
case Stmt::CXXBindTemporaryExprClass:
E = cast<CXXBindTemporaryExpr>(E)->getSubExpr();
continue;
+ case Stmt::SubstNonTypeTemplateParmExprClass:
+ E = cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement();
+ continue;
case Stmt::ObjCStringLiteralClass: {
MemRegionManager &MRMgr = svalBuilder.getRegionManager();
const ObjCStringLiteral *SL = cast<ObjCStringLiteral>(E);
Modified: cfe/trunk/lib/StaticAnalyzer/Core/ExprEngine.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/ExprEngine.cpp?rev=160846&r1=160845&r2=160846&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/ExprEngine.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/ExprEngine.cpp Thu Jul 26 20:15:02 2012
@@ -528,7 +528,6 @@
// We don't handle default arguments either yet, but we can fake it
// for now by just skipping them.
- case Stmt::SubstNonTypeTemplateParmExprClass:
case Stmt::CXXDefaultArgExprClass:
break;
@@ -622,6 +621,7 @@
case Stmt::StringLiteralClass:
case Stmt::ObjCStringLiteralClass:
case Stmt::CXXBindTemporaryExprClass:
+ case Stmt::SubstNonTypeTemplateParmExprClass:
case Stmt::CXXNullPtrLiteralExprClass: {
Bldr.takeNodes(Pred);
ExplodedNodeSet preVisit;
Modified: cfe/trunk/test/Analysis/templates.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/templates.cpp?rev=160846&r1=160845&r2=160846&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/templates.cpp (original)
+++ cfe/trunk/test/Analysis/templates.cpp Thu Jul 26 20:15:02 2012
@@ -1,4 +1,6 @@
-// RUN: %clang_cc1 -analyze -analyzer-checker=core -fblocks -verify %s
+// RUN: %clang_cc1 -analyze -analyzer-checker=core,debug.ExprInspection -fblocks -verify %s
+
+void clang_analyzer_eval(bool);
// Do not crash on this templated code which uses a block.
typedef void (^my_block)(void);
@@ -27,3 +29,16 @@
Mf m;
m.I();
}
+
+
+// <rdar://problem/11949235>
+template<class T, unsigned N>
+inline unsigned array_lengthof(T (&)[N]) {
+ return N;
+}
+
+void testNonTypeTemplateInstantiation() {
+ const char *S[] = { "a", "b" };
+ clang_analyzer_eval(array_lengthof(S) == 2); // expected-warning{{TRUE}}
+}
+
More information about the cfe-commits
mailing list