From e1850ffcb9e17a27975a4c64ecb723e0bb187cc5 Mon Sep 17 00:00:00 2001 From: Akos Hajdu Date: Thu, 28 Mar 2024 13:09:34 -0700 Subject: [PATCH] [annot] Simplify spec parsing Summary: With the move to `Yojson.Safe`, we can simplify parsing. Reviewed By: rgrig Differential Revision: D55479552 fbshipit-source-id: fb91c63803e3d37572e16ea556fc3551d20716e9 --- infer/src/checkers/annotationReachability.ml | 39 ++++++-------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/infer/src/checkers/annotationReachability.ml b/infer/src/checkers/annotationReachability.ml index 25c5cda110a..7d43b652f55 100644 --- a/infer/src/checkers/annotationReachability.ml +++ b/infer/src/checkers/annotationReachability.ml @@ -429,39 +429,22 @@ module ExpensiveAnnotationSpec = struct } end -(* parse user-defined specs from .inferconfig *) -let parse_user_defined_specs = function - | `List user_specs -> - let parse_user_spec json = - let open Yojson.Safe in - let sources = Util.member "sources" json |> Util.to_list |> List.map ~f:Util.to_string in - let sinks = Util.member "sinks" json |> Util.to_list |> List.map ~f:Util.to_string in - let sanitizers = - match Util.member "sanitizers" json with - | `List s -> - List.map ~f:Util.to_string s - | _ -> - L.user_warning "Expected a list of strings as sanitizers, ignoring.\n" ; - [] - in - (sources, sinks, sanitizers) - in - List.map ~f:parse_user_spec user_specs - | _ -> - L.user_warning "Expected a list as custom annotations, ignoring.\n" ; - [] +type user_defined_spec = + {sources: string list; sinks: string list; sanitizers: string list [@yojson.default []]} +[@@deriving of_yojson] +type user_defined_specs = user_defined_spec list [@@deriving of_yojson] let annot_specs = - let parse_one_spec (str_src_annots, str_snk_annots, str_sanitizer_annots) = - List.map - ~f:(fun str_snk_annot -> - StandardAnnotationSpec.from_annotations str_src_annots str_snk_annot str_sanitizer_annots ) - str_snk_annots + let make_standard_spec_from_user_spec {sources; sinks; sanitizers} = + List.map ~f:(fun sink -> StandardAnnotationSpec.from_annotations sources sink sanitizers) sinks in let user_defined_specs = - parse_user_defined_specs Config.annotation_reachability_custom_pairs - |> List.map ~f:parse_one_spec + let specs = + try user_defined_specs_of_yojson Config.annotation_reachability_custom_pairs + with _ -> L.die ExternalError "Could not parse annotation reachability custom pairs@." + in + List.map specs ~f:make_standard_spec_from_user_spec in let user_defined_specs = List.concat user_defined_specs in [ (Language.Clang, CxxAnnotationSpecs.from_config ())