Skip to content

Instantly share code, notes, and snippets.

@stolen
Created June 5, 2018 11:34
Show Gist options
  • Save stolen/6a55221ffb906bde712cd939a729718d to your computer and use it in GitHub Desktop.
Save stolen/6a55221ffb906bde712cd939a729718d to your computer and use it in GitHub Desktop.
build code both in OTP20 and OTP21 without deprecation warnings
-module(otp_compat_transform).
-export([parse_transform/2]).
-export([add_otp21_nowarn/1]). % Export to avoid unused function warning on OTP20
%% OTP_RELEASE is defined in OTP21, not before
-ifdef(OTP_RELEASE).
parse_transform(AST, _) ->
add_otp21_nowarn(AST).
-else.
parse_transform(AST, _) ->
AST.
-endif.
add_otp21_nowarn(Exprs) ->
{Before, [ModAttr|After]} = lists:splitwith(fun not_module_attr/1, Exprs),
SuddenlyDeprecated = [{erlang, get_stacktrace, 0}, {ssl, ssl_accept, 2}],
NoWarnAttr = {attribute, 0, compile, {nowarn_deprecated_function, SuddenlyDeprecated}},
Before ++ [ModAttr, NoWarnAttr] ++ After.
not_module_attr({attribute, _, module, _}) ->
true;
not_module_attr(_) ->
false.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment