From 6ca447fa6a44909bbcc70d0e96015bbc56c03f8d Mon Sep 17 00:00:00 2001 From: Andrzej Kotulski Date: Mon, 7 Aug 2017 11:53:10 -0700 Subject: [PATCH] [C] Fix issue with enum initialization lists Summary: Fail quietly to translate enum initializer lists. Reviewed By: grievejia Differential Revision: D5574903 fbshipit-source-id: 6d3c4ea --- infer/src/clang/cTrans.ml | 6 +++++- .../initialization/enum_initlistexpr.c | 18 ++++++++++++++++++ .../initialization/enum_initlistexpr.c.dot | 10 ++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 infer/tests/codetoanalyze/c/frontend/initialization/enum_initlistexpr.c create mode 100644 infer/tests/codetoanalyze/c/frontend/initialization/enum_initlistexpr.c.dot diff --git a/infer/src/clang/cTrans.ml b/infer/src/clang/cTrans.ml index ef3d3cfab..849096c24 100644 --- a/infer/src/clang/cTrans.ml +++ b/infer/src/clang/cTrans.ml @@ -1950,7 +1950,11 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s let init_field field_exp_typ stmt = init_expr_trans trans_state field_exp_typ stmt_info (Some stmt) in - List.map2_exn field_exps stmts ~f:init_field + match List.map2 field_exps stmts ~f:init_field with + | Ok result + -> result + | Unequal_lengths + -> (* This can happen with union initializers. Skip them for now *) [] and initListExpr_builtin_trans trans_state stmt_info stmts var_exp var_typ = let stmt = match stmts with [s] -> s | _ -> assert false in diff --git a/infer/tests/codetoanalyze/c/frontend/initialization/enum_initlistexpr.c b/infer/tests/codetoanalyze/c/frontend/initialization/enum_initlistexpr.c new file mode 100644 index 000000000..06723508b --- /dev/null +++ b/infer/tests/codetoanalyze/c/frontend/initialization/enum_initlistexpr.c @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2017 - 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. + */ +union U { + int f1; + int f2[3]; +}; + +void union_initialize_FIXME() { + union U set_f1 = {.f1 = 2}; + union U set_f2 = {.f2 = {1, 2, 3}}; + union U set_f1_implicit = {1}; +} diff --git a/infer/tests/codetoanalyze/c/frontend/initialization/enum_initlistexpr.c.dot b/infer/tests/codetoanalyze/c/frontend/initialization/enum_initlistexpr.c.dot new file mode 100644 index 000000000..2eea159ce --- /dev/null +++ b/infer/tests/codetoanalyze/c/frontend/initialization/enum_initlistexpr.c.dot @@ -0,0 +1,10 @@ +/* @generated */ +digraph iCFG { +"union_initialize_FIXME.324b85335f5d2e418a28cb97eb896f20_1" [label="1: Start union_initialize_FIXME\nFormals: \nLocals: set_f1_implicit:U set_f2:U set_f1:U \n DECLARE_LOCALS(&return,&set_f1_implicit,&set_f2,&set_f1); [line 14]\n " color=yellow style=filled] + + + "union_initialize_FIXME.324b85335f5d2e418a28cb97eb896f20_1" -> "union_initialize_FIXME.324b85335f5d2e418a28cb97eb896f20_2" ; +"union_initialize_FIXME.324b85335f5d2e418a28cb97eb896f20_2" [label="2: Exit union_initialize_FIXME \n " color=yellow style=filled] + + +}