typeck.c (cp_truthvalue_conversion): Add tsubst_flags_t parameter and use it in calls...
[official-gcc.git] / gcc / dbgcnt.c
blobc675c644007d8cf7549a4103a2c5fa1d2d0f275b
1 /* Debug counter for debugging support
2 Copyright (C) 2006-2019 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>.
20 See dbgcnt.def for usage information. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "diagnostic-core.h"
26 #include "dumpfile.h"
27 #include "selftest.h"
29 #include "dbgcnt.h"
31 struct string2counter_map {
32 const char *name;
33 enum debug_counter counter;
36 #define DEBUG_COUNTER(a) { #a , a },
38 static struct string2counter_map map[debug_counter_number_of_counters] =
40 #include "dbgcnt.def"
42 #undef DEBUG_COUNTER
44 typedef std::pair<unsigned int, unsigned int> limit_tuple;
46 static vec<limit_tuple> limits[debug_counter_number_of_counters];
48 static unsigned int count[debug_counter_number_of_counters];
50 static void
51 print_limit_reach (const char *counter, int limit, bool upper_p)
53 char buffer[128];
54 sprintf (buffer, "***dbgcnt: %s limit %d reached for %s.***\n",
55 upper_p ? "upper" : "lower", limit, counter);
56 fputs (buffer, stderr);
57 if (dump_file)
58 fputs (buffer, dump_file);
61 bool
62 dbg_cnt (enum debug_counter index)
64 unsigned v = ++count[index];
66 if (!limits[index].exists ())
67 return true;
68 else if (limits[index].is_empty ())
69 return false;
71 unsigned last = limits[index].length () - 1;
72 unsigned int min = limits[index][last].first;
73 unsigned int max = limits[index][last].second;
75 if (v < min)
76 return false;
77 else if (v == min)
79 print_limit_reach (map[index].name, v, false);
80 if (min == max)
81 limits[index].pop ();
82 return true;
84 else if (v < max)
85 return true;
86 else if (v == max)
88 print_limit_reach (map[index].name, v, true);
89 limits[index].pop ();
90 return true;
92 else
93 return false;
96 /* Compare limit_tuple intervals by first item in descending order. */
98 static int
99 cmp_tuples (const void *ptr1, const void *ptr2)
101 const limit_tuple *p1 = (const limit_tuple *)ptr1;
102 const limit_tuple *p2 = (const limit_tuple *)ptr2;
104 if (p1->first < p2->first)
105 return 1;
106 else if (p1->first > p2->first)
107 return -1;
108 return 0;
111 static bool
112 dbg_cnt_set_limit_by_index (enum debug_counter index, const char *name,
113 unsigned int low, unsigned int high)
115 if (!limits[index].exists ())
116 limits[index].create (1);
118 limits[index].safe_push (limit_tuple (low, high));
119 limits[index].qsort (cmp_tuples);
121 for (unsigned i = 0; i < limits[index].length () - 1; i++)
123 limit_tuple t1 = limits[index][i];
124 limit_tuple t2 = limits[index][i + 1];
125 if (t1.first <= t2.second)
127 error ("Interval overlap of %<-fdbg-cnt=%s%>: [%u, %u] and "
128 "[%u, %u]\n", name, t2.first, t2.second, t1.first, t1.second);
129 return false;
133 return true;
136 static bool
137 dbg_cnt_set_limit_by_name (const char *name, unsigned int low,
138 unsigned int high)
140 if (high < low)
142 error ("%<-fdbg-cnt=%s:%d-%d%> has smaller upper limit than the lower",
143 name, low, high);
144 return false;
147 int i;
148 for (i = debug_counter_number_of_counters - 1; i >= 0; i--)
149 if (strcmp (map[i].name, name) == 0)
150 break;
152 if (i < 0)
153 return false;
155 return dbg_cnt_set_limit_by_index ((enum debug_counter) i, name, low, high);
158 /* Process a single "low:high" pair.
159 Returns NULL if there's no valid pair is found.
160 Otherwise returns a pointer to the end of the pair. */
162 static bool
163 dbg_cnt_process_single_pair (char *name, char *str)
165 char *value1 = strtok (str, "-");
166 char *value2 = strtok (NULL, "-");
168 unsigned int high, low;
170 if (value1 == NULL)
171 return false;
173 if (value2 == NULL)
175 low = 1;
176 high = strtol (value1, NULL, 10);
178 else
180 low = strtol (value1, NULL, 10);
181 high = strtol (value2, NULL, 10);
184 return dbg_cnt_set_limit_by_name (name, low, high);
187 void
188 dbg_cnt_process_opt (const char *arg)
190 char *str = xstrdup (arg);
191 unsigned int start = 0;
193 auto_vec<char *> tokens;
194 for (char *next = strtok (str, ","); next != NULL; next = strtok (NULL, ","))
195 tokens.safe_push (next);
197 unsigned i;
198 for (i = 0; i < tokens.length (); i++)
200 auto_vec<char *> ranges;
201 char *name = strtok (tokens[i], ":");
202 for (char *part = strtok (NULL, ":"); part; part = strtok (NULL, ":"))
203 ranges.safe_push (part);
205 for (unsigned j = 0; j < ranges.length (); j++)
207 if (!dbg_cnt_process_single_pair (name, ranges[j]))
208 break;
210 start += strlen (tokens[i]) + 1;
213 if (i != tokens.length ())
215 char *buffer = XALLOCAVEC (char, start + 2);
216 sprintf (buffer, "%*c", start + 1, '^');
217 error ("cannot find a valid counter:value pair:");
218 error ("%<-fdbg-cnt=%s%>", arg);
219 error (" %s", buffer);
223 /* Print name, limit and count of all counters. */
225 void
226 dbg_cnt_list_all_counters (void)
228 int i;
229 printf (" %-30s %s\n", "counter name", "closed intervals");
230 printf ("-----------------------------------------------------------------\n");
231 for (i = 0; i < debug_counter_number_of_counters; i++)
233 printf (" %-30s ", map[i].name);
234 if (limits[i].exists ())
236 for (int j = limits[i].length () - 1; j >= 0; j--)
238 printf ("[%u, %u]", limits[i][j].first, limits[i][j].second);
239 if (j > 0)
240 printf (", ");
242 putchar ('\n');
244 else
245 printf ("unset\n");
247 printf ("\n");
250 #if CHECKING_P
252 namespace selftest {
254 /* Selftests. */
256 static void
257 test_sorted_dbg_counters ()
259 for (unsigned i = 0; i < debug_counter_number_of_counters - 1; i++)
260 ASSERT_LT (strcmp (map[i].name, map[i + 1].name), 0);
263 void
264 dbgcnt_c_tests ()
266 test_sorted_dbg_counters ();
269 } // namespace selftest
271 #endif /* #if CHECKING_P */