End location of DeclStmt

Hi all,

My goal is to remove a DeclStmt from the beginning to end (including the semicolon) using the Rewriter.
However, calling getEnd() on a DeclStmt does not give the correct location. Is there a way to get the end location properly?

Hi Pardis,

which location does the getEnd() return?
I assume it’s one byte to early: You can do offset-calculations with SourceLocation, therefore YourStmt->getEnd().getLocWithOffset(1) returns one element past the end-location.
Be careful with corner-cases as the new Location might be invalid, in a Macro-Range (isMarcoID()) and so on. Please note, that the DeclStmt might not always have a semicolon after itself.
E.g. int my_variable = 42 /* This is my variable */; could be such a case. You might want to take a look clang-tidy/utils/LexerUtils.{h,cpp} as it has some functionality to deal with such situations.

Best, Jonas