1/* Subclasses of logical_location with knowledge of "tree".
2 Copyright (C) 2022-2023 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#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
29/* class compiler_logical_location : public logical_location. */
30
31/* Get a string for DECL suitable for use by the SARIF logicalLocation
32 "name" property (SARIF v2.1.0 section 3.33.4). */
33
34const char *
35compiler_logical_location::get_short_name_for_tree (tree decl)
36{
37 gcc_assert (decl);
38 return identifier_to_locale (lang_hooks.decl_printable_name (decl, 0));
39}
40
41/* Get a string for DECL suitable for use by the SARIF logicalLocation
42 "fullyQualifiedName" property (SARIF v2.1.0 section 3.33.5). */
43
44const char *
45compiler_logical_location::get_name_with_scope_for_tree (tree decl)
46{
47 gcc_assert (decl);
48 return identifier_to_locale (lang_hooks.decl_printable_name (decl, 1));
49}
50
51/* Get a string for DECL suitable for use by the SARIF logicalLocation
52 "decoratedName" property (SARIF v2.1.0 section 3.33.6). */
53
54const char *
55compiler_logical_location::get_internal_name_for_tree (tree decl)
56{
57 gcc_assert (decl);
58 if (HAS_DECL_ASSEMBLER_NAME_P (decl))
59 if (tree id = DECL_ASSEMBLER_NAME (decl))
60 return IDENTIFIER_POINTER (id);
61 return NULL;
62}
63
64/* Get what kind of SARIF logicalLocation DECL is (if any). */
65
66enum logical_location_kind
67compiler_logical_location::get_kind_for_tree (tree decl)
68{
69 if (!decl)
70 return LOGICAL_LOCATION_KIND_UNKNOWN;
71
72 switch (TREE_CODE (decl))
73 {
74 default:
75 return LOGICAL_LOCATION_KIND_UNKNOWN;
76 case FUNCTION_DECL:
77 return LOGICAL_LOCATION_KIND_FUNCTION;
78 case PARM_DECL:
79 return LOGICAL_LOCATION_KIND_PARAMETER;
80 case VAR_DECL:
81 return LOGICAL_LOCATION_KIND_VARIABLE;
82 }
83}
84
85/* class tree_logical_location : public compiler_logical_location. */
86
87/* Implementation of the logical_location vfuncs, using m_decl. */
88
89const char *
90tree_logical_location::get_short_name () const
91{
92 gcc_assert (m_decl);
93 return get_short_name_for_tree (decl: m_decl);
94}
95
96const char *
97tree_logical_location::get_name_with_scope () const
98{
99 gcc_assert (m_decl);
100 return get_name_with_scope_for_tree (decl: m_decl);
101}
102
103const char *
104tree_logical_location::get_internal_name () const
105{
106 gcc_assert (m_decl);
107 return get_internal_name_for_tree (decl: m_decl);
108}
109
110enum logical_location_kind
111tree_logical_location::get_kind () const
112{
113 gcc_assert (m_decl);
114 return get_kind_for_tree (decl: m_decl);
115}
116
117/* class current_fndecl_logical_location : public compiler_logical_location. */
118
119/* Implementation of the logical_location vfuncs, using
120 current_function_decl. */
121
122const char *
123current_fndecl_logical_location::get_short_name () const
124{
125 gcc_assert (current_function_decl);
126 return get_short_name_for_tree (decl: current_function_decl);
127}
128
129const char *
130current_fndecl_logical_location::get_name_with_scope () const
131{
132 gcc_assert (current_function_decl);
133 return get_name_with_scope_for_tree (decl: current_function_decl);
134}
135
136const char *
137current_fndecl_logical_location::get_internal_name () const
138{
139 gcc_assert (current_function_decl);
140 return get_internal_name_for_tree (decl: current_function_decl);
141}
142
143enum logical_location_kind
144current_fndecl_logical_location::get_kind () const
145{
146 gcc_assert (current_function_decl);
147 return get_kind_for_tree (decl: current_function_decl);
148}
149

source code of gcc/tree-logical-location.cc