Permalink
Browse files

* lang_cmt/: new directory, moved code from lang_ml/

  • Loading branch information...
aryx committed Mar 4, 2014
1 parent f331678 commit 80ed8fe199381eeef60d94b1a22612ccf338d574
View
@@ -145,6 +145,8 @@ LIBS= commons/lib.cma \
lang_ml/parsing/lib.cma \
lang_ml/analyze/visual/lib.cma \
lang_ml/analyze/lib.cma \
+ lang_cmt/parsing/lib.cma \
+ lang_cmt/analyze/lib.cma \
lang_nw/parsing/lib.cma \
lang_nw/analyze/lib.cma \
lang_lisp/parsing/lib.cma \
@@ -203,6 +205,8 @@ MAKESUBDIRS=commons \
matcher \
lang_ml/parsing \
lang_ml/analyze \
+ lang_cmt/parsing \
+ lang_cmt/analyze \
lang_nw/parsing \
lang_nw/analyze \
lang_lisp/parsing \
View
@@ -0,0 +1,48 @@
+TOP=../..
+##############################################################################
+# Variables
+##############################################################################
+TARGET=lib
+
+-include $(TOP)/Makefile.config
+
+SRC= graph_code_cmt_helpers.ml graph_code_cmt.ml \
+ unit_analyze_cmt.ml
+
+OCAMLCOMPILERDIR=$(shell ocamlc -where)/compiler-libs
+
+INCLUDEDIRS= $(TOP)/commons $(TOP)/commons/ocamlextra \
+ $(OCAMLCOMPILERDIR) \
+ $(TOP)/globals \
+ $(TOP)/h_program-lang $(TOP)/graph_code \
+ $(TOP)/lang_cmt/parsing \
+ $(TOP)/lang_ml/parsing \
+
+
+PP=-pp "cpp -DOCAML_VERSION=$(OCAMLVERSION)"
+
+##############################################################################
+# Generic variables
+##############################################################################
+-include $(TOP)/Makefile.common
+
+##############################################################################
+# Top rules
+##############################################################################
+all:: $(TARGET).cma
+all.opt:: $(TARGET).cmxa
+
+$(TARGET).cma: $(OBJS)
+ $(OCAMLC) -a -o $(TARGET).cma $(OBJS)
+
+$(TARGET).cmxa: $(OPTOBJS) $(LIBS:.cma=.cmxa)
+ $(OCAMLOPT) -a -o $(TARGET).cmxa $(OPTOBJS)
+
+$(TARGET).top: $(OBJS) $(LIBS)
+ $(OCAMLMKTOP) -o $(TARGET).top $(SYSLIBS) $(LIBS) $(OBJS)
+
+clean::
+ rm -f $(TARGET).top
+
+
+
@@ -0,0 +1,121 @@
+open Common
+open OUnit
+
+module E = Database_code
+module G = Graph_code
+
+(*****************************************************************************)
+(* Prelude *)
+(*****************************************************************************)
+
+(*****************************************************************************)
+(* Helpers *)
+(*****************************************************************************)
+let verbose = false
+
+let with_graph ~files f =
+ Common2.with_tmp_dir (fun tmp_dir ->
+ let root = tmp_dir in
+ (* generating .cmt files *)
+ files +> List.iter (fun (filename, content) ->
+ Common.write_file ~file:(Filename.concat tmp_dir filename) content
+ );
+ (* otherwise will get many lookup failure when build the graph_code *)
+ let extra_args = "-nostdlib -nopervasives" in
+ Common.command2 (spf "cd %s; ocamlc -c %s -bin-annot %s"
+ tmp_dir
+ extra_args
+ (* dependency order pbs? assume the given list of files
+ * is ordered for ocamlc to work, which means generic
+ * files first and main files at the end.
+ *)
+ (files +> List.map fst +> Common.join " "));
+ let cmt_files = Lib_parsing_ml.find_cmt_files_of_dir_or_files [tmp_dir] in
+ let ml_files = [] in
+ let g = Graph_code_cmt.build ~verbose:verbose ~root ~cmt_files ~ml_files in
+ f tmp_dir g
+ )
+
+
+let prolog_query ~files query =
+ with_graph ~files (fun tmp_dir g ->
+ let facts = Graph_code_prolog.build g in
+ let facts_pl_file = Filename.concat tmp_dir "facts.pl" in
+ Common.with_open_outfile facts_pl_file (fun (pr_no_nl, _chan) ->
+ let pr s = pr_no_nl (s ^ "\n") in
+ facts +> List.iter (fun x -> pr (Graph_code_prolog.string_of_fact x))
+ );
+ let predicates_file =
+ Filename.concat Config_pfff.path "h_program-lang/database_code.pl" in
+ if verbose
+ then Common.cat facts_pl_file +> List.iter pr2;
+ let cmd =
+ spf "swipl -s %s -f %s -t halt --quiet -g \"%s ,fail\""
+ facts_pl_file predicates_file query
+ in
+ let xs = Common.cmd_to_list cmd in
+ xs
+ )
+
+let unittest =
+"analyze_cmt" >::: [
+
+(*****************************************************************************)
+(* Prolog queries *)
+(*****************************************************************************)
+ "prolog_ml" >::: ([
+
+ "kind" >:: (fun () ->
+ let files = [
+"pervasives.ml", "
+type 'a ref = { mutable contents : 'a }
+external ref : 'a -> 'a ref = \"%makemutable\"
+";
+"foo.ml", "
+let f x = x
+let g = Pervasives.ref 0
+";] in
+ assert_equal
+ ["function"] (prolog_query ~files "kind(('Foo','f'), X), writeln(X)");
+ assert_equal
+ ["global"] (prolog_query ~files "kind(('Foo','g'), X), writeln(X)");
+ );
+
+ "at" >:: (fun () ->
+ let files = [
+"foo.ml", " (* line 1 *)
+let f x = x (* line 2 *)
+let c = 1 (* line 3 *)
+";] in
+ assert_equal
+ ["3"] (prolog_query ~files "at(('Foo','c'), _, X), writeln(X)");
+ );
+
+ ]);
+
+(*****************************************************************************)
+(* Codegraph *)
+(*****************************************************************************)
+ "codegraph_ml" >::: [
+ "basic def/uses" >:: (fun () ->
+ let file_content = "
+let foo () = ()
+let bar () = foo ()
+"
+ in
+ with_graph ~files:["foo.ml", file_content] (fun _tmp_dir g ->
+
+ let src = ("Foo.foo", E.Function) in
+ let pred = G.pred src G.Use g in
+ assert_equal
+ ~msg:"it should link the use of a function to its def"
+ ["Foo.bar", E.Function]
+ pred;
+ )
+ );
+ ];
+
+(*****************************************************************************)
+(* Postlude *)
+(*****************************************************************************)
+]
@@ -0,0 +1,6 @@
+
+(* Returns the testsuite for analyze_ml/. To be concatenated by
+ * the caller (e.g. in pfff/main_test.ml ) with other testsuites and
+ * run via OUnit.run_test_tt
+ *)
+val unittest: OUnit.test
View
@@ -0,0 +1,47 @@
+TOP=../..
+##############################################################################
+# Variables
+##############################################################################
+TARGET=lib
+
+-include $(TOP)/Makefile.config
+
+SRC= ast_cmt.ml meta_ast_cmt.ml
+
+SYSLIBS= str.cma unix.cma
+
+# test_analyze_ml.ml unit_analyze_ml.ml
+
+OCAMLCOMPILERDIR=$(shell ocamlc -where)/compiler-libs
+
+INCLUDEDIRS= $(TOP)/commons \
+ $(OCAMLCOMPILERDIR) \
+
+##############################################################################
+# Generic variables
+##############################################################################
+-include $(TOP)/Makefile.common
+
+##############################################################################
+# Top rules
+##############################################################################
+all:: $(TARGET).cma
+all.opt:: $(TARGET).cmxa
+
+$(TARGET).cma: $(OBJS)
+ $(OCAMLC) -a -o $(TARGET).cma $(OBJS)
+
+$(TARGET).cmxa: $(OPTOBJS) $(LIBS:.cma=.cmxa)
+ $(OCAMLOPT) -a -o $(TARGET).cmxa $(OPTOBJS)
+
+$(TARGET).top: $(OBJS) $(LIBS)
+ $(OCAMLMKTOP) -o $(TARGET).top $(SYSLIBS) $(LIBS) $(OBJS)
+
+clean::
+ rm -f $(TARGET).top
+
+PP=-pp "cpp -DOCAML_VERSION=$(OCAMLVERSION)"
+
+##############################################################################
+# Literate Programming rules
+##############################################################################
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,23 @@
+open Common
+
+(*
+let dump_cmt_headers file =
+ let info = Cmt_format.read_cmt file in
+ pr2_gen info.Cmt_format.cmt_modname;
+ pr2_gen info.Cmt_format.cmt_imports;
+ ()
+*)
+
+let dump_cmt file =
+ let info = Cmt_format.read_cmt file in
+ match info.Cmt_format.cmt_annots with
+ | Cmt_format.Implementation x ->
+ let v = Meta_ast_cmt.vof_structure x in
+ let str = Ocaml.string_of_v v in
+ pr str
+ | _ -> raise Todo
+
+let actions () = [
+ "-dump_cmt", " <file>",
+ Common.mk_action_1_arg dump_cmt;
+]
@@ -0,0 +1,2 @@
+
+val actions: unit -> Common.cmdline_actions
File renamed without changes.
View
@@ -8,8 +8,6 @@ SRC= module_ml.ml \
graph_code_ml.ml \
tags_ml.ml \
database_light_ml.ml \
- ast_cmt.ml meta_ast_cmt.ml \
- graph_code_cmt_helpers.ml graph_code_cmt.ml \
coverage_ml.ml \
test_analyze_ml.ml unit_analyze_ml.ml
@@ -23,10 +21,7 @@ LIBS=$(TOP)/commons/lib.cma \
MAKESUBDIRS=visual
-OCAMLCOMPILERDIR=$(shell ocamlc -where)/compiler-libs
-
INCLUDEDIRS= $(TOP)/commons \
- $(OCAMLCOMPILERDIR) \
$(TOP)/commons/ocollection $(TOP)/commons/ocamlextra \
$(TOP)/globals \
$(TOP)/external/ \
@@ -90,21 +85,6 @@ $(TARGET).top: $(OBJS) $(LIBS)
clean::
rm -f $(TARGET).top
-
-PP=-pp "cpp -DOCAML_VERSION=$(OCAMLVERSION)"
-
-meta_ast_cmt.cmo: meta_ast_cmt.ml
- $(OCAMLC) $(PP) -c $<
-
-meta_ast_cmt.cmx: meta_ast_cmt.ml
- $(OCAMLOPT) $(PP) -c $<
-
-graph_code_cmt.cmo: graph_code_cmt.ml
- $(OCAMLC) $(PP) -c $<
-
-graph_code_cmt.cmx: graph_code_cmt.ml
- $(OCAMLOPT) $(PP) -c $<
-
##############################################################################
# Literate Programming rules
##############################################################################
@@ -1,23 +1,3 @@
-open Common
-
-(*
-let dump_cmt_headers file =
- let info = Cmt_format.read_cmt file in
- pr2_gen info.Cmt_format.cmt_modname;
- pr2_gen info.Cmt_format.cmt_imports;
- ()
-*)
-
-let dump_cmt file =
- let info = Cmt_format.read_cmt file in
- match info.Cmt_format.cmt_annots with
- | Cmt_format.Implementation x ->
- let v = Meta_ast_cmt.vof_structure x in
- let str = Ocaml.string_of_v v in
- pr str
- | _ -> raise Todo
let actions () = [
- "-dump_cmt", " <file>",
- Common.mk_action_1_arg dump_cmt;
]
Oops, something went wrong.

0 comments on commit 80ed8fe

Please sign in to comment.