GCC Middle and Back End API Reference
metadata.h
Go to the documentation of this file.
1/* Additional metadata for a diagnostic.
2 Copyright (C) 2019-2025 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21#ifndef GCC_DIAGNOSTICS_METADATA_H
22#define GCC_DIAGNOSTICS_METADATA_H
23
24#include "lazily-created.h"
25
26namespace diagnostics {
27
28 class sarif_object;
29 namespace digraphs { class digraph; }
30
31/* A bundle of additional metadata that can be associated with a
32 diagnostic.
33
34 This supports an optional CWE identifier, and zero or more
35 "rules".
36
37 Additionally, this provides a place to associate a diagnostic
38 with zero or more directed graphs. */
39
41{
42 public:
45
46 /* Abstract base class for referencing a rule that has been violated,
47 such as within a coding standard, or within a specification. */
48 class rule
49 {
50 public:
51 virtual char *make_description () const = 0;
52 virtual char *make_url () const = 0;
53 };
54
55 /* Concrete subclass. */
56 class precanned_rule : public rule
57 {
58 public:
59 precanned_rule (const char *desc, const char *url)
60 : m_desc (desc), m_url (url)
61 {}
63 char *make_description () const final override
64 {
65 return m_desc ? xstrdup (m_desc) : NULL;
66 }
68 char *make_url () const final override
69 {
70 return m_url ? xstrdup (m_url) : NULL;
71 }
72
73 private:
74 const char *m_desc;
75 const char *m_url;
76 };
78 metadata () : m_cwe (0), m_lazy_digraphs (nullptr) {}
79 virtual ~metadata () {}
80
81 /* Hook for SARIF output to allow for adding diagnostic-specific
82 properties to the result object's property bag. */
83 virtual void
84 maybe_add_sarif_properties (sarif_object &/*result_obj*/) const
85 {
86 }
88 void add_cwe (int cwe) { m_cwe = cwe; }
89 int get_cwe () const { return m_cwe; }
90
91 /* Associate R with the diagnostic. R must outlive
92 the metadata. */
93 void add_rule (const rule &r)
94 {
95 m_rules.safe_push (&r);
96 }
98 unsigned get_num_rules () const { return m_rules.length (); }
99 const rule &get_rule (unsigned idx) const { return *(m_rules[idx]); }
100
101 void
102 set_lazy_digraphs (const lazy_digraphs *lazy_digraphs_)
103 {
104 m_lazy_digraphs = lazy_digraphs_;
105 }
106
108 get_lazy_digraphs () const
109 {
110 return m_lazy_digraphs;
111 }
112
113 private:
114 int m_cwe;
116
117 /* An optional way to create directed graphs associated with the
118 diagnostic, for the sinks that support this (e.g. SARIF). */
120};
121
122extern char *get_cwe_url (int cwe);
123
124} // namespace diagnostics
125
126#endif /* ! GCC_DIAGNOSTICS_METADATA_H */
Definition vec.h:1667
Definition digraphs.h:88
precanned_rule(const char *desc, const char *url)
Definition metadata.h:58
const char * m_desc
Definition metadata.h:73
const char * m_url
Definition metadata.h:74
char * make_description() const final override
Definition metadata.h:62
char * make_url() const final override
Definition metadata.h:67
Definition metadata.h:48
virtual char * make_url() const =0
virtual char * make_description() const =0
virtual ~metadata()
Definition metadata.h:78
void add_rule(const rule &r)
Definition metadata.h:92
const lazy_digraphs * m_lazy_digraphs
Definition metadata.h:118
unsigned get_num_rules() const
Definition metadata.h:97
virtual void maybe_add_sarif_properties(sarif_object &) const
Definition metadata.h:83
metadata()
Definition metadata.h:77
const lazy_digraphs * get_lazy_digraphs() const
Definition metadata.h:107
lazily_created< std::vector< std::unique_ptr< digraphs::digraph > > > lazy_digraphs
Definition metadata.h:43
int m_cwe
Definition metadata.h:113
void set_lazy_digraphs(const lazy_digraphs *lazy_digraphs_)
Definition metadata.h:101
void add_cwe(int cwe)
Definition metadata.h:87
const rule & get_rule(unsigned idx) const
Definition metadata.h:98
int get_cwe() const
Definition metadata.h:88
auto_vec< const rule * > m_rules
Definition metadata.h:114
Definition sarif-sink.h:151
Definition lazily-created.h:31
void final(rtx_insn *first, FILE *file, int optimize_p)
Definition final.cc:2009
Definition diagnostics/context.h:39
Definition coretypes.h:167
char * get_cwe_url(int cwe)
Definition diagnostics/context.cc:1215
poly_int< N, C > r
Definition poly-int.h:774
#define NULL
Definition system.h:50