incorrect AST

I wrote a small Plugin that inherits the ASTConsumer and
RecursiveASTVisitor classes and writes out the AST with ASTWriter in
the ASTCOnsumer::HandleTranslationUnit method. When I compile down to
Bitcode everything works fine. When I try to add trival Attributs with
the newish VarDecl::addAttr() method (inherited by Decl) on VarDecls
and then compile down to Bitcode I get different results. Besides from
looking different there is no output. How can I fix this?

I use the VisitCallExpr(), VisitDecl() and VisitStmt() methods from
RecursiveASTVisitor to add the attributes. Here is how I write the AST
out. Does the AST need to be "finalized" or something?

Thank you

virtual void HandleTranslationUnit(ASTContext &ctx) {
/* Run */
TraverseDecl(ctx.getTranslationUnitDecl());

            /\* Save AST \*/
            string error\_s;
            const char \*fileName = astFileName\.c\_str\(\);

            raw\_fd\_ostream output\(fileName, error\_s\);
            if\(error\_s\.empty\(\) == true\) \{
                    vector<unsigned char> buf;
                    BitstreamWriter stream\_writer\(buf\);
                    ASTWriter \*ast\_writer = new ASTWriter\(stream\_writer\);

                    ast\_writer\->WriteAST\(ci\->getSema\(\), NULL,

fileName, true, StringRef("wtf"));
for(vector<unsigned char>::iterator it =
buf.begin(); it != buf.end(); ++it)
output << *it;

                    delete ast\_writer;
            \} else \{
                    errs\(\) &lt;&lt; &quot;Error writing AST file: &quot;;
                    errs\(\) &lt;&lt; error\_s &lt;&lt; &quot;\\n&quot;;
            \}

            output\.close\(\);

}

I wrote a small Plugin that inherits the ASTConsumer and
RecursiveASTVisitor classes and writes out the AST with ASTWriter in
the ASTCOnsumer::HandleTranslationUnit method. When I compile down to
Bitcode everything works fine. When I try to add trival Attributs with
the newish VarDecl::addAttr() method (inherited by Decl) on VarDecls
and then compile down to Bitcode I get different results. Besides from
looking different there is no output. How can I fix this?

Adding attributes can change the generated IR, so there may be nothing to "fix" here. I suggest looking at the actual changes to see if they are expected.

I use the VisitCallExpr(), VisitDecl() and VisitStmt() methods from
RecursiveASTVisitor to add the attributes. Here is how I write the AST
out. Does the AST need to be "finalized" or something?

The AST does not need to be "finalized", but if you're modifying the AST directly then you need to be certain that the resulting AST is well-formed. Adding attributes is generally fine.

  - Doug

The output of llvm-dis is below. For some reason they are different
enough to cause problems. I do not understand what is going on. When I
comment out all code that adds attributes it works fine.

function @qv_assignHook_0x7fff99c1d8c0 exists only in left module
function @qv_callHook_0x7fff99c1d8c0 exists only in left module
function @llvm.var.annotation exists only in right module
in function main:
  in block %entry:
    < %"Arg: Shadow" = bitcast i32 (i32)** @"Shadow Variable" to i8*
    < %"Arg: Value" = bitcast i32 (i32)* @f1 to i8*
        %"Arg: Pointer" = bitcast i32 (i32)** %ptr1 to i8*
    > call void @llvm.var.annotation(i8* %ptr11, i8* getelementptr
inbounds ([24 x i8]* @.str6, i32 0, i32 0), i8* getelementptr inbounds
([45 x i8]* @.str7, i32 0, i32 0), i32 9)
    < %"Arg: Function" = bitcast i32 ()* @main to i8*
    < %"Shadow check" = icmp ne i32 (i32)** @"Shadow Variable", %ptr1
    < store i32 (i32)* @f1, i32 (i32)** @"Shadow Variable"
    < call void @qv_assignHook_0x7fff99c1d8c0(i8* %"Arg: Shadow",
i8* %"Arg: Value", i8* %"Arg: Pointer", i8* %"Arg: Function")
        store i32 (i32)* @f1, i32 (i32)** %ptr1, align 8
        %tmp = load i32 (i32)** %ptr1, align 8
    < %"Arg: Shadow1" = bitcast i32 (i32)** @"Shadow Variable" to i8*
    < %"Arg: Value2" = bitcast i32 (i32)* %tmp to i8*
    < %"Arg: Pointer3" = bitcast i32 (i32)* %tmp to i8*
    < %"Arg: Function4" = bitcast i32 ()* @main to i8*
    < call void @qv_callHook_0x7fff99c1d8c0(i8* %"Arg: Shadow1", i8*
%"Arg: Value2", i8* %"Arg: Pointer3", i8* %"Arg: Function4")