From 70853c809963d9308b2e109a1fde3658a289db79 Mon Sep 17 00:00:00 2001 From: Hans Bolinder Date: Fri, 8 Jan 2021 14:39:12 +0100 Subject: [PATCH] Handle annotations and locations better This PR is one of several affecting repositories on Github. It aims at fixing bad use of annotations (see erl_anno(3)). The following remarks are common to all PR:s. Typically the second element of abstract code tuples is assumed to be an integer, which is no longer always true. For instance, the parse transform implementing QLC tables (see qlc(3)) returns code where some annotations are marked as `generated'. Such an annotation is a list, not an integer (it used to be a negative integer). As of Erlang/OTP 24.0, the location can be a tuple {Line, Column}, which is another reason to handle annotations properly. --- src/aversion.erl | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/aversion.erl b/src/aversion.erl index baa620b..dd973a7 100644 --- a/src/aversion.erl +++ b/src/aversion.erl @@ -26,13 +26,11 @@ -type ast_record() :: term(). -type ast_record_attribute() :: term(). --record(clause, { line_num :: integer(), - args :: list(), +-record(clause, { args :: list(), guards :: list(), body :: list() }). --record(function, { line_num :: integer(), - fn_name :: atom(), +-record(function, { fn_name :: atom(), arity :: integer(), clauses :: [#clause{}] }). @@ -222,8 +220,8 @@ ast_var_name({var,_,Var_name}) -> Var_name. %% Creates a var, which is a variable name in erlang AST -new_ast_var(Ln_num, Var_name) when is_integer(Ln_num), is_atom(Var_name) -> - {var, Ln_num, Var_name}. +new_ast_var(Anno, Var_name) when is_atom(Var_name) -> + {var, erl_anno:location(Anno), Var_name}. %%% ============================================================================ @@ -320,8 +318,8 @@ walk_record_unpack_ast(Rec, Record_var_name, Acc_1) when is_atom(Record_var_name %% ..the record and field name we have keyed against it. Fields = ast_record_fields(Rec), Record_name = ast_record_name(Rec), - Ln_num = element(2,Rec), - Record_var = new_ast_var(Ln_num, Record_var_name), + Anno = element(2,Rec), + Record_var = new_ast_var(Anno, Record_var_name), Acc_2 = lists:foldl( fun(Field, Acc_x) ->