Debian Codegraph @

gcc/tree-logical-location.cc

raw
1 /* Subclasses of logical_location with knowledge of "tree".
2 Copyright (C) 2022-2025 Free Software Foundation, Inc.
3 Contributed by David Malcolm <[email protected]>.
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 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tree.h"
25 #include "pretty-print.h"
26 #include "tree-logical-location.h"
27 #include "langhooks.h"
28 #include "intl.h"
29
30 /* class compiler_logical_location : public logical_location. */
31
32 /* Get a string for DECL suitable for use by the SARIF logicalLocation
33 "name" property (SARIF v2.1.0 section 3.33.4). */
34
35 const char *
36 compiler_logical_location::get_short_name_for_tree (tree decl)
37 {
38 gcc_assert (decl);
39 return identifier_to_locale (lang_hooks.decl_printable_name (decl, 0));
40 }
41
42 /* Get a string for DECL suitable for use by the SARIF logicalLocation
43 "fullyQualifiedName" property (SARIF v2.1.0 section 3.33.5). */
44
45 const char *
46 compiler_logical_location::get_name_with_scope_for_tree (tree decl)
47 {
48 gcc_assert (decl);
49 return identifier_to_locale (lang_hooks.decl_printable_name (decl, 1));
50 }
51
52 /* Get a string for DECL suitable for use by the SARIF logicalLocation
53 "decoratedName" property (SARIF v2.1.0 section 3.33.6). */
54
55 const char *
56 compiler_logical_location::get_internal_name_for_tree (tree decl)
57 {
58 gcc_assert (decl);
59 if (HAS_DECL_ASSEMBLER_NAME_P (decl))
60 if (tree id = DECL_ASSEMBLER_NAME (decl))
61 return IDENTIFIER_POINTER (id);
62 return NULL;
63 }
64
65 /* Get what kind of SARIF logicalLocation DECL is (if any). */
66
67 enum logical_location_kind
68 compiler_logical_location::get_kind_for_tree (tree decl)
69 {
70 if (!decl)
71 return LOGICAL_LOCATION_KIND_UNKNOWN;
72
73 switch (TREE_CODE (decl))
74 {
75 default:
76 return LOGICAL_LOCATION_KIND_UNKNOWN;
77 case FUNCTION_DECL:
78 return LOGICAL_LOCATION_KIND_FUNCTION;
79 case PARM_DECL:
80 return LOGICAL_LOCATION_KIND_PARAMETER;
81 case VAR_DECL:
82 return LOGICAL_LOCATION_KIND_VARIABLE;
83 }
84 }
85
86 label_text
87 compiler_logical_location::get_name_for_tree_for_path_output (tree decl)
88 {
89 gcc_assert (decl);
90 const char *n = DECL_NAME (decl)
91 ? identifier_to_locale (lang_hooks.decl_printable_name (decl, 2))
92 : _("<anonymous>");
93 return label_text::borrow (n);
94 }
95
96 /* class tree_logical_location : public compiler_logical_location. */
97
98 /* Implementation of the logical_location vfuncs, using m_decl. */
99
100 const char *
101 tree_logical_location::get_short_name () const
102 {
103 gcc_assert (m_decl);
104 return get_short_name_for_tree (m_decl);
105 }
106
107 const char *
108 tree_logical_location::get_name_with_scope () const
109 {
110 gcc_assert (m_decl);
111 return get_name_with_scope_for_tree (m_decl);
112 }
113
114 const char *
115 tree_logical_location::get_internal_name () const
116 {
117 gcc_assert (m_decl);
118 return get_internal_name_for_tree (m_decl);
119 }
120
121 enum logical_location_kind
122 tree_logical_location::get_kind () const
123 {
124 gcc_assert (m_decl);
125 return get_kind_for_tree (m_decl);
126 }
127
128 label_text
129 tree_logical_location::get_name_for_path_output () const
130 {
131 gcc_assert (m_decl);
132 return get_name_for_tree_for_path_output (m_decl);
133 }
134
135 /* class current_fndecl_logical_location : public compiler_logical_location. */
136
137 /* Implementation of the logical_location vfuncs, using
138 current_function_decl. */
139
140 const char *
141 current_fndecl_logical_location::get_short_name () const
142 {
143 gcc_assert (current_function_decl);
144 return get_short_name_for_tree (current_function_decl);
145 }
146
147 const char *
148 current_fndecl_logical_location::get_name_with_scope () const
149 {
150 gcc_assert (current_function_decl);
151 return get_name_with_scope_for_tree (current_function_decl);
152 }
153
154 const char *
155 current_fndecl_logical_location::get_internal_name () const
156 {
157 gcc_assert (current_function_decl);
158 return get_internal_name_for_tree (current_function_decl);
159 }
160
161 enum logical_location_kind
162 current_fndecl_logical_location::get_kind () const
163 {
164 gcc_assert (current_function_decl);
165 return get_kind_for_tree (current_function_decl);
166 }
167
168 label_text
169 current_fndecl_logical_location::get_name_for_path_output () const
170 {
171 gcc_assert (current_function_decl);
172 return get_name_for_tree_for_path_output (current_function_decl);
173 }