summaryrefslogtreecommitdiffstats
path: root/dwarflint/check_die_tree.hh
blob: f8967809d1e208e0ac973ad6e803d4ce0a33f9ea (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
/* Pedantic checking of DWARF files
   Copyright (C) 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/>.  */

#ifndef _CHECK_DIE_TREE_H_
#define _CHECK_DIE_TREE_H_

#include "all-dies-it.hh"
#include "highlevel_check.hh"
#include "check_die_tree_i.hh"

#include <c++/dwarf>

struct die_check_item
{
  virtual checkdescriptor const *descriptor () const = 0;
  virtual ~die_check_item () {}
  virtual die_check *create (highlevel_check_i *check,
			     checkstack &stack, dwarflint &lint) = 0;
};

/// Top-level check that iterates over all DIEs in a file and
/// dispatches per-DIE checks on each one.  Per-DIE checks are written
/// as subclasses of die_check (see below) and registered using
/// reg_die_check (see further below).
class check_die_tree
  : public highlevel_check<check_die_tree>
{
public:
  static void register_check (die_check_item *check);

  static checkdescriptor const *descriptor ()
  {
    static checkdescriptor cd
      (checkdescriptor::create ("check_die_tree")
       .hidden ()
       .description ("A pass over the DIE tree that dispatches to various per-DIE checks.\n"));
    return &cd;
  }

  check_die_tree (checkstack &stack, dwarflint &lint);
};

class die_check
{
public:
  virtual ~die_check () {}
  virtual void die (all_dies_iterator<elfutils::dwarf> const &it) = 0;
};

template <class T>
struct reg_die_check
  : public die_check_item
{
  reg_die_check ()
  {
    check_die_tree::register_check (this);
  }

  virtual die_check *create (highlevel_check_i *check,
			     checkstack &stack, dwarflint &lint)
  {
    return new T (check, stack, lint);
  }

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

private:
  /// The top-level scheduler needs to see per-DIE checks as real
  /// checks, which they are not.  So the per-DIE registrar creates
  /// this check stub that's here only to trick the check_die_tree to
  /// run.  check_die_tree then does the per-DIE check scheduling
  /// itself, down in die_check_context.
  class check_stub
    : public highlevel_check<check_stub>
  {
    check_die_tree *_m_die_tree_check;
  public:
    static checkdescriptor const *descriptor ()
    {
      return T::descriptor ();
    }

    check_stub (checkstack &stack, dwarflint &lint)
      : highlevel_check<check_stub> (stack, lint)
      , _m_die_tree_check (lint.check (stack, _m_die_tree_check))
    {}
  };

  ::reg<check_stub> _m_reg_stub;
};

#endif /* _CHECK_DIE_TREE_H_ */