[cfe-users] Determining if a template type came from a typedef in clang-tidy

Lewis, Cannada via cfe-users cfe-users at lists.llvm.org
Wed Apr 29 10:47:04 PDT 2020


I have the following example: 

In a header somewhere:
namespace Kokkos {
  using DefaultHostExecutionSpace = Serial;
}

In a different header somewhere

namespace Kokkos{
template<typename T>
class RangePolicy;
}

struct GoodClass {
  int foo;

  KOKKOS_INLINE_FUNCTION GoodClass(int f) : foo(f) {}
  KOKKOS_INLINE_FUNCTION void method() {
    Kokkos::parallel_for(
        Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>(0, 42),
        KOKKOS_LAMBDA(int i) { do_something(foo, i); });
  }
};

Once I have the CallExpr to the parallel_for I use the following matcher on it:

auto FilterArgs = hasAnyArgument(
    expr(hasType(cxxRecordDecl(isDerivedFrom(cxxRecordDecl(
                                   matchesName("Impl::PolicyTraits")))) // This will cause it to match the first argument
                     .bind("decl")))
        .bind("expr"));

I am trying to detect that the template type on the RangePolicy is Kokkos::DefaultHostExecutionSpace and not Serial. 

My question is can I only see the typedef at the expression that constructs the object or can I deduce that Serial was actually the result of the typedef from the CXXRecordDecl? I can’t just find the typedefdecl because I only want to detect when it was explicitly Kokkos::DefaultHostExecutionSpace, Serial could come from some other context that I don’t want to match on. 

When I dump the expr from the the above matcher I get:
`-CXXTemporaryObjectExpr 0x7007f50 'Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>':'class Kokkos::RangePolicy<class Kokkos::Serial>' 'void (const Kokkos::RangePolicy<class Kokkos::Serial>::member_type, const Kokkos::RangePolicy<class Kokkos::Serial>::member_type)’

But if I dump the decl I can only see:

ClassTemplateSpecializationDecl 0x7e6eb70 </path/to/the/file/Kokkos_ExecPolicy.hpp:93:1, line:279:1> line:94:7 class RangePolicy definition
|-DefinitionData pass_in_registers standard_layout trivially_copyable has_user_declared_ctor can_const_default_init
| |-DefaultConstructor exists non_trivial user_provided
| |-CopyConstructor trivial user_declared has_const_param needs_overload_resolution implicit_has_const_param
| |-MoveConstructor exists trivial user_declared
| |-CopyAssignment trivial has_const_param implicit_has_const_param
| |-MoveAssignment
| `-Destructor simple irrelevant trivial
|-public 'Impl::PolicyTraits<Serial>':'Kokkos::Impl::PolicyTraits<Kokkos::Serial>'
|-TemplateArgument pack
| `-TemplateArgument type 'Kokkos::Serial' 

Thanks,
Drew 


More information about the cfe-users mailing list