]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/dbgcnt.c
c++: Handle multiple aggregate overloads [PR95319].
[thirdparty/gcc.git] / gcc / dbgcnt.c
CommitLineData
6fb5fa3c 1/* Debug counter for debugging support
8d9254fc 2 Copyright (C) 2006-2020 Free Software Foundation, Inc.
6fb5fa3c
DB
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
6fb5fa3c
DB
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
9dcd6f09 17along with GCC; see the file COPYING3. If not see
b8698a0f 18<http://www.gnu.org/licenses/>.
6fb5fa3c
DB
19
20See dbgcnt.def for usage information. */
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
718f9c0f 25#include "diagnostic-core.h"
7ee2468b 26#include "dumpfile.h"
5024c8bb 27#include "selftest.h"
03df119d 28#include "intl.h"
6fb5fa3c
DB
29
30#include "dbgcnt.h"
31
32struct string2counter_map {
33 const char *name;
34 enum debug_counter counter;
35};
36
37#define DEBUG_COUNTER(a) { #a , a },
38
39static struct string2counter_map map[debug_counter_number_of_counters] =
40{
41#include "dbgcnt.def"
42};
43#undef DEBUG_COUNTER
44
83a49336 45typedef std::pair<unsigned int, unsigned int> limit_tuple;
6fb5fa3c 46
83a49336 47static vec<limit_tuple> limits[debug_counter_number_of_counters];
cdc3b883 48
6fb5fa3c
DB
49static unsigned int count[debug_counter_number_of_counters];
50
dd7fa0d4
ML
51static void
52print_limit_reach (const char *counter, int limit, bool upper_p)
53{
54 char buffer[128];
55 sprintf (buffer, "***dbgcnt: %s limit %d reached for %s.***\n",
56 upper_p ? "upper" : "lower", limit, counter);
57 fputs (buffer, stderr);
58 if (dump_file)
59 fputs (buffer, dump_file);
60}
61
6fb5fa3c
DB
62bool
63dbg_cnt (enum debug_counter index)
64{
83a49336
ML
65 unsigned v = ++count[index];
66
67 if (!limits[index].exists ())
68 return true;
69 else if (limits[index].is_empty ())
70 return false;
cdc3b883 71
83a49336
ML
72 unsigned last = limits[index].length () - 1;
73 unsigned int min = limits[index][last].first;
74 unsigned int max = limits[index][last].second;
cc806ac1 75
83a49336
ML
76 if (v < min)
77 return false;
78 else if (v == min)
79 {
80 print_limit_reach (map[index].name, v, false);
81 if (min == max)
82 limits[index].pop ();
83 return true;
84 }
85 else if (v < max)
86 return true;
87 else if (v == max)
88 {
89 print_limit_reach (map[index].name, v, true);
90 limits[index].pop ();
91 return true;
92 }
93 else
94 return false;
6fb5fa3c
DB
95}
96
83a49336 97/* Compare limit_tuple intervals by first item in descending order. */
6fb5fa3c 98
83a49336
ML
99static int
100cmp_tuples (const void *ptr1, const void *ptr2)
101{
102 const limit_tuple *p1 = (const limit_tuple *)ptr1;
103 const limit_tuple *p2 = (const limit_tuple *)ptr2;
104
105 if (p1->first < p2->first)
106 return 1;
107 else if (p1->first > p2->first)
108 return -1;
109 return 0;
6fb5fa3c
DB
110}
111
0a090f42 112static bool
83a49336
ML
113dbg_cnt_set_limit_by_index (enum debug_counter index, const char *name,
114 unsigned int low, unsigned int high)
6fb5fa3c 115{
83a49336
ML
116 if (!limits[index].exists ())
117 limits[index].create (1);
cdc3b883 118
83a49336
ML
119 limits[index].safe_push (limit_tuple (low, high));
120 limits[index].qsort (cmp_tuples);
121
122 for (unsigned i = 0; i < limits[index].length () - 1; i++)
cdc3b883 123 {
83a49336
ML
124 limit_tuple t1 = limits[index][i];
125 limit_tuple t2 = limits[index][i + 1];
126 if (t1.first <= t2.second)
127 {
128 error ("Interval overlap of %<-fdbg-cnt=%s%>: [%u, %u] and "
129 "[%u, %u]\n", name, t2.first, t2.second, t1.first, t1.second);
130 return false;
131 }
cdc3b883
ML
132 }
133
83a49336
ML
134 return true;
135}
136
137static bool
138dbg_cnt_set_limit_by_name (const char *name, unsigned int low,
139 unsigned int high)
140{
141 if (high < low)
cdc3b883 142 {
83a49336
ML
143 error ("%<-fdbg-cnt=%s:%d-%d%> has smaller upper limit than the lower",
144 name, low, high);
cdc3b883
ML
145 return false;
146 }
147
6fb5fa3c
DB
148 int i;
149 for (i = debug_counter_number_of_counters - 1; i >= 0; i--)
cdc3b883 150 if (strcmp (map[i].name, name) == 0)
6fb5fa3c
DB
151 break;
152
153 if (i < 0)
342ae9ad
ML
154 {
155 error ("cannot find a valid counter name %qs of %<-fdbg-cnt=%> option",
156 name);
157 return false;
158 }
6fb5fa3c 159
83a49336 160 return dbg_cnt_set_limit_by_index ((enum debug_counter) i, name, low, high);
6fb5fa3c
DB
161}
162
83a49336 163/* Process a single "low:high" pair.
0a090f42
SP
164 Returns NULL if there's no valid pair is found.
165 Otherwise returns a pointer to the end of the pair. */
166
cdc3b883 167static bool
83a49336 168dbg_cnt_process_single_pair (char *name, char *str)
6fb5fa3c 169{
83a49336
ML
170 char *value1 = strtok (str, "-");
171 char *value2 = strtok (NULL, "-");
cdc3b883 172
83a49336 173 unsigned int high, low;
cdc3b883
ML
174
175 if (value1 == NULL)
5433e401 176 return false;
cdc3b883
ML
177
178 if (value2 == NULL)
179 {
cdc3b883 180 high = strtol (value1, NULL, 10);
342ae9ad
ML
181 /* Let's allow 0:0. */
182 low = high == 0 ? 0 : 1;
cdc3b883
ML
183 }
184 else
185 {
186 low = strtol (value1, NULL, 10);
187 high = strtol (value2, NULL, 10);
188 }
189
2a7108b9 190 return dbg_cnt_set_limit_by_name (name, low, high);
0a090f42 191}
6fb5fa3c 192
0a090f42
SP
193void
194dbg_cnt_process_opt (const char *arg)
195{
cdc3b883 196 char *str = xstrdup (arg);
cdc3b883
ML
197 unsigned int start = 0;
198
83a49336
ML
199 auto_vec<char *> tokens;
200 for (char *next = strtok (str, ","); next != NULL; next = strtok (NULL, ","))
2a7108b9
ML
201 tokens.safe_push (next);
202
203 unsigned i;
204 for (i = 0; i < tokens.length (); i++)
205 {
83a49336
ML
206 auto_vec<char *> ranges;
207 char *name = strtok (tokens[i], ":");
208 for (char *part = strtok (NULL, ":"); part; part = strtok (NULL, ":"))
209 ranges.safe_push (part);
210
211 for (unsigned j = 0; j < ranges.length (); j++)
212 {
213 if (!dbg_cnt_process_single_pair (name, ranges[j]))
214 break;
215 }
216 start += strlen (tokens[i]) + 1;
2a7108b9 217 }
6fb5fa3c 218}
0a090f42
SP
219
220/* Print name, limit and count of all counters. */
221
b8698a0f 222void
cc806ac1 223dbg_cnt_list_all_counters (void)
0a090f42
SP
224{
225 int i;
03df119d 226 printf (" %-30s %s\n", G_("counter name"), G_("closed intervals"));
cdc3b883 227 printf ("-----------------------------------------------------------------\n");
0a090f42 228 for (i = 0; i < debug_counter_number_of_counters; i++)
83a49336
ML
229 {
230 printf (" %-30s ", map[i].name);
231 if (limits[i].exists ())
232 {
233 for (int j = limits[i].length () - 1; j >= 0; j--)
234 {
235 printf ("[%u, %u]", limits[i][j].first, limits[i][j].second);
236 if (j > 0)
237 printf (", ");
238 }
239 putchar ('\n');
240 }
241 else
242 printf ("unset\n");
243 }
0a090f42
SP
244 printf ("\n");
245}
5024c8bb
ML
246
247#if CHECKING_P
248
249namespace selftest {
250
251/* Selftests. */
252
253static void
254test_sorted_dbg_counters ()
255{
256 for (unsigned i = 0; i < debug_counter_number_of_counters - 1; i++)
257 ASSERT_LT (strcmp (map[i].name, map[i + 1].name), 0);
258}
259
260void
261dbgcnt_c_tests ()
262{
263 test_sorted_dbg_counters ();
264}
265
266} // namespace selftest
267
268#endif /* #if CHECKING_P */