help -- how can i know whether a variable has user-defined attributes or not in Clang?

Dear Clang Developers,

I am Shen, a beginner in Clang and LLVM. I hope to add some attributes to some variables, and check which variables include which attributes in my program. For example, I can write a statement as:

int x _attribute(foo); (here “foo” is an attribute defined by myself)

and then how can i check whether x has attribute “foo”? In Clang manual, the macro “__has_attribute” is just used for adding attribute for clang, not for a concrete variable. Can you give me some help?

This is the first time for me to use this way to ask questions about LLVM, if anything wrong please let me know so i can use a better way to get your help.

Thank you very much and have a nice day!


Hi Shen, documentation for __has_attribute says: This function-like macro takes a single identifier argument that is the name of an attribute. It evaluates to 1 if the attribute is supported by the current compilation target, or 0 if not.

So this macro is used to query the compiler support for a specific attribute.

As far as I understand attributes are mostly useful “inside” the compiler but you’d like to annotate your variable and query this attribute from the code? As in:

int x _attribute(foo);

if (__has_attribute(foo, x)) { … some code }

Is that what you’re trying to do?