summaryrefslogtreecommitdiff
path: root/dwarflint/messages.cc
blob: 6b6cade84ad988d3de4215bb3361c9b1f3759bb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/* Pedantic checking of DWARF files
   Copyright (C) 2009-2011 Red Hat, Inc.
   This file is part of elfutils.

   This file is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   elfutils is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include "messages.hh"
#include "coverage.hh"
#include "option.hh"

#include <vector>
#include <sstream>
#include <cassert>
#include <cstdarg>
#include <libintl.h>

unsigned error_count = 0;
bool message_context::_m_last_emitted;

bool
message_accept (struct message_criteria const *cri,
		unsigned long cat)
{
  for (size_t i = 0; i < cri->size (); ++i)
    {
      message_term const &t = cri->at (i);
      if ((t.positive & cat) == t.positive
	  && (t.negative & cat) == 0)
	return true;
    }

  return false;
}

namespace
{
  struct cat_to_str
    : public std::vector<std::string>
  {
    cat_to_str ()
    {
      int count = 0;
#define MC(CAT, ID) if (ID > count) count = ID;
      MESSAGE_CATEGORIES
#undef MC

      resize (count + 1);
#define MC(CAT, ID) (*this)[ID] = #CAT;
      MESSAGE_CATEGORIES
#undef MC
    }
  } cat_names;
  size_t cat_max = cat_names.size ();
}


message_category
operator | (message_category a, message_category b)
{
  return static_cast<message_category> ((unsigned long)a | b);
}

message_category &
operator |= (message_category &a, message_category b)
{
  a = a | b;
  return a;
}

std::string
message_term::str () const
{
  std::ostringstream os;
  os << '(';

  bool got = false;
  for (size_t i = 0; i <= cat_max; ++i)
    {
      size_t mask = 1u << i;
      if ((positive & mask) != 0
	  || (negative & mask) != 0)
	{
	  if (got)
	    os << " & ";
	  if ((negative & (1u << i)) != 0)
	    os << '~';
	  os << cat_names[i];
	  got = true;
	}
    }

  if (!got)
    os << '1';

  os << ')';
  return os.str ();
}

std::string
message_criteria::str () const
{
  std::ostringstream os;

  for (size_t i = 0; i < size (); ++i)
    {
      message_term const &t = at (i);
      if (i > 0)
	os << " | ";
      os << t.str ();
    }

  return os.str ();
}

void
message_criteria::operator &= (message_term const &term)
{
  assert ((term.positive & term.negative) == 0);
  for (size_t i = 0; i < size (); )
    {
      message_term &t = at (i);
      t.positive = t.positive | term.positive;
      t.negative = t.negative | term.negative;
      if ((t.positive & t.negative) != 0)
	/* A ^ ~A -> drop the term.  */
	erase (begin () + i);
      else
	++i;
    }
}

void
message_criteria::operator |= (message_term const &term)
{
  assert ((term.positive & term.negative) == 0);
  push_back (term);
}

// xxx this one is inaccessible from the outside.  Make it like &=, |=
// above
/* NEG(a&b&~c) -> (~a + ~b + c) */
message_criteria
operator ! (message_term const &term)
{
  assert ((term.positive & term.negative) == 0);

  message_criteria ret;
  for (size_t i = 0; i < cat_max; ++i)
    {
      unsigned mask = 1u << i;
      if ((term.positive & mask) != 0)
	ret |= message_term ((message_category)(1u << i), mc_none);
      else if ((term.negative & mask) != 0)
	ret |= message_term (mc_none, (message_category)(1u << i));
    }

  return ret;
}

std::ostream &
operator<< (std::ostream &o, message_category cat)
{
  o << '(';

  bool got = false;
  for (size_t i = 0; i <= cat_max; ++i)
    {
      size_t mask = 1u << i;
      if ((cat & mask) != 0)
	{
	  if (got)
	    o << ",";
	  o << cat_names[i];
	  got = true;
	}
    }

  if (!got)
    o << "none";

  return o << ')';
}

std::ostream &
operator<< (std::ostream &o, message_term const &term)
{
  return o << term.str ();
}

std::ostream &
operator<< (std::ostream &o, __attribute__ ((unused)) message_criteria const &criteria)
{
  return o << criteria.str ();
}

/* MUL((a&b + c&d), (e&f + g&h)) -> (a&b&e&f + a&b&g&h + c&d&e&f + c&d&g&h) */
void
message_criteria::operator *= (message_criteria const &rhs)
{
  message_criteria ret;
  for (size_t i = 0; i < size (); ++i)
    for (size_t j = 0; j < rhs.size (); ++j)
      {
	message_term t1 = at (i);
	message_term const &t2 = rhs.at (j);
	t1.positive |= t2.positive;
	t1.negative |= t2.negative;
	if (t1.positive & t1.negative)
	  /* A ^ ~A -> drop the term.  */
	  continue;
	ret |= t1;
      }

  *this = ret;
}

// xxx this one is inaccessible from the outside.  Bind it properly
/* Reject message if TERM passes.  */
void
message_criteria::and_not (message_term const &term)
{
  // xxxxx really??  "!"??
  message_criteria tmp = !message_term (term.negative, term.positive);
  *this *= tmp;
}

static void
wr_verror (locus const &loc, const char *format, va_list ap)
{
  printf ("error: %s", loc.format ().c_str ());
  vprintf (format, ap);
  ++error_count;
}

static void
wr_vwarning (locus const &loc, const char *format, va_list ap)
{
  printf ("%s", loc.format ().c_str ());
  vprintf (format, ap);
  ++error_count;
}

void
wr_error (locus const *loc, const char *format, ...)
{
  va_list ap;
  va_start (ap, format);
  wr_verror (*loc, format, ap);
  va_end (ap);
}

void
wr_message (unsigned long category, locus const *loc,
	    const char *format, ...)
{
  va_list ap;
  va_start (ap, format);
  // Clumsy duplicate filtering. Use format as key.
  bool whether = false;
  message_category cat = (message_category) category;
  wr_message (cat).id (format, whether);
  if (whether && message_accept (&warning_criteria, category))
    {
      if (message_accept (&error_criteria, category))
	wr_verror (*loc, format, ap);
      else
	wr_vwarning (*loc, format, ap);
    }
  va_end (ap);
}

namespace
{
  class nostream: public std::ostream {};
  static nostream nostream;

  std::ostream &
  get_stream ()
  {
    return std::cout;
  }
}

global_opt<unsigned_option>
  dup_threshold_opt ("Threshold for duplicate messages."
		     " Defaults to 16."
		     " Use zero for no limit.",
		     "count", "dups");

namespace
{
  unsigned
  dup_threshold ()
  {
    static unsigned t = dup_threshold_opt.value (16);
    if (t == 0)
      t = -1;
    return t;
  }
}

int
message_count_filter::should_emit (void const *key)
{
  unsigned count = ++_m_counters[key];
  if (count > dup_threshold ())
    return 0;
  else if (count == dup_threshold ())
    return -1;
  else
    return 1;
}

message_context::message_context (message_count_filter *filter,
				  locus const *loc, char const *prefix)
  : _m_filter (filter)
  , _m_loc (loc)
  , _m_prefix (prefix)
{}

std::ostream &
message_context::when (bool whether) const
{
  _m_last_emitted = false;
  if (whether)
    {
      ++error_count;
      _m_last_emitted = true;

      std::ostream &ret = get_stream ();
      ret << _m_prefix;
      if (_m_loc != NULL)
	ret << _m_loc->format () << ": ";
      return ret;
    }
  else
    return nostream;
}

std::ostream &
message_context::when_prev () const
{
  return when (wr_prev_emitted ());
}

std::ostream &
message_context::id (void const *key, bool &whether)
{
  if (_m_filter == NULL)
    return nostream;
  else if (int status = _m_filter->should_emit (key))
    {
      if (status == -1)
	get_stream () << "(threshold [--dups=" << dup_threshold ()
		      << "] reached for the following message)"
		      << std::endl;
      whether = true;
      return when (true);
    }
  else
    return nostream;
}

std::ostream &
message_context::id (void const *key)
{
  bool whether;
  return id (key, whether);
}

std::ostream &
message_context::operator << (char const *message)
{
  return id (message) << message;
}

std::ostream &
message_context::operator << (std::string const &message)
{
  return *this << message.c_str ();
}

std::ostream &
wr_error ()
{
  ++error_count;
  return get_stream () << gettext ("error: ");
}

std::ostream &
wr_error (locus const &loc)
{
  std::string fmt = loc.format ();
  return wr_error () << fmt << ": ";
}

message_context
message_context::filter_message (locus const *loc, message_category category)
{
  if (!message_accept (&warning_criteria, category))
    return message_context (NULL, NULL, NULL);
  else if (message_accept (&error_criteria, category))
    return message_context (message_count_filter::inst (),
			    loc, "error: ");
  else
    return message_context (message_count_filter::inst (),
			    loc, "warning: ");
}

message_context
wr_message (message_category category)
{
  return message_context::filter_message (NULL, category);
}

message_context
wr_message (locus const &loc, message_category category)
{
  return message_context::filter_message (&loc, category);
}

void
wr_format_padding_message (message_category category,
			   locus const &loc,
			   uint64_t start, uint64_t end, char const *kind)
{
  char msg[128];
  wr_message (loc, category)
    << range_fmt (msg, sizeof msg, start, end)
    << ": " << kind << "." << std::endl;
}

void
wr_format_leb128_message (locus const &loc,
			  const char *what,
			  const char *purpose,
			  const unsigned char *begin, const unsigned char *end)
{
  message_category category = mc_leb128 | mc_acc_bloat | mc_impact_3;
  char buf[(end - begin) * 3 + 1]; // 2 hexa digits+" " per byte, and term. 0
  char *ptr = buf;
  for (; begin < end; ++begin)
    ptr += sprintf (ptr, " %02x", *begin);
  wr_message (loc, category)
    << what << ": value " << purpose << " encoded as `"
    << (buf + 1) << "'." << std::endl;
}

void
wr_message_padding_0 (message_category category,
		      locus const &loc,
		      uint64_t start, uint64_t end)
{
  wr_format_padding_message (category | mc_acc_bloat | mc_impact_1,
			     loc, start, end,
			     "unnecessary padding with zero bytes");
}

void
wr_message_padding_n0 (message_category category,
		       locus const &loc,
		       uint64_t start, uint64_t end)
{
  wr_format_padding_message (category | mc_acc_bloat | mc_impact_1,
			     loc, start, end,
			     "unreferenced non-zero bytes");
}

void
wr_reset_counters ()
{
  error_count = 0;
  message_count_filter::inst ()->clear ();
}

bool
wr_prev_emitted ()
{
  return message_context::_m_last_emitted;
}