Skip to content

Commit d940fd0

Browse files
authored
refines the Theory.Target.matches and adds the matching function (#1429)
The matching function now takes the nicknames of the ancestors into account. And a new `Theory.Target.matching` function returns the actual target that matched with the name.
1 parent 17e2a42 commit d940fd0

File tree

4 files changed

+48
-14
lines changed

4 files changed

+48
-14
lines changed

lib/bap_core_theory/bap_core_theory.mli

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,7 +1458,7 @@ module Theory : sig
14581458

14591459
(** [read ?package name] is a synonym for [get ?package name].
14601460
1461-
Introduces for the consistency with the [Enum.S] interface.
1461+
Introduced for the consistency with the [Enum.S] interface.
14621462
*)
14631463
val read : ?package:string -> string -> t
14641464

@@ -1480,10 +1480,30 @@ module Theory : sig
14801480
(** [name target] is the unique name of the target. *)
14811481
val name : t -> KB.Name.t
14821482

1483-
(** [matches target name] is true if [name] matches either
1484-
the unqualified name of the target itsef or one of its
1485-
ancestors; or if the name matches one of the target
1486-
nicknames. E.g., [matches target "mips"].
1483+
1484+
(** [matching t name] the target that matches [name].
1485+
1486+
[matching t name] is [Some r] where [r] is [t] or the closest
1487+
ancestor of [t] such that [r]'s name is equal to [name] or one
1488+
of name is [r]'s nicknames.
1489+
1490+
@since 2.5.0
1491+
*)
1492+
val matching : t -> string -> t option
1493+
1494+
1495+
(** [matches t name] when {!matching t name} is not [None].
1496+
1497+
[matching t name] is true when [name] matches either the
1498+
unqualified name of the target itsef or one of its ancestors;
1499+
or if the name matches one of the target nicknames or the
1500+
target parents nicknames.
1501+
1502+
E.g., [matches target "mips"].
1503+
1504+
@before 2.5.0 the nicknames of the ancestors weren't taken
1505+
into account
1506+
@after 2.5.0 uses the ancestors nicknames for matching
14871507
*)
14881508
val matches : t -> string -> bool
14891509

@@ -1515,7 +1535,7 @@ module Theory : sig
15151535
*)
15161536
val parents : t -> t list
15171537

1518-
(** [family p] returns an ordered list of targets that {!belongs} [p].
1538+
(** [family p] an ordered list of targets s.t. each {!belongs} to [p].
15191539
15201540
The family members are ordered according to their hierarchy
15211541
with [p] comming first.
@@ -2342,6 +2362,12 @@ module Theory : sig
23422362
?options:string list ->
23432363
string -> compiler
23442364

2365+
2366+
(** [name compiler] returns the compiler name.
2367+
2368+
@since 2.5.0 *)
2369+
val name : compiler -> string
2370+
23452371
(** [version] the compiler version.
23462372
23472373
The least specific (aka major) version comes first in the

lib/bap_core_theory/bap_core_theory_program.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ module Compiler : sig
6969
?version:string list ->
7070
?options:string list ->
7171
string -> compiler
72+
val name : compiler -> string
7273
val version : compiler -> string list
7374
val options : compiler -> string list
7475
val specs : compiler -> string Map.M(String).t

lib/bap_core_theory/bap_core_theory_target.ml

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -547,14 +547,6 @@ let is_known c = not@@is_unknown c
547547
let rec belongs p c =
548548
Self.equal p c || is_known c && belongs p (parent c)
549549

550-
let rec matches_name t name =
551-
String.Caseless.equal (Name.unqualified (Self.name t)) name ||
552-
is_known t && matches_name (parent t) name
553-
554-
let rec matches t name =
555-
let nicks = (info t).names in
556-
Set.mem nicks name || matches_name t name
557-
558550
let order t1 t2 : KB.Order.partial =
559551
if Self.equal t1 t2 then EQ
560552
else if belongs t1 t2 then LT
@@ -597,6 +589,20 @@ let partition xs =
597589

598590
let families () = partition@@declared ()
599591

592+
let matches_name t name =
593+
String.Caseless.equal (Name.unqualified (Self.name t)) name
594+
595+
let rec matching t name =
596+
if matches_name t name || Set.mem (info t).names name
597+
then Some t
598+
else if is_known t then matching (parent t) name
599+
else None
600+
601+
602+
let matches t name =
603+
Option.is_some (matching t name)
604+
605+
let nicknames t = (info t).names
600606

601607
type alias = Alias.t
602608

lib/bap_core_theory/bap_core_theory_target.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ val unknown : t
4646
val is_unknown : t -> bool
4747
val name : t -> KB.Name.t
4848
val matches : t -> string -> bool
49+
val matching : t -> string -> t option
4950
val order : t -> t -> KB.Order.partial
5051
val belongs : t -> t -> bool
5152
val parent : t -> t

0 commit comments

Comments
 (0)