From fbddeba8a717e3ab4c23fda4033abb52258707ab Mon Sep 17 00:00:00 2001 From: Andrzej Kotulski Date: Tue, 28 Mar 2017 04:38:50 -0700 Subject: [PATCH] [clang] Don't inline global var initializers Reviewed By: jberdine Differential Revision: D4786653 fbshipit-source-id: 22fd2b3 --- infer/src/clang/cTrans.ml | 24 +-------------- .../frontend/globals/global_const1.cpp.dot | 6 ++-- .../frontend/globals/global_const2.cpp.dot | 29 +++---------------- .../cpp/shared/namespace/namespace.cpp.dot | 4 +-- .../codetoanalyze/objc/errors/issues.exp | 1 + .../frontend/global_const/global_const.mm.dot | 2 +- 6 files changed, 12 insertions(+), 54 deletions(-) diff --git a/infer/src/clang/cTrans.ml b/infer/src/clang/cTrans.ml index ff0e9d13d..b7e093e33 100644 --- a/infer/src/clang/cTrans.ml +++ b/infer/src/clang/cTrans.ml @@ -636,28 +636,6 @@ struct let pvar = CVar_decl.sil_var_of_decl_ref context decl_ref procname in CContext.add_block_static_var context procname (pvar, typ); let var_exp = Exp.Lvar pvar in - (* handle references to global const *) - (* if there's a reference to a global const, add a fake instruction that *) - (* assigns the global again to its initialization value right before the *) - (* place where it is used *) - let trans_result' = - let is_global_const, init_expr = - match CAst_utils.get_decl decl_ref.dr_decl_pointer with - | Some VarDecl (_, _, qual_type, vdi) -> ( - match ast_typ with - | Tstruct _ - when not (CGeneral_utils.is_cpp_translation context.translation_unit_context) -> - (* Do not convert a global struct to a local because SIL - values do not include structs, they must all be heap-allocated *) - (false, None) - | _ -> - (vdi.vdi_is_global && (vdi.vdi_is_const_expr || qual_type.qt_is_const), - vdi.vdi_init_expr) - ) - | _ -> false, None in - if is_global_const then - init_expr_trans trans_state (var_exp, typ) stmt_info init_expr - else empty_res_trans in let exps = if Self.is_var_self pvar (CContext.is_objc_method context) then let class_typename = CContext.get_curr_class_typename context in if (CType.is_class typ) then @@ -667,7 +645,7 @@ struct [(var_exp, typ)] else [(var_exp, typ)] in Logging.out_debug "\n\n PVAR ='%s'\n\n" (Pvar.to_string pvar); - let res_trans = { trans_result' with exps } in + let res_trans = { empty_res_trans with exps } in match typ with | Tptr (_, Pk_reference) -> (* dereference pvar due to the behavior of reference types in clang's AST *) diff --git a/infer/tests/codetoanalyze/cpp/frontend/globals/global_const1.cpp.dot b/infer/tests/codetoanalyze/cpp/frontend/globals/global_const1.cpp.dot index d2e63bc19..cee187fd6 100644 --- a/infer/tests/codetoanalyze/cpp/frontend/globals/global_const1.cpp.dot +++ b/infer/tests/codetoanalyze/cpp/frontend/globals/global_const1.cpp.dot @@ -29,11 +29,11 @@ digraph iCFG { "test2#_Z5test2v.51af7cbda728abeafa25300302478ca4_2" [label="2: Exit test2 \n " color=yellow style=filled] -"test2#_Z5test2v.51af7cbda728abeafa25300302478ca4_3" [label="3: Return Stmt \n *&#GB$v:int=2 [line 21]\n n$0=*&#GB$v:int [line 21]\n *&return:int=n$0 [line 21]\n " shape="box"] +"test2#_Z5test2v.51af7cbda728abeafa25300302478ca4_3" [label="3: Return Stmt \n n$0=*&#GB$v:int [line 21]\n *&return:int=n$0 [line 21]\n " shape="box"] "test2#_Z5test2v.51af7cbda728abeafa25300302478ca4_3" -> "test2#_Z5test2v.51af7cbda728abeafa25300302478ca4_2" ; -"test2#_Z5test2v.51af7cbda728abeafa25300302478ca4_4" [label="4: DeclStmt \n *&#GB$v:int=2 [line 20]\n n$1=*&#GB$v:int [line 20]\n *&local:int=n$1 [line 20]\n " shape="box"] +"test2#_Z5test2v.51af7cbda728abeafa25300302478ca4_4" [label="4: DeclStmt \n n$1=*&#GB$v:int [line 20]\n *&local:int=n$1 [line 20]\n " shape="box"] "test2#_Z5test2v.51af7cbda728abeafa25300302478ca4_4" -> "test2#_Z5test2v.51af7cbda728abeafa25300302478ca4_3" ; @@ -44,7 +44,7 @@ digraph iCFG { "test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_2" [label="2: Exit test \n " color=yellow style=filled] -"test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_3" [label="3: Return Stmt \n n$0=*&__return_param:class X* [line 15]\n _fun_X_X(&#GB$global:class X*) [line 13]\n _fun_X_X(n$0:class X*,&#GB$global:class X&) [line 15]\n " shape="box"] +"test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_3" [label="3: Return Stmt \n n$0=*&__return_param:class X* [line 15]\n _fun_X_X(n$0:class X*,&#GB$global:class X&) [line 15]\n " shape="box"] "test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_3" -> "test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_2" ; diff --git a/infer/tests/codetoanalyze/cpp/frontend/globals/global_const2.cpp.dot b/infer/tests/codetoanalyze/cpp/frontend/globals/global_const2.cpp.dot index 2f8366e77..b45fb71ec 100644 --- a/infer/tests/codetoanalyze/cpp/frontend/globals/global_const2.cpp.dot +++ b/infer/tests/codetoanalyze/cpp/frontend/globals/global_const2.cpp.dot @@ -32,36 +32,15 @@ digraph iCFG { "__infer_globals_initializer_global.bdc08c089842ce08b974b22a75daf78e_8" -> "__infer_globals_initializer_global.bdc08c089842ce08b974b22a75daf78e_2" ; -"test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_1" [label="1: Start test\nFormals: \nLocals: 0$?%__sil_tmpSIL_temp_conditional___n$0:int \n DECLARE_LOCALS(&return,&0$?%__sil_tmpSIL_temp_conditional___n$0); [line 11]\n " color=yellow style=filled] +"test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_1" [label="1: Start test\nFormals: \nLocals: \n DECLARE_LOCALS(&return); [line 11]\n " color=yellow style=filled] - "test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_1" -> "test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_4" ; - "test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_1" -> "test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_5" ; + "test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_1" -> "test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_3" ; "test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_2" [label="2: Exit test \n " color=yellow style=filled] -"test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_3" [label="3: + \n " ] +"test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_3" [label="3: Return Stmt \n n$0=*&#GB$global:int [line 11]\n *&return:int=n$0 [line 11]\n " shape="box"] - "test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_3" -> "test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_8" ; -"test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_4" [label="4: Prune (true branch) \n PRUNE((1 != 0), true); [line 10]\n " shape="invhouse"] - - - "test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_4" -> "test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_6" ; -"test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_5" [label="5: Prune (false branch) \n PRUNE((1 == 0), false); [line 10]\n " shape="invhouse"] - - - "test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_5" -> "test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_7" ; -"test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_6" [label="6: ConditinalStmt Branch \n *&0$?%__sil_tmpSIL_temp_conditional___n$0:int=2 [line 10]\n " shape="box"] - - - "test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_6" -> "test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_3" ; -"test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_7" [label="7: ConditinalStmt Branch \n *&0$?%__sil_tmpSIL_temp_conditional___n$0:int=3 [line 10]\n " shape="box"] - - - "test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_7" -> "test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_3" ; -"test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_8" [label="8: Return Stmt \n n$1=*&0$?%__sil_tmpSIL_temp_conditional___n$0:int [line 10]\n *&#GB$global:int=n$1 [line 11]\n n$2=*&#GB$global:int [line 11]\n *&return:int=n$2 [line 11]\n " shape="box"] - - - "test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_8" -> "test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_2" ; + "test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_3" -> "test#_Z4testv.1b928d988491fdd2fa78fcb048d46e8c_2" ; } diff --git a/infer/tests/codetoanalyze/cpp/shared/namespace/namespace.cpp.dot b/infer/tests/codetoanalyze/cpp/shared/namespace/namespace.cpp.dot index 29fdb9b33..cf1379350 100644 --- a/infer/tests/codetoanalyze/cpp/shared/namespace/namespace.cpp.dot +++ b/infer/tests/codetoanalyze/cpp/shared/namespace/namespace.cpp.dot @@ -29,7 +29,7 @@ digraph iCFG { "value#bar#_ZN3bar5valueEv.c4f3702e0a2c665b92d9ddc705c63d00_2" [label="2: Exit bar::value \n " color=yellow style=filled] -"value#bar#_ZN3bar5valueEv.c4f3702e0a2c665b92d9ddc705c63d00_3" [label="3: Return Stmt \n *&#GB$bar::pi:double=3.141600 [line 30]\n n$0=*&#GB$bar::pi:double [line 30]\n *&return:double=(2 * n$0) [line 30]\n " shape="box"] +"value#bar#_ZN3bar5valueEv.c4f3702e0a2c665b92d9ddc705c63d00_3" [label="3: Return Stmt \n n$0=*&#GB$bar::pi:double [line 30]\n *&return:double=(2 * n$0) [line 30]\n " shape="box"] "value#bar#_ZN3bar5valueEv.c4f3702e0a2c665b92d9ddc705c63d00_3" -> "value#bar#_ZN3bar5valueEv.c4f3702e0a2c665b92d9ddc705c63d00_2" ; @@ -55,7 +55,7 @@ digraph iCFG { "main.fad58de7366495db4650cfefac2fcd61_3" -> "main.fad58de7366495db4650cfefac2fcd61_2" ; -"main.fad58de7366495db4650cfefac2fcd61_4" [label="4: BinaryOperatorStmt: Assign \n *&#GB$bar::pi:double=3.141600 [line 57]\n n$0=*&#GB$bar::pi:double [line 57]\n *&j:double=n$0 [line 57]\n " shape="box"] +"main.fad58de7366495db4650cfefac2fcd61_4" [label="4: BinaryOperatorStmt: Assign \n n$0=*&#GB$bar::pi:double [line 57]\n *&j:double=n$0 [line 57]\n " shape="box"] "main.fad58de7366495db4650cfefac2fcd61_4" -> "main.fad58de7366495db4650cfefac2fcd61_3" ; diff --git a/infer/tests/codetoanalyze/objc/errors/issues.exp b/infer/tests/codetoanalyze/objc/errors/issues.exp index b64419653..99cf323fc 100644 --- a/infer/tests/codetoanalyze/objc/errors/issues.exp +++ b/infer/tests/codetoanalyze/objc/errors/issues.exp @@ -1,5 +1,6 @@ codetoanalyze/objc/errors/field_superclass/field.c, field_superclass_main, 3, RETAIN_CYCLE, [start of procedure field_superclass_main()] codetoanalyze/objc/errors/global_const/global_const.m, SimpleRoot_doSomethingBadWithDict:andString:, 3, NULL_DEREFERENCE, [start of procedure doSomethingBadWithDict:andString:,Message stringByAppendingString: with receiver nil returns nil.] +codetoanalyze/objc/errors/global_const/global_const.m, SimpleRoot_doSomethingOkWithDict:andString:, 3, NULL_DEREFERENCE, [start of procedure doSomethingOkWithDict:andString:,Message stringByAppendingString: with receiver nil returns nil.] codetoanalyze/objc/errors/initialization/compound_literal.c, init_with_compound_literal, 2, DIVIDE_BY_ZERO, [start of procedure init_with_compound_literal()] codetoanalyze/objc/errors/memory_leaks_benchmark/CADisplayLinkRetainCycle.m, testCycle, 3, RETAIN_CYCLE, [start of procedure testCycle(),start of procedure init,return from a call to CADisplay_init] codetoanalyze/objc/errors/memory_leaks_benchmark/RetainCycleStaticVar.m, RetainCSVycleStaticVar, 2, RETAIN_CYCLE, [start of procedure RetainCSVycleStaticVar(),start of procedure init,return from a call to RetainCSV_init,start of procedure foo,start of procedure block,start of procedure init,return from a call to RetainCSV_init,return from a call to __objc_anonymous_block_RetainCSV_foo______3,start of procedure block,start of procedure init,return from a call to RetainCSV_init,return from a call to __objc_anonymous_block_RetainCSV_foo______2,return from a call to RetainCSV_foo] diff --git a/infer/tests/codetoanalyze/objcpp/frontend/global_const/global_const.mm.dot b/infer/tests/codetoanalyze/objcpp/frontend/global_const/global_const.mm.dot index 6ff5c7f91..eba2df69f 100644 --- a/infer/tests/codetoanalyze/objcpp/frontend/global_const/global_const.mm.dot +++ b/infer/tests/codetoanalyze/objcpp/frontend/global_const/global_const.mm.dot @@ -18,7 +18,7 @@ digraph iCFG { "fields#_Z6fieldsv.9f46b295429b26ed77896571ab767beb_2" [label="2: Exit fields \n " color=yellow style=filled] -"fields#_Z6fieldsv.9f46b295429b26ed77896571ab767beb_3" [label="3: Return Stmt \n n$0=*&__return_param:class Fields* [line 20]\n *&#GB$__someFields.field1:float=1 [line 16]\n *&#GB$__someFields.field2:float=2 [line 16]\n *&#GB$__someFields.field3:float=3 [line 16]\n _fun_Fields_(n$0:class Fields*,&#GB$__someFields:class Fields&) [line 20]\n " shape="box"] +"fields#_Z6fieldsv.9f46b295429b26ed77896571ab767beb_3" [label="3: Return Stmt \n n$0=*&__return_param:class Fields* [line 20]\n _fun_Fields_(n$0:class Fields*,&#GB$__someFields:class Fields&) [line 20]\n " shape="box"] "fields#_Z6fieldsv.9f46b295429b26ed77896571ab767beb_3" -> "fields#_Z6fieldsv.9f46b295429b26ed77896571ab767beb_2" ;