diff options
| author | David Malcolm <dmalcolm@redhat.com> | 2025-06-23 18:46:51 -0400 |
|---|---|---|
| committer | David Malcolm <dmalcolm@redhat.com> | 2025-06-23 18:51:10 -0400 |
| commit | d0142e147486e6f319704d35930720f6dec648fb (patch) | |
| tree | 5d9039156c74a13c5b63b2793a6a2ba945bcf124 /contrib | |
| parent | e6406aefd1a25b6dba845a52cfd9484188ff5720 (diff) | |
libgdiagnostics: sarif-replay: add extra sinks via -fdiagnostics-add-output= [PR116792,PR116163]
This patch refactors the support for -fdiagnostics-add-output=SCHEME
from GCC's options parsing so that it is also available to
sarif-replay and to other clients of libgdiagnostics.
With this users of sarif-replay and other such tools can generate HTML
or SARIF as well as text output, using the same
-fdiagnostics-add-output=SCHEME
as GCC.
As a test, the patch adds support for this option to the dg-lint
script below "contrib". For example dg-lint can now generate text,
html, and sarif output via:
LD_LIBRARY_PATH=../build/gcc/ \
./contrib/dg-lint/dg-lint \
contrib/dg-lint/test-*.c \
-fdiagnostics-add-output=experimental-html:file=dg-lint-tests.html \
-fdiagnostics-add-output=sarif:file=dg-lint-tests.sarif
where the HTML output from dg-lint can be seen here:
https://dmalcolm.fedorapeople.org/gcc/2025-06-20/dg-lint-tests.html
the sarif output here:
https://dmalcolm.fedorapeople.org/gcc/2025-06-23/dg-lint-tests.sarif
and a screenshot of VS Code viewing the sarif output is here:
https://dmalcolm.fedorapeople.org/gcc/2025-06-23/vscode-viewing-dg-lint-sarif-output.png
As well as allowing sarif-replay to generate HTML, this patch allows
sarif-replay to also generate SARIF. Ideally this would faithfully
round-trip all the data, but it's not perfect (which I'm tracking as
PR sarif-replay/120792).
contrib/ChangeLog:
PR other/116792
PR testsuite/116163
PR sarif-replay/120792
* dg-lint/dg-lint: Add -fdiagnostics-add-output.
* dg-lint/libgdiagnostics.py: Add
diagnostic_manager_add_sink_from_spec.
(Manager.add_sink_from_spec): New.
gcc/ChangeLog:
PR other/116792
PR testsuite/116163
PR sarif-replay/120792
* Makefile.in (OBJS-libcommon): Add diagnostic-output-spec.o.
* diagnostic-format-html.cc (html_builder::html_builder): Ensure
title is non-empty.
* diagnostic-output-spec.cc: New file, taken from material in
opts-diagnostic.cc.
* diagnostic-output-spec.h: New file.
* diagnostic.cc (diagnostic_context::set_main_input_filename):
New.
* diagnostic.h (diagnostic_context::set_main_input_filename): New
decl.
* doc/libgdiagnostics/topics/compatibility.rst
(LIBGDIAGNOSTICS_ABI_2): New.
* doc/libgdiagnostics/topics/diagnostic-manager.rst
(diagnostic_manager_add_sink_from_spec): New.
(diagnostic_manager_set_analysis_target): New.
* libgdiagnostics++.h (manager::add_sink_from_spec): New.
(manager::set_analysis_target): New.
* libgdiagnostics.cc: Include "diagnostic-output-spec.h".
(struct spec_context): New.
(diagnostic_manager_add_sink_from_spec): New.
(diagnostic_manager_set_analysis_target): New.
* libgdiagnostics.h
(LIBDIAGNOSTICS_HAVE_diagnostic_manager_add_sink_from_spec): New
define.
(diagnostic_manager_add_sink_from_spec): New decl.
(LIBDIAGNOSTICS_HAVE_diagnostic_manager_set_analysis_target): New
define.
(diagnostic_manager_set_analysis_target): New decl.
* libgdiagnostics.map (LIBGDIAGNOSTICS_ABI_2): New.
* libsarifreplay.cc (sarif_replayer::handle_artifact_obj): Looks
for "analysisTarget" in roles and call set_analysis_target using
the artifact if found.
* opts-diagnostic.cc: Refactor, moving material to
diagnostic-output-spec.cc.
(struct opt_spec_context): New.
(handle_OPT_fdiagnostics_add_output_): Use opt_spec_context.
(handle_OPT_fdiagnostics_set_output_): Likewise.
* sarif-replay.cc: Define INCLUDE_STRING.
(struct options): Add m_extra_output_specs.
(usage_msg): Add -fdiagnostics-add-output=SCHEME.
(str_starts_with): New.
(parse_options): Add -fdiagnostics-add-output=SCHEME.
(main): Likewise.
* selftest-run-tests.cc (selftest::run_tests): Call
diagnostic_output_spec_cc_tests rather than
opts_diagnostic_cc_tests.
* selftest.h (selftest::diagnostic_output_spec_cc_tests):
Replace...
(selftest::opts_diagnostic_cc_tests): ...this.
gcc/testsuite/ChangeLog:
PR other/116792
PR testsuite/116163
PR sarif-replay/120792
* sarif-replay.dg/2.1.0-valid/signal-1-check-html.py: New test
script.
* sarif-replay.dg/2.1.0-valid/signal-1.c.sarif: Add html and sarif
generation to options. Invoke the new script to verify that HTML
and SARIF is generated.
Signed-off-by: David Malcolm <dmalcolm@redhat.com>
Diffstat (limited to 'contrib')
| -rwxr-xr-x | contrib/dg-lint/dg-lint | 8 | ||||
| -rw-r--r-- | contrib/dg-lint/libgdiagnostics.py | 17 |
2 files changed, 25 insertions, 0 deletions
diff --git a/contrib/dg-lint/dg-lint b/contrib/dg-lint/dg-lint index 01d58d7a3e9..4ae0686b975 100755 --- a/contrib/dg-lint/dg-lint +++ b/contrib/dg-lint/dg-lint @@ -380,9 +380,17 @@ def skip_file(filename): def main(argv): parser = argparse.ArgumentParser()#usage=__doc__) parser.add_argument('paths', nargs='+', type=pathlib.Path) + parser.add_argument('-fdiagnostics-add-output', action='append') opts = parser.parse_args(argv[1:]) ctxt = Context() + control_mgr = libgdiagnostics.Manager() + control_mgr.add_text_sink() + for scheme in opts.fdiagnostics_add_output: + ctxt.mgr.add_sink_from_spec("-fdiagnostics-add-output=", + scheme, + control_mgr) + for path in opts.paths: if path.is_dir(): for dirpath, dirnames, filenames in os.walk(path): diff --git a/contrib/dg-lint/libgdiagnostics.py b/contrib/dg-lint/libgdiagnostics.py index 03a6440a3e3..8c8cc4887cd 100644 --- a/contrib/dg-lint/libgdiagnostics.py +++ b/contrib/dg-lint/libgdiagnostics.py @@ -124,6 +124,13 @@ cdll.diagnostic_add_fix_it_hint_replace.argtypes \ ctypes.c_char_p] cdll.diagnostic_add_fix_it_hint_replace.restype = None +cdll.diagnostic_manager_add_sink_from_spec.argtypes \ + = [c_diagnostic_manager_ptr, + ctypes.c_char_p, + ctypes.c_char_p, + c_diagnostic_manager_ptr] +cdll.diagnostic_manager_add_sink_from_spec.restype = ctypes.c_int + # Helper functions def _to_utf8(s: str): @@ -156,6 +163,16 @@ class Manager: c_stderr, DIAGNOSTIC_COLORIZE_IF_TTY) + def add_sink_from_spec(self, option_name: str, scheme: str, control_mgr): + assert self.c_mgr + assert control_mgr.c_mgr + res = cdll.diagnostic_manager_add_sink_from_spec (self.c_mgr, + _to_utf8(option_name), + _to_utf8(scheme), + control_mgr.c_mgr) + if res: + raise RuntimeError() + def get_file(self, path: str, sarif_lang: str = None): assert self.c_mgr assert path |
