From d9479c05025dad9541ecee4fbfea061f1e41ae24 Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Thu, 5 May 2022 10:57:31 -0700 Subject: [PATCH] --no-usage-warnings flag to suppress analysis --- src/ID.cc | 5 ++++- src/Options.cc | 6 ++++++ src/Options.h | 1 + src/Var.cc | 5 ++++- src/script_opt/ScriptOpt.cc | 7 ++++++- src/script_opt/ScriptOpt.h | 5 +++-- src/zeek-setup.cc | 7 +++++-- 7 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/ID.cc b/src/ID.cc index dd9b39a73d..18494c7fd1 100644 --- a/src/ID.cc +++ b/src/ID.cc @@ -1,4 +1,3 @@ - // See the file "COPYING" in the main distribution directory for copyright. #include "zeek/ID.h" @@ -18,6 +17,7 @@ #include "zeek/Val.h" #include "zeek/module_util.h" #include "zeek/script_opt/IDOptInfo.h" +#include "zeek/script_opt/UsageAnalyzer.h" #include "zeek/zeekygen/IdentifierInfo.h" #include "zeek/zeekygen/Manager.h" #include "zeek/zeekygen/ScriptInfo.h" @@ -167,6 +167,9 @@ void ID::SetVal(ValPtr v) handler = new EventHandler(name); handler->SetFunc(func); event_registry->Register(handler); + + if ( ! IsExport() ) + register_new_event({NewRef{}, this}); } else { diff --git a/src/Options.cc b/src/Options.cc index b9938bbb12..c7ec33bf29 100644 --- a/src/Options.cc +++ b/src/Options.cc @@ -107,6 +107,7 @@ void usage(const char* prog, int code) fprintf(stderr, " -s|--rulefile | read rules from given file\n"); fprintf(stderr, " -t|--tracefile | activate execution tracing\n"); fprintf(stderr, " -u|--usage-issues | find variable usage issues and exit\n"); + fprintf(stderr, " --no-usage-warnings | suppress warnings of unused functions/hooks/events\n"); fprintf(stderr, " -v|--version | print version and exit\n"); fprintf(stderr, " -w|--writefile | write to given tcpdump file\n"); #ifdef DEBUG @@ -367,6 +368,7 @@ Options parse_cmdline(int argc, char** argv) } int profile_scripts = 0; + int no_usage_warnings = 0; struct option long_opts[] = { {"parse-only", no_argument, nullptr, 'a'}, @@ -410,6 +412,7 @@ Options parse_cmdline(int argc, char** argv) #endif {"profile-scripts", optional_argument, &profile_scripts, 1}, + {"no-usage-warnings", no_argument, &no_usage_warnings, 1}, {"pseudo-realtime", optional_argument, nullptr, '~'}, {"jobs", optional_argument, nullptr, 'j'}, {"test", no_argument, nullptr, '#'}, @@ -613,6 +616,9 @@ Options parse_cmdline(int argc, char** argv) activate_script_profiling(optarg); profile_scripts = 0; } + + if ( no_usage_warnings ) + rval.no_usage_warnings = true; break; case '?': diff --git a/src/Options.h b/src/Options.h index 0065b527b9..feea673c47 100644 --- a/src/Options.h +++ b/src/Options.h @@ -58,6 +58,7 @@ struct Options bool perftools_profile = false; bool deterministic_mode = false; bool abort_on_scripting_errors = false; + bool no_usage_warnings = false; bool run_unit_tests = false; std::vector doctest_args; diff --git a/src/Var.cc b/src/Var.cc index 35b37ac56b..3527054fd1 100644 --- a/src/Var.cc +++ b/src/Var.cc @@ -17,8 +17,8 @@ #include "zeek/Traverse.h" #include "zeek/Val.h" #include "zeek/module_util.h" -#include "zeek/script_opt/ScriptOpt.h" #include "zeek/script_opt/StmtOptInfo.h" +#include "zeek/script_opt/UsageAnalyzer.h" namespace zeek::detail { @@ -658,6 +658,9 @@ void begin_func(IDPtr id, const char* module_name, FunctionFlavor flavor, bool i id->Error("event cannot yield a value", t.get()); t->ClearYieldType(flavor); + + if ( ! event_registry->Lookup(id->Name()) ) + register_new_event(id); } std::optional prototype; diff --git a/src/script_opt/ScriptOpt.cc b/src/script_opt/ScriptOpt.cc index 18a5b9d33a..ef7ae065ba 100644 --- a/src/script_opt/ScriptOpt.cc +++ b/src/script_opt/ScriptOpt.cc @@ -14,6 +14,7 @@ #include "zeek/script_opt/Inline.h" #include "zeek/script_opt/ProfileFunc.h" #include "zeek/script_opt/Reduce.h" +#include "zeek/script_opt/UsageAnalyzer.h" #include "zeek/script_opt/UseDefs.h" #include "zeek/script_opt/ZAM/Compile.h" @@ -519,7 +520,7 @@ static void analyze_scripts_for_ZAM(std::unique_ptr& pfs) finalize_functions(funcs); } -void analyze_scripts() +void analyze_scripts(bool no_usage_warnings) { static bool did_init = false; @@ -529,6 +530,10 @@ void analyze_scripts() did_init = true; } + std::unique_ptr ua; + if ( ! no_usage_warnings ) + ua = std::make_unique(funcs); + auto& ofuncs = analysis_options.only_funcs; auto& ofiles = analysis_options.only_files; diff --git a/src/script_opt/ScriptOpt.h b/src/script_opt/ScriptOpt.h index 95d7889d7e..423dfd7218 100644 --- a/src/script_opt/ScriptOpt.h +++ b/src/script_opt/ScriptOpt.h @@ -182,8 +182,9 @@ extern void add_file_analysis_pattern(AnalyOpt& opts, const char* pat); // it should be skipped. extern bool should_analyze(const ScriptFuncPtr& f, const StmtPtr& body); -// Analyze all of the parsed scripts collectively for optimization. -extern void analyze_scripts(); +// Analyze all of the parsed scripts collectively for usage issues (unless +// suppressed by the flag) and optimization. +extern void analyze_scripts(bool no_usage_warnings); // Called when Zeek is terminating. extern void finish_script_execution(); diff --git a/src/zeek-setup.cc b/src/zeek-setup.cc index df8294816d..6a3ba211f0 100644 --- a/src/zeek-setup.cc +++ b/src/zeek-setup.cc @@ -589,6 +589,9 @@ SetupResult setup(int argc, char** argv, Options* zopts) plugin_mgr = new plugin::Manager(); fragment_mgr = new detail::FragmentManager(); + if ( options.no_usage_warnings && options.analysis_options.usage_issues > 0 ) + reporter->FatalError("-u incompatible with --no-usage-warnings"); + #ifdef DEBUG if ( options.debug_log_streams ) { @@ -926,7 +929,7 @@ SetupResult setup(int argc, char** argv, Options* zopts) if ( options.parse_only ) { if ( analysis_options.usage_issues > 0 ) - analyze_scripts(); + analyze_scripts(options.no_usage_warnings); early_shutdown(); exit(reporter->Errors() != 0); @@ -934,7 +937,7 @@ SetupResult setup(int argc, char** argv, Options* zopts) auto init_stmts = stmts ? analyze_global_stmts(stmts) : nullptr; - analyze_scripts(); + analyze_scripts(options.no_usage_warnings); if ( analysis_options.report_recursive ) {