From 352b8edff9dfee42a4e6a5962be54edc411fa1ec Mon Sep 17 00:00:00 2001 From: Jules Villard Date: Mon, 10 Aug 2015 08:25:51 -0100 Subject: [PATCH] [copyrightor] turn checkCopyright into a standalone script Summary: Usage: `./scripts/checkCopyright [-i] file1 ...`. --- .gitignore | 1 + infer/Makefile | 7 +- infer/src/Makefile | 24 +++- infer/src/backend/sil.ml | 2 - .../{backend => scripts}/checkCopyright.ml | 114 ++++++++---------- 5 files changed, 79 insertions(+), 69 deletions(-) rename infer/src/{backend => scripts}/checkCopyright.ml (68%) diff --git a/.gitignore b/.gitignore index 4a70114eb..2613dc427 100644 --- a/.gitignore +++ b/.gitignore @@ -65,6 +65,7 @@ buck-out/ /infer/lib/specs/cpp_models /infer/lib/specs/objc_models /infer/lib/specs/clean_models +/scripts/checkCopyright #atdgen stubs /infer/src/backend/jsonbug_* diff --git a/infer/Makefile b/infer/Makefile index 94f23bec8..0470bc0c9 100644 --- a/infer/Makefile +++ b/infer/Makefile @@ -6,9 +6,9 @@ MODELS = models SRCDIR = src -.PHONY: clean clang java llvm models +.PHONY: clean clang java llvm checkCopyright models -all: clang java llvm +all: clang java llvm checkCopyright java: make -C $(SRCDIR) java @@ -22,6 +22,9 @@ clang: llvm: make -C $(SRCDIR) llvm +checkCopyright: + make -C $(SRCDIR) checkCopyright + clean: make -C $(SRCDIR) clean make -C $(MODELS) clean diff --git a/infer/src/Makefile b/infer/src/Makefile index 5f81aeab4..44820512a 100644 --- a/infer/src/Makefile +++ b/infer/src/Makefile @@ -37,6 +37,7 @@ GLOBAL_LIBS = unix str BUILDDIR = ../_build-infer ANNOTDIR = $(ROOT)/infer/src/_build BINDIR = $(ROOT)/infer/bin +SCRIPTDIR = $(ROOT)/scripts ifneq ($(wildcard $(BUILDDIR)/sanitize.sh),) SANITIZE_SCRIPT = $(BUILDDIR)/sanitize.sh @@ -111,6 +112,13 @@ LLVM_SOURCES = llvm INFERLLVM_MAIN = $(LLVM_SOURCES)/lMain INFERLLVM_BINARY = $(BINDIR)/InferLLVM +#### scripts declarations #### + +SCRIPT_SOURCES = scripts + +CHECKCOPYRIGHT_BINARY = $(SCRIPTDIR)/checkCopyright +CHECKCOPYRIGHT_MAIN = $(SCRIPT_SOURCES)/checkCopyright + #### End of declarations #### # Check whether .facebook file exists in a root directory. @@ -125,9 +133,9 @@ DEPENDENCIES = $(BACKEND_SOURCES) checkers facebook/checkers facebook/checkers/g OCAMLBUILD = ocamlbuild -build-dir $(BUILDDIR) -j 0 $(addprefix -I , $(DEPENDENCIES)) $(GLOBAL_OPTIONS) $(ATDGEN_OPTIONS) $(JAVA_OPTIONS) -.PHONY: all java clang llvm build_java build_clang build_llvm annotations init sanitize version clean +.PHONY: all java clang llvm checkCopyright build_java build_clang build_llvm build_checkCopyright annotations init sanitize version clean -all: java clang llvm +all: java clang llvm checkCopyright java: build_java annotations $(INFERANALYZE_BINARY) $(INFERPRINT_BINARY) $(INFERJAVA_BINARY) @@ -135,6 +143,8 @@ clang: build_clang annotations $(INFERANALYZE_BINARY) $(INFERPRINT_BINARY) $(INF llvm: build_llvm annotations $(INFERLLVM_BINARY) +checkCopyright: build_checkCopyright annotations $(CHECKCOPYRIGHT_BINARY) + build_java: init $(INFERPRINT_ATDGEN_STUBS) $(OCAMLBUILD) $(TYPEPROP_MAIN).native $(INFERANALYZE_MAIN).native $(INFERPRINT_MAIN).native $(INFERJAVA_MAIN).native @@ -144,11 +154,14 @@ build_clang: init $(INFERPRINT_ATDGEN_STUBS) check_clang_plugin $(CLANG_ATDGEN_S build_llvm: $(OCAMLBUILD) -use-menhir $(INFERLLVM_MAIN).native +build_checkCopyright: + $(OCAMLBUILD) -I $(SCRIPT_SOURCES) $(CHECKCOPYRIGHT_MAIN).native + annotations: rsync -r --delete --exclude=*.ml* --exclude=*.o --exclude=*.cm* --exclude=*.native $(BUILDDIR)/* $(ANNOTDIR) check_clang_plugin: - $(ROOT)/scripts/check_clang_plugin_version.sh $(CLANG_PLUGIN_ROOT) + $(SCRIPTDIR)/check_clang_plugin_version.sh $(CLANG_PLUGIN_ROOT) $(INFERPRINT_ATDGEN_STUBS): $(INFERPRINT_ATDGEN_STUB_ATD) atdgen -t $(INFERPRINT_ATDGEN_STUB_ATD) -o $(INFERPRINT_ATDGEN_STUB_BASE) @@ -214,7 +227,10 @@ $(INFERLLVM_BINARY): $(BUILDDIR)/$(INFERLLVM_MAIN).native $(TYPEPROP_BINARY): $(BUILDDIR)/$(TYPEPROP_MAIN).native $(COPY) $(BUILDDIR)/$(TYPEPROP_MAIN).native $(TYPEPROP_BINARY) +$(CHECKCOPYRIGHT_BINARY): $(BUILDDIR)/$(CHECKCOPYRIGHT_MAIN).native + $(COPY) $(BUILDDIR)/$(CHECKCOPYRIGHT_MAIN).native $(CHECKCOPYRIGHT_BINARY) + clean: $(BUILDDIR) $(OCAMLBUILD) -clean $(REMOVE_DIR) $(ANNOTDIR) - $(REMOVE) $(TYPEPROP_BINARY) $(INFERANALYZE_BINARY) $(INFERPRINT_BINARY) $(INFERJAVA_BINARY) $(INFERCLANG_BINARY) $(INFERLLVM_BINARY) + $(REMOVE) $(TYPEPROP_BINARY) $(INFERANALYZE_BINARY) $(INFERPRINT_BINARY) $(INFERJAVA_BINARY) $(INFERCLANG_BINARY) $(INFERLLVM_BINARY) $(CHECKCOPYRIGHT_BINARY) diff --git a/infer/src/backend/sil.ml b/infer/src/backend/sil.ml index 505ad1e55..87f0c5c2d 100644 --- a/infer/src/backend/sil.ml +++ b/infer/src/backend/sil.ml @@ -14,8 +14,6 @@ module L = Logging module F = Format open Utils -let () = if CheckCopyright.active then CheckCopyright.check () - (** {2 Programs and Types} *) (** Type to represent one @Annotation. *) diff --git a/infer/src/backend/checkCopyright.ml b/infer/src/scripts/checkCopyright.ml similarity index 68% rename from infer/src/backend/checkCopyright.ml rename to infer/src/scripts/checkCopyright.ml index c5b2d9bd3..fa373e706 100644 --- a/infer/src/backend/checkCopyright.ml +++ b/infer/src/scripts/checkCopyright.ml @@ -31,11 +31,8 @@ let lang_of_com_style style = else if style = comment_style_shell then "shell" else "??unknown??" -(** If active, check copyright messages and exit. *) -let active = Config.from_env_variable "INFER_CHECK_COPYRIGHT" - (** If true, update the copyright message of the files. *) -let update_files = true +let update_files = ref false let line_contains_copyright line = string_contains "opyright " line @@ -175,61 +172,56 @@ let file_should_have_copyright fname lines = list_exists (Filename.check_suffix fname) extensions -let get_files_from_git () = - let tmpfile = Filename.temp_file "git_ls" "" in - let _ = Sys.command ("git ls-files >" ^ tmpfile) in - let git_files = match read_file tmpfile with - | Some git_files -> git_files - | None -> [] in - Sys.remove tmpfile; - git_files - - -let check_copyright () = - let check_file fname = - match read_file fname with - | None -> () - | Some lines -> - begin - match find_copyright_line lines 0 with - | None -> - if file_should_have_copyright fname lines - then L.stderr "Copyright not found in %s@." fname - | Some n -> - let lines_arr = Array.of_list lines in - let line = lines_arr.(n) in - let len = String.length line in - let (cstart, com_style) = find_comment_start_and_style lines_arr n in - let cend = find_comment_end lines_arr n com_style in - if looks_like_copyright_message cstart cend lines_arr then - begin - let mono = contains_monoidics cstart cend lines_arr in - match get_fb_year cstart cend lines_arr with - | None -> - L.stderr "Can't find fb year: %s@." fname - | Some fb_year -> - let prefix = if com_style = comment_style_ocaml then " " else "" in - if copyright_has_changed mono fb_year com_style prefix cstart cend lines_arr then - begin - let range = cend - cstart in - let lang = lang_of_com_style com_style in - L.stdout "%s (start:%d n:%d end:%d len:%d range:%d lang:%s mono:%b year:%d)@." - fname cstart n cend len range lang mono fb_year; - for i = cstart to cend do - L.stdout " %s@." lines_arr.(i) - done; - L.stdout "-----@."; - L.stdout " @[%a@]" (pp_copyright mono fb_year com_style) prefix; - L.flush_streams (); - if update_files then - update_file fname mono fb_year com_style prefix cstart cend lines_arr - end - end - else - L.stderr "Copyright not recognized: %s@." fname - end in - list_iter check_file (get_files_from_git ()); - exit 0 +let check_copyright fname = match read_file fname with + | None -> () + | Some lines -> + match find_copyright_line lines 0 with + | None -> + if file_should_have_copyright fname lines + then L.stderr "Copyright not found in %s@." fname + | Some n -> + let lines_arr = Array.of_list lines in + let line = lines_arr.(n) in + let len = String.length line in + let (cstart, com_style) = find_comment_start_and_style lines_arr n in + let cend = find_comment_end lines_arr n com_style in + if looks_like_copyright_message cstart cend lines_arr then + begin + let mono = contains_monoidics cstart cend lines_arr in + match get_fb_year cstart cend lines_arr with + | None -> + L.stderr "Can't find fb year: %s@." fname + | Some fb_year -> + let prefix = if com_style = comment_style_ocaml then " " else "" in + if copyright_has_changed mono fb_year com_style prefix cstart cend lines_arr then + begin + let range = cend - cstart in + let lang = lang_of_com_style com_style in + L.stdout "%s (start:%d n:%d end:%d len:%d range:%d lang:%s mono:%b year:%d)@." + fname cstart n cend len range lang mono fb_year; + for i = cstart to cend do + L.stdout " %s@." lines_arr.(i) + done; + L.stdout "-----@."; + L.stdout " @[%a@]" (pp_copyright mono fb_year com_style) prefix; + L.flush_streams (); + if !update_files then + update_file fname mono fb_year com_style prefix cstart cend lines_arr + end + end + else + L.stderr "Copyright not recognized: %s@." fname + +let speclist = [ + ("-i", Arg.Set update_files, "Update copyright notice in-place"); +] -let check () = - if active then check_copyright () +let usage_msg = "checkCopyright [-i] file1 ..." + +let () = + let to_check = ref [] in + let add_file_to_check fname = + to_check := fname :: !to_check in + Arg.parse speclist add_file_to_check usage_msg; + list_iter check_copyright (list_rev !to_check); + exit 0