PLUGIN_FINISH_TYPE for incomplete types and enums

mbndr@posteo.de mbndr@posteo.de
Fri Dec 17 11:51:10 GMT 2021


Hello,

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 
(https://gcc.gnu.org/legacy-ml/gcc-patches/2015-02/msg00016.html) but it 
doesn't seem to work for me.

Plugin code simplified (plugin.cpp):
```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");
         return;
     }

     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):
```cpp
class incomplete;
class clazz {};

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

Compilation and running test:
```sh
$ g++ -shared -I$(gcc -print-file-name=plugin)/include -fPIC -fno-rtti 
-O2 plugin.cpp -o plugin.so
$ gcc -c -Wall -Wextra -fplugin=./plugin.so 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.

Maxi



More information about the Gcc-help mailing list