Need to use function "getAsCXXRecordDecl" of ASTMatchFinder.cpp in a clang-tidy check

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 9 17:17:08 PST 2016


On Tue, Feb 9, 2016 at 4:15 PM, Cong Liu <congliu at google.com> wrote:
> Hi Richard,
>
> Thank you for your reply. Yes, the case I need to deal with is like what you
> said:
>>
>> If you want to make the assumption that the primary template will be
>> used for an unknown specialization, you'll need something like that
>> function in ASTMatchFinder.
>
>
> For example,
>
>   template <typename T>
>   struct Base {};
>   template <typename T>
>   struct Derived : Base<T>{};
>
>   Derived<int> T1;
>
> In this case, I need to firstly get the CXXBaseSpecifier from line 4, then
> get the QualType of primary template (Base<T>), then get its declaration.
> For this case, that function in ASTMatchFinder works but
> Type::getAsCXXRecordDecl does not.
>
> So, what do you suggest?

Using that may not be correct when analysing virtual function
overrides. Consider this:

template<typename T> struct Base {
  virtual void f(T);
};
template<> struct Base<void> {
  virtual void f();
};
template<typename T, bool = is_same<T, void>::value> struct Derived : Base<T> {
  virtual void f();
};
template<typename T> struct Derived<T, false> : Base<T> {
  virtual void f(T);
};

Here, it would be wrong to report that the Derived primary template
fails to override a virtual function from the Base primary template,
as the Derived primary template is actually only ever used when T ==
void, and there's a specialization of the Base template for that case.
I have no idea whether that's acceptable for your check.

In principle, I'm fine with us moving the functionality in
ASTMatchFinder (that I recently renamed to
getAsCXXRecordDeclOrPrimaryTemplate to better express its purpose) to
somewhere more central, but my concern is that most uses of it will in
fact be subtle bugs.


More information about the cfe-commits mailing list