aboutsummaryrefslogtreecommitdiff
path: root/plugin.cxx
blob: 8a93a5e610f036702b16782f0b70d8a21d9a1dcc (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
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 <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.
//

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

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

  void
  traverse (tree global_scope)
  {
    traverse_namespace (global_scope);
  }

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

    for (; decl != NULL_TREE; decl = TREE_CHAIN (decl))
    {
      switch (TREE_CODE (decl))
      {
        /*
          case FUNCTION_DECL:
          {
          if (DECL_ANTICIPATED (decl))
          break;

          warning (0, G_ ("function declaration %s"),
          IDENTIFIER_POINTER (DECL_NAME (decl)));
          break;
          }
        */

        /*
          case RECORD_TYPE:
          {
          warning (0, G_ ("class declaration %s"),
          IDENTIFIER_POINTER (DECL_NAME (decl)));
          break;
          }
        */

      case TYPE_DECL:
        {
          if (DECL_ARTIFICIAL (decl))
          {
            tree type = TREE_TYPE (decl);
            tree name = DECL_NAME (decl);

            if (name != NULL_TREE && TREE_CODE (type) == RECORD_TYPE)
            {
              warning (0, G_ ("class declaration %s in %s:%i"),
                       IDENTIFIER_POINTER (name),
                       DECL_SOURCE_FILE (decl),
                       DECL_SOURCE_LINE (decl));

              traverse_class (type);
            }
          }

          break;
        }
      case NAMESPACE_DECL:
        {
          tree target = DECL_NAMESPACE_ALIAS (decl);
          assert (target != NULL_TREE);

          warning (0, G_ ("namespace alias declaration %s=%s in %s:%i"),
                   IDENTIFIER_POINTER (DECL_NAME (decl)),
                   IDENTIFIER_POINTER (DECL_NAME (target)),
                   DECL_SOURCE_FILE (decl),
                   DECL_SOURCE_LINE (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;
        }
      }

      //warning (0, G_ ("declaration %u"), (int) (TREE_CODE (decl)));


      //warning (0, G_ ("declaration %s"),
      //         IDENTIFIER_POINTER (DECL_NAME (decl)));
    }

    for(decl = level->namespaces; decl != NULL_TREE; decl = TREE_CHAIN (decl))
    {
      if (DECL_NAMESPACE_STD_P (decl) || DECL_IS_BUILTIN (decl))
        continue;

      tree name = DECL_NAME (decl);

      if (name == NULL_TREE)
      {
        warning (0, G_ ("anonymous namespace declaration in %s:%i"),
                 DECL_SOURCE_FILE (decl),
                 DECL_SOURCE_LINE (decl));
      }
      else
      {
        warning (0, G_ ("namespace declaration %s in %s:%i"),
                 IDENTIFIER_POINTER (name),
                 DECL_SOURCE_FILE (decl),
                 DECL_SOURCE_LINE (decl));
      }

      traverse_namespace (decl);
    }
  }

  void
  traverse_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:
        {
          //if (!DECL_IS_BUILTIN (decl))
          //{
          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:
  string file_;
};

extern "C" void
start_unit_callback (void* gcc_data, void* user_data)
{
  warning (0, G_ ("strating unit processing"));
}

extern "C" void
finish_type_callback (void* gcc_data, void* user_data)
{
  warning (0, G_ ("finished type processing"));
}

extern "C" void
finish_unit_callback (void* gcc_data, void* user_data)
{
  //traverse_namespace (global_namespace);
  //done = true;
  warning (0, G_ ("finished unit processing"));
}

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

  if (!errorcount && !sorrycount)
  {
    collector c;
    c.traverse (global_namespace);
  }

  exit (0);

  // Disable every pass.
  //
  /*
  warning (0, G_ ("disabling pass %s"), current_pass->name);
  *static_cast<int*> (gcc_data) = 0;
  */
}

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

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

  //register_callback (plugin_info->base_name, PLUGIN_START_UNIT, &start_unit_callback, NULL);
  //register_callback (plugin_info->base_name, PLUGIN_FINISH_TYPE, &finish_type_callback, NULL);
  //register_callback (plugin_info->base_name, PLUGIN_FINISH_UNIT, &finish_unit_callback, NULL);
  register_callback (plugin_info->base_name, PLUGIN_OVERRIDE_GATE, &gate_callback, NULL);

  return 0;
}