Questions tagged [clang-ast-matchers]
67 questions
1
vote
2 answers
Clang AST Matchers: How to find calls to a perfectly forwarding function called with rvalues?
Given a function template such as:
template void function(T &&t) { /*...*/ }
how do I find calls to the function that pass rvalues:
function(1); // MATCH
int i;
function(i); // SKIP
int foo();
function(foo()); // MATCH
...
You get the…

Marc Mutz - mmutz
- 24,485
- 12
- 80
- 90
0
votes
1 answer
Writing AST Matcher expressions using an AST tree dump as a guide
I am new to clang-query and I need help to understand how to build ASTMatcher expressions for a code profiling tool I am trying to create. Eventually I would like to combine these AST matchers with a libTooling application to rewrite the source…

johnco3
- 2,401
- 4
- 35
- 67
0
votes
1 answer
How to get the details of DeclRefExpr from VarDecl in clang?
My ast node looks like this:
|-VarDecl 0x158f5190 col:6 used ptr1 'int *' cinit
| |-UnaryOperator 0x158f62c0 'int *' prefix '&' cannot overflow
| | `-DeclRefExpr 0x158f62a0 col:14 'int' lvalue Var 0x158f50f0 'foo'…

RBB
- 1
- 1
0
votes
1 answer
How do I get clang-query or AST to recognize the underlying pair / type inside a map?
I'd like to create an AST matcher for the following code snippet
#include
int main() {
int a = 3333, b = 4444, c = 5555;
std::unordered_map unorderedMapPtr = {{&a, b}, {&b, c}};
for (auto mapIter : unorderedMapPtr)…

ginginsha
- 142
- 1
- 11
0
votes
0 answers
How can I match a CXXTypeidExpr with clang-query?
Minimum "working" example on compiler explorer: https://godbolt.org/z/r6jebPWMe
I have this C++ code:
#include
namespace {
struct Base {
virtual ~Base() {}
};
struct Derived : Base {
virtual void name() {}
};
} //…
0
votes
1 answer
(clang AST matchers) How to match consecutive statements?
I'm planning a bunch of refactorings on a large code base that I'd like to automate using Clang tooling. For this, I'm trying to write a Clang AST Matcher expression.
Specifically, I'm trying to match pairs of statements that I'd like to replace…

Marc Mutz - mmutz
- 24,485
- 12
- 80
- 90
0
votes
0 answers
How to get C++ mangled Function name in clang by using void MangleContext::mangleName(GlobalDecl GD, raw_ostream &Out) API?
By using FrontendAction and ASTConsumer API, we can print the name of each function, such as:
bool VisitFunctionDecl(FunctionDecl *f) {
DeclarationName DeclName = f->getNameInfo().getName();
std::string FuncName = DeclName.getAsString();
…

StudyingCui
- 21
- 2
0
votes
1 answer
how to modify forStmt condtion by clang tools?
I want to modify C source code like this:
I wany to modify loop condition in AST, how can I do this?
for example:
for(int i = 0; i < 10; i ++){...}
change to :
for (int i = 0; i < 1; i ++){...}
I get the forStmt from AST by ASTMatcher in clang…

haoshuo xu
- 1
- 2
0
votes
1 answer
Is there a LLVM Matcher for any conditional?
Is there a LLVM AST matcher for the use of a C conditional? I know there is the hasCondition() option for ifStmt, but that is only good for an if statement. In particular, I'm looking to match for a boolean condition that has no operator (e.g. if…

Robert Ankeney
- 91
- 5
0
votes
1 answer
AST MATCHER:How to match un init double param in constructor
I 'm having a question to match uninit double field in constructor.
Given the code below
class un_init_double {
public:
un_init_double() {
init_param_ = 0;
}
bool compare(un_init_double& other) {
if (other.un_init_param_ ==…

Doggy
- 1
- 1
- 1
0
votes
1 answer
Create a new LabelDecl and add to Clang AST
I have a problem inserting LabelDecl. My goal is to create a goto statement in the program and to do that, first I need to create LabelDecl and add it to Clang AST.
There is an API to Create Label, but I am not able to get the IdentifierInfo.
static…

Bernard Nongpoh
- 1,028
- 11
- 20
0
votes
1 answer
Clang matcher that hasGlobalStorage but is non-static
I was trying to compose a matcher that finds global variables that does not have static storage
This is what I have so far
varDecl(hasGlobalStorage(), isDefinition())
Accoring to Clang AST Matcher Reference
hasGlobalStorage will also match static…

fskoras
- 68
- 7
0
votes
1 answer
How to detect SEXP type by using clang-tool
I want to detect "SEXP" return type function by using clang-tool , and print it .
My clang tool :
//define vistor
class myVisitor : public RecursiveASTVisitor{
private:
ASTContext *ast_c ;
public:
explicit…

Moriarty
- 9
- 3
0
votes
1 answer
Matching the argument passed to a function in a ForStmt's Cond using Clang AST Visitors
I am trying to match the value of an integer passed to a ForStmt's condition. However, the loopbound is a call to a FunctionDecl and I don't seem to get this with ParamVarDecl.
MWE:
My Test Example:
void testASTVistor (int N) {
N = 123;
for (int…

Amir
- 1,348
- 3
- 21
- 44
0
votes
1 answer
Need Explaination of Clang AST Viewer form my Example
I tried to print the AST of the code below using x86-64 clang 10.0.0 Ast Viewer
int main() {return 'c';}
And I got this:
TranslationUnitDecl
`-FunctionDecl col:5 main 'int ()'
`-CompoundStmt
`-ReturnStmt…

Wasif
- 1
- 2