Fix error when processing non-existent include_lib file

Since process_attr/3 searches source code for attributes, it can
happen that it finds an attribute which is eventually not needed
by the compilation, e.g. hidden by ifdef macro - see enclosed test.
Such attribute can reference a file which is not in the path or
which even doesn't exist at all, and current code doesn't expect
such a situation.  We fix things by simply ignoring such files.
This commit is contained in:
David Kubecka 2015-04-05 01:34:25 +02:00
parent ee5a75cb33
commit 07a4a14af6
2 changed files with 9 additions and 5 deletions

View file

@ -5,6 +5,9 @@
-export([parse_transform/2]). -export([parse_transform/2]).
-include("lambda.hrl"). -include("lambda.hrl").
-ifdef(NOT_DEFINED).
-include_lib("include/non/existent.hrl").
-endif.
parse_transform(Forms, _Options) -> parse_transform(Forms, _Options) ->
Forms. Forms.

View file

@ -594,8 +594,7 @@ process_attr(include, Form, Includes) ->
process_attr(include_lib, Form, Includes) -> process_attr(include_lib, Form, Includes) ->
[FileNode] = erl_syntax:attribute_arguments(Form), [FileNode] = erl_syntax:attribute_arguments(Form),
RawFile = erl_syntax:string_value(FileNode), RawFile = erl_syntax:string_value(FileNode),
File = maybe_expand_include_lib_path(RawFile), maybe_expand_include_lib_path(RawFile) ++ Includes;
[File|Includes];
process_attr(behaviour, Form, Includes) -> process_attr(behaviour, Form, Includes) ->
[FileNode] = erl_syntax:attribute_arguments(Form), [FileNode] = erl_syntax:attribute_arguments(Form),
File = module_to_erl(erl_syntax:atom_value(FileNode)), File = module_to_erl(erl_syntax:atom_value(FileNode)),
@ -631,7 +630,7 @@ module_to_erl(Mod) ->
maybe_expand_include_lib_path(File) -> maybe_expand_include_lib_path(File) ->
case filelib:is_regular(File) of case filelib:is_regular(File) of
true -> true ->
File; [File];
false -> false ->
expand_include_lib_path(File) expand_include_lib_path(File)
end. end.
@ -646,8 +645,10 @@ expand_include_lib_path(File) ->
Split = filename:split(filename:dirname(File)), Split = filename:split(filename:dirname(File)),
Lib = hd(Split), Lib = hd(Split),
SubDir = filename:join(tl(Split)), SubDir = filename:join(tl(Split)),
Dir = code:lib_dir(list_to_atom(Lib), list_to_atom(SubDir)), case code:lib_dir(list_to_atom(Lib), list_to_atom(SubDir)) of
filename:join(Dir, File1). {error, bad_name} -> [];
Dir -> [filename:join(Dir, File1)]
end.
%% %%
%% Ensure all files in a list are present and abort if one is missing %% Ensure all files in a list are present and abort if one is missing