[llvm-dev] AnnotateAttribute on Classes

Robert Olliff via llvm-dev llvm-dev at lists.llvm.org
Mon Sep 28 14:06:55 PDT 2015


Greetings,

Before I go around creating bugs and what not, I'd like to some
feedback on a problem I'm having.

Problem Definition:
    Class annotations are not emitted as part of @llvm.global.annotations
    It is expected that, since CXXRecordDecl is a TopLevel declaration,
    its annotations should appear as part of @llvm.global.annotations

Reproduction:
    // ClassAnnotation.cpp

    // class annotation
    class __attribute__((annotate("ClassAnnotation"))) Foo {
    public:
        int Function();
    };

    int main() {
        Foo* f = new Foo();
        return f->Function();
    }
    // EOF

    clang -cc1 -S -emit-llvm ClassAnnotation.cpp -o - | grep annotation
    <nothing,expecting annotations>

Amateur Analysis:
    Facts:

    1.  The class annotation is recognized as valid syntax and included in
        the AST (See Exhibit A)

    2.  The class annotation is not included in the llvm module emission
        (See Exhibit B)

    3.  All global annotations are associated with a GlobalValue
        (i.e. llvm::Function, llvm::GlobalVariable)
        (See Exhibit C)

    Theory:
    a GlobalValue is created from a ValueDecl (i.e. FunctionDecl, VarDecl)
    CXXRecordDecl is not a ValueDecl, it is a TypeDecl, and thus cannot be
    associated with a global annotation.

    Solution:
    Unknown

Exhibits:

    Exhibit A
        AnnotateAttr is recognized as being a child node of CXXRecordDecl

        clang -cc1 -S -ast-dump ClassAnnotation.cpp
        TranslationUnitDecl 0x4ac2358 <<invalid sloc>> <invalid sloc>
        |-TypedefDecl 0x4ac2648 <<invalid sloc>> <invalid sloc> implicit
        |-CXXRecordDecl 0x4ac2708 <ClassAnnotation.cpp:4:1, line:7:1>
        | |-AnnotateAttr 0x4ac2780 <col:22, col:48> "ClassAnnotation"
        | |-CXXRecordDecl 0x4ac2820 <col:1, col:52> col:52 implicit class Foo
        | |-AccessSpecDecl 0x4ac2880 <line:5:1, col:7> col:1 public
        ...<truncated for bevity>

    Exhibit B
        The AnnotateAttr object was not emitted in the IR

        clang -cc1 -S -emit-llvm ClassAnnotation.cpp -o -| grep annotation
        <nothing,expecting annotations>

    Exhibit C
        This function is called after GV has been created from D via
            EmitGlobalFunctionDefinition
            EmitGlobalVarDefinition

        void CodeGenModule::AddGlobalAnnotations(const ValueDecl *D,
                                             llvm::GlobalValue *GV)


More information about the llvm-dev mailing list