hi.
I'm trying to develop a gcc plugin which will support a new attribute.
I want my new attribute to receive as an argument, an identifier. it
should looks like that:
__attribute__ ((attribute_name(identifier_name_as_arg)))
I know that the gcc attribute "access" can get such an argument but I
didn't manage to do it for my own attribute.
Any idea how I could do it?
I'm attaching an example of my code and its output. if you replace my
attribute name with "access" (in test.c) it will work without error:
//plugin.cc:
#include "gcc-plugin.h"
#include "plugin-version.h"
#include <iostream>
#include <tree.h>
int plugin_is_GPL_compatible;
static tree
coustom_attribute_handler(tree *node, tree name, tree args,
int flags, bool *no_add_attrs){
std::cout << "my custom attribute plugin executed!" << std::endl;
return NULL_TREE;
}
static struct attribute_spec my_attr =
{"custom", 1, 3, false, true, true, false, coustom_attribute_handler, NULL };
static void
register_attributes(void *event_data, void *data){
register_attribute(&my_attr);
}
int plugin_init(struct plugin_name_args *plugin_info,
struct plugin_gcc_version *version){
if (!plugin_default_version_check(version, &gcc_version)){
return 1;
}
register_callback(plugin_info->base_name, PLUGIN_ATTRIBUTES,
register_attributes, NULL);
return 0;
}
//test.c:
__attribute__ ((custom(write_only, 1)))
void foo(char *);
int main(void){
return 0;
}
Execution:
GCC_PLUGINS=`gcc -print-file-name=plugin`
g++ -I$GCC_PLUGINS/include -fPIC -shared -fno-rtti -O2 plugin.cc -o plugin.so
sudo cp plugin.so $GCC_PLUGINS/plugin.so
gcc test.c -fplugin=plugin -o test.o
output:
test.c:3:24: error: ‘write_only’ undeclared here (not in a function)
3 | __attribute__ ((custom(write_only, 1)))
| ^~~~~~~~~~
my custom attribute plugin executed!
Thanks, didi.