./ct_report/coverage/dynamic_compile.COVER.html

1 %% Copyright (c) 2007
2 %% Mats Cronqvist <mats.cronqvist@ericsson.com>
3 %% Chris Newcombe <chris.newcombe@gmail.com>
4 %% Jacob Vorreuter <jacob.vorreuter@gmail.com>
5 %%
6 %% Permission is hereby granted, free of charge, to any person
7 %% obtaining a copy of this software and associated documentation
8 %% files (the "Software"), to deal in the Software without
9 %% restriction, including without limitation the rights to use,
10 %% copy, modify, merge, publish, distribute, sublicense, and/or sell
11 %% copies of the Software, and to permit persons to whom the
12 %% Software is furnished to do so, subject to the following
13 %% conditions:
14 %%
15 %% The above copyright notice and this permission notice shall be
16 %% included in all copies or substantial portions of the Software.
17 %%
18 %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 %% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 %% OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 %% NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 %% HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 %% WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 %% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 %% OTHER DEALINGS IN THE SOFTWARE.
26
27 %%%-------------------------------------------------------------------
28 %%% File : dynamic_compile.erl
29 %%% Description :
30 %%% Authors : Mats Cronqvist <mats.cronqvist@ericsson.com>
31 %%% Chris Newcombe <chris.newcombe@gmail.com>
32 %%% Jacob Vorreuter <jacob.vorreuter@gmail.com>
33 %%% TODO :
34 %%% - add support for limit include-file depth (and prevent circular references)
35 %%% prevent circular macro expansion set FILE correctly when -module() is found
36 %%% -include_lib support $ENVVAR in include filenames
37 %%% substitute-stringize (??MACRO)
38 %%% -undef/-ifdef/-ifndef/-else/-endif
39 %%% -file(File, Line)
40 %%%-------------------------------------------------------------------
41
42 %% This module is still used in tests
43 -module(dynamic_compile).
44
45 %% API
46 -export([from_string/1, from_string/2]).
47
48 -ignore_xref([from_string/1]).
49 -ignore_xref([from_string/2]).
50
51 -import(lists, [reverse/1, keyreplace/4]).
52
53 -type macro_dict() :: dict:dict(term(), term()).
54
55 %%====================================================================
56 %% API
57 %%====================================================================
58
59 %% @doc Returns a binary that can be used with
60 %% code:load_binary(Module, ModuleFilenameForInternalRecords, Binary).
61 -spec from_string('eof' | string()) -> {_, binary()}.
62 from_string(CodeStr) ->
63 3 from_string(CodeStr, []).
64
65 %% @doc takes Options as for compile:forms/2
66 -spec from_string('eof' | string(), [any()]) -> {_, binary()}.
67 from_string(CodeStr, CompileFormsOptions) ->
68 %% Initialise the macro dictionary with the default predefined macros,
69 %% (adapted from epp.erl:predef_macros/1
70 3 Filename = "compiled_from_string",
71 %%Machine = list_to_atom(erlang:system_info(machine)),
72 3 Ms0 = dict:new(),
73 % Ms1 = dict:store('FILE', {[], "compiled_from_string"}, Ms0),
74 % Ms2 = dict:store('LINE', {[], 1}, Ms1), % actually we might add special code for this
75 % Ms3 = dict:store('MODULE', {[], undefined}, Ms2),
76 % Ms4 = dict:store('MODULE_STRING', {[], undefined}, Ms3),
77 % Ms5 = dict:store('MACHINE', {[], Machine}, Ms4),
78 % InitMD = dict:store(Machine, {[], true}, Ms5),
79 3 InitMD = Ms0,
80
81 %% From the docs for compile:forms:
82 %% When encountering an -include or -include_dir directive, the compiler searches for header files in the following directories:
83 %% 1. ".", the current working directory of the file server;
84 %% 2. the base name of the compiled file;
85 %% 3. the directories specified using the i option. The directory specified last is searched first.
86 %% In this case, #2 is meaningless.
87 3 IncludeSearchPath = ["." | reverse([Dir || {i, Dir} <- CompileFormsOptions])],
88 3 {RevForms, _OutMacroDict} = scan_and_parse(CodeStr, Filename, 1, [], InitMD, IncludeSearchPath),
89 3 Forms = reverse(RevForms),
90
91 %% note: 'binary' is forced as an implicit option, whether it is provided or not.
92 3 case compile:forms(Forms, CompileFormsOptions) of
93 {ok, ModuleName, CompiledCodeBinary} when is_binary(CompiledCodeBinary) ->
94 3 {ModuleName, CompiledCodeBinary};
95 {ok, ModuleName, CompiledCodeBinary, []} when is_binary(CompiledCodeBinary) -> % empty warnings list
96
:-(
{ModuleName, CompiledCodeBinary};
97 {ok, _ModuleName, _CompiledCodeBinary, Warnings} ->
98
:-(
throw({?MODULE, warnings, Warnings});
99 Other ->
100
:-(
throw({?MODULE, compile_forms, Other})
101 end.
102
103 %%====================================================================
104 %% Internal functions
105 %%====================================================================
106
107 %% @doc Code from Mats Cronqvist<br/>
108 %% See http://www.erlang.org/pipermail/erlang-questions/2007-March/025507.html
109 %% 'scan_and_parse'
110 %% basically we call the OTP scanner and parser (erl_scan and
111 %% erl_parse) line-by-line, but check each scanned line for (or
112 %% definitions of) macros before parsing. returns {ReverseForms, FinalMacroDict}
113 %% @private
114 -spec scan_and_parse('eof' | string(),
115 _CurrFilename :: file:name(),
116 _CurrLine :: integer() | {integer(), pos_integer()},
117 RevForms :: [any()],
118 MacroDict :: macro_dict(),
119 _IncludeSearchPath :: [file:name()]) -> {[any()], macro_dict()}.
120 scan_and_parse([], _CurrFilename, _CurrLine, RevForms, MacroDict, _IncludeSearchPath) ->
121 3 {RevForms, MacroDict};
122 scan_and_parse(RemainingText, CurrFilename, CurrLine, RevForms, MacroDict, IncludeSearchPath) ->
123 27 case scanner(RemainingText, CurrLine, MacroDict) of
124 {tokens, NLine, NRemainingText, Toks} ->
125 27 {ok, Form} = erl_parse:parse_form(Toks),
126 27 scan_and_parse(NRemainingText, CurrFilename, NLine, [Form | RevForms], MacroDict, IncludeSearchPath);
127 {macro, NLine, NRemainingText, NMacroDict} ->
128
:-(
scan_and_parse(NRemainingText, CurrFilename, NLine, RevForms, NMacroDict, IncludeSearchPath);
129 {include, NLine, NRemainingText, IncludeFilename} ->
130
:-(
IncludeFileRemainingTextents = read_include_file(IncludeFilename, IncludeSearchPath),
131 %%io:format("include file ~p contents: ~n~p~nRemainingText = ~p~n", [IncludeFilename, IncludeFileRemainingTextents, RemainingText]),
132 %% Modify the FILE macro to reflect the filename
133 %%IncludeMacroDict = dict:store('FILE', {[], IncludeFilename}, MacroDict),
134
:-(
IncludeMacroDict = MacroDict,
135
136 %% Process the header file (inc. any nested header files)
137
:-(
{RevIncludeForms, IncludedMacroDict} = scan_and_parse(IncludeFileRemainingTextents, IncludeFilename, 1, [], IncludeMacroDict, IncludeSearchPath),
138 %io:format("include file results = ~p~n", [R]),
139 %% Restore the FILE macro in the NEW MacroDict (so we keep any macros defined in the header file)
140 %%NMacroDict = dict:store('FILE', {[], CurrFilename}, IncludedMacroDict),
141
:-(
NMacroDict = IncludedMacroDict,
142
143 %% Continue with the original file
144
:-(
scan_and_parse(NRemainingText, CurrFilename, NLine, RevIncludeForms ++ RevForms, NMacroDict, IncludeSearchPath);
145 done ->
146
:-(
scan_and_parse([], CurrFilename, CurrLine, RevForms, MacroDict, IncludeSearchPath)
147 end.
148
149 %% @private
150 -spec scanner(Text :: 'eof' | string(),
151 Line :: integer() | {integer(), pos_integer()},
152 MacroDict :: macro_dict()) ->
153 'done'
154 | {'include', integer() | {integer(), pos_integer()}, 'eof' | string(), _}
155 | {'macro', integer() | {integer(), pos_integer()}, 'eof' | string(), macro_dict()}
156 | {'tokens', integer() | {integer(), pos_integer()}, 'eof' | string(), [any()]}.
157 scanner(Text, Line, MacroDict) ->
158 27 case erl_scan:tokens([], Text, Line) of
159 {done, {ok, Toks, NLine}, LeftOverChars} ->
160 27 case pre_proc(Toks, MacroDict) of
161 27 {tokens, NToks} -> {tokens, NLine, LeftOverChars, NToks};
162
:-(
{macro, NMacroDict} -> {macro, NLine, LeftOverChars, NMacroDict};
163
:-(
{include, Filename} -> {include, NLine, LeftOverChars, Filename}
164 end;
165 {more, _Continuation} ->
166 %% This is supposed to mean "term is not yet complete" (i.e. a '.' has
167 %% not been reached yet).
168 %% However, for some bizarre reason we also get this if there is a comment after the final '.' in a file.
169 %% So we check to see if Text only consists of comments.
170
:-(
case is_only_comments(Text) of
171 true ->
172
:-(
done;
173 false ->
174
:-(
throw({incomplete_term, Text, Line})
175 end
176 end.
177
178 %% @private
179 -spec is_only_comments('eof' | string()) -> boolean().
180
:-(
is_only_comments(Text) -> is_only_comments(Text, not_in_comment).
181
182 %% @private
183 -spec is_only_comments('eof' | string(), 'in_comment' | 'not_in_comment') -> boolean().
184
:-(
is_only_comments([], _) -> true;
185
:-(
is_only_comments([$ |T], not_in_comment) -> is_only_comments(T, not_in_comment); % skipping whitspace outside of comment
186
:-(
is_only_comments([$\t |T], not_in_comment) -> is_only_comments(T, not_in_comment); % skipping whitspace outside of comment
187
:-(
is_only_comments([$\n |T], not_in_comment) -> is_only_comments(T, not_in_comment); % skipping whitspace outside of comment
188
:-(
is_only_comments([$% |T], not_in_comment) -> is_only_comments(T, in_comment); % found start of a comment
189
:-(
is_only_comments(_, not_in_comment) -> false;
190 % found any significant char NOT in a comment
191
:-(
is_only_comments([$\n |T], in_comment) -> is_only_comments(T, not_in_comment); % found end of a comment
192
:-(
is_only_comments([_ |T], in_comment) -> is_only_comments(T, in_comment). % skipping over in-comment chars
193
194 %% @doc 'pre-proc'
195 %% have to implement a subset of the pre-processor, since epp insists
196 %% on running on a file. Only handles 2 cases;
197 %% -define(MACRO, something).
198 %% -define(MACRO(VAR1, VARN), {stuff, VAR1, more, stuff, VARN, extra, stuff}).
199 %% @private
200 -spec pre_proc([{_, _} | {_, _, _}], macro_dict()) -> {'include', _} | {'macro', macro_dict()} | {'tokens', [any()]}.
201 pre_proc([{'-', _}, {atom, _, define}, {'(', _}, {_, _, Name}|DefToks], MacroDict) ->
202
:-(
false = dict:is_key(Name, MacroDict),
203
:-(
case DefToks of
204 [{', ', _} | Macro] ->
205
:-(
{macro, dict:store(Name, {[], macro_body_def(Macro, [])}, MacroDict)};
206 [{'(', _} | Macro] ->
207
:-(
{macro, dict:store(Name, macro_params_body_def(Macro, []), MacroDict)}
208 end;
209 pre_proc([{'-', _}, {atom, _, include}, {'(', _}, {string, _, Filename}, {')', _}, {dot, _}], _MacroDict) ->
210
:-(
{include, Filename};
211 pre_proc(Toks, MacroDict) ->
212 27 {tokens, subst_macros(Toks, MacroDict)}.
213
214 %% @private
215 -spec macro_params_body_def(Tokens :: [{_, _} | {_, _, _}, ...],
216 RevParams :: [any()]
217 ) -> {[any()], [{_, _} | {_, _, _}]}.
218 macro_params_body_def([{')', _}, {', ', _} | Toks], RevParams) ->
219
:-(
{reverse(RevParams), macro_body_def(Toks, [])};
220 macro_params_body_def([{var, _, Param} | Toks], RevParams) ->
221
:-(
macro_params_body_def(Toks, [Param | RevParams]);
222 macro_params_body_def([{', ', _}, {var, _, Param} | Toks], RevParams) ->
223
:-(
macro_params_body_def(Toks, [Param | RevParams]).
224
225 %% @private
226 -spec macro_body_def(Tokens :: [{_, _} | {_, _, _}, ...],
227 RevMacroBodyTokens :: [{_, _} | {_, _, _}]
228 ) -> [{_, _} | {_, _, _}].
229 macro_body_def([{')', _}, {dot, _}], RevMacroBodyToks) ->
230
:-(
reverse(RevMacroBodyToks);
231 macro_body_def([Tok|Toks], RevMacroBodyToks) ->
232
:-(
macro_body_def(Toks, [Tok | RevMacroBodyToks]).
233
234 %% @private
235 -spec subst_macros(Toks :: [{_, _} | {_, _, _}], MacroDict :: macro_dict()) -> [any()].
236 subst_macros(Toks, MacroDict) ->
237 27 reverse(subst_macros_rev(Toks, MacroDict, [])).
238
239 %% @doc Returns a reversed list of tokens
240 %% @private
241 -spec subst_macros_rev(Tokens :: maybe_improper_list(),
242 MacroDict :: macro_dict(),
243 RevOutToks :: [any()]) -> [any()].
244 subst_macros_rev([{'?', _}, {_, LineNum, 'LINE'} | Toks], MacroDict, RevOutToks) ->
245 %% special-case for ?LINE, to avoid creating a new MacroDict for every line in the source file
246
:-(
subst_macros_rev(Toks, MacroDict, [{integer, LineNum, LineNum}] ++ RevOutToks);
247 subst_macros_rev([{'?', _}, {_, _, Name}, {'(', _} = Paren | Toks], MacroDict, RevOutToks) ->
248
:-(
case dict:fetch(Name, MacroDict) of
249 {[], MacroValue} ->
250 %% This macro does not have any vars, so ignore the fact that the invocation is followed by "(...stuff"
251 %% Recursively expand any macro calls inside this macro's value
252 %% TODO: avoid infinite expansion due to circular references (even indirect ones)
253
:-(
RevExpandedOtherMacrosToks = subst_macros_rev(MacroValue, MacroDict, []),
254
:-(
subst_macros_rev([Paren|Toks], MacroDict, RevExpandedOtherMacrosToks ++ RevOutToks);
255 ParamsAndBody ->
256 %% This macro does have vars.
257 %% Collect all of the passe arguments, in an ordered list
258
:-(
{NToks, Arguments} = subst_macros_get_args(Toks, []),
259 %% Expand the varibles
260
:-(
ExpandedParamsToks = subst_macros_subst_args_for_vars(ParamsAndBody, Arguments),
261 %% Recursively expand any macro calls inside this macro's value
262 %% TODO: avoid infinite expansion due to circular references (even indirect ones)
263
:-(
RevExpandedOtherMacrosToks = subst_macros_rev(ExpandedParamsToks, MacroDict, []),
264
:-(
subst_macros_rev(NToks, MacroDict, RevExpandedOtherMacrosToks ++ RevOutToks)
265 end;
266 subst_macros_rev([{'?', _}, {_, _, Name} | Toks], MacroDict, RevOutToks) ->
267 %% This macro invocation does not have arguments.
268 %% Therefore the definition should not have parameters
269
:-(
{[], MacroValue} = dict:fetch(Name, MacroDict),
270
271 %% Recursively expand any macro calls inside this macro's value
272 %% TODO: avoid infinite expansion due to circular references (even indirect ones)
273
:-(
RevExpandedOtherMacrosToks = subst_macros_rev(MacroValue, MacroDict, []),
274
:-(
subst_macros_rev(Toks, MacroDict, RevExpandedOtherMacrosToks ++ RevOutToks);
275 subst_macros_rev([Tok|Toks], MacroDict, RevOutToks) ->
276 1124 subst_macros_rev(Toks, MacroDict, [Tok|RevOutToks]);
277 27 subst_macros_rev([], _MacroDict, RevOutToks) -> RevOutToks.
278
279 %% @private
280 -spec subst_macros_get_args(Toks :: nonempty_maybe_improper_list(),
281 RevArgs :: [any()]) -> {_, [any()]}.
282 subst_macros_get_args([{')', _} | Toks], RevArgs) ->
283
:-(
{Toks, reverse(RevArgs)};
284 subst_macros_get_args([{', ', _}, {var, _, ArgName} | Toks], RevArgs) ->
285
:-(
subst_macros_get_args(Toks, [ArgName| RevArgs]);
286 subst_macros_get_args([{var, _, ArgName} | Toks], RevArgs) ->
287
:-(
subst_macros_get_args(Toks, [ArgName | RevArgs]).
288
289 %% @private
290 -spec subst_macros_subst_args_for_vars({[any()], _}, [any()]) -> any().
291 subst_macros_subst_args_for_vars({[], BodyToks}, []) ->
292
:-(
BodyToks;
293 subst_macros_subst_args_for_vars({[Param | Params], BodyToks}, [Arg|Args]) ->
294
:-(
NBodyToks = keyreplace(Param, 3, BodyToks, {var, 1, Arg}),
295
:-(
subst_macros_subst_args_for_vars({Params, NBodyToks}, Args).
296
297 %% @private
298 -spec read_include_file(Filename :: file:name(),
299 IncludeSearchPath :: [file:name(), ...]
300 ) -> [byte()].
301 read_include_file(Filename, IncludeSearchPath) ->
302
:-(
case file:path_open(IncludeSearchPath, Filename, [read, raw, binary]) of
303 {ok, IoDevice, FullName} ->
304
:-(
{ok, Data} = file:read(IoDevice, filelib:file_size(FullName)),
305
:-(
file:close(IoDevice),
306
:-(
binary_to_list(Data);
307 {error, Reason} ->
308
:-(
throw({failed_to_read_include_file, Reason, Filename, IncludeSearchPath})
309 end.
Line Hits Source