I'm beginner with libtooling, I try to learn with easy c++ code. I try to parse/print typedef expression like below lines:
namespace DEBUG {
typedef void(*function_pointer_t)(int&);
typedef int myInt;
}
clang++ -Xclang -ast-dump -fsyntax-only output:
`-NamespaceDecl 0x3e4fe331a8 <test.h:3:1, line:7:1> line:3:11 TESTS
|-TypedefDecl 0x3e4fe333b8 <line:4:2, col:42> col:18 function_pointer_t 'double (*)(int &)'
| `-PointerType 0x3e4fe33350 'double (*)(int &)'
| `-ParenType 0x3e4fe332f0 'double (int &)' sugar
| `-FunctionProtoType 0x3e4fe332b0 'double (int &)' cdecl
| |-BuiltinType 0x3e4fe32b60 'double'
| `-LValueReferenceType 0x3e4fe33210 'int &'
| `-BuiltinType 0x3e4fe32a40 'int'
`-TypedefDecl 0x3e4fe33420 <line:5:2, col:14> col:14 myInt 'int'
`-BuiltinType 0x3e4fe32a40 'int'
To parse it I create a class that inherite from MatchFinder::MatchCallback and overload MatchFinder::MatchCallback::run:
class TypdefDeclFinder : public MatchFinder::MatchCallback {
public:
virtual void run(const MatchFinder::MatchResult& result)
{
auto Item = result.Nodes.getNodeAs<clang::TypedefDecl>("typedefDeclMatch");
if (!Item) return;
if (!IsDeclFromInputFiles(Item, result.SourceManager)) return;
if (!Item->getIdentifier()) return;
if (IsInsideTemplateContext(Item)) return;
print(Item);
}
};
But the Item pointer is equal to null. I can parse/print function, variable, class, struct template, method templates, enum ... with MacthFinder::MatchCallback, but this way doesn't run on typedef.
What's wrong with this code?
Related
I have the following sample code.
template<class T, class... Args>
T add(T first, Args... rest) {
return first + add(rest...);
}
int add(int a, int b) {
return a + b;
}
When I run it through clang-check -ast-dump, I get the following tree.
FunctionDecl 0x4df0b08 <tmp.cpp:2:1, line:4:1> line:2:3 add 'T (T, Args...)'
|-ParmVarDecl 0x4df0928 <col:7, col:9> col:9 referenced first 'T'
|-ParmVarDecl 0x4df09f8 <col:16, col:24> col:24 referenced rest 'Args...' pack
`-CompoundStmt 0x4df0dd0 <col:30, line:4:1>
`-ReturnStmt 0x4df0dc0 <line:3:5, col:31>
`-BinaryOperator 0x4df0da0 <col:12, col:31> '<dependent type>' '+'
|-DeclRefExpr 0x4df0cd0 <col:12> 'T' lvalue ParmVar 0x4df0928 'first' 'T'
`-CallExpr 0x4df0d78 <col:20, col:31> '<dependent type>'
|-UnresolvedLookupExpr 0x4df0cf0 <col:20> '<overloaded function type>' lvalue (ADL) = 'add' 0x4df0ba8
`-PackExpansionExpr 0x4df0d58 <col:24, col:28> '<dependent type>' lvalue
`-DeclRefExpr 0x4df0d38 <col:24> 'Args' lvalue ParmVar 0x4df09f8 'rest' 'Args...'
FunctionDecl 0x4df0f60 <tmp.cpp:6:1, line:8:1> line:6:5 add 'int (int, int)'
|-ParmVarDecl 0x4df0e00 <col:9, col:13> col:13 used a 'int'
|-ParmVarDecl 0x4df0e80 <col:16, col:20> col:20 used b 'int'
`-CompoundStmt 0x4df10b0 <col:23, line:8:1>
`-ReturnStmt 0x4df10a0 <line:7:5, col:16>
`-BinaryOperator 0x4df1080 <col:12, col:16> 'int' '+'
|-DeclRefExpr 0x4df1010 <col:12> 'int' lvalue ParmVar 0x4df0e00 'a' 'int'
`-DeclRefExpr 0x4df1030 <col:16> 'int' lvalue ParmVar 0x4df0e80 'b' 'int'
I would like to write a matcher for the first case, where one of the arguments of the function is variadic. From the AST Matcher Reference, I have found that there is a isVariadic matcher, though as the documentation says,
Example matches f, but not g or h. The function i will not match, even when
compiled in C mode.
void f(...);
void g(int);
template <typename... Ts> void h(Ts...);
void i();
Is there any way to match the variadic parameter function declaration, and further bind the variadic parameter declaration to some node? I would like to do something like functionDecl(has(parmVarDecl(hasType(packExpansionType().bind("parameter_type"))))), but it seems that this not possible since there is no packExpansionType matcher. Am I missing something here?
For future reference, I found a solution to this. It is actually possible to define custom matchers within a check.
namespace {
const AstTypeMatcher<PackExpansionType> packExpansionType;
const internal::VariadicDynCastAllOfMatcher<Stmt, PackExpansionExpr> packExpansionExpr;
}
// You can also use the macros from ASTMatchersMacros.h
...
Say I have a dummy class like this:
class Stack
{
public:
template<typename T>
void push(T val)
{ (void)val; }
template<typename T>
T pop()
{ return 0; }
bool empty() const
{ return true; }
};
The AST dump of which looks like:
...
|-CXXRecordDecl 0x5610df147d60 <col:1, col:7> col:7 implicit class Stack
|-AccessSpecDecl 0x5610df147df0 <line:3:1, col:7> col:1 public
|-FunctionTemplateDecl 0x5610df1480e0 <line:4:3, line:6:16> line:5:8 push
| |-TemplateTypeParmDecl 0x5610df147e18 <line:4:12, col:21> col:21 referenced typename depth 0 index 0 T
| `-CXXMethodDecl 0x5610df148040 <line:5:3, line:6:16> line:5:8 push 'void (T)'
| |-ParmVarDecl 0x5610df147f00 <col:13, col:15> col:15 referenced val 'T'
| `-CompoundStmt 0x5610df1484f0 <line:6:3, col:16>
| `-CStyleCastExpr 0x5610df1484c8 <col:5, col:11> 'void' <ToVoid>
| `-DeclRefExpr 0x5610df148498 <col:11> 'T' lvalue ParmVar 0x5610df147f00 'val' 'T'
|-FunctionTemplateDecl 0x5610df148300 <line:8:3, line:10:15> line:9:5 pop
| |-TemplateTypeParmDecl 0x5610df148140 <line:8:12, col:21> col:21 referenced typename depth 0 index 0 T
| `-CXXMethodDecl 0x5610df148260 <line:9:3, line:10:15> line:9:5 pop 'T ()'
| `-CompoundStmt 0x5610df148538 <line:10:3, col:15>
| `-ReturnStmt 0x5610df148528 <col:5, col:12>
| `-IntegerLiteral 0x5610df148508 <col:12> 'int' 0
`-CXXMethodDecl 0x5610df1483e0 <line:12:3, line:13:18> line:12:8 empty 'bool () const'
`-CompoundStmt 0x5610df148570 <line:13:3, col:18>
`-ReturnStmt 0x5610df148560 <col:5, col:12>
`-CXXBoolLiteralExpr 0x5610df148550 <col:12> 'bool' true
Given access to the CXXRecordDecl node for Stack, how can I access the FunctionTemplateDecl objects for push and pop? CXXRecorDecl::methods only returns the method declaration for empty and I don't see any other functions that might be helpful. Do I have to match the function template declarations separately and then later associate them with the class declaration?
I believe I have now figured it out, I will answer my own question in case this is useful for someone else:
The way to do this is to first cast the given CXXRecordDecl to DeclContext, iterate over the contained declarations and try to cast them to FunctionTemplateDecl, pseudocode:
clang::CXXRecordDecl const *record = // ...
for (auto const *inner : static_cast<clang::DeclContext>(record)->decls()) {
auto const *member_function_template = llvm::dyn_cast<clang::FunctionTemplateDecl>(inner);
if (!member_function_template)
continue;
// do something useful
}
I'm writing a delegate class but it fails to take const member functions.
Here is a test case :
class foo
{
public:
void MemberFunction()
{
printf("non const member function\n");
}
void ConstMemberFunction() const
{
printf("const member function\n");
}
};
template <class C, void (C::*Function)()>
void Call(C* instance)
{
(instance->*Function)();
}
int main (int argc, char** argv)
{
foo bar;
Call<foo,&foo::MemberFunction>(&bar);
Call<foo,&foo::ConstMemberFunction>(&bar);
}
Now the compiler (visual studio 2010) gives me an error he cannot convert the const member function to a non-const function :
2>..\src\main.cpp(54): error C2440: 'specialization' : cannot convert from 'void (__cdecl foo::* )(void) const' to 'void (__cdecl foo::* const )(void)'
2> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
2>..\src\main.cpp(54): error C2973: 'Call' : invalid template argument 'void (__cdecl foo::* )(void) const'
2> ..\src\main.cpp(37) : see declaration of 'Call'
ok, easy fix (I though :P ) by adding this :
template <class C, void (C::*Function)() const>
void Call(C* instance)
{
(instance->*Function)();
}
but now the compiler is completly confused (and me with it). it looks like he now tries to use the const function for the non-const member function and the non-const function for the const member function.
2>..\src\main.cpp(53): error C2440: 'specialization' : cannot convert from 'void (__cdecl foo::* )(void)' to 'void (__cdecl foo::* const )(void) const'
2> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
2>..\src\main.cpp(53): error C2973: 'Call' : invalid template argument 'void (__cdecl foo::* )(void)'
2> ..\src\main.cpp(43) : see declaration of 'Call'
2>..\src\main.cpp(53): error C2668: 'Call' : ambiguous call to overloaded function
2> ..\src\main.cpp(43): could be 'void Call<foo,void foo::MemberFunction(void)>(C *)'
2> with
2> [
2> C=foo
2> ]
2> ..\src\main.cpp(37): or 'void Call<foo,void foo::MemberFunction(void)>(C *)'
2> with
2> [
2> C=foo
2> ]
2> while trying to match the argument list '(foo *)'
2>..\src\main.cpp(54): error C2440: 'specialization' : cannot convert from 'void (__cdecl foo::* )(void) const' to 'void (__cdecl foo::* const )(void)'
2> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
2>..\src\main.cpp(54): error C2973: 'Call' : invalid template argument 'void (__cdecl foo::* )(void) const'
2> ..\src\main.cpp(37) : see declaration of 'Call'
2>..\src\main.cpp(54): error C2668: 'Call' : ambiguous call to overloaded function
2> ..\src\main.cpp(43): could be 'void Call<foo,void foo::ConstMemberFunction(void) const>(C *)'
2> with
2> [
2> C=foo
2> ]
2> ..\src\main.cpp(37): or 'void Call<foo,void foo::ConstMemberFunction(void) const>(C *)'
2> with
2> [
2> C=foo
2> ]
2> while trying to match the argument list '(foo *)'
If I would rename the second Call function (with the const), it all works fine but I would rather use one function.
So, can anybody point me towards what I'm doing wrong and how I can make this work ?
Thx!
I think you might be able to address this by removing the function pointer from the template type signature and instead relying on overloading:
template <class C>
void Call(C* ptr, void (C::*function)()) {
(ptr->*function)();
}
template <class C>
void Call(C* ptr, void (C::*function)() const) {
(ptr->*function)();
}
This now uses normal function overloading to select which of the two functions should be called. const member function pointers will call down to the second version, while non-const functions will call up to the first version. This also means that you don't need to explicitly provide any type information to the template function; the compiler can deduce C in both contexts.
Let me know if (1) this doesn't work or (2) this does work, but isn't what you want.
Hope this helps!
Use std::bind or lambdas, which VS2010 supports, and std::function and this will cease to be a problem for you.
Your problem is that a member function pointer is a different type than a const member function pointer. I modified your Call function to this:
template< typename C, typename funcptr_t, funcptr_t ptr >
void Call( C *instance )
{
(instance->*ptr)();
}
and now using the additional template parameter, I can call it like this:
Call<foo,void (foo::*)(),&foo::MemberFunction>(&bar);
Call<foo,void (foo::*)() const, &foo::ConstMemberFunction>(&bar);
That's a bit messy, though. The overloading solution is better! :-)
I have a bunch of templated code that compiles fine under g++, but now when I try to build under windows with Visual C++ 2010 I get a bunch of errors.
I have a collection of template functions for getting and setting values in C++ objects from Lua code. For example, I have this template:
// Class Return type Getter function
template <typename T, typename U, U (T::*Getter)() const>
int luaU_get(lua_State* L)
{
T* obj = luaW_check<T>(L, 1); // Gets userdata from stack and checks if it's of type T
luaU_push(L, (obj->*Getter)()); // Runs the getter function specified in the template, and pushes the
return 1;
}
(The complete file can be found here)
Which is instantiated here:
static luaL_reg TextArea_MT[] =
{
// Class Return type Getter function
{ "GetCharacterSize", luaU_get<TextArea, unsigned int, &TextArea::GetCharacterSize> },
{ NULL, NULL }
};
The signature for that getter is as follows:
unsigned int GetCharacterSize() const;
I'm getting a bunch of errors like this:
2>C:\Users\Alex\Documents\Visual Studio 2010\Projects\game\dev\src\game\lua\LuaTextArea.cpp(103): error C2440: 'specialization' : cannot convert from 'unsigned int (__thiscall ag::ui::TextArea::* )(void) const' to 'unsigned int *(__thiscall ag::ui::TextArea::* const )(void) const'
2> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
2>C:\Users\Alex\Documents\Visual Studio 2010\Projects\game\dev\src\game\lua\LuaTextArea.cpp(103): error C2973: 'luaU_get' : invalid template argument 'unsigned int (__thiscall ag::ui::TextArea::* )(void) const'
2> C:\Users\Alex\Documents\Visual Studio 2010\Projects\game\dev\src\extern\LuaWrapper\LuaWrapperUtil.hpp(147) : see declaration of 'luaU_get'
2>C:\Users\Alex\Documents\Visual Studio 2010\Projects\game\dev\src\game\lua\LuaTextArea.cpp(103): error C2440: 'specialization' : cannot convert from 'unsigned int (__thiscall ag::ui::TextArea::* )(void) const' to 'unsigned int *ag::ui::TextArea::* const '
2> There is no context in which this conversion is possible
2>C:\Users\Alex\Documents\Visual Studio 2010\Projects\game\dev\src\game\lua\LuaTextArea.cpp(103): error C2973: 'luaU_get' : invalid template argument 'unsigned int (__thiscall ag::ui::TextArea::* )(void) const'
2> C:\Users\Alex\Documents\Visual Studio 2010\Projects\game\dev\src\extern\LuaWrapper\LuaWrapperUtil.hpp(131) : see declaration of 'luaU_get'
2>C:\Users\Alex\Documents\Visual Studio 2010\Projects\game\dev\src\game\lua\LuaTextArea.cpp(103): error C2440: 'specialization' : cannot convert from 'unsigned int (__thiscall ag::ui::TextArea::* )(void) const' to 'unsigned int ag::ui::TextArea::* const '
2> There is no context in which this conversion is possible
2>C:\Users\Alex\Documents\Visual Studio 2010\Projects\game\dev\src\game\lua\LuaTextArea.cpp(103): error C2973: 'luaU_get' : invalid template argument 'unsigned int (__thiscall ag::ui::TextArea::* )(void) const'
2> C:\Users\Alex\Documents\Visual Studio 2010\Projects\game\dev\src\extern\LuaWrapper\LuaWrapperUtil.hpp(123) : see declaration of 'luaU_get'
2>C:\Users\Alex\Documents\Visual Studio 2010\Projects\game\dev\src\game\lua\LuaTextArea.cpp(103): error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'lua_CFunction'
2> None of the functions with this name in scope match the target type
This is a compiler bug in VC++. The following code is valid:
#include <iostream>
struct TextArea
{
unsigned GetCharacterSize() const { return 0; }
};
template<typename T, typename U, U (T::*)() const>
int foo()
{
return 1;
}
template<typename T, typename U, U* (T::*)() const>
int foo()
{
return 2;
}
int main()
{
std::cout << foo<TextArea, unsigned, &TextArea::GetCharacterSize>() << '\n';
}
And compiles with GCC 4.3.4, GCC 4.5.1, and Comeau 4.3.10.1 Beta2 (no link), but yields the following error with VC++ 2010 SP1:
error C2668: 'foo' : ambiguous call to overloaded function
EDIT: As for a workaround, it's ugly, but the only thing I can think of offhand is to use an extra layer of indirection so that there is no overloading involved:
#include <iostream>
struct WithPointer
{
unsigned* GetCharacterSize() const { return nullptr; }
};
struct WithoutPointer
{
unsigned GetCharacterSize() const { return 0u; }
};
template<bool UsePointerImplB>
struct kludge
{
template<typename T, typename U, U (T::*Getter)() const>
static int foo() { return 1; }
};
template<>
struct kludge<true>
{
template<typename T, typename U, U* (T::*Getter)() const>
static int foo() { return 2; }
};
int main()
{
std::cout
<< kludge<false>::foo<WithoutPointer, unsigned, &WithoutPointer::GetCharacterSize>() << '\n'
<< kludge<true>::foo<WithPointer, unsigned, &WithPointer::GetCharacterSize>() << '\n';
}
Effectively this is no different than just giving each overload a different name...
If you can force user to pick the actual return type of the function, the following works. Maybe, it'll be useful to you:
#include <iostream>
struct FooBar
{
int Foo( void ) const
{
std::cout << "FooBar::Foo()" << std::endl;
return ( 0 );
}
int * Bar( void ) const
{
std::cout << "FooBar::Bar()" << std::endl;
return ( 0 );
}
};
template< typename P00, typename P01, P01(P00::*p02)( void ) const >
void Call()
{
P00 lT;
( lT.*p02 )();
}
int main( void )
{
Call< FooBar, int, &FooBar::Foo > ();
Call< FooBar, int*, &FooBar::Bar > ();
return( 0 );
}
Program output:
FooBar::Foo()
FooBar::Bar()
I am trying to build DAME. Environment details:
GCC - gcc version 4.4.2 (GCC)
ANTLR - ANTLR Parser Generator Version 2.7.2
when i am trying to compile the following code snippet:
#include <functional>
#include <iomanip>
struct compare_without_case_char : public std::binary_function<char, char, bool>
{
const std::ctype<char>& ct ;
compare_without_case_char (const std::ctype<char>& c = std::use_facet<std::ctype<char> > (std::locale::classic()))
: ct(c) {}
bool operator() (char x, char y) const
{
return (ct.toupper(x) == ct.toupper(y)) ;
}
} ;
Following error is coming:
test_ctype.cpp: In member function 'bool compare_without_case_char::operator()(char, char) const':
test_ctype.cpp:16: error: invalid use of incomplete type 'const struct std::ctype<char>'
/storage11/oracle/ANAND_BM/gcc-4.4.2/gcc_4_4_2_release/gcc-4.4.2_build/lib/gcc/sparc-sun-solaris2.10/4.4.2/../../../../include/c++/4.4.2/bits/localefwd.h:115: error: declaration of 'const struct std::ctype<char>'
test_ctype.cpp:16: error: invalid use of incomplete type 'const struct std::ctype<char>'
/storage11/oracle/ANAND_BM/gcc-4.4.2/gcc_4_4_2_release/gcc-4.4.2_build/lib/gcc/sparc-sun-solaris2.10/4.4.2/../../../../include/c++/4.4.2/bits/localefwd.h:115: error: declaration of 'const struct std::ctype<char>'
In file included from /storage11/oracle/ANAND_BM/gcc-4.4.2/gcc_4_4_2_release/gcc-4.4.2_build/lib/gcc/sparc-sun-solaris2.10/4.4.2/../../../../include/c++/4.4.2/bits/locale_classes.h:809,
from /storage11/oracle/ANAND_BM/gcc-4.4.2/gcc_4_4_2_release/gcc-4.4.2_build/lib/gcc/sparc-sun-solaris2.10/4.4.2/../../../../include/c++/4.4.2/bits/ios_base.h:43,
from /storage11/oracle/ANAND_BM/gcc-4.4.2/gcc_4_4_2_release/gcc-4.4.2_build/lib/gcc/sparc-sun-solaris2.10/4.4.2/../../../../include/c++/4.4.2/iomanip:42,
from test_ctype.cpp:2:
/storage11/oracle/ANAND_BM/gcc-4.4.2/gcc_4_4_2_release/gcc-4.4.2_build/lib/gcc/sparc-sun-solaris2.10/4.4.2/../../../../include/c++/4.4.2/bits/locale_classes.tcc: In function 'const _Facet& std::use_facet(const std::locale&) [with _Facet = std::ctype<char>]':
test_ctype.cpp:11: instantiated from here
/storage11/oracle/ANAND_BM/gcc-4.4.2/gcc_4_4_2_release/gcc-4.4.2_build/lib/gcc/sparc-sun-solaris2.10/4.4.2/../../../../include/c++/4.4.2/bits/locale_classes.tcc:107: error: incomplete type 'std::ctype<char>' used in nested name specifier
/storage11/oracle/ANAND_BM/gcc-4.4.2/gcc_4_4_2_release/gcc-4.4.2_build/lib/gcc/sparc-sun-solaris2.10/4.4.2/../../../../include/c++/4.4.2/bits/locale_classes.tcc:112: error: cannot dynamic_cast '* *(__facets + ((unsigned int)(((unsigned int)__i) * 4u)))' (of type 'const class std::locale::facet') to type 'const struct std::ctype<char>&' (target is not pointer or reference to complete type)
What is the real issue and how I can resolve it.
Regards,
Anand Kumar Keshri
The only problem here is that your compiler cannot find std::ctype. You have to #include the file containing it :
#include <locale>