Minimum "working" example on compiler explorer: https://godbolt.org/z/r6jebPWMe
I have this C++ code:
#include <typeinfo>
namespace {
struct Base {
virtual ~Base() {}
};
struct Derived : Base {
virtual void name() {}
};
} // namespace
const std::type_info& test2 = typeid(Base);
I'd like to match typeid(Base)
, and detect if base was declared in an anonymous namespace.
This corresponds to the following AST node:
`-VarDecl <line:13:1, col:42> col:23 test2 'const std::type_info &' cinit
`-CXXTypeidExpr <col:31, col:42> 'const std::type_info' lvalue
(Note that I don't think I care about the VarDecl, but I'm including it for completeness).
What I'd really like is a cxxTypeiDExpr()
matcher, but that doesn't appear to exist: https://clang.llvm.org/docs/LibASTMatchersReference.html
My attempts to grab this information from an expr() matcher are as follows:
enable output detailed-ast
set print-matcher true
set traversal IgnoreUnlessSpelledInSource
m expr(isExpansionInMainFile()) # Not specific enough, I need to specialise, but...
# None of these work
m expr(isExpansionInMainFile(), hasType(asString("CXXTypeidExpr")))
m expr(isExpansionInMainFile(), hasType(asString("const std::type_info")))
m expr(isExpansionInMainFile(), hasType(asString("std::type_info")))
m expr(isExpansionInMainFile(), hasType(asString("type_info")))