From 3c2968566748b0b70f6027e28187727afc8f69c9 Mon Sep 17 00:00:00 2001 From: Sungkeun Cho Date: Fri, 21 Feb 2020 09:45:18 -0800 Subject: [PATCH] [infer] Find dead module (.ml file) Summary: This diff finds dead modules, i.e, .ml files that is not used in the binaries. Reviewed By: ngorogiannis Differential Revision: D20035984 fbshipit-source-id: 56ac2e817 --- .gitignore | 2 ++ infer/src/deadcode/Makefile | 45 +++++++++++++++++++++++++++++++++---- infer/src/llvm/Makefile | 26 --------------------- infer/src/llvm/llvm_sil.ml | 38 ------------------------------- 4 files changed, 43 insertions(+), 68 deletions(-) delete mode 100644 infer/src/llvm/Makefile delete mode 100644 infer/src/llvm/llvm_sil.ml diff --git a/.gitignore b/.gitignore index fec14c4b2..77292e29f 100644 --- a/.gitignore +++ b/.gitignore @@ -183,6 +183,8 @@ infer/src/.project # generated when looking for dead code /infer/src/deadcode/.depend +/infer/src/deadcode/all_ml_files +/infer/src/deadcode/all_ml_files_copied /infer/src/deadcode/dune /infer/src/deadcode/*.ml /infer/src/deadcode/*.mli diff --git a/infer/src/deadcode/Makefile b/infer/src/deadcode/Makefile index 0f014cfc3..501702982 100644 --- a/infer/src/deadcode/Makefile +++ b/infer/src/deadcode/Makefile @@ -31,6 +31,8 @@ include $(ROOT_DIR)/Makefile.config INFER_BUILD_DIR = ../_build/test ALL_INFER_IN_ONE_FILE_ML = all_infer_in_one_file.ml +ALL_ML_FILES = all_ml_files +ALL_ML_FILES_COPIED = all_ml_files_copied default: detect_dead_code @@ -96,10 +98,12 @@ depend: $(QUIET)if [ $@ = "infer.cmi" ] \ || [ $@ = "unit/inferunit.cmi" ] \ || [ $@ = "facebook/InferCreateTraceViewLinks.cmi" ] \ + || [ $@ = "scripts/checkCopyright.cmi" ] \ ; then \ echo '[@warning "-60"] ' >> $(ALL_INFER_IN_ONE_FILE_ML);\ fi $(QUIET)echo >> $(ALL_INFER_IN_ONE_FILE_ML) + $(QUIET)echo "$*.ml" >> $(ALL_ML_FILES_COPIED) $(ml_src_files_without_mli:.ml=.cmx): # again mostly duplicated from above @@ -113,6 +117,7 @@ $(ml_src_files_without_mli:.ml=.cmx): $(QUIET)echo " end " >> $(ALL_INFER_IN_ONE_FILE_ML) $(QUIET)echo "(* END OF MODULE $@ *)" >> $(ALL_INFER_IN_ONE_FILE_ML) $(QUIET)echo >> $(ALL_INFER_IN_ONE_FILE_ML) + $(QUIET)echo $@ | $(GNU_SED) -e 's/\.cmx$$/.ml/' >> $(ALL_ML_FILES_COPIED) %.cmx: $(QUIET)echo skipping '$@' @@ -124,7 +129,7 @@ $(ml_src_files_without_mli:.ml=.cmx): # root .cmx to include all the code. Any code not used in the construction of these "root .cmx" will # be considered dead. .PHONY: flatten_infer -flatten_infer: infer.cmx unit/inferunit.cmx facebook/InferCreateTraceViewLinks.cmx +flatten_infer: infer.cmx unit/inferunit.cmx facebook/InferCreateTraceViewLinks.cmx scripts/checkCopyright.cmx $(QUIET)echo "see results in $(ALL_INFER_IN_ONE_FILE_ML)" .PHONY: detect_dead_code @@ -153,8 +158,11 @@ detect_dead_code: # have changed. For both of these reasons, run another `make`. # Create a temp file so that the build doesn't break if this step gets interrupted. tmp_file=$$(mktemp -t all_infer_in_one_file_XXXXX.ml); \ - $(MAKE) -j 1 ALL_INFER_IN_ONE_FILE_ML="$$tmp_file" flatten_infer; \ - mv "$$tmp_file" $(ALL_INFER_IN_ONE_FILE_ML) + tmp_file_copied=$$(mktemp -t all_ml_files_copied); \ + $(MAKE) -j 1 ALL_INFER_IN_ONE_FILE_ML="$$tmp_file" ALL_ML_FILES_COPIED="$$tmp_file_copied" flatten_infer; \ + mv "$$tmp_file" $(ALL_INFER_IN_ONE_FILE_ML); \ + sort -u "$$tmp_file_copied" > $(ALL_ML_FILES_COPIED) + $(MAKE) -j 1 detect_dead_ml_file # build and get dead code warnings; clean in case of errors so as not to leave rubbish around if ! dune build $(INFER_BUILD_DIR)/deadcode/all_infer_in_one_file.bc; then \ $(MAKE) clean; \ @@ -164,6 +172,35 @@ detect_dead_code: # accident and I don't know enough dune to be positive that it won't happen $(MAKE) clean +.PHONY: detect_dead_ml_file +detect_dead_ml_file: + function is_in_blacklist { \ + if [ "$$1" = "infertop.ml" ] \ + || [ "$$1" = "deadcode/all_infer_in_one_file.ml" ] \ + || [[ "$$1" =~ ^"labs/" ]] \ + || [[ "$$1" =~ ^"opensource/" ]]; then \ + exit 0; \ + else \ + exit 1; \ + fi; \ + }; \ + tmp_file=$$(mktemp -t all_ml_files); \ + for i in $(ml_src_files) $(ml_src_files_without_mli); do \ + (is_in_blacklist "$$i") || (echo "$$i" >> "$$tmp_file"); \ + done; \ + sort -u "$$tmp_file" > $(ALL_ML_FILES); \ + tmp_file=$$(mktemp -t all_ml_files_copied); \ + for i in $$(cat $(ALL_ML_FILES_COPIED)); do \ + (is_in_blacklist "$$i") || (echo "$$i" >> "$$tmp_file"); \ + done; \ + sort -u "$$tmp_file" > $(ALL_ML_FILES_COPIED) + + if ! diff $(ALL_ML_FILES) $(ALL_ML_FILES_COPIED) 1>&2; then \ + $(MAKE) clean; \ + exit 1; \ + fi + .PHONY: clean clean: - $(REMOVE) .depend *.ml *.mli dune + $(REMOVE) .depend *.ml *.mli dune $(ALL_ML_FILES) $(ALL_ML_FILES_COPIED) + diff --git a/infer/src/llvm/Makefile b/infer/src/llvm/Makefile deleted file mode 100644 index 7949350f8..000000000 --- a/infer/src/llvm/Makefile +++ /dev/null @@ -1,26 +0,0 @@ -# Copyright (c) Facebook, Inc. and its affiliates. -# -# This source code is licensed under the MIT license found in the -# LICENSE file in the root directory of this source tree. - -ROOT_DIR = ../../.. -include $(ROOT_DIR)/Makefile.config - -INFER_BUILD_DIR = ../_build/default - -default: llvm_sil - -$(BIN_DIR)/llvm_sil: - rm -f $(INFER_BUILD_DIR)/llvm_sil.ml - make -C .. infer - cp llvm_sil.ml $(INFER_BUILD_DIR) - cd $(INFER_BUILD_DIR) && \ - ocamlfind ocamlopt -package llair -linkpkg llvm_sil.ml -o $(BIN_DIR)/llvm_sil - rm -f $(INFER_BUILD_DIR)/llvm_sil.ml - -.PHONY: llvm_sil -llvm_sil: $(BIN_DIR)/llvm_sil - -.PHONY: clean -clean: - rm -f $(INFER_BUILD_DIR)/llvm_sil.ml $(BIN_DIR)/llvm_sil diff --git a/infer/src/llvm/llvm_sil.ml b/infer/src/llvm/llvm_sil.ml deleted file mode 100644 index f74d336d3..000000000 --- a/infer/src/llvm/llvm_sil.ml +++ /dev/null @@ -1,38 +0,0 @@ -(* - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - *) - -open Import -open Core_kernel -open Llair_ - -let main ~input ~output = - try - let program = Frontend.translate input in - Option.iter output ~f:(function - | "-" -> - Format.printf "%a@." Llair.fmt program - | filename -> - Out_channel.with_file filename ~f:(fun oc -> - let ff = Format.formatter_of_out_channel oc in - Format.fprintf ff "%a@." Llair.fmt program ) ) ; - Format.printf "@\nRESULT: Success@." - with exn -> - let bt = Caml.Printexc.get_raw_backtrace () in - ( match exn with - | Frontend.Invalid_llvm msg -> - Format.printf "@\nRESULT: Invalid input: %s@." msg - | Unimplemented msg -> - Format.printf "@\nRESULT: Unimplemented: %s@." msg - | Failure msg -> - Format.printf "@\nRESULT: Internal error: %s@." msg - | _ -> - Format.printf "@\nRESULT: Unknown error: %s@." (Caml.Printexc.to_string exn) ) ; - Caml.Printexc.raise_with_backtrace exn bt - - -;; -main ~input:Caml.Sys.argv.(1) ~output:(Some "-")