Issue 108821
Summary [Clang Libtooling] Unexpected Word Splitting
Labels clang
Assignees
Reporter bajinsheng
    I wrote a code based on libtooling to add a simple printf in each IF statement as follows:
```cpp
bool VisitIfStmt(IfStmt *If) {
    FullSourceLoc FullLocation = Context->getFullLoc(If->getBeginLoc());
    if (FullLocation.isValid()) {
      if (Stmt *Then = If->getThen()) {
 SourceLocation StartLoc = Then->getBeginLoc();
        SourceLocation EndLoc = Then->getEndLoc().getLocWithOffset(1);
        IfCounter++;
 std::string NewIfStmt = "";
        // Check if the body is a compound statement
        if (isa<CompoundStmt>(Then)) {
 NewIfStmt += "  printf(\"Executing code block " + std::to_string(IfCounter) + "[\\n\](file://n/)");\n";
 NewIfStmt += TheRewriter.getRewrittenText(SourceRange(StartLoc, EndLoc));
 } else {
          NewIfStmt += "{\n";
          NewIfStmt += "  printf(\"Executing code block " + std::to_string(IfCounter) + "[\\n\](file://n/)");\n";
          NewIfStmt += TheRewriter.getRewrittenText(SourceRange(StartLoc, EndLoc));
 NewIfStmt += "\n}";
        }
        NewIfStmt += "\n}";
 TheRewriter.ReplaceText(SourceRange(StartLoc, EndLoc), NewIfStmt);
 }
    }
    return true;
  }
```


However, for this target code:
```cpp
int main (){
    int a = 1;
    if (a == 1)
        if (a > 0) 
            return true;
}
```
It will be changed into 
![image](https://github.com/user-attachments/assets/c7960f77-f3e6-4d71-bbb6-feec45a2ffbb)


The `return` is incorrectly split.

Full test case, please see here: [test.txt](https://github.com/user-attachments/files/17012840/test.txt)


LLVM version: 36adf8ecedb64047021265a1e1730773d3b3a9e8

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to