简体   繁体   中英

locate constructor line number in cpp source with clang-query

I'm looking to get the line number for the last line of the constructor with clang-query, does anyone know how to get there?

i have a lot of source files to go through, and needing to update the constructors. i spent time trying to parse the source file with regex, and it works okay with a lot of corner cases. so this was the next item on the list.

I tried search around the the web and stack overflow, but i couldnt find too much and at a bit of a lost for how the language parser works. in addition, i played with this tool, http://ce.steveire.com/ , with m cxxConstructorDecl() being my input to the clang-query, but it gives me matches for 4 lines that contains "MyClass" and it only gives me the line of the first line of the constructor. any help is appreciated!

class MyClass {       // The class
  public:             // Access specifier
    int myNum;        // Attribute (int variable)

    MyClass (int in_val);

};

// Constructor definition outside the class
MyClass::MyClass(int in_val) {

  myNum = in_val;
  // <<< this line >>>
}

I found this amazing tutorial that helped me dive into this AST world a little more. https://devblogs.microsoft.com/cppblog/exploring-clang-tooling-part-2-examining-the-clang-ast-with-clang-query/

so at the end, the answer to my question is: m cxxConstructorDecl(has(compoundStmt().bind("my_constructor_statement")))

which produced this result. note the lines and columns numbers are different since i splitted up the fake.cpp to have a header file.

CompoundStmt 0x20b9e18 </home/ME/fake.cpp:4:30, line:7:1>
`-BinaryOperator 0x20b9df8 <line:6:3, col:11> 'int' lvalue '='
  |-MemberExpr 0x20b9d90 <col:3> 'int' lvalue ->myNum 0x20b9628
  | `-CXXThisExpr 0x20b9d80 <col:3> 'class MyClass *' implicit this
  `-ImplicitCastExpr 0x20b9de0 <col:11> 'int' <LValueToRValue>
    `-DeclRefExpr 0x20b9dc0 <col:11> 'int' lvalue ParmVar 0x20b98b0 'in_val' 'int'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM