[cfe-dev] CXXRecordDecl: How to access AnnoteAttr in case of preprocessor macros?

Marcel Schaible via cfe-dev cfe-dev at lists.llvm.org
Tue Jan 12 14:44:11 PST 2021


Hi,

I am using in my tool annotation to mark specific structure and 
structure elements.

If I pass into the annotate a string directly everything works like 
expected witrh the proposed solution:

     clang::AttrVec attrs = Declaration->getAttrs();

     for ( auto attr : attrs) {
       llvm::outs() << "Found attribute " << attr->getSpelling()
		   << " at "
		   << FullLocation.getSpellingLineNumber() << ":"
		   << FullLocation.getSpellingColumnNumber() << "\n";
	...
      }

But if I use for preprocessor macros attr->getSpelling contains just the 
string "annotate".

The interesting observation is that the dump of CXXRecordDecl contains
prints the AnnotateAttr with the correct resolve string (see below).

How can I access this string? Any idea is appreciated.

Thanks

Marcel

------- Example input source code:
#include <stdint.h>

#define _STRINGIFY(x) #x
#define STRINGIFY(x) _STRINGIFY(x)

#define XR(GROUP) __attribute__((annotate(STRINGIFY(IFX_ROOT_##GROUP))))

struct XR(RUBY)  struct
{
	...
};

-------- Output of CXXRecordDecl dump:
CXXRecordDecl 0x2e14780 <test2.cpp:9:1, line:12:1> line:9:17 struct i_s1 
definition
|-DefinitionData pass_in_registers aggregate standard_layout 
trivially_copyable pod trivial literal
| |-DefaultConstructor exists trivial needs_implicit
| |-CopyConstructor simple trivial has_const_param needs_implicit 
implicit_has_const_param
| |-MoveConstructor exists simple trivial needs_implicit
| |-CopyAssignment trivial has_const_param needs_implicit 
implicit_has_const_param
| |-MoveAssignment exists simple trivial needs_implicit
| `-Destructor simple irrelevant trivial needs_implicit
|-AnnotateAttr 0x2e14898 <line:6:35, col:71> "MY_ROOT_RUBY" <------?????
|-CXXRecordDecl 0x2e37490 <line:9:1, col:17> col:17 implicit struct i_s1
`-FieldDecl 0x2e375f8 <line:11:3, col:25> col:25 s 'uint32_t':'unsigned int'
   `-AnnotateAttr 0x2e37648 <line:7:34, col:69> "MY_TYPE_c_uint32"


Found attribute annotate at 9:1



More information about the cfe-dev mailing list