Questions tagged [clang-ast-matchers]

67 questions
2
votes
1 answer

How to reuse a clang AST matcher?

I'm using AST matchers from lib clang to ensures that some code is present in the body of a foo function. So all my matchers starts like this: auto matcher1 = functiondecl(hasname("foo"), hasdescendant(...)))); auto matcher2 =…
Dorian
  • 490
  • 2
  • 10
2
votes
0 answers

How to check variable type given a clang::VarDecl?

I am writing a clang-tidy check to automate renaming of some variables of a certain type. I can successfully match their declarations with matcher varDecl(hasType(asString("class MyType"))). But then I want to rename these variables and their…
Mikhail
  • 20,685
  • 7
  • 70
  • 146
2
votes
1 answer

Clang AST: access member function templates from CXXRecordDecl

Say I have a dummy class like this: class Stack { public: template void push(T val) { (void)val; } template T pop() { return 0; } bool empty() const { return true; } }; The AST dump of which looks like: …
Peter
  • 2,919
  • 1
  • 16
  • 35
2
votes
0 answers

Using tools to create a new namespace

I have a large codebase filled with names such as MyCompanyMyDeptMyType. This is making the code rather hard to read. I would like to use a tool to move all these names to a nested namespace MyCompany::MyDept. My header files would look…
Touloudou
  • 2,079
  • 1
  • 17
  • 28
2
votes
0 answers

matching a @import declaration with ASTMatchers

I'm trying to match objc @import declarations using AST-matchers for a custom tool I'm creating; I'm using Result.Context->local_imports() but although I have @import statements in my source code and I'm passing the fmodules flag, the iterator…
2
votes
1 answer

Clang AST Matchers: How to match on lambda captured variables?

How can I match against variables in a lambda that are defined outside of the lambda and captured by reference? The problem I'm trying to solve: I have a database transaction system whose code looks something like this: std::set values; auto f…
Hesky Fisher
  • 1,145
  • 7
  • 14
2
votes
1 answer

How to use clang ast matcher to match a typedef

I'm writing a checker for clang-tidy, which checks cast between int and pointer. for example, for code: int val = 0xbaddeef; char* ptr = (char*)val; I want to fix it to: char* ptr = (char*)(uintptr_t)val; But if a is already uintptr_t, I don't…
2
votes
1 answer

How to parse only user defined source files with clang tools

I am writing a clang tool, yet I am quite new to it, so i came across a problem, that I couldn't find in the docs (yet). I am using the great Matchers API to find some nodes that I will later want to manipulate in the AST. The problem is, that the…
Julian
  • 525
  • 5
  • 19
2
votes
1 answer

clang AST matching ostream <<

I'm trying to write a clang-tidy check for certain scenarios involving streaming. Consider this simple function: #include #include void foo(std::ostream& os, uint8_t i) { os << i; os << 4 << i; } If I run clang-query…
Barry
  • 286,269
  • 29
  • 621
  • 977
1
vote
1 answer

How to match a struct member access using clang AST matcher?

I am trying to write a clang-tidy check to rename struct members. For this, I need to match MemberExpr nodes that access a certain member, from certain structs. In the following code, it should only match all uses of member on types S1 and S2 (8…
maarten
  • 336
  • 2
  • 10
1
vote
1 answer

Can Clang AST get the virtual function table?

I tried using CXXRecordDecl, to print the virtual functions, but they are in declaration order, which may not be the order actually in the vtable. sample code namespace test { class Foo { public: virtual void v_func() {} virtual void…
zcfh
  • 101
  • 1
  • 9
1
vote
1 answer

TransformerClangTidyCheck drops required parentheses around bound `expr()`

I'd like to rename a function on one of the classes: class Container { public: int count() const; // old int size() const; // new }; So I've written a TransformerClangTidyCheck along the lines of the tutorial…
Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90
1
vote
2 answers

Clang AST matching method call on class, derived class or typedef to either

I have a matcher that works perfectly for matching operator() calls on instances of a class or classes derived from that class. For example, it matches the final line of: class MyBase { void operator()(...) {} }; MyBase b; b(parameters); using a…
Mike Crowe
  • 642
  • 6
  • 18
1
vote
0 answers

How to insert statements at the beginning of a function block using Clang AST?

I am writing a source to source transformer using clang. I want to insert one flag variable for each parameter in a function. So if my original source is like below: int f(int x, int y) { // do something ... return 0; } I want to transform…
Sabya
  • 11,534
  • 17
  • 67
  • 94
1
vote
0 answers

Matching multiple arguments to a particular function individually with Clang AST matcher

I'm trying to match unnecessary calls to c_str() when calling functions that are happy to take the std::string directly so that I can remove the unnecessary call so that I can write a clang-tidy check to automatically turn statements…
Mike Crowe
  • 642
  • 6
  • 18