[cfe-dev] Comparing a canonical type to a type with a known name
Jordan Rose
jordan_rose at apple.com
Mon Sep 9 18:46:00 PDT 2013
On Sep 9, 2013, at 4:38 , Gábor Kozár <kozargabor at gmail.com> wrote:
> I'm trying to write a checker to determine when the std::find is passed an argument of type e.g. std::set::iterator. Unfortunately, in the AST, I cannot find the original type of the argument of std::find. This is what the AST dump shows:
>
> CXXConstructExpr 0xc09a530 'struct std::_Rb_tree_const_iterator<int>':'struct std::_Rb_tree_const_iterator<int>' 'void (const struct std::_Rb_tree_const_iterator<int> &) noexcept'
> `-ImplicitCastExpr 0xc09a488 'const struct std::_Rb_tree_const_iterator<int>' lvalue <NoOp>
> `-DeclRefExpr 0xc09a344 'struct std::_Rb_tree_const_iterator<int>':'struct std::_Rb_tree_const_iterator<int>' lvalue Var 0xc09a1b0 'it' 'struct std::_Rb_tree_const_iterator<int>':'struct std::_Rb_tree_const_iterator<int>'
>
> For:
>
> auto it = s.begin();
> auto itEnd = s.end();
> std::find(it, itEnd, 45); // dumping argument 0
>
> I would need to compare the type of argument 0 to std::set::iterator. However, I do not know how to get the Type representation of the latter only by the name. I tried using the IdentifierTable, but dumping it (iterating through it) just gives me a lot garbage keys, and it doesn't seem to recognize neither "set" or "std::set".
>
> So how should I go about doing this?
Huh. You'll have to do this building up from the bottom, starting from the TU and looking up "iterator" inside "set" inside "std". (Bailing out, of course, if any of those are not defined or not the kind of decl you expect.) Take a look at DeclContext::lookup.
Jordan
More information about the cfe-dev
mailing list