From 0c3e568fa430d35d6a859e9864135a9592fa376d Mon Sep 17 00:00:00 2001 From: Mitya Lyubarskiy Date: Thu, 31 Oct 2019 07:06:10 -0700 Subject: [PATCH] [Pp] Rename Pp.to_string Summary: Now that we have two similar functions, it becomes confusing, because `Pp.to_string` and `Pp.string_of_pp` can seem to do the same stuff, while in reality they do the opposite. Well, it is still bit confusing, because the proper names would be `Pp.pp_of_to_string` and `Pp.to_string_of_pp`, but I think this high level order names are not necessary given that in most cases they will be used as concrete functions. I think `Pp.of_string` captures such usages better than `to_string` used to do: you need to pp stuff, but you have a string (or, technically, a function that returns a string), so you pretty print OF that string, aren't you? Reviewed By: jvillard Differential Revision: D18245876 fbshipit-source-id: fd4b6ab68 --- infer/src/IR/DecompiledExp.ml | 2 +- infer/src/IR/ProcAttributes.ml | 4 ++-- infer/src/al/cPredicates.ml | 2 +- infer/src/base/PerfEvent.ml | 4 ++-- infer/src/checkers/registerCheckers.ml | 2 +- infer/src/clang/SwitchCase.ml | 2 +- infer/src/clang/ast_expressions.ml | 2 +- infer/src/clang/cArithmetic_trans.ml | 2 +- infer/src/clang/cFrontend_decl.ml | 2 +- infer/src/clang/cGeneral_utils.ml | 4 ++-- infer/src/clang/cMethodSignature.ml | 4 ++-- infer/src/clang/cScope.ml | 6 +++--- infer/src/clang/cTrans.ml | 28 +++++++++++++------------- infer/src/clang/cTrans_utils.ml | 4 ++-- infer/src/clang/cVar_decl.ml | 2 +- infer/src/istd/Pp.ml | 2 +- infer/src/istd/Pp.mli | 6 +++--- 17 files changed, 39 insertions(+), 39 deletions(-) diff --git a/infer/src/IR/DecompiledExp.ml b/infer/src/IR/DecompiledExp.ml index 02bc0f7e2..5aa7a4e49 100644 --- a/infer/src/IR/DecompiledExp.ml +++ b/infer/src/IR/DecompiledExp.ml @@ -45,7 +45,7 @@ let rec pp fmt = function | Darray (de1, de2) -> F.fprintf fmt "%a[%a]" pp de1 pp de2 | Dbinop (op, de1, de2) -> - F.fprintf fmt "(%a%a%a)" pp de1 (Pp.to_string ~f:(Binop.str Pp.text)) op pp de2 + F.fprintf fmt "(%a%a%a)" pp de1 (Pp.of_string ~f:(Binop.str Pp.text)) op pp de2 | Dconst (Cfun pn) -> ( match builtin_functions_to_string pn with | Some str -> diff --git a/infer/src/IR/ProcAttributes.ml b/infer/src/IR/ProcAttributes.ml index c88e22820..ca79cb2ca 100644 --- a/infer/src/IR/ProcAttributes.ml +++ b/infer/src/IR/ProcAttributes.ml @@ -126,7 +126,7 @@ let pp f F.fprintf f "@[{ proc_name= %a@,; translation_unit= %a@," Typ.Procname.pp proc_name SourceFile.pp translation_unit ; if not (PredSymb.equal_access default.access access) then - F.fprintf f "; access= %a@," (Pp.to_string ~f:PredSymb.string_of_access) access ; + F.fprintf f "; access= %a@," (Pp.of_string ~f:PredSymb.string_of_access) access ; if not ([%compare.equal: (Mangled.t * Typ.t) list] default.captured captured) then F.fprintf f "; captured= [@[%a@]]@," pp_parameters captured ; if not ([%compare.equal: string list] default.exceptions exceptions) then @@ -158,7 +158,7 @@ let pp f pp_bool_default ~default:default.is_variadic "is_variadic" is_variadic f () ; if not (ClangMethodKind.equal default.clang_method_kind clang_method_kind) then F.fprintf f "; clang_method_kind= %a@," - (Pp.to_string ~f:ClangMethodKind.to_string) + (Pp.of_string ~f:ClangMethodKind.to_string) clang_method_kind ; if not (Location.equal default.loc loc) then F.fprintf f "; loc= %a@," Location.pp_file_pos loc ; F.fprintf f "; locals= [@[%a@]]@," (Pp.semicolon_seq ~print_env:Pp.text_break pp_var_data) locals ; diff --git a/infer/src/al/cPredicates.ml b/infer/src/al/cPredicates.ml index fa9dffa88..cb5f81d3c 100644 --- a/infer/src/al/cPredicates.ml +++ b/infer/src/al/cPredicates.ml @@ -588,7 +588,7 @@ let has_init_list_const_expr an = in L.(debug Analysis Verbose) "@\n\n[has_init_list_const_expr] EVALUATE EXP '%a' result = '%b'@\n" - (Pp.to_string ~f:Clang_ast_proj.get_stmt_kind_string) + (Pp.of_string ~f:Clang_ast_proj.get_stmt_kind_string) exp res ; res in diff --git a/infer/src/base/PerfEvent.ml b/infer/src/base/PerfEvent.ml index 6bf0919e3..0594aab59 100644 --- a/infer/src/base/PerfEvent.ml +++ b/infer/src/base/PerfEvent.ml @@ -86,7 +86,7 @@ module JsonFragment = struct | _ -> L.die InternalError "Unexpected json fragment \"%s\" in state [%a]" (to_string json_fragment) - (Pp.seq (Pp.to_string ~f:string_of_state)) + (Pp.seq (Pp.of_string ~f:string_of_state)) !pp_state @@ -102,7 +102,7 @@ module JsonFragment = struct | _ -> L.die InternalError "Unexpected assoc field \"%t\" in state [%a]" (fun f -> Json.pp_field pp_value f key value) - (Pp.seq (Pp.to_string ~f:string_of_state)) + (Pp.seq (Pp.of_string ~f:string_of_state)) !pp_state end diff --git a/infer/src/checkers/registerCheckers.ml b/infer/src/checkers/registerCheckers.ml index 7e7583d36..5f4742018 100644 --- a/infer/src/checkers/registerCheckers.ml +++ b/infer/src/checkers/registerCheckers.ml @@ -176,5 +176,5 @@ let pp_checker fmt {name; callbacks} = |> LanguageSet.elements in F.fprintf fmt "%s (%a)" name - (Pp.seq ~sep:", " (Pp.to_string ~f:Language.to_string)) + (Pp.seq ~sep:", " (Pp.of_string ~f:Language.to_string)) langs_of_callbacks diff --git a/infer/src/clang/SwitchCase.ml b/infer/src/clang/SwitchCase.ml index 442d6f14c..cc6b41b85 100644 --- a/infer/src/clang/SwitchCase.ml +++ b/infer/src/clang/SwitchCase.ml @@ -26,7 +26,7 @@ let add switch_case = current_cases := switch_case :: !current_cases let pp_condition fmt = function | Case stmt -> - F.fprintf fmt "case %a:" (Pp.to_string ~f:Clang_ast_j.string_of_stmt) stmt + F.fprintf fmt "case %a:" (Pp.of_string ~f:Clang_ast_j.string_of_stmt) stmt | Default -> F.pp_print_string fmt "default:" diff --git a/infer/src/clang/ast_expressions.ml b/infer/src/clang/ast_expressions.ml index 01ff50488..f55270d06 100644 --- a/infer/src/clang/ast_expressions.ml +++ b/infer/src/clang/ast_expressions.ml @@ -167,7 +167,7 @@ let make_next_object_exp stmt_info item items = | _ -> CFrontend_errors.incorrect_assumption __POS__ stmt_info.Clang_ast_t.si_source_range "unexpected item %a" - (Pp.to_string ~f:Clang_ast_j.string_of_stmt) + (Pp.of_string ~f:Clang_ast_j.string_of_stmt) item ) in let var_decl_ref, var_type = get_decl_ref item in diff --git a/infer/src/clang/cArithmetic_trans.ml b/infer/src/clang/cArithmetic_trans.ml index c22ba6833..2ad9319ed 100644 --- a/infer/src/clang/cArithmetic_trans.ml +++ b/infer/src/clang/cArithmetic_trans.ml @@ -62,7 +62,7 @@ let binary_operation_instruction source_range boi ((e1, t1) as e1_with_typ) typ | `PtrMemD | `PtrMemI -> CFrontend_errors.unimplemented __POS__ source_range "Pointer-to-member constructs are unsupported. Got '%a'." - (Pp.to_string ~f:Clang_ast_j.string_of_binary_operator_info) + (Pp.of_string ~f:Clang_ast_j.string_of_binary_operator_info) boi | `Add -> if Typ.is_pointer t1 then (binop_exp Binop.PlusPI, []) diff --git a/infer/src/clang/cFrontend_decl.ml b/infer/src/clang/cFrontend_decl.ml index 5aa96401c..b11c5a005 100644 --- a/infer/src/clang/cFrontend_decl.ml +++ b/infer/src/clang/cFrontend_decl.ml @@ -424,7 +424,7 @@ module CFrontend_decl_funct (T : CModule_type.CTranslation) : CModule_type.CFron ~recover:(fun () -> ()) ~pp_context:(fun fmt () -> F.fprintf fmt "Error adding types from decl '%a'" - (Pp.to_string ~f:Clang_ast_j.string_of_decl) + (Pp.of_string ~f:Clang_ast_j.string_of_decl) dec ) trans_unit_ctx ; List.iter ~f:translate method_decls diff --git a/infer/src/clang/cGeneral_utils.ml b/infer/src/clang/cGeneral_utils.ml index b6c040c4c..c79dafd4e 100644 --- a/infer/src/clang/cGeneral_utils.ml +++ b/infer/src/clang/cGeneral_utils.ml @@ -89,9 +89,9 @@ let get_var_name_mangled decl_info name_info var_decl_info = | "", None -> CFrontend_errors.incorrect_assumption __POS__ decl_info.Clang_ast_t.di_source_range "Got both empty clang_name and None for param_idx in get_var_name_mangled (%a) (%a)" - (Pp.to_string ~f:Clang_ast_j.string_of_named_decl_info) + (Pp.of_string ~f:Clang_ast_j.string_of_named_decl_info) name_info - (Pp.to_string ~f:Clang_ast_j.string_of_var_decl_info) + (Pp.of_string ~f:Clang_ast_j.string_of_var_decl_info) var_decl_info | _ -> clang_name diff --git a/infer/src/clang/cMethodSignature.ml b/infer/src/clang/cMethodSignature.ml index 01f65aec9..b93b12b3b 100644 --- a/infer/src/clang/cMethodSignature.ml +++ b/infer/src/clang/cMethodSignature.ml @@ -71,7 +71,7 @@ let mk name class_param params ret_type ?(has_added_return_param = false) attrib let pp fmt ms = let pp_param fmt {name; typ} = F.fprintf fmt "%a, %a" Mangled.pp name (Typ.pp Pp.text) typ in Format.fprintf fmt "Method %a [%a]->%a %a" - (Pp.to_string ~f:Typ.Procname.to_string) + (Pp.of_string ~f:Typ.Procname.to_string) ms.name (Pp.comma_seq pp_param) ms.params (Typ.pp Pp.text) (fst ms.ret_type) - (Pp.to_string ~f:Clang_ast_j.string_of_source_range) + (Pp.of_string ~f:Clang_ast_j.string_of_source_range) ms.loc diff --git a/infer/src/clang/cScope.ml b/infer/src/clang/cScope.ml index 0387d3517..3c417d2bc 100644 --- a/infer/src/clang/cScope.ml +++ b/infer/src/clang/cScope.ml @@ -143,7 +143,7 @@ module Variables = struct let rec visit_stmt stmt ((scope, map) as scope_map) = L.debug Capture Verbose "%a{%a}@;" - (Pp.to_string ~f:Clang_ast_proj.get_stmt_kind_string) + (Pp.of_string ~f:Clang_ast_proj.get_stmt_kind_string) stmt (Pp.seq ~sep:"," pp_var_decl) scope.current ; match (stmt : Clang_ast_t.stmt) with | ReturnStmt (stmt_info, _) @@ -253,10 +253,10 @@ module CXXTemporaries = struct and visit_stmt context stmt temporaries = L.debug Capture Verbose "<@[%a|@," - (Pp.to_string ~f:Clang_ast_proj.get_stmt_kind_string) + (Pp.of_string ~f:Clang_ast_proj.get_stmt_kind_string) stmt ; let r = visit_stmt_aux context stmt temporaries in - L.debug Capture Verbose "@]@;/%a>" (Pp.to_string ~f:Clang_ast_proj.get_stmt_kind_string) stmt ; + L.debug Capture Verbose "@]@;/%a>" (Pp.of_string ~f:Clang_ast_proj.get_stmt_kind_string) stmt ; r diff --git a/infer/src/clang/cTrans.ml b/infer/src/clang/cTrans.ml index 7d0bb5829..ce9fba9ff 100644 --- a/infer/src/clang/cTrans.ml +++ b/infer/src/clang/cTrans.ml @@ -167,7 +167,7 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s let stmt_info, _ = Clang_ast_proj.get_stmt_tuple stmt in CFrontend_errors.incorrect_assumption __POS__ stmt_info.Clang_ast_t.si_source_range "Clang_ast_proj.get_expr_tuple stmt returns None, stmt is %a" - (Pp.to_string ~f:Clang_ast_j.string_of_stmt) + (Pp.of_string ~f:Clang_ast_j.string_of_stmt) stmt in let res_trans = f trans_state stmt in @@ -537,7 +537,7 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s (* FIXME(t21762295): we do not expect this to happen but it does *) CFrontend_errors.incorrect_assumption __POS__ stmt_info.Clang_ast_t.si_source_range "di_parent_pointer should be always set for fields/ivars, but got %a" - (Pp.option (Pp.to_string ~f:Clang_ast_j.string_of_decl)) + (Pp.option (Pp.of_string ~f:Clang_ast_j.string_of_decl)) decl in let field_name = CGeneral_utils.mk_class_field_name class_tname field_string in @@ -812,7 +812,7 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s | _ -> CFrontend_errors.unimplemented __POS__ stmt_info.Clang_ast_t.si_source_range "Decl ref expression %a with pointer %d still needs to be translated" - (Pp.to_string ~f:Clang_ast_j.string_of_decl_kind) + (Pp.of_string ~f:Clang_ast_j.string_of_decl_kind) decl_kind decl_ref.Clang_ast_t.dr_decl_pointer @@ -912,7 +912,7 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s and binaryOperator_trans trans_state binary_operator_info stmt_info expr_info stmt_list = L.(debug Capture Verbose) " BinaryOperator '%a' " - (Pp.to_string ~f:Clang_ast_j.string_of_binary_operator_kind) + (Pp.of_string ~f:Clang_ast_j.string_of_binary_operator_kind) binary_operator_info.Clang_ast_t.boi_kind ; L.(debug Capture Verbose) " priority node free = '%s'@\n@." @@ -2196,7 +2196,7 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s (List.length field_exps) (Pp.seq ~sep:"," (Pp.pair ~fst:Exp.pp ~snd:(Typ.pp Pp.text))) field_exps (List.length stmts) - (Pp.seq ~sep:"," (Pp.to_string ~f:Clang_ast_proj.get_stmt_kind_string)) + (Pp.seq ~sep:"," (Pp.of_string ~f:Clang_ast_proj.get_stmt_kind_string)) stmts ; let control, _ = instructions trans_state stmts in [mk_trans_result (var_exp, var_typ) control] ) @@ -3226,9 +3226,9 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s | _ -> CFrontend_errors.unimplemented __POS__ stmt_info.Clang_ast_t.si_source_range "attributedStmt with:@\nstmts=[%a]@\nattrs=[%a]@\n" - (Pp.semicolon_seq (Pp.to_string ~f:Clang_ast_j.string_of_stmt)) + (Pp.semicolon_seq (Pp.of_string ~f:Clang_ast_j.string_of_stmt)) stmts - (Pp.semicolon_seq (Pp.to_string ~f:Clang_ast_j.string_of_attribute)) + (Pp.semicolon_seq (Pp.of_string ~f:Clang_ast_j.string_of_attribute)) attrs @@ -3244,7 +3244,7 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s | None (* t21762295 *) -> CFrontend_errors.incorrect_assumption __POS__ stmt_info.Clang_ast_t.si_source_range "Break stmt without continuation: %a" - (Pp.to_string ~f:Clang_ast_j.string_of_stmt_info) + (Pp.of_string ~f:Clang_ast_j.string_of_stmt_info) stmt_info @@ -3261,7 +3261,7 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s | None (* t21762295 *) -> CFrontend_errors.incorrect_assumption __POS__ stmt_info.Clang_ast_t.si_source_range "Continue stmt without continuation: %a" - (Pp.to_string ~f:Clang_ast_j.string_of_stmt_info) + (Pp.of_string ~f:Clang_ast_j.string_of_stmt_info) stmt_info @@ -3314,7 +3314,7 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s in L.(debug Capture Verbose) "Translating statement '%a' (pointer= '%a')@\n@[" - (Pp.to_string ~f:Clang_ast_proj.get_stmt_kind_string) + (Pp.of_string ~f:Clang_ast_proj.get_stmt_kind_string) instr pp_pointer instr ; let trans_result = try instruction_scope trans_state instr @@ -3353,7 +3353,7 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s in (if should_display_error then L.internal_error else L.debug Capture Quiet) "%a: ERROR translating statement '%a'@\n" Location.pp_range (loc_start, loc_end) - (Pp.to_string ~f:Clang_ast_proj.get_stmt_kind_string) + (Pp.of_string ~f:Clang_ast_proj.get_stmt_kind_string) instr ) ) in L.(debug Capture Verbose) "@]" ; @@ -3631,16 +3631,16 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s CFrontend_errors.unimplemented __POS__ si_source_range ~ast_node:(Clang_ast_proj.get_stmt_kind_string instr) "Translation of templated code is unsupported: %a" - (Pp.to_string ~f:Clang_ast_j.string_of_stmt) + (Pp.of_string ~f:Clang_ast_j.string_of_stmt) instr | ForStmt ({Clang_ast_t.si_source_range}, _) | WhileStmt ({Clang_ast_t.si_source_range}, _) | DoStmt ({Clang_ast_t.si_source_range}, _) | ObjCForCollectionStmt ({Clang_ast_t.si_source_range}, _) -> CFrontend_errors.incorrect_assumption __POS__ si_source_range "Unexpected shape for %a: %a" - (Pp.to_string ~f:Clang_ast_proj.get_stmt_kind_string) + (Pp.of_string ~f:Clang_ast_proj.get_stmt_kind_string) instr - (Pp.to_string ~f:Clang_ast_j.string_of_stmt) + (Pp.of_string ~f:Clang_ast_j.string_of_stmt) instr | MSAsmStmt _ | CapturedStmt _ diff --git a/infer/src/clang/cTrans_utils.ml b/infer/src/clang/cTrans_utils.ml index bf24be9ea..7f3613ff6 100644 --- a/infer/src/clang/cTrans_utils.ml +++ b/infer/src/clang/cTrans_utils.ml @@ -19,7 +19,7 @@ let extract_item_from_singleton l pp source_range warning_string = item | _ -> L.die InternalError "At %a: List has %d elements, 1 expected:@\n[@[%a@]]@\n%s" - (Pp.to_string ~f:Clang_ast_j.string_of_source_range) + (Pp.of_string ~f:Clang_ast_j.string_of_source_range) source_range (List.length l) (Pp.semicolon_seq pp) l warning_string @@ -561,7 +561,7 @@ let is_null_stmt s = match s with Clang_ast_t.NullStmt _ -> true | _ -> false let extract_stmt_from_singleton stmt_list source_range warning_string = extract_item_from_singleton stmt_list - (Pp.to_string ~f:Clang_ast_j.string_of_stmt) + (Pp.of_string ~f:Clang_ast_j.string_of_stmt) source_range warning_string diff --git a/infer/src/clang/cVar_decl.ml b/infer/src/clang/cVar_decl.ml index 12c62edda..0675aed11 100644 --- a/infer/src/clang/cVar_decl.ml +++ b/infer/src/clang/cVar_decl.ml @@ -50,7 +50,7 @@ let sil_var_of_decl_ref context source_range decl_ref procname = (* FIXME(t21762295) *) CFrontend_errors.incorrect_assumption __POS__ source_range "pointer '%d' for var decl not found. The var decl was: %a" pointer - (Pp.to_string ~f:Clang_ast_j.string_of_decl_ref) + (Pp.of_string ~f:Clang_ast_j.string_of_decl_ref) decl_ref ) diff --git a/infer/src/istd/Pp.ml b/infer/src/istd/Pp.ml index 7a2c2f2d4..f2e2cfb53 100644 --- a/infer/src/istd/Pp.ml +++ b/infer/src/istd/Pp.ml @@ -132,7 +132,7 @@ let option pp fmt = function F.fprintf fmt "[Some %a]" pp x -let to_string ~f fmt x = F.pp_print_string fmt (f x) +let of_string ~f fmt x = F.pp_print_string fmt (f x) let string_of_pp pp = Format.asprintf "%a" pp diff --git a/infer/src/istd/Pp.mli b/infer/src/istd/Pp.mli index 630393dce..bdb47594e 100644 --- a/infer/src/istd/Pp.mli +++ b/infer/src/istd/Pp.mli @@ -85,11 +85,11 @@ val comma_seq : ?print_env:env -> (F.formatter -> 'a -> unit) -> F.formatter -> val semicolon_seq : ?print_env:env -> (F.formatter -> 'a -> unit) -> F.formatter -> 'a list -> unit (** Pretty print a ;-separated sequence *) -val to_string : f:('a -> string) -> F.formatter -> 'a -> unit -(** turn a "to_string" function into a "pp_foo" *) +val of_string : f:('a -> string) -> F.formatter -> 'a -> unit +(** If all you have is to_string, but you need pp_foo. *) val string_of_pp : (F.formatter -> 'a -> unit) -> 'a -> string -(** turn "pp_foo" to "to_string" function *) +(** If all you have is pp_foo, but you need to_string. *) val current_time : F.formatter -> unit -> unit (** Print the current time and date in a format similar to the "date" command *)