25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-22 17:28:25 +01:00

Simulate vars usage in ejabberd_sql_pt to avoid unused var warnings

This commit is contained in:
Alexey Shchepin 2017-11-03 17:42:53 +03:00
parent ec3ea61585
commit 44293235e5

View File

@ -40,7 +40,8 @@
res = [], res = [],
res_vars = [], res_vars = [],
res_pos = 0, res_pos = 0,
server_host_used = false}). server_host_used = false,
used_vars = []}).
-define(QUERY_RECORD, "sql_query"). -define(QUERY_RECORD, "sql_query").
@ -90,13 +91,23 @@ transform(Form) ->
S = erl_syntax:string_value(Arg), S = erl_syntax:string_value(Arg),
Pos = erl_syntax:get_pos(Arg), Pos = erl_syntax:get_pos(Arg),
ParseRes = parse(S, Pos), ParseRes = parse(S, Pos),
if UnusedVars =
ParseRes#state.server_host_used -> case ParseRes#state.server_host_used of
ok; {true, SHVar} ->
true -> case ?USE_NEW_SCHEMA of
add_warning(Pos, no_server_host) true -> [];
false -> [SHVar]
end;
false ->
add_warning(
Pos, no_server_host),
[]
end, end,
set_pos(make_sql_query(ParseRes), Pos); set_pos(
add_unused_vars(
make_sql_query(ParseRes),
UnusedVars),
Pos);
_ -> _ ->
throw({error, erl_syntax:get_pos(Form), throw({error, erl_syntax:get_pos(Form),
"?SQL argument must be " "?SQL argument must be "
@ -124,10 +135,13 @@ transform(Form) ->
false -> false ->
add_warning(Pos, no_server_host) add_warning(Pos, no_server_host)
end, end,
ParseRes2 = {ParseRes2, UnusedVars} =
filter_upsert_sh(Table, ParseRes), filter_upsert_sh(Table, ParseRes),
set_pos( set_pos(
add_unused_vars(
make_sql_upsert(Table, ParseRes2, Pos), make_sql_upsert(Table, ParseRes2, Pos),
UnusedVars
),
Pos); Pos);
_ -> _ ->
throw({error, erl_syntax:get_pos(Form), throw({error, erl_syntax:get_pos(Form),
@ -156,10 +170,13 @@ transform(Form) ->
false -> false ->
add_warning(Pos, no_server_host) add_warning(Pos, no_server_host)
end, end,
ParseRes2 = {ParseRes2, UnusedVars} =
filter_upsert_sh(Table, ParseRes), filter_upsert_sh(Table, ParseRes),
set_pos( set_pos(
add_unused_vars(
make_sql_insert(Table, ParseRes2), make_sql_insert(Table, ParseRes2),
UnusedVars
),
Pos); Pos);
_ -> _ ->
throw({error, erl_syntax:get_pos(Form), throw({error, erl_syntax:get_pos(Form),
@ -254,7 +271,9 @@ parse1([$%, $( | S], Acc, State) ->
State4 = State4 =
case Type of case Type of
host -> host ->
State3 = State2#state{server_host_used = true}, State3 =
State2#state{server_host_used = {true, Name},
used_vars = [Name | State2#state.used_vars]},
case ?USE_NEW_SCHEMA of case ?USE_NEW_SCHEMA of
true -> true ->
Convert = Convert =
@ -284,7 +303,8 @@ parse1([$%, $( | S], Acc, State) ->
State2#state{'query' = [{var, Var} | State2#state.'query'], State2#state{'query' = [{var, Var} | State2#state.'query'],
args = [Convert | State2#state.args], args = [Convert | State2#state.args],
params = [Var | State2#state.params], params = [Var | State2#state.params],
param_pos = State2#state.param_pos + 1} param_pos = State2#state.param_pos + 1,
used_vars = [Name | State2#state.used_vars]}
end, end,
parse1(S1, [], State4); parse1(S1, [], State4);
parse1([C | S], Acc, State) -> parse1([C | S], Acc, State) ->
@ -695,12 +715,23 @@ filter_upsert_sh(Table, ParseRes) ->
true -> true ->
ParseRes; ParseRes;
false -> false ->
lists:filter( lists:foldr(
fun({Field, _Match, _ST}) -> fun({Field, _Match, ST} = P, {Acc, Vars}) ->
Field /= "server_host" orelse Table == "route" if
end, ParseRes) Field /= "server_host" orelse Table == "route" ->
{[P | Acc], Vars};
true ->
{Acc, ST#state.used_vars ++ Vars}
end
end, {[], []}, ParseRes)
end. end.
add_unused_vars(Tree, []) ->
Tree;
add_unused_vars(Tree, Vars) ->
erl_syntax:block_expr(
lists:map(fun erl_syntax:variable/1, Vars) ++ [Tree]).
-ifdef(ENABLE_PT_WARNINGS). -ifdef(ENABLE_PT_WARNINGS).
add_warning(Pos, Warning) -> add_warning(Pos, Warning) ->