]> granicus.if.org Git - clang/commitdiff
[analyzer] Handle new-expressions with initializers for scalars.
authorJordan Rose <jordan_rose@apple.com>
Mon, 16 Jul 2012 23:38:09 +0000 (23:38 +0000)
committerJordan Rose <jordan_rose@apple.com>
Mon, 16 Jul 2012 23:38:09 +0000 (23:38 +0000)
<rdar://problem/11818967>

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@160328 91177308-0d34-0410-b5e6-96231b3b80d8

lib/StaticAnalyzer/Core/ExprEngineC.cpp
lib/StaticAnalyzer/Core/ExprEngineCXX.cpp
test/Analysis/new.cpp

index fe9adb1e1549f8fc68954acdf9b237de1e70322f..0254b756ee4c531d439b4d48ebf7b336d69501b1 100644 (file)
@@ -571,18 +571,17 @@ void ExprEngine::VisitInitListExpr(const InitListExpr *IE,
                                    svalBuilder.makeCompoundVal(T, vals)));
     return;
   }
-  
-  if (Loc::isLocType(T) || T->isIntegerType()) {
-    assert(IE->getNumInits() == 1);
-    const Expr *initEx = IE->getInit(0);
-    B.generateNode(IE, Pred, state->BindExpr(IE, LCtx,
-                                             state->getSVal(initEx, LCtx)));
-    return;
-  }
 
-  assert(IE->getNumInits() == 1);
-  B.generateNode(IE, Pred, state->BindExpr(IE, LCtx, UnknownVal()));
-  return;
+  // Handle scalars: int{5} and int{}.
+  assert(NumInitElements <= 1);
+
+  SVal V;
+  if (NumInitElements == 0)
+    V = getSValBuilder().makeZeroVal(T);
+  else
+    V = state->getSVal(IE->getInit(0), LCtx);
+
+  B.generateNode(IE, Pred, state->BindExpr(IE, LCtx, V));
 }
 
 void ExprEngine::VisitGuardedExpr(const Expr *Ex,
index 3fa052817c2751548af50e167e89469070a3aab6..9a1264e17c4b39866858ef1df9ef34c153f4cde1 100644 (file)
@@ -136,6 +136,19 @@ void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred,
     State = State->BindExpr(CNE, LCtx, symVal);
   }
 
+  // If the type is not a record, we won't have a CXXConstructExpr as an
+  // initializer. Copy the value over.
+  if (const Expr *Init = CNE->getInitializer()) {
+    if (!isa<CXXConstructExpr>(Init)) {
+      QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType();
+      (void)ObjTy;
+      assert(!ObjTy->isRecordType());
+      SVal Location = State->getSVal(CNE, LCtx);
+      if (isa<Loc>(Location))
+        State = State->bindLoc(cast<Loc>(Location), State->getSVal(Init, LCtx));
+    }
+  }
+
   Bldr.generateNode(CNE, Pred, State);
 }
 
index 5dad9430c67e483bfd153147d82435e7244d8601..fb77de22f1276a50e70fc51aa07e092e31e11aac 100644 (file)
@@ -1,8 +1,8 @@
-// RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-store region -verify %s
+// RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-store region -std=c++11 -verify %s
 
 void clang_analyzer_eval(bool);
 
-typedef typeof(sizeof(int)) size_t;
+typedef __typeof__(sizeof(int)) size_t;
 extern "C" void *malloc(size_t);
 
 int someGlobal;
@@ -59,23 +59,42 @@ void *testCustomNewMalloc() {
   return y;
 }
 
+void testScalarInitialization() {
+  int *n = new int(3);
+  clang_analyzer_eval(*n == 3); // expected-warning{{TRUE}}
+
+  new (n) int();
+  clang_analyzer_eval(*n == 0); // expected-warning{{TRUE}}
+
+  new (n) int{3};
+  clang_analyzer_eval(*n == 3); // expected-warning{{TRUE}}
+
+  new (n) int{};
+  clang_analyzer_eval(*n == 0); // expected-warning{{TRUE}}
+}
+
 
 //--------------------------------
 // Incorrectly-modelled behavior
 //--------------------------------
 
-void testZeroInitialization() {
+int testNoInitialization() {
   int *n = new int;
 
   // Should warn that *n is uninitialized.
   if (*n) { // no-warning
+    return 0;
   }
+  return 1;
 }
 
-void testValueInitialization() {
-  int *n = new int(3);
+int testNoInitializationPlacement() {
+  int n;
+  new (&n) int;
 
-  // Should be TRUE (and have no uninitialized variable warning)
-  clang_analyzer_eval(*n == 3); // expected-warning{{UNKNOWN}}
+  // Should warn that n is uninitialized.
+  if (n) { // no-warning
+    return 0;
+  }
+  return 1;
 }
-