From e42bd8cd6c4924a2f27e19f5150fb8e70392a8e0 Mon Sep 17 00:00:00 2001 From: Nikos Gorogiannis Date: Fri, 13 Dec 2019 03:13:58 -0800 Subject: [PATCH] [typ][fieldname] further reduce and improve interface Summary: - Remove `to_flat_string` as there is `get_field_name` that unambiguously does the same thing. - Make `pp` print only the field in all languages. - Fix `to_full_string` so that it has unified behaviour across java/clang and so that it doesn't print `class Foo.x`, but rather `Foo.x`. Reviewed By: ezgicicek Differential Revision: D18963033 fbshipit-source-id: e2c803c7d --- infer/src/IR/AccessPath.ml | 4 ++-- infer/src/IR/DecompiledExp.ml | 4 ++-- infer/src/IR/Typ.ml | 10 ++-------- infer/src/IR/Typ.mli | 3 --- infer/src/backend/errdesc.ml | 2 +- infer/src/biabduction/Rearrange.ml | 2 +- infer/src/bufferoverrun/bufferOverrunField.ml | 2 +- infer/src/checkers/fragmentRetainsViewChecker.ml | 2 +- infer/src/nullsafe/AssignmentRule.ml | 2 +- infer/src/nullsafe/ErrorRenderingUtils.ml | 2 +- infer/src/nullsafe/NullabilitySuggest.ml | 4 ++-- infer/src/nullsafe/typeErr.ml | 2 +- infer/src/nullsafe/typeOrigin.ml | 2 +- 13 files changed, 16 insertions(+), 25 deletions(-) diff --git a/infer/src/IR/AccessPath.ml b/infer/src/IR/AccessPath.ml index 1acc9e8e2..b65b567cd 100644 --- a/infer/src/IR/AccessPath.ml +++ b/infer/src/IR/AccessPath.ml @@ -33,7 +33,7 @@ module Raw = struct let rec pp_access fmt = function | FieldAccess field_name -> - F.pp_print_string fmt (Typ.Fieldname.to_flat_string field_name) + F.pp_print_string fmt (Typ.Fieldname.get_field_name field_name) | ArrayAccess (typ, []) -> F.pp_print_string fmt "[_]" ; may_pp_typ fmt typ | ArrayAccess (typ, index_aps) -> @@ -310,7 +310,7 @@ let inner_class_normalize p = |> Option.bind ~f:(fun outer_name -> let outer_class = mk ~default:cls (Tstruct outer_name) in let outer_ptr = mk ~default:ptr (Tptr (outer_class, pkind)) in - let varname = Fieldname.to_flat_string first |> Mangled.from_string in + let varname = Fieldname.get_field_name first |> Mangled.from_string in mk_pvar_as varname pvar |> Option.map ~f:(fun new_pvar -> let base = base_of_pvar new_pvar outer_ptr in diff --git a/infer/src/IR/DecompiledExp.ml b/infer/src/IR/DecompiledExp.ml index 46f737894..df28e6b29 100644 --- a/infer/src/IR/DecompiledExp.ml +++ b/infer/src/IR/DecompiledExp.ml @@ -100,14 +100,14 @@ let rec pp fmt = function F.pp_print_string fmt (Typ.Fieldname.to_simplified_string f) | Darrow (de, f) -> if Language.curr_language_is Java then - F.fprintf fmt "%a.%s" pp de (Typ.Fieldname.to_flat_string f) + F.fprintf fmt "%a.%s" pp de (Typ.Fieldname.get_field_name f) else F.fprintf fmt "%a->%s" pp de (Typ.Fieldname.to_string f) | Ddot (Dpvar _, fe) when eradicate_java () -> (* static field access *) F.pp_print_string fmt (Typ.Fieldname.to_simplified_string fe) | Ddot (de, f) -> let field_text = - if Language.curr_language_is Java then Typ.Fieldname.to_flat_string f + if Language.curr_language_is Java then Typ.Fieldname.get_field_name f else Typ.Fieldname.to_string f in F.fprintf fmt "%a.%s" pp de field_text diff --git a/infer/src/IR/Typ.ml b/infer/src/IR/Typ.ml index 9ebe24c2d..596261f70 100644 --- a/infer/src/IR/Typ.ml +++ b/infer/src/IR/Typ.ml @@ -1460,16 +1460,10 @@ module Fieldname = struct let to_full_string fld = - if is_java fld then dot_join (Name.name fld.class_name) fld.field_name - else cc_join (Name.to_string fld.class_name) fld.field_name + (if is_java fld then dot_join else cc_join) (Name.name fld.class_name) fld.field_name - let to_flat_string {field_name} = field_name - - let pp f ({class_name; field_name} as field) = - F.pp_print_string f - (if is_java field then dot_join (Name.name class_name) field_name else field_name) - + let pp f fld = F.pp_print_string f fld.field_name let is_java_captured_parameter ({field_name} as field) = is_java field && String.is_prefix ~prefix:"val$" field_name diff --git a/infer/src/IR/Typ.mli b/infer/src/IR/Typ.mli index b1b2958f4..547e446e3 100644 --- a/infer/src/IR/Typ.mli +++ b/infer/src/IR/Typ.mli @@ -669,9 +669,6 @@ module Fieldname : sig val to_simplified_string : t -> string (** Convert a fieldname to a simplified string with at most one-level path. *) - val to_flat_string : t -> string - (** Convert a fieldname to a flat string without path. *) - val pp : Format.formatter -> t -> unit (** Pretty print a field name. *) end diff --git a/infer/src/backend/errdesc.ml b/infer/src/backend/errdesc.ml index cb9d6b82d..a897f1a12 100644 --- a/infer/src/backend/errdesc.ml +++ b/infer/src/backend/errdesc.ml @@ -32,7 +32,7 @@ let is_method_of_objc_cpp_class pname matcher = let is_vector_method pname = is_method_of_objc_cpp_class pname vector_matcher let is_special_field matcher field_name_opt field = - let field_name = Typ.Fieldname.to_flat_string field in + let field_name = Typ.Fieldname.get_field_name field in let field_ok = match field_name_opt with | Some field_name' -> diff --git a/infer/src/biabduction/Rearrange.ml b/infer/src/biabduction/Rearrange.ml index a004426c8..9072dfd93 100644 --- a/infer/src/biabduction/Rearrange.ml +++ b/infer/src/biabduction/Rearrange.ml @@ -790,7 +790,7 @@ let add_guarded_by_constraints tenv prop lexp pdesc = let is_guarded_by_fld guarded_by_str fld _ = (* this comparison needs to be somewhat fuzzy, since programmers are free to write @GuardedBy("mLock"), @GuardedBy("MyClass.mLock"), or use other conventions *) - String.equal (Typ.Fieldname.to_flat_string fld) guarded_by_str + String.equal (Typ.Fieldname.get_field_name fld) guarded_by_str || String.equal (Typ.Fieldname.to_string fld) guarded_by_str in let get_fld_strexp_and_typ typ f flds = diff --git a/infer/src/bufferoverrun/bufferOverrunField.ml b/infer/src/bufferoverrun/bufferOverrunField.ml index b95ef9bc4..87842463e 100644 --- a/infer/src/bufferoverrun/bufferOverrunField.ml +++ b/infer/src/bufferoverrun/bufferOverrunField.ml @@ -13,7 +13,7 @@ module L = Logging Create invisible phantom fields by giving them a name ending in '.' The name preceeding the '.' will be used in debug mode. *) let pp ~pp_lhs ~pp_lhs_alone ~sep f lhs fn = - let fieldname = Typ.Fieldname.to_flat_string fn in + let fieldname = Typ.Fieldname.get_field_name fn in if String.is_empty fieldname then if Config.bo_debug > 0 then let fieldname = diff --git a/infer/src/checkers/fragmentRetainsViewChecker.ml b/infer/src/checkers/fragmentRetainsViewChecker.ml index d60197852..f5a61705a 100644 --- a/infer/src/checkers/fragmentRetainsViewChecker.ml +++ b/infer/src/checkers/fragmentRetainsViewChecker.ml @@ -41,7 +41,7 @@ let report_warning class_name fld fld_typ summary = the back stack, a reference to this (probably dead) View will be retained. In general, it \ is a good idea to initialize View's in %a, then nullify them in %a." pp_m (Typ.Name.name class_name) pp_m - (Typ.Fieldname.to_flat_string fld) + (Typ.Fieldname.get_field_name fld) pp_m (format_typ fld_typ) pp_m (format_method pname) pp_m on_create_view pp_m on_destroy_view in Reporting.log_warning summary ~loc IssueType.checkers_fragment_retain_view description diff --git a/infer/src/nullsafe/AssignmentRule.ml b/infer/src/nullsafe/AssignmentRule.ml index 22bf2b971..ca0fb2a6f 100644 --- a/infer/src/nullsafe/AssignmentRule.ml +++ b/infer/src/nullsafe/AssignmentRule.ml @@ -175,7 +175,7 @@ let violation_description {is_strict_mode; lhs; rhs} ~assignment_location assign Logging.die InternalError "Invariant violation: unexpected nullability" in Format.asprintf "%a is declared non-nullable but is assigned %s%s." MF.pp_monospaced - (Typ.Fieldname.to_flat_string field_name) + (Typ.Fieldname.get_field_name field_name) rhs_description nullability_evidence_as_suffix | ReturningFromFunction function_proc_name -> let return_description = diff --git a/infer/src/nullsafe/ErrorRenderingUtils.ml b/infer/src/nullsafe/ErrorRenderingUtils.ml index 6e2e3886d..4d8ff4d5f 100644 --- a/infer/src/nullsafe/ErrorRenderingUtils.ml +++ b/infer/src/nullsafe/ErrorRenderingUtils.ml @@ -31,7 +31,7 @@ let is_object_nullability_self_explanatory ~object_expression object_origin = | TypeOrigin.Field {field_name} -> (* Either local variable or expression like `.field_name`. Latter case is trivial: the user can quickly go to field_name definition and see if its annotation. *) - let field_name_str = Typ.Fieldname.to_flat_string field_name in + let field_name_str = Typ.Fieldname.get_field_name field_name in String.is_suffix object_expression ~suffix:field_name_str | TypeOrigin.MethodCall {pname; annotated_signature= {model_source}} -> let is_modelled = Option.is_some model_source in diff --git a/infer/src/nullsafe/NullabilitySuggest.ml b/infer/src/nullsafe/NullabilitySuggest.ml index 9f6002980..276f6a5eb 100644 --- a/infer/src/nullsafe/NullabilitySuggest.ml +++ b/infer/src/nullsafe/NullabilitySuggest.ml @@ -125,7 +125,7 @@ let make_error_trace astate ap ud = let name_of ap = match AccessPath.get_last_access ap with | Some (AccessPath.FieldAccess field_name) -> - "Field " ^ Typ.Fieldname.to_flat_string field_name + "Field " ^ Typ.Fieldname.get_field_name field_name | Some (AccessPath.ArrayAccess _) -> "Some array element" | None -> @@ -163,7 +163,7 @@ let pretty_field_name proc_data field_name = | Typ.Procname.Java jproc_name -> let proc_class_name = Typ.Procname.Java.get_class_name jproc_name in let field_class_name = Typ.Fieldname.get_class_name field_name |> Typ.Name.name in - if String.equal proc_class_name field_class_name then Typ.Fieldname.to_flat_string field_name + if String.equal proc_class_name field_class_name then Typ.Fieldname.get_field_name field_name else Typ.Fieldname.to_simplified_string field_name | _ -> (* This format is subject to change once this checker gets to run on C/Cpp/ObjC *) diff --git a/infer/src/nullsafe/typeErr.ml b/infer/src/nullsafe/typeErr.ml index af783d4fd..38ac87d11 100644 --- a/infer/src/nullsafe/typeErr.ml +++ b/infer/src/nullsafe/typeErr.ml @@ -232,7 +232,7 @@ let get_error_info err_instance = "Field %a is declared non-nullable, so it should be initialized in the constructor or in \ an `@Initializer` method" MF.pp_monospaced - (Typ.Fieldname.to_flat_string field_name) + (Typ.Fieldname.get_field_name field_name) , IssueType.eradicate_field_not_initialized , None ) | Bad_assignment {rhs_origin; assignment_location; assignment_type; assignment_violation} -> diff --git a/infer/src/nullsafe/typeOrigin.ml b/infer/src/nullsafe/typeOrigin.ml index e3a19ef7d..eea44a432 100644 --- a/infer/src/nullsafe/typeOrigin.ml +++ b/infer/src/nullsafe/typeOrigin.ml @@ -144,7 +144,7 @@ let get_description origin = | NullConst loc -> Some ("null constant" ^ atline loc) | Field {field_name; access_loc} -> - Some ("field " ^ Typ.Fieldname.to_flat_string field_name ^ atline access_loc) + Some ("field " ^ Typ.Fieldname.get_field_name field_name ^ atline access_loc) | MethodParameter {mangled} -> Some ("method parameter " ^ Mangled.to_string mangled) | MethodCall {pname; call_loc; annotated_signature} ->