PLUGIN_FINISH_TYPE for incomplete types and enums
Fri Dec 17 11:51:10 GMT 2021


I'm developing an analysis plugin for C++ code in GCC andwanted to get 
all types in a source unit (class, struct, enum, union) but my code does 
not work for enums and incomplete types.

1) Is there a way to get incomplete types instead of an error_mark_node?
    My expected behavior: I can check that with COMPLETE_TYPE_P
2) Is there a way to also get enums working?
    There was a patch in 2015 
( but it 
doesn't seem to work for me.

Plugin code simplified (plugin.cpp):
#include "gcc-plugin.h"
#include "plugin.h"
#include "tree.h"
#include <cstdio>

int plugin_is_GPL_compatible;

static void cb_type(void *gcc_data, void *user_data)
     tree type = (tree) gcc_data;
     if (type == error_mark_node) {
         fprintf(stderr, "error handling type\n");

     const char *name = IDENTIFIER_POINTER(DECL_NAME(TYPE_NAME(type)));
     fprintf(stderr, "found type: %s\n", name);

int plugin_init(plugin_name_args *plugin_info, plugin_gcc_version *version)
     register_callback(plugin_info->base_name, PLUGIN_FINISH_TYPE, 
cb_type, NULL);
     return 0;

Test file (test.cpp):
class incomplete;
class clazz {};

enum my_enum { ITEM };
enum class my_enum_class { ITEM };

Compilation and running test:
$ g++ -shared -I$(gcc -print-file-name=plugin)/include -fPIC -fno-rtti 
-O2 plugin.cpp -o
$ gcc -c -Wall -Wextra -fplugin=./ test.cpp
error handling type
found type: clazz
The "error handling type" refers to the incomplete class.

I'm using GCC 11.1.0 but tested it also on 5, 6, 7, 9, 10.

I am grateful for any help. Thanks in advance.


More information about the Gcc-help mailing list