Translate CXXScalarValueInitExpr

Summary:
public
This expression is used to value-initialize non-class types. Per definition of value initialization for non-class types:
1. If it's an array, value-initialize each of its elements
2. Otherwise, zero-initialize it
http://en.cppreference.com/w/cpp/language/value_initialization

I was unable to reproduce (1) in a way that produced CXXScalarValueInitExpr and so this diff
deals with case (2)

Reviewed By: jvillard

Differential Revision: D2901311

fb-gh-sync-id: beeafa2
master
Andrzej Kotulski 9 years ago committed by facebook-github-bot-7
parent 359ad9dd4b
commit 49d7bdb6bd

@ -363,6 +363,18 @@ struct
exps = [(exp, typ)]; exps = [(exp, typ)];
ids = ids; } ids = ids; }
let cxxScalarValueInitExpr_trans trans_state stmt_info expr_info =
let typ = CTypes_decl.get_type_from_expr_info expr_info trans_state.context.CContext.tenv in
(* constant will be different depending on type *)
let zero_opt = match typ with
| Sil.Tfloat _ -> Some (Sil.Cfloat 0.0)
| Sil.Tptr _ -> Some (Sil.Cint Sil.Int.null)
| Sil.Tvoid -> None
| _ -> Some (Sil.Cint Sil.Int.zero) in
match zero_opt with
| Some zero -> { empty_res_trans with exps = [(Sil.Const zero, typ)] }
| _ -> empty_res_trans
let nullStmt_trans succ_nodes stmt_info = let nullStmt_trans succ_nodes stmt_info =
{ empty_res_trans with root_nodes = succ_nodes } { empty_res_trans with root_nodes = succ_nodes }
@ -2151,6 +2163,9 @@ struct
| FloatingLiteral (stmt_info, stmts, expr_info, float_string) -> | FloatingLiteral (stmt_info, stmts, expr_info, float_string) ->
floatingLiteral_trans trans_state stmt_info expr_info float_string floatingLiteral_trans trans_state stmt_info expr_info float_string
| CXXScalarValueInitExpr (stmt_info, stmts, expr_info) ->
cxxScalarValueInitExpr_trans trans_state stmt_info expr_info
| ObjCBoxedExpr (stmt_info, stmts, info, sel) -> | ObjCBoxedExpr (stmt_info, stmts, info, sel) ->
objCBoxedExpr_trans trans_state info sel stmt_info stmts objCBoxedExpr_trans trans_state info sel stmt_info stmts

@ -0,0 +1,26 @@
/*
* Copyright (c) 2016 - present Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
enum ENUM {
val1,
val2
};
template<class T> T get() {
return T();
}
void test() {
int i = get<int>();
float f = get<float>();
float* fp = get<float*>();
get<void>();
ENUM x = get<ENUM>();
float f2 = float();
}

@ -0,0 +1,88 @@
digraph iCFG {
23 [label="23: DeclStmt \n n$3=_fun_get<int>() [line 20]\n *&i:int =n$3 [line 20]\n REMOVE_TEMPS(n$3); [line 20]\n NULLIFY(&i,false); [line 20]\n " shape="box"]
23 -> 22 ;
22 [label="22: DeclStmt \n n$2=_fun_get<float>() [line 21]\n *&f:float =n$2 [line 21]\n REMOVE_TEMPS(n$2); [line 21]\n NULLIFY(&f,false); [line 21]\n " shape="box"]
22 -> 21 ;
21 [label="21: DeclStmt \n n$1=_fun_get<float *>() [line 22]\n *&fp:float *=n$1 [line 22]\n REMOVE_TEMPS(n$1); [line 22]\n NULLIFY(&fp,false); [line 22]\n " shape="box"]
21 -> 20 ;
20 [label="20: Call _fun_get<void> \n _fun_get<void>() [line 23]\n " shape="box"]
20 -> 19 ;
19 [label="19: DeclStmt \n n$0=_fun_get<ENUM>() [line 24]\n *&x:int =n$0 [line 24]\n REMOVE_TEMPS(n$0); [line 24]\n NULLIFY(&x,false); [line 24]\n " shape="box"]
19 -> 18 ;
18 [label="18: DeclStmt \n *&f2:float =0.000000 [line 25]\n NULLIFY(&f2,false); [line 25]\n APPLY_ABSTRACTION; [line 25]\n " shape="box"]
18 -> 17 ;
17 [label="17: Exit test \n " color=yellow style=filled]
16 [label="16: Start test\nFormals: \nLocals: f2:float x:int fp:float * f:float i:int \n DECLARE_LOCALS(&return,&f2,&x,&fp,&f,&i); [line 19]\n NULLIFY(&f,false); [line 19]\n NULLIFY(&f2,false); [line 19]\n NULLIFY(&fp,false); [line 19]\n NULLIFY(&i,false); [line 19]\n NULLIFY(&x,false); [line 19]\n " color=yellow style=filled]
16 -> 23 ;
15 [label="15: Return Stmt \n *&return:int =0 [line 16]\n APPLY_ABSTRACTION; [line 16]\n " shape="box"]
15 -> 14 ;
14 [label="14: Exit get<ENUM> \n " color=yellow style=filled]
13 [label="13: Start get<ENUM>\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 15]\n " color=yellow style=filled]
13 -> 15 ;
12 [label="12: Return Stmt \n *&return:void =-1 [line 16]\n APPLY_ABSTRACTION; [line 16]\n " shape="box"]
12 -> 11 ;
11 [label="11: Exit get<void> \n " color=yellow style=filled]
10 [label="10: Start get<void>\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 15]\n " color=yellow style=filled]
10 -> 12 ;
9 [label="9: Return Stmt \n *&return:float *=null [line 16]\n APPLY_ABSTRACTION; [line 16]\n " shape="box"]
9 -> 8 ;
8 [label="8: Exit get<float *> \n " color=yellow style=filled]
7 [label="7: Start get<float *>\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 15]\n " color=yellow style=filled]
7 -> 9 ;
6 [label="6: Return Stmt \n *&return:float =0.000000 [line 16]\n APPLY_ABSTRACTION; [line 16]\n " shape="box"]
6 -> 5 ;
5 [label="5: Exit get<float> \n " color=yellow style=filled]
4 [label="4: Start get<float>\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 15]\n " color=yellow style=filled]
4 -> 6 ;
3 [label="3: Return Stmt \n *&return:int =0 [line 16]\n APPLY_ABSTRACTION; [line 16]\n " shape="box"]
3 -> 2 ;
2 [label="2: Exit get<int> \n " color=yellow style=filled]
1 [label="1: Start get<int>\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 15]\n " color=yellow style=filled]
1 -> 3 ;
}

@ -24,10 +24,18 @@ public class LiteralsTest {
public DebuggableTemporaryFolder folder = new DebuggableTemporaryFolder(); public DebuggableTemporaryFolder folder = new DebuggableTemporaryFolder();
@Test @Test
public void whenCaptureRunCommaThenDotFilesAreTheSame() public void whenCaptureRunNullptrThenDotFilesAreTheSame()
throws InterruptedException, IOException, InferException { throws InterruptedException, IOException, InferException {
String src = String src =
"infer/tests/codetoanalyze/cpp/frontend/literals/nullptr.cpp"; "infer/tests/codetoanalyze/cpp/frontend/literals/nullptr.cpp";
ClangFrontendUtils.createAndCompareCppDotFiles(folder, src); ClangFrontendUtils.createAndCompareCppDotFiles(folder, src);
} }
@Test
public void whenCaptureRunScalarValueInitThenDotFilesAreTheSame()
throws InterruptedException, IOException, InferException {
String src =
"infer/tests/codetoanalyze/cpp/frontend/literals/scalar_value_init.cpp";
ClangFrontendUtils.createAndCompareCppDotFiles(folder, src);
}
} }

Loading…
Cancel
Save