Skip to content

Commit

Permalink
Scan aliased class template member functions
Browse files Browse the repository at this point in the history
The main change is in iwyu_ast_util.h. It allows not to short-circuit
aliased template specialization types inside
TraverseFunctionIfInstantiatedTpl method.

The change in iwyu.cc is to avoid regression with cases like

Outer<Providing>::Inner<Providing>::AliasedTpl pp;

(see tests/cxx/typedef_in_template.cc). It facilitates collecting
provided types from member function (ctor or dtor in that case) base
type qualifiers.
  • Loading branch information
bolshakov-a committed Aug 4, 2024
1 parent cbc19e0 commit c75230d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
10 changes: 6 additions & 4 deletions iwyu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4412,7 +4412,7 @@ class IwyuAstConsumer
data.provided_types);
}

set<const Type*> GetProvidedByTplArg(const Type* type) {
set<const Type*> GetProvidedByTplArg(const Type* type) const {
set<const Type*> res;
if (!type)
return res;
Expand Down Expand Up @@ -4464,15 +4464,17 @@ class IwyuAstConsumer

private:
set<const Type*> GetProvidedTypeComponents(const Type* type) const {
set<const Type*> res;
const Type* desugared_until_typedef = Desugar(type);
if (const auto* typedef_type =
dyn_cast_or_null<TypedefType>(desugared_until_typedef)) {
const TypedefNameDecl* decl = typedef_type->getDecl();
return GetProvidedTypes(decl->getUnderlyingType().getTypePtr(),
GetLocation(decl));
res = GetProvidedTypes(decl->getUnderlyingType().getTypePtr(),
GetLocation(decl));
InsertAllInto(GetProvidedByTplArg(type), &res);
}
// TODO(bolshakov): handle alias templates.
return set<const Type*>();
return res;
}

TemplateInstantiationData GetTplInstData(const Type* type) const {
Expand Down
2 changes: 1 addition & 1 deletion iwyu_ast_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,7 @@ const Type* Desugar(const Type* type) {
}

bool IsTemplatizedType(const Type* type) {
return (type && isa<TemplateSpecializationType>(Desugar(type)));
return type && type->getAs<TemplateSpecializationType>();
}

bool InvolvesTypeForWhich(const Type* type, function<bool(const Type*)> pred) {
Expand Down
3 changes: 3 additions & 0 deletions tests/cxx/badinc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ typedef I1_Class Cc_typedef_array[kI1ConstInt];
typedef I1_TemplateClass<I1_TemplateClass<I1_Class,I2_Class> > Cc_tpl_typedef;
// TODO(csilvers): it would be nice to be able to take this line out and
// still have the above tests pass:
// TODO(bolshakov): figure out how to determine at the use site that a typedef
// provides not only the types but also the member functions.
// IWYU: I2_Class::~I2_Class is...*badinc-i2-inl.h
Cc_tpl_typedef cc_tpl_typedef;
// IWYU: I2_Class is...*badinc-i2.h
// IWYU: I2_Class::I2_Class is...*badinc-i2-inl.h
Expand Down
43 changes: 43 additions & 0 deletions tests/cxx/template_member_functions.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//===--- template_member_functions.cc - test input file for iwyu ----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// IWYU_ARGS: -I .

// Tests scanning class template member functions.

#include "tests/cxx/direct.h"

template <typename T>
struct Tpl {
static void StaticFn() {
T t;
}
};

class IndirectClass;
using NonProviding = Tpl<IndirectClass>;

void Fn() {
// IWYU: IndirectClass is...*indirect.h
NonProviding::StaticFn();
}

/**** IWYU_SUMMARY
tests/cxx/template_member_functions.cc should add these lines:
#include "tests/cxx/indirect.h"
tests/cxx/template_member_functions.cc should remove these lines:
- #include "tests/cxx/direct.h" // lines XX-XX
- class IndirectClass; // lines XX-XX
The full include-list for tests/cxx/template_member_functions.cc:
#include "tests/cxx/indirect.h" // for IndirectClass
***** IWYU_SUMMARY */

0 comments on commit c75230d

Please sign in to comment.