diff --git a/infer/src/clang/cTrans.ml b/infer/src/clang/cTrans.ml index 06dc18598..974831a76 100644 --- a/infer/src/clang/cTrans.ml +++ b/infer/src/clang/cTrans.ml @@ -363,6 +363,18 @@ struct exps = [(exp, typ)]; 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 = { empty_res_trans with root_nodes = succ_nodes } @@ -2151,6 +2163,9 @@ struct | FloatingLiteral (stmt_info, stmts, 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_trans trans_state info sel stmt_info stmts diff --git a/infer/tests/codetoanalyze/cpp/frontend/literals/scalar_value_init.cpp b/infer/tests/codetoanalyze/cpp/frontend/literals/scalar_value_init.cpp new file mode 100644 index 000000000..0faa0cd89 --- /dev/null +++ b/infer/tests/codetoanalyze/cpp/frontend/literals/scalar_value_init.cpp @@ -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 T get() { + return T(); +} + +void test() { + int i = get(); + float f = get(); + float* fp = get(); + get(); + ENUM x = get(); + float f2 = float(); +} diff --git a/infer/tests/codetoanalyze/cpp/frontend/literals/scalar_value_init.cpp.dot b/infer/tests/codetoanalyze/cpp/frontend/literals/scalar_value_init.cpp.dot new file mode 100644 index 000000000..cc75cfad5 --- /dev/null +++ b/infer/tests/codetoanalyze/cpp/frontend/literals/scalar_value_init.cpp.dot @@ -0,0 +1,88 @@ +digraph iCFG { +23 [label="23: DeclStmt \n n$3=_fun_get() [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() [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() [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 \n _fun_get() [line 23]\n " shape="box"] + + + 20 -> 19 ; +19 [label="19: DeclStmt \n n$0=_fun_get() [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 \n " color=yellow style=filled] + + +13 [label="13: Start get\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 \n " color=yellow style=filled] + + +10 [label="10: Start get\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 \n " color=yellow style=filled] + + +7 [label="7: Start get\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 \n " color=yellow style=filled] + + +4 [label="4: Start get\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 \n " color=yellow style=filled] + + +1 [label="1: Start get\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 15]\n " color=yellow style=filled] + + + 1 -> 3 ; +} diff --git a/infer/tests/frontend/cpp/LiteralsTest.java b/infer/tests/frontend/cpp/LiteralsTest.java index 2660bd2bd..eded6bb9e 100644 --- a/infer/tests/frontend/cpp/LiteralsTest.java +++ b/infer/tests/frontend/cpp/LiteralsTest.java @@ -24,10 +24,18 @@ public class LiteralsTest { public DebuggableTemporaryFolder folder = new DebuggableTemporaryFolder(); @Test - public void whenCaptureRunCommaThenDotFilesAreTheSame() + public void whenCaptureRunNullptrThenDotFilesAreTheSame() throws InterruptedException, IOException, InferException { String src = "infer/tests/codetoanalyze/cpp/frontend/literals/nullptr.cpp"; 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); + } }