summaryrefslogtreecommitdiff
path: root/odb/plugin.cxx
blob: b1faa32b0b0855f8abbafab3a945534d85a827cd (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
extern "C"
{
#include "gcc-plugin.h"
}

#include <stdlib.h>
#include <gmp.h>

extern "C"
{
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tree.h"
#include "tree-pass.h"
#include "intl.h"

/* reqs */
#include "tm.h"

/* gcc/ headers. */
#include "diagnostic.h"
#include "c-common.h"
#include "c-pretty-print.h"
#include "tree-iterator.h"
#include "plugin.h"
#include "tree-flow.h"
#include "langhooks.h"
#include "cp/cp-tree.h"
#include "cp/cxx-pretty-print.h"
#include "cp/name-lookup.h"
}

#include <map>
#include <string>
#include <cassert>

using namespace std;

int plugin_is_GPL_compatible;

// TODO:
//
//
// * Can plugin define a macro which can then be tested in the code?
//   A wrapper that calls g++ with plugin can do that easily.
//
// * Will need to disable as many warnings as possible.
//
// * How am I going to handle a case where the type of a private
//   member is also private (i.e., local class or typedef -- fairly
//   common).
//

enum class_access { ca_public, ca_protected, ca_private };
const char* class_access_str[] = {"public", "protected", "private"};

class traverser
{
public:
  traverser ()
      : file_ (main_input_filename)
  {
  }

  void
  traverse (tree scope)
  {
    // First collect all the declarations we are interested in
    // in the line-decl map so that they appear in the source
    // code order.
    //
    collect (scope);
    emit ();
  }

private:
  void
  collect (tree ns)
  {
    cp_binding_level* level = NAMESPACE_LEVEL (ns);
    tree decl = level->names;

    // Collect declarations.
    //
    for (; decl != NULL_TREE; decl = TREE_CHAIN (decl))
    {
      switch (TREE_CODE (decl))
      {
      case TYPE_DECL:
        {
          if (DECL_ARTIFICIAL (decl) &&
              DECL_NAME (decl) != NULL_TREE &&
              TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE &&
              DECL_SOURCE_FILE (decl) == file_)
          {
            decls_[DECL_SOURCE_LINE (decl)] = decl;
          }

          break;
        }
      default:
        {
          /*
          if (!DECL_IS_BUILTIN (decl))
          {
            tree name = DECL_NAME (decl);

            if (name != NULL_TREE)
            {
              warning (0, G_ ("some declaration %s in %s:%i"),
                       IDENTIFIER_POINTER (name),
                       DECL_SOURCE_FILE (decl),
                       DECL_SOURCE_LINE (decl));
            }
            else
            {
              warning (0, G_ ("some unnamed declaration in %s:%i"),
                       DECL_SOURCE_FILE (decl),
                       DECL_SOURCE_LINE (decl));
            }
          }
          */
          break;
        }
      }
    }

    // Traverse namespaces.
    //
    for(decl = level->namespaces; decl != NULL_TREE; decl = TREE_CHAIN (decl))
    {
      if (!DECL_NAMESPACE_STD_P (decl) &&
          !DECL_IS_BUILTIN (decl) &&
          DECL_SOURCE_FILE (decl) == file_)
      {
        tree name = DECL_NAME (decl);

        warning (0, G_ ("namespace declaration %s in %s:%i"),
                 name ? IDENTIFIER_POINTER (name) : "<anonymous>",
                 DECL_SOURCE_FILE (decl),
                 DECL_SOURCE_LINE (decl));

        collect (decl);
      }
    }
  }

  void
  emit ()
  {
    for (decl_map::const_iterator i (decls_.begin ()), e (decls_.end ());
         i != e; ++i)
    {
      tree decl (i->second);

      switch (TREE_CODE (decl))
      {
      case TYPE_DECL:
        {
          tree type = TREE_TYPE (decl);
          tree name = DECL_NAME (decl);

          warning (0, G_ ("class declaration %s in %s:%i"),
                   IDENTIFIER_POINTER (name),
                   DECL_SOURCE_FILE (decl),
                   DECL_SOURCE_LINE (decl));

          emit_class (type);
          break;
        }
      }
    }
  }

  void
  emit_class (tree c)
  {
    // Traverse base information.
    //
    tree bis (TYPE_BINFO (c));
    size_t n (bis ? BINFO_N_BASE_BINFOS (bis) : 0);

    for (size_t i (0); i < n; i++)
    {
      tree bi (BINFO_BASE_BINFO (bis, i));

      class_access a (ca_public);

      if (BINFO_BASE_ACCESSES (bis))
      {
        tree ac (BINFO_BASE_ACCESS (bis, i));

        if (ac == NULL_TREE || ac == access_public_node)
        {
          a = ca_public;
        }
        else if (ac == access_protected_node)
        {
          a = ca_protected;
        }
        else
        {
          assert (ac == access_private_node);
          a = ca_private;
        }
      }

      bool v (BINFO_VIRTUAL_P (bi));
      tree b (BINFO_TYPE (bi));
      tree b_decl (TYPE_NAME (b)); // Typedef decl for this base.

      warning (0, G_ ("\t%s%s base %s"),
               class_access_str[a],
               (v ? " virtual" : ""),
               IDENTIFIER_POINTER (DECL_NAME (b_decl)));
    }

    // Traverse data members.
    //
    for (tree decl (TYPE_FIELDS (c));
         decl != NULL_TREE ;
         decl = TREE_CHAIN (decl))
    {
      //if (DECL_ARTIFICIAL (field))
      //  continue;

      // if (TREE_CODE (field) == TYPE_DECL && TREE_TYPE (field) == c)
      //   continue;

      switch (TREE_CODE (decl))
      {
      case FIELD_DECL:
        {
          if (!DECL_ARTIFICIAL (decl))
          {
            tree name = DECL_NAME (decl);
            tree type = TREE_TYPE (decl);
            tree type_decl = TYPE_NAME (type);

            warning (0, G_ ("\tdata member declaration %s %s in %s:%i"),
                     (type_decl ? IDENTIFIER_POINTER (DECL_NAME (type_decl)) : "?"),
                     IDENTIFIER_POINTER (name),
                     DECL_SOURCE_FILE (decl),
                     DECL_SOURCE_LINE (decl));
            break;
          }
        }
      default:
        {
          /*
          tree name = DECL_NAME (decl);

          if (name != NULL_TREE)
          {
            warning (0, G_ ("\tsome declaration %s in %s:%i"),
                     IDENTIFIER_POINTER (name),
                     DECL_SOURCE_FILE (decl),
                     DECL_SOURCE_LINE (decl));
          }
          else
          {
            warning (0, G_ ("\tsome unnamed declaration in %s:%i"),
                     DECL_SOURCE_FILE (decl),
                     DECL_SOURCE_LINE (decl));
          }
          */
          break;
        }
      }
    }
  }

private:
  typedef map<size_t, tree> decl_map;

  string file_;
  decl_map decls_;
};

extern "C" void
gate_callback (void* gcc_data, void* user_data)
{
  warning (0, G_ ("main file is %s"), main_input_filename);

  if (!errorcount && !sorrycount)
  {
    traverser t;
    t.traverse (global_namespace);
  }

  exit (0);
}

extern "C" int
plugin_init (struct plugin_name_args *plugin_info,
             struct plugin_gcc_version *version)
{
  warning (0, G_ ("starting plugin %s"), plugin_info->base_name);

  // Disable assembly output.
  //
  asm_file_name = HOST_BIT_BUCKET;

  register_callback (plugin_info->base_name,
                     PLUGIN_OVERRIDE_GATE,
                     &gate_callback,
                     NULL);

  return 0;
}