How to get reason of deprecated attribute using libclang

55 Views Asked by At

I am writing a tool that needs to inspect C++ header files. For this I use libclang (actually ClangSharp, which is a .NET wrapper around libclang, but that wrapper is mapping the native types directly onto .NET types). The current version uses libclang version 16.0.6.

I have constructs in the code as follows:

#define ENUM_DATA_TYPE "This enumeration"
#define DEPRECATED_TEXT " should not be used anymore"
#define DEPRECATED_ENUM [[deprecated(ENUM_DATA_TYPE DEPRECATED_TEXT)]]
enum DEPRECATED_ENUM DoNotUseMe
{
    Value = 1
}

For the analysis I want to get the text "This enumeration should not be used anymore" from the attribute using libclang, but I can't seem to get that done.

What I do is that I retrieve the attributes of the declaration (this is C# code, but the mechanism and the names of the libclang datatypes are the same in C++):

string GetDeprecatedText(Decl decl)
{
    string deprecatedText = null;

    foreach (Attr attribute in decl.Attrs)
    {
        if (CX_AttrKind.CX_AttrKind_Deprecated == attribute.Kind)
        {
            // What should I do here to get the text??
        }
    }

    return deprecatedText;
}

The actual object type seems to be an InheritableAttr, but none of the methods for that class (or any of its base classes) will give me the opportunity to retrieve the deprecation reason.

The Attr object does give me a lot of information of where the attribute occurs in the original code. However, then I have the following challenges:

  1. Given the source location: how can I get the original text from that location, without reading the file again and parsing it myself?
  2. When I have the name of the macro being used in that location, I can find the MacroDefinitionRecord for it. With this object, how can I get the fully expanded text for that macro?

My preference would be to be able to get the reason directly from the attribute, but if that is not possible, I would like some guidance in trying to get the information myself via the macro definition.

0

There are 0 best solutions below