summaryrefslogtreecommitdiffstats
path: root/dwarflint/checks.hh
blob: a29f83cbc7e227495f47b9b7abbd1312973ed77c (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
/*
   Copyright (C) 2009,2010,2011 Red Hat, Inc.
   This file is part of Red Hat elfutils.

   Red Hat elfutils 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; version 2 of the License.

   Red Hat 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 Red Hat elfutils; if not, write to the Free Software Foundation,
   Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.

   Red Hat elfutils is an included package of the Open Invention Network.
   An included package of the Open Invention Network is a package for which
   Open Invention Network licensees cross-license their patents.  No patent
   license is granted, either expressly or impliedly, by designation as an
   included package.  Should you wish to participate in the Open Invention
   Network licensing program, please visit www.openinventionnetwork.com
   <http://www.openinventionnetwork.com>.  */

#ifndef DWARFLINT_CHECKS_HH
#define DWARFLINT_CHECKS_HH

#include "where.h"
#include "dwarflint.hh"
#include "checkdescriptor.hh"
#include "messages.hh"
#include <string>
#include <cassert>

struct check_base
{
  struct failed {};
  struct unscheduled: public failed {};
  virtual ~check_base () {}
};

template<class T>
class check
  : public check_base
{
private:
  template <class X>
  friend X *dwarflint::check (checkstack &stack);
  static void const *key ()
  {
    return reinterpret_cast <void const *> (&key);
  }
};

struct reporter
{
  checkstack const &stack;
  checkdescriptor const &cd;

  reporter (checkstack const &s, checkdescriptor const &a_cd);
  void operator () (char const *what, bool ext = false);
};

template <class T>
T *
dwarflint::check (checkstack &stack)
{
  void const *key = T::key ();
  T *c = static_cast <T *> (find_check (key));

  if (c == NULL)
    {
      checkdescriptor const &cd = *T::descriptor ();

      struct popper {
	checkstack &guard_stack;
	popper (checkstack &a_guard_stack) : guard_stack (a_guard_stack) {}
	~popper () { guard_stack.pop_back (); }
      };

      // Put a marker there indicating that we are trying to satisfy
      // that dependency.
      bool inserted
	= _m_checks.insert (std::make_pair (key, (T *)marker)).second;
      assert (inserted || !"duplicate key");

#define FAIL					\
      /* Put the anchor in the table.  */	\
	_m_checks[key] = NULL;			\
	report ("FAIL")

      reporter report (stack, cd);
      try
	{
	  stack.push_back (&cd);
	  popper p (stack);

	  if (!_m_rules.should_check (stack))
	    throw check_base::unscheduled ();

	  // Now do the check.
	  c = new T (stack, *this);
	}
      catch (check_base::unscheduled &e)
	{
	  report ("skipped");
	  _m_checks.erase (key);
	  throw;
	}
      catch (check_base::failed &e)
	{
	  // We can assume that the check emitted error message.
	  FAIL;
	  throw;
	}
      catch (std::exception &e)
	{
	  wr_error () << "A check failed: " << (cd.name () ?: "(nil)") << ": "
		      << e.what () << std::endl;
	  FAIL;
	  throw check_base::failed ();
	}
      catch (...)
	{
	  wr_error () << "A check failed: " << (cd.name () ?: "(nil)") << "."
		      << std::endl;
	  FAIL;
	  throw check_base::failed ();
	}

#undef FAIL

      report ("done");

      // On success, put the actual check object there instead of the
      // marker.
      _m_checks[key] = c;
    }
  return c;
}

template <class T>
inline T *
dwarflint::toplev_check (checkstack &stack,
			 __attribute__ ((unused)) T *tag)
{
  try
    {
      return check<T> (stack);
    }
  catch (check_base::failed const &f)
    {
      return NULL;
    }
}

template <class T>
struct reg
  : public dwarflint::check_registrar::item
{
  reg ()
  {
    dwarflint::check_registrar::inst ()->add (this);
  }

  virtual void run (checkstack &stack, dwarflint &lint)
  {
    lint.toplev_check <T> (stack);
  }

  virtual checkdescriptor const *descriptor () const
  {
    return T::descriptor ();
  }
};

#endif//DWARFLINT_CHECKS_HH