Inserting Text After Brace

I have been working on a source-to-source translator using Clang. I have created a Clang plugin with my own ASTConsumer that mainly uses the Rewriter class for performing code transformations. Just had a quick question.

I am trying to insert some code at the top of the main method, before any other statements. To do this I start by getting the location of the left brace using CompoundStmts getLBracLoc method. This works fine, but when I then try to insert text after that location with Rewriter’s InsertTextAfter, it inserts it before the brace. Is this an expected behavior? If so, any recommendations on how to accomplish what I’m trying to do?

Thanks!
Gabriel

I have been working on a source-to-source translator using Clang. I have created a Clang plugin with my own ASTConsumer that mainly uses the Rewriter class for performing code transformations. Just had a quick question.

I am trying to insert some code at the top of the main method, before any other statements. To do this I start by getting the location of the left brace using CompoundStmts getLBracLoc method. This works fine, but when I then try to insert text after that location with Rewriter’s InsertTextAfter, it inserts it before the brace. Is this an expected behavior?

Yes, insertion inserts before the beginning of the source location.

If so, any recommendations on how to accomplish what I’m trying to do?

You want Preprocessor::getLocForEndOfToken().

  • Doug