summaryrefslogtreecommitdiff
path: root/dwarflint/coverage.cc
blob: 9ce6954cdc6df116f5e94cd10ff2b58e01c7696a (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
/* Implementation of coverage analysis.

   Copyright (C) 2008, 2009, 2010 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/>.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "coverage.hh"
#include "pri.hh"

#include <stdbool.h>
#include <assert.h>
#include <string.h>
#include <inttypes.h>

coverage::const_iterator
coverage::find (uint64_t start) const
{
  assert (!empty ());

  size_t a = 0;
  size_t b = size ();

  while (a < b)
    {
      size_t i = (a + b) / 2;
      cov_range const &r = at (i);

      if (r.start > start)
	b = i;
      else if (r.start < start)
	a = i + 1;
      else
	return begin () + i;
    }

  return begin () + a;
}

coverage::iterator
coverage::find (uint64_t start)
{
  const_iterator it = const_cast<coverage const *> (this)->find (start);
  return begin () + (it - begin ());
}

void
coverage::add (uint64_t start, uint64_t length)
{
  cov_range nr = (struct cov_range){start, length};
  if (empty ())
    {
      push_back (nr);
      return;
    }

  iterator r_i = find (start);

  cov_range *to_insert = &nr;
  cov_range *coalesce = &nr;

  // Coalesce with previous range?
  if (r_i > begin ())
    {
      iterator p_i = r_i - 1;
      if (coalesce->start <= p_i->start + p_i->length)
	{
	  uint64_t coalesce_end = coalesce->start + coalesce->length;
	  if (coalesce_end > p_i->start + p_i->length)
	    {
	      p_i->length = coalesce_end - p_i->start;
	      coalesce = &*p_i;
	    }
	  else
	    coalesce = NULL;
	  to_insert = NULL;
	}
    }

  // Coalesce with one or more following ranges?
  if (coalesce != NULL && r_i != end ())
    {
      iterator p_i = r_i;
      while (p_i != end ()
	     && coalesce->start + coalesce->length >= p_i->start)
	{
	  uint64_t p_end = p_i->start + p_i->length;
	  if (p_end > coalesce->start + coalesce->length)
	    coalesce->length = p_end - coalesce->start;
	  if (to_insert != NULL)
	    {
	      *p_i = *to_insert;
	      to_insert = NULL;
	      coalesce = &*p_i;
	      assert (p_i == r_i);
	      ++r_i; // keep this element
	    }
	  ++p_i;
	}
      if (p_i > r_i)
	erase (r_i, p_i);
    }

  if (to_insert != NULL)
    {
      size_t idx = r_i - begin ();
      insert (begin () + idx, *to_insert);
    }
}

bool
coverage::remove (uint64_t start,
		  uint64_t length)
{
  uint64_t a_end = start + length;
  if (empty () || start == a_end)
    return false;

  iterator r_i = find (start);
  iterator erase_begin_i = end ();
  iterator erase_end_i = r_i; // end exclusive
  bool overlap = false;

  // Cut from previous range?
  if (r_i > begin ())
    {
      iterator p_i = r_i - 1;
      if (start < p_i->start + p_i->length)
	{
	  uint64_t r_end = p_i->start + p_i->length;
	  // Do we cut the beginning of the range?
	  if (start == p_i->start)
	    p_i->length = a_end >= r_end ? 0 : r_end - a_end;
	  else
	    {
	      p_i->length = start - p_i->start;
	      // Do we shoot a hole in that range?
	      if (a_end < r_end)
		{
		  add (a_end, r_end - a_end);
		  return true;
		}
	    }

	  overlap = true;
	  if (p_i->length == 0)
	    erase_begin_i = p_i;
	}
    }

  if (erase_begin_i == end ())
    erase_begin_i = r_i;

  // Cut from next range?
  while (r_i < end () && r_i->start < a_end)
    {
      overlap = true;
      if (a_end >= r_i->start + r_i->length)
	{
	  ++erase_end_i;
	  ++r_i;
	}
      else
	{
	  uint64_t end0 = r_i->start + r_i->length;
	  r_i->length = end0 - a_end;
	  r_i->start = a_end;
	  assert (end0 == r_i->start + r_i->length);
	}
    }

  // Did we cut out anything completely?
  if (erase_end_i > erase_begin_i)
    erase (erase_begin_i, erase_end_i);

  return overlap;
}

bool
coverage::is_covered (uint64_t start, uint64_t length) const
{
  if (empty ())
    return false;

  const_iterator r_i = find (start);
  uint64_t a_end = start + length;
  if (r_i < end ())
    if (start >= r_i->start)
      return a_end <= r_i->start + r_i->length;

  if (r_i > begin ())
    {
      --r_i;
      return a_end <= r_i->start + r_i->length;
    }

  return false;
}

char *
range_fmt (char *buf, size_t buf_size, uint64_t start, uint64_t end)
{
  std::stringstream ss;
  ss << pri::range (start, end);
  std::string s = ss.str ();
  strncpy (buf, s.c_str (), buf_size);
  return buf;
}

namespace
{
  bool overlaps (uint64_t start, uint64_t end, cov_range const &r)
  {
    return (start >= r.start && start < r.start + r.length)
      || (end > r.start && end <= r.start + r.length)
      || (start < r.start && end > r.start + r.length);
  }
}

bool
coverage::is_overlap (uint64_t start, uint64_t length) const
{
  if (empty ())
    return false;
  if (length == 0)
    return is_covered (start, length);

  uint64_t a_end = start + length;
  const_iterator r_i = find (start);

  if (r_i < end () && overlaps (start, a_end, *r_i))
    return true;

  if (r_i > begin ())
    return overlaps (start, a_end, *--r_i);

  return false;
}

bool
coverage::find_holes (uint64_t start, uint64_t length,
		      bool (*hole)(uint64_t start, uint64_t length,
				   void *user_data),
		      void *user_data) const
{
  if (length == 0)
    return true;

  if (empty ())
    return hole (start, length, user_data);

  if (start < front ().start)
    if (!hole (start, front ().start - start, user_data))
      return false;

  for (size_t i = 0; i < size () - 1; ++i)
    {
      uint64_t end_i = at (i).end ();
      if (!hole (end_i, at (i+1).start - end_i, user_data))
	return false;
    }

  if (start + length > back ().end ())
    {
      uint64_t end_last = back ().end ();
      return hole (end_last, start + length - end_last, user_data);
    }

  return true;
}

bool
coverage::find_ranges (bool (*cb)(uint64_t start, uint64_t length, void *data),
		       void *user_data) const
{
  for (const_iterator it = begin (); it != end (); ++it)
    if (!cb (it->start, it->length, user_data))
      return false;

  return true;
}

void
coverage::add_all (coverage const &other)
{
  for (size_t i = 0; i < other.size (); ++i)
    add (other[i].start, other[i].length);
}

bool
coverage::remove_all (coverage const &other)
{
  bool ret = false;
  for (size_t i = 0; i < other.size (); ++i)
    if (remove (other[i].start, other[i].length))
      ret = true;
  return ret;
}

coverage
coverage::operator+ (coverage const &rhs) const
{
  coverage ret = *this;
  ret.add_all (rhs);
  return ret;
}

coverage
coverage::operator- (coverage const &rhs) const
{
  coverage ret = *this;
  ret.remove_all (rhs);
  return ret;
}


bool
cov::_format_base::fmt (uint64_t start, uint64_t length)
{
  if (_m_seen)
    _m_os << _m_delim;
  _m_os << pri::range (start, start + length);
  _m_seen = true;
  return true;
}

bool
cov::_format_base::wrap_fmt (uint64_t start, uint64_t length, void *data)
{
  _format_base *self = static_cast <_format_base *> (data);
  return self->fmt (start, length);
}

cov::_format_base::_format_base (std::string const &delim)
  : _m_delim (delim),
    _m_seen (false)
{}

cov::format_ranges::format_ranges (coverage const &cov,
				   std::string const &delim)
  : _format_base (delim)
{
  cov.find_ranges (&wrap_fmt, this);
}

cov::format_holes::format_holes (coverage const &cov,
				 uint64_t start, uint64_t length,
				 std::string const &delim)
  : _format_base (delim)
{
  cov.find_holes (start, length, &wrap_fmt, this);
}