From 26b3d406b4e9b20072ef4dc881954b984ec3390f Mon Sep 17 00:00:00 2001 From: Jon Siwek Date: Thu, 9 Jul 2020 20:27:59 -0700 Subject: [PATCH] Emit deprecation warning for use of &deprecated function parameters Particularly, this is meant for using &deprecated on canonical event/hook prototype parameters to encourage users to create handlers to another, non-deprecated prototype. i.e. for canonical prototypes, we may not always want to put &deprecated directly on the prototype itself since that signals deprecation of the ID entirely. --- src/Var.cc | 10 ++++++- .../out | 5 ++++ .../alternate-prototypes-deprecated-args.zeek | 28 +++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 testing/btest/Baseline/language.alternate-prototypes-deprecated-args/out create mode 100644 testing/btest/language/alternate-prototypes-deprecated-args.zeek diff --git a/src/Var.cc b/src/Var.cc index baa4c0dae7..0b19d389e2 100644 --- a/src/Var.cc +++ b/src/Var.cc @@ -450,7 +450,15 @@ static std::optional func_type_check(const zeek::Func return {}; } - return decl->FindPrototype(*impl->Params()); + auto rval = decl->FindPrototype(*impl->Params()); + + if ( rval ) + for ( auto i = 0; i < rval->args->NumFields(); ++i ) + if ( rval->args->FieldDecl(i)->GetAttr(zeek::detail::ATTR_DEPRECATED) ) + impl->Warn(fmt("use of deprecated parameter '%s'", + rval->args->FieldName(i)), decl, true); + + return rval; } static bool canonical_arg_types_match(const zeek::FuncType* decl, const zeek::FuncType* impl) diff --git a/testing/btest/Baseline/language.alternate-prototypes-deprecated-args/out b/testing/btest/Baseline/language.alternate-prototypes-deprecated-args/out new file mode 100644 index 0000000000..8acfd27bf5 --- /dev/null +++ b/testing/btest/Baseline/language.alternate-prototypes-deprecated-args/out @@ -0,0 +1,5 @@ +warning in /home/jon/pro/zeek/zeek/testing/btest/.tmp/language.alternate-prototypes-deprecated-args/alternate-prototypes-deprecated-args.zeek, line 9 and /home/jon/pro/zeek/zeek/testing/btest/.tmp/language.alternate-prototypes-deprecated-args/alternate-prototypes-deprecated-args.zeek, line 5: use of deprecated parameter 'b' (event(a:string; b:string; c:string;)) +warning in /home/jon/pro/zeek/zeek/testing/btest/.tmp/language.alternate-prototypes-deprecated-args/alternate-prototypes-deprecated-args.zeek, line 19 and /home/jon/pro/zeek/zeek/testing/btest/.tmp/language.alternate-prototypes-deprecated-args/alternate-prototypes-deprecated-args.zeek, line 5: use of deprecated prototype (event(a:string; b:string;) and myev) +myev (canon), one, two, three +myev (new), one, three +myev (old), one, two diff --git a/testing/btest/language/alternate-prototypes-deprecated-args.zeek b/testing/btest/language/alternate-prototypes-deprecated-args.zeek new file mode 100644 index 0000000000..659c4c7a16 --- /dev/null +++ b/testing/btest/language/alternate-prototypes-deprecated-args.zeek @@ -0,0 +1,28 @@ +# @TEST-EXEC: zeek -b %INPUT >out 2>&1 +# +# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff out + +global myev: event(a: string, b: string &deprecated="Don't use 'b'", c: string); +global myev: event(a: string, c: string); +global myev: event(a: string, b: string) &deprecated="Don't use this prototype"; + +event myev(a: string, b: string, c: string) &priority=11 + { + print "myev (canon)", a, b, c; + } + +event myev(a: string, c: string) &priority = 7 + { + print "myev (new)", a, c; + } + +event myev(a: string, b: string) &priority = 5 + { + print "myev (old)", a, b; + } + +event zeek_init() + { + event myev("one", "two", "three"); + } +