0

I'm trying to refactor if-else branches and am unable to generate accurate Replacements for it. I try to fetch nodes using:

clang::IfStmt *ifstmt = const_cast<clang::IfStmt *>(
                result.Nodes.getNodeAs<clang::IfStmt>("if_simple_else_bind_name"))

and then calculate its source range using

ifstmt->getSourceRange()

However, I find that after using this SourceRange for calculating the start and end for the branch sometimes misses a semicolon for cases like these:

if(cond) {
     return a;
}
else
     return b; <-- here

How do I find the correct source range and further on generate the correct replacement rewriting the whole branch regardless of whether there are any braces or simple statements?

Any help would be highly appreciated!

gPats
  • 315
  • 2
  • 17
  • Does this answer your question? [Why is the source location end off by two characters for a statement ending in a semicolon?](https://stackoverflow.com/questions/37320484/why-is-the-source-location-end-off-by-two-characters-for-a-statement-ending-in-a) – Scott McPeak Aug 24 '23 at 17:34

0 Answers0