Browse Source

dialyzer.mk: Use the shell to parse command line args

Splitting arguments on `-` was dangerous: if a path contained such a
character, it would be split and the second half thrown away by the
filtering.

Now, `$(ERLC_OPTS)` is passed to erl(1) after `-extra`. Thus
arguments parsing is done by the shell and we only have to call
init:get_plain_arguments/0 to get them as a list.
Jean-Sébastien Pédron 8 years ago
parent
commit
6c546accfc
2 changed files with 16 additions and 13 deletions
  1. 12 9
      plugins/dialyzer.mk
  2. 4 4
      test/plugin_dialyzer.mk

+ 12 - 9
plugins/dialyzer.mk

@@ -27,14 +27,17 @@ help::
 # Plugin-specific targets.
 
 define filter_opts.erl
-	Opts = binary:split(<<"$1">>, <<"-">>, [global]),
-	Filtered = lists:reverse(lists:foldl(fun
-		(O = <<"pa ", _/bits>>, Acc) -> [O|Acc];
-		(O = <<"D ", _/bits>>, Acc) -> [O|Acc];
-		(O = <<"I ", _/bits>>, Acc) -> [O|Acc];
-		(_, Acc) -> Acc
-	end, [], Opts)),
-	io:format("~s~n", [[["-", O] || O <- Filtered]]),
+	Opts = init:get_plain_arguments(),
+	{Filtered, _} = lists:foldl(fun
+		(O,                         {Os, true}) -> {[O|Os], false};
+		(O = "-D",                  {Os, _})    -> {[O|Os], true};
+		(O = [\\$$-, \\$$D, _ | _], {Os, _})    -> {[O|Os], false};
+		(O = "-I",                  {Os, _})    -> {[O|Os], true};
+		(O = [\\$$-, \\$$I, _ | _], {Os, _})    -> {[O|Os], false};
+		(O = "-pa",                 {Os, _})    -> {[O|Os], true};
+		(_,                         Acc)        -> Acc
+	end, {[], false}, Opts),
+	io:format("~s~n", [string:join(lists:reverse(Filtered), " ")]),
 	halt().
 endef
 
@@ -51,4 +54,4 @@ dialyze:
 else
 dialyze: $(DIALYZER_PLT)
 endif
-	$(verbose) dialyzer --no_native `$(call erlang,$(call filter_opts.erl,$(ERLC_OPTS)))` $(DIALYZER_DIRS) $(DIALYZER_OPTS)
+	$(verbose) dialyzer --no_native `$(ERL) -eval "$(subst $(newline),,$(subst ",\",$(call filter_opts.erl)))" -extra $(ERLC_OPTS)` $(DIALYZER_DIRS) $(DIALYZER_OPTS)

+ 4 - 4
test/plugin_dialyzer.mk

@@ -166,18 +166,18 @@ dialyzer-erlc-opts: build clean
 	$t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
 
 	$i "Create a header file in a non-standard directory"
-	$t mkdir $(APP)/exotic/
-	$t touch $(APP)/exotic/dialyze.hrl
+	$t mkdir $(APP)/exotic-include-path/
+	$t touch $(APP)/exotic-include-path/dialyze.hrl
 
 	$i "Create a module that includes this header"
 	$t printf "%s\n" \
 		"-module(no_warn)." \
 		"-export([doit/0])." \
 		"-include(\"dialyze.hrl\")." \
-		"doit() -> ok." > $(APP)/src/no_warn.erl
+		"doit() -> ?OK." > $(APP)/src/no_warn.erl
 
 	$i "Point ERLC_OPTS to the non-standard include directory"
-	$t perl -ni.bak -e 'print;if ($$.==1) {print "ERLC_OPTS += -I exotic\n"}' $(APP)/Makefile
+	$t perl -ni.bak -e 'print;if ($$.==1) {print "ERLC_OPTS += -I exotic-include-path -DOK=ok\n"}' $(APP)/Makefile
 
 	$i "Run Dialyzer"
 	$t $(DIALYZER_MUTEX) $(MAKE) -C $(APP) dialyze $v