145
|
1 /* Utility functions for the analyzer.
|
|
2 Copyright (C) 2019-2020 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
|
|
8 under the terms of the GNU General Public License as published by
|
|
9 the Free Software Foundation; either version 3, or (at your option)
|
|
10 any later version.
|
|
11
|
|
12 GCC is distributed in the hope that it will be useful, but
|
|
13 WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
15 General Public License 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 "function.h"
|
|
26 #include "basic-block.h"
|
|
27 #include "gimple.h"
|
|
28 #include "diagnostic.h"
|
|
29 #include "intl.h"
|
|
30 #include "function.h"
|
|
31 #include "analyzer/analyzer.h"
|
|
32
|
|
33 #if ENABLE_ANALYZER
|
|
34
|
|
35 /* Helper function for checkers. Is the CALL to the given function name,
|
|
36 and with the given number of arguments?
|
|
37
|
|
38 This doesn't resolve function pointers via the region model;
|
|
39 is_named_call_p should be used instead, using a fndecl from
|
|
40 get_fndecl_for_call; this function should only be used for special cases
|
|
41 where it's not practical to get at the region model, or for special
|
|
42 analyzer functions such as __analyzer_dump. */
|
|
43
|
|
44 bool
|
|
45 is_special_named_call_p (const gcall *call, const char *funcname,
|
|
46 unsigned int num_args)
|
|
47 {
|
|
48 gcc_assert (funcname);
|
|
49
|
|
50 tree fndecl = gimple_call_fndecl (call);
|
|
51 if (!fndecl)
|
|
52 return false;
|
|
53
|
|
54 return is_named_call_p (fndecl, funcname, call, num_args);
|
|
55 }
|
|
56
|
|
57 /* Helper function for checkers. Is FNDECL an extern fndecl at file scope
|
|
58 that has the given FUNCNAME?
|
|
59
|
|
60 Compare with special_function_p in calls.c. */
|
|
61
|
|
62 bool
|
|
63 is_named_call_p (tree fndecl, const char *funcname)
|
|
64 {
|
|
65 gcc_assert (fndecl);
|
|
66 gcc_assert (funcname);
|
|
67
|
|
68 if (!maybe_special_function_p (fndecl))
|
|
69 return false;
|
|
70
|
|
71 tree identifier = DECL_NAME (fndecl);
|
|
72 const char *name = IDENTIFIER_POINTER (identifier);
|
|
73 const char *tname = name;
|
|
74
|
|
75 /* Potentially disregard prefix _ or __ in FNDECL's name, but not if
|
|
76 FUNCNAME itself has leading underscores (e.g. when looking for
|
|
77 "__analyzer_eval"). */
|
|
78 if (funcname[0] != '_' && name[0] == '_')
|
|
79 {
|
|
80 if (name[1] == '_')
|
|
81 tname += 2;
|
|
82 else
|
|
83 tname += 1;
|
|
84 }
|
|
85
|
|
86 return 0 == strcmp (tname, funcname);
|
|
87 }
|
|
88
|
|
89 /* Helper function for checkers. Is FNDECL an extern fndecl at file scope
|
|
90 that has the given FUNCNAME, and does CALL have the given number of
|
|
91 arguments? */
|
|
92
|
|
93 bool
|
|
94 is_named_call_p (tree fndecl, const char *funcname,
|
|
95 const gcall *call, unsigned int num_args)
|
|
96 {
|
|
97 gcc_assert (fndecl);
|
|
98 gcc_assert (funcname);
|
|
99
|
|
100 if (!is_named_call_p (fndecl, funcname))
|
|
101 return false;
|
|
102
|
|
103 if (gimple_call_num_args (call) != num_args)
|
|
104 return false;
|
|
105
|
|
106 return true;
|
|
107 }
|
|
108
|
|
109 /* Return true if stmt is a setjmp or sigsetjmp call. */
|
|
110
|
|
111 bool
|
|
112 is_setjmp_call_p (const gcall *call)
|
|
113 {
|
|
114 if (is_special_named_call_p (call, "setjmp", 1)
|
|
115 || is_special_named_call_p (call, "sigsetjmp", 2))
|
|
116 return true;
|
|
117
|
|
118 return false;
|
|
119 }
|
|
120
|
|
121 /* Return true if stmt is a longjmp or siglongjmp call. */
|
|
122
|
|
123 bool
|
|
124 is_longjmp_call_p (const gcall *call)
|
|
125 {
|
|
126 if (is_special_named_call_p (call, "longjmp", 2)
|
|
127 || is_special_named_call_p (call, "siglongjmp", 2))
|
|
128 return true;
|
|
129
|
|
130 return false;
|
|
131 }
|
|
132
|
|
133 /* For a CALL that matched is_special_named_call_p or is_named_call_p for
|
|
134 some name, return a name for the called function suitable for use in
|
|
135 diagnostics (stripping the leading underscores). */
|
|
136
|
|
137 const char *
|
|
138 get_user_facing_name (const gcall *call)
|
|
139 {
|
|
140 tree fndecl = gimple_call_fndecl (call);
|
|
141 gcc_assert (fndecl);
|
|
142
|
|
143 tree identifier = DECL_NAME (fndecl);
|
|
144 gcc_assert (identifier);
|
|
145
|
|
146 const char *name = IDENTIFIER_POINTER (identifier);
|
|
147
|
|
148 /* Strip prefix _ or __ in FNDECL's name. */
|
|
149 if (name[0] == '_')
|
|
150 {
|
|
151 if (name[1] == '_')
|
|
152 return name + 2;
|
|
153 else
|
|
154 return name + 1;
|
|
155 }
|
|
156
|
|
157 return name;
|
|
158 }
|
|
159
|
|
160 /* Generate a label_text instance by formatting FMT, using a
|
|
161 temporary clone of the global_dc's printer (thus using its
|
|
162 formatting callbacks).
|
|
163
|
|
164 Colorize if the global_dc supports colorization and CAN_COLORIZE is
|
|
165 true. */
|
|
166
|
|
167 label_text
|
|
168 make_label_text (bool can_colorize, const char *fmt, ...)
|
|
169 {
|
|
170 pretty_printer *pp = global_dc->printer->clone ();
|
|
171 pp_clear_output_area (pp);
|
|
172
|
|
173 if (!can_colorize)
|
|
174 pp_show_color (pp) = false;
|
|
175
|
|
176 text_info ti;
|
|
177 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
|
|
178
|
|
179 va_list ap;
|
|
180
|
|
181 va_start (ap, fmt);
|
|
182
|
|
183 ti.format_spec = _(fmt);
|
|
184 ti.args_ptr = ≈
|
|
185 ti.err_no = 0;
|
|
186 ti.x_data = NULL;
|
|
187 ti.m_richloc = &rich_loc;
|
|
188
|
|
189 pp_format (pp, &ti);
|
|
190 pp_output_formatted_text (pp);
|
|
191
|
|
192 va_end (ap);
|
|
193
|
|
194 label_text result = label_text::take (xstrdup (pp_formatted_text (pp)));
|
|
195 delete pp;
|
|
196 return result;
|
|
197 }
|
|
198
|
|
199 #endif /* #if ENABLE_ANALYZER */
|