Issue 54734
Summary cindex.py TranslationUnit skips std::string::operator+=
Labels new issue
Assignees
Reporter MartinsSmirnovs
    I try to find all occurrences of `std::string::operator+=` in my C++ source files using clang's Python cindex. Here is my sample code:
```
#include <iostream>

class A {
public:
  A &operator+=(const int &arg) {
    number += arg;
    return *this;
  }

private:
  int number = 0;
};

int main() {
  std::string str = "test";
  str += "a";

  A obj;
  obj += 3;
}
```

In this example I would like cindex to find `str += "a"` and point to it using it's `Cursor` object. However, it gets skipped. Here is Python code I used:
 ```
from clang.cindex import Cursor, TranslationUnit

translationUnit: TranslationUnit = TranslationUnit.from_source( "main.cpp" )

cursor: Cursor
for cursor in translationUnit.cursor.walk_preorder():
    for token in cursor.get_tokens():
        print( token.spelling, end=" " )
    print()
```

Interestingly, occurrence of `A::operator+=` as ` obj += 3;` was found and printed. Here is output of the script (ignoring `iostream` include that was printed too):
```
class A { public : A & operator += ( const int & arg ) { number += arg ; return * this ; } private : int number = 0 ; } 
public : 
A & operator += ( const int & arg ) { number += arg ; return * this ; } 
A 
const int & arg 
{ number += arg ; return * this ; } 
number += arg 
number 
arg 
arg 
return * this 
* this 
this 
private : 
int number = 0 
0 
int main ( ) { std :: string str = "test" ; str += "a" ; A obj ; obj += 3 ; } 
{ std :: string str = "test" ; str += "a" ; A obj ; obj += 3 ; } 
std :: string str = "test" ; 
std :: string str 
std 
string 
A obj ; 
A obj 
A 
obj 
obj += 3 
obj += 3 
obj 
+= 
+= 
3 
3 
```
As can be seen, only declaration of `std::string str` gets printed, but addition of character "t" on next line gets skipped.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to