Branch data Line data Source code
1 : : /* Additional metadata for a diagnostic.
2 : : Copyright (C) 2019-2025 Free Software Foundation, Inc.
3 : : Contributed by David Malcolm <dmalcolm@redhat.com>
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it under
8 : : the terms of the GNU General Public License as published by the Free
9 : : Software Foundation; either version 3, or (at your option) any later
10 : : version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : : for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along 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 : :
26 : : namespace 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 : :
40 : : class metadata
41 : : {
42 : : public:
43 : : using lazy_digraphs
44 : : = lazily_created<std::vector<std::unique_ptr<digraphs::digraph>>>;
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 : 409 : 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 : 11 : precanned_rule (const char *desc, const char *url)
60 : 11 : : m_desc (desc), m_url (url)
61 : : {}
62 : :
63 : 13 : char *make_description () const final override
64 : : {
65 : 13 : return m_desc ? xstrdup (m_desc) : NULL;
66 : : }
67 : :
68 : 5 : char *make_url () const final override
69 : : {
70 : 5 : return m_url ? xstrdup (m_url) : NULL;
71 : : }
72 : :
73 : : private:
74 : : const char *m_desc;
75 : : const char *m_url;
76 : : };
77 : :
78 : 4380 : metadata () : m_cwe (0), m_lazy_digraphs (nullptr) {}
79 : 4386 : 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 : 3 : maybe_add_sarif_properties (sarif_object &/*result_obj*/) const
85 : : {
86 : 3 : }
87 : :
88 : 3306 : void add_cwe (int cwe) { m_cwe = cwe; }
89 : 4334 : int get_cwe () const { return m_cwe; }
90 : :
91 : : /* Associate R with the diagnostic. R must outlive
92 : : the metadata. */
93 : 409 : void add_rule (const rule &r)
94 : : {
95 : 409 : m_rules.safe_push (&r);
96 : : }
97 : :
98 : 4723 : unsigned get_num_rules () const { return m_rules.length (); }
99 : 411 : 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 : :
107 : : const lazy_digraphs *
108 : 26 : get_lazy_digraphs () const
109 : : {
110 : 26 : return m_lazy_digraphs;
111 : : }
112 : :
113 : : private:
114 : : int m_cwe;
115 : : auto_vec<const rule *> m_rules;
116 : :
117 : : /* An optional way to create directed graphs associated with the
118 : : diagnostic, for the sinks that support this (e.g. SARIF). */
119 : : const lazy_digraphs *m_lazy_digraphs;
120 : : };
121 : :
122 : : extern char *get_cwe_url (int cwe);
123 : :
124 : : } // namespace diagnostics
125 : :
126 : : #endif /* ! GCC_DIAGNOSTICS_METADATA_H */
|