LibToolingExample icon indicating copy to clipboard operation
LibToolingExample copied to clipboard

Scope issue using rewriter

Open DerekMuk opened this issue 8 years ago • 0 comments

First, thanks for your write up, it has helped a ton while trying to get my head around Clang. I do think there is a bug in example.cpp though. At the end of main we have

// print out the rewritten source code ("rewriter" is a global var.) rewriter.getEditBuffer(rewriter.getSourceMgr().getMainFileID()).write(errs());

It's true that rewriter is a global variable, but the source manager came from CompilerInstance, which is scoped - and is out of scope by the end of main(). This means getSourceMgr in the above line will return an address to a freed object.

I moved these lines to the end of HandleTranslationUnit, where the CompilerInstance is still valid. That seems to work well. Thanks again for the effort you've put into this tutorial.

DerekMuk avatar Dec 08 '17 20:12 DerekMuk