Skip to content

Commit

Permalink
Fix Cilfacade.split_anoncomp_name for empty names (closes #1171)
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Sep 18, 2023
1 parent eb48502 commit 1feb75e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/domains/access.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ let is_ignorable_type (t: typ): bool =
| TComp ({ cname = "__pthread_mutex_s" | "__pthread_rwlock_arch_t" | "__jmp_buf_tag" | "_pthread_cleanup_buffer" | "__pthread_cleanup_frame" | "__cancel_jmp_buf_tag"; _}, _) -> true
| TComp ({ cname; _}, _) when String.starts_with_stdlib ~prefix:"__anon" cname ->
begin match Cilfacade.split_anoncomp_name cname with
| (true, ("__once_flag" | "__pthread_unwind_buf_t" | "__cancel_jmp_buf"), _) -> true (* anonstruct *)
| (false, ("pthread_mutexattr_t" | "pthread_condattr_t" | "pthread_barrierattr_t"), _) -> true (* anonunion *)
| (true, Some ("__once_flag" | "__pthread_unwind_buf_t" | "__cancel_jmp_buf"), _) -> true (* anonstruct *)
| (false, Some ("pthread_mutexattr_t" | "pthread_condattr_t" | "pthread_barrierattr_t"), _) -> true (* anonunion *)
| _ -> false
end
| TComp ({ cname = "lock_class_key"; _ }, _) -> true
Expand Down
9 changes: 5 additions & 4 deletions src/util/cilfacade.ml
Original file line number Diff line number Diff line change
Expand Up @@ -342,22 +342,23 @@ let makeBinOp binop e1 e2 =
let (_, e) = Cabs2cil.doBinOp binop e1 t1 e2 t2 in
e

let anoncomp_name_regexp = Str.regexp {|^__anon\(struct\|union\)_\(.+\)_\([0-9]+\)$|}
let anoncomp_name_regexp = Str.regexp {|^__anon\(struct\|union\)\(_\(.+\)\)?_\([0-9]+\)$|}

let split_anoncomp_name name =
(* __anonunion_pthread_mutexattr_t_488594144 *)
(* __anonunion_50 *)
if Str.string_match anoncomp_name_regexp name 0 then (
let struct_ = match Str.matched_group 1 name with
| "struct" -> true
| "union" -> false
| _ -> assert false
in
let name' = Str.matched_group 2 name in
let id = int_of_string (Str.matched_group 3 name) in
let name' = try Some (Str.matched_group 3 name) with Not_found -> None in
let id = int_of_string (Str.matched_group 4 name) in
(struct_, name', id)
)
else
invalid_arg "Cilfacade.split_anoncomp_name"
invalid_arg ("Cilfacade.split_anoncomp_name: " ^ name)

(** Pretty-print typsig like typ, because
{!d_typsig} prints with CIL constructors. *)
Expand Down
1 change: 1 addition & 0 deletions unittest/mainTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ let all_tests = ("" >:::
LvalTest.test ();
CompilationDatabaseTest.tests;
LibraryDslTest.tests;
CilfacadeTest.tests;
(* etc *)
"domaintest" >::: QCheck_ounit.to_ounit2_test_list Maindomaintest.all_testsuite;
IntOpsTest.tests;
Expand Down
14 changes: 14 additions & 0 deletions unittest/util/cilfacadeTest.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
open Goblint_lib
open OUnit2
open Cilfacade

let test_split_anoncomp_name _ =
let assert_equal = assert_equal ~printer:[%show: bool * string option * int] in
assert_equal (false, Some "pthread_mutexattr_t", 488594144) (split_anoncomp_name "__anonunion_pthread_mutexattr_t_488594144");
assert_equal (true, Some "__once_flag", 1234) (split_anoncomp_name "__anonstruct___once_flag_1234");
assert_equal (false, None, 50) (split_anoncomp_name "__anonunion_50")

let tests =
"cilfacadeTest" >::: [
"split_anoncomp_name" >:: test_split_anoncomp_name;
]

0 comments on commit 1feb75e

Please sign in to comment.