Questions tagged [clang-ast-matchers]
67 questions
8
votes
0 answers
Given two CXXRecordDecl for class A and B, is there an easy way to see if A is implicitly convertible to B?
The X: Trying to write an internal clang tidy tool that finds an expression that contains two types (A, B) and then throws if const A& is implicitly convertible to B?
Given I can find two CXXRecordDecl for the classes, can I easily detect if A is…

IdeaHat
- 7,641
- 1
- 22
- 53
7
votes
1 answer
How to get the source text from line number using clang?
I am using clang matcher to obtain the result nodes. From the result nodes, I am able to get the line number, let us say 17. Now, I would like to get the entire source code in that line. Please help.
Let me explain in detail. I have a clang matcher…

The Voyager
- 617
- 8
- 21
6
votes
1 answer
clang-query: Examining name of template parameter of a function argument's type
I have a big project, and a slew of C++ class member functions of the form:
Return CClass::MemberFunction(
Arg1 arg1,
//...
std::weak_ptr listener) {
//...
}
I'm trying to write a matcher that finds…

Eric Niebler
- 5,927
- 2
- 29
- 43
5
votes
1 answer
fatal error: 'stddef.h' file not found when using clang-llvm ASTMatcher
I am new to using ASTMatcher and following a code from a tutorial - https://github.com/peter-can-talk/cppnow-2017. Here, the tool clang-variables can be run using the following command:
cd code/clang-variables
docker run -it -v $PWD:/home clang…

The Voyager
- 617
- 8
- 21
5
votes
1 answer
Clang AST Matcher's "AND" and "OR"
Is it possible to use or in function declarations? like:
functionDecl(hasName("a") or hasName("b"))
or we have to use addMatcher to add more matchers to get the same result?

ignorer
- 327
- 1
- 11
4
votes
0 answers
Clang AST Matchers: matching on nestedNamespaces
Given a class with a private member of a different namespace
namespace Test { namespace Cfg {
class ExampleApi {
public:
ExampleApi() = default;
int someVar;
};
}}
class Example {
public:
Example() = default;
private:
…

jonah jolley
- 41
- 1
- 2
3
votes
0 answers
How do I find string literals containing a given substring with clang-query?
I am doing some automated refactoring and I want to understand if some name is used in strings. E.g. in the code below I want to find all string literals containing substring "hello", which will yield me the first string.
int hello;
const char* str…

Mikhail
- 20,685
- 7
- 70
- 146
3
votes
0 answers
Getting file not found error using clang LibASTMatchers
I am experiencing an issue with CLang's libastmatchers while working with Postgres sources: it can't find include file. This error is reproduced only when CLangTool is created from two files. If it is created for a separate file, there is no error…

Vadim Volodin
- 377
- 1
- 2
- 14
3
votes
1 answer
Writing AST matcher to find all case statements having no break statement
I want to find all the case statement having no break statement. I using clang-query to build my matcher. My matcher is failing in some of the test cases.
I wrote simple matcher as
match caseStmt(unless(has(breakStmt())))
it works with follwing…

sunil sarode
- 154
- 1
- 13
3
votes
1 answer
Replace expression with macro using clang AST
I am looking to change the following code, with the help of the clang ast matcher.
foo(NUM << DEV_SHIFT | DEVICE);
to
foo(ADDR(NUM, DEVICE));
with
#define ADDR(a, b) (((a) << NUM_SHIFT) | (b))
I have the following AST-matcher that seems to…

eimer
- 81
- 7
3
votes
0 answers
How do I construct a syntax-only match-finder tool?
I am building an ASTMatcher-based tool that I would like to run over my sources:
int main(int argc, const char** argv) {
CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
ClangTool Tool(OptionsParser.getCompilations(),
…

fbrereto
- 35,429
- 19
- 126
- 178
2
votes
1 answer
How to find the clang::SourceRange of a deleted function?
I am working on a Clang AST generated from the following source code:
struct has_deleted_function_member
{
void deleted_function1() = delete;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^ col:33
};
void deleted_function2() = delete;
//~~~~~~~~~~~~~~~~~~~~~^…

Jared Hoberock
- 11,118
- 3
- 40
- 76
2
votes
1 answer
How to find SourceLocation of the outer template parameter list of a class template member function definition?
I am working on a Clang AST generated from the following source code:
template
struct my_class
{
template
void foo(U arg);
};
template
template void my_class::foo(W arg)
{
}
int main()
{
return 0;
}
I…

Jared Hoberock
- 11,118
- 3
- 40
- 76
2
votes
0 answers
How to see all UnresolvedLookupExpr node in Clang AST
I am looking into various scenarios where UnresolvedLookupExpr node occurs. I notice that the output of -ast-dump and clang-query don't match.
Here is an example code in c++/9/bits/range_access.h.
template
inline constexpr…

Linc Ghibling
- 21
- 1
2
votes
1 answer
clang-tidy: Warn when a container holds a particular class
Using LLVM-15's clang-tidy linter, I'd like to enforce the use of a typedef. Instances of
std::unordered_set
used as a variable declaration, function return, or in a using/typedef, should instead be written…

Richard
- 56,349
- 34
- 180
- 251