Test template instantiation reporting

This commit is contained in:
Bolshakov 2022-07-13 12:31:13 +03:00 committed by Kim Gräsman
parent 526827957b
commit 06f7c21cbc
4 changed files with 138 additions and 3 deletions

View File

@ -24,8 +24,27 @@ class A {
// IWYU: IndirectClass is...*indirect.h
return &(b[i]);
}
// IWYU: IndirectTemplate needs a declaration
// IWYU: IndirectClass needs a declaration
IndirectTemplate<IndirectClass> *getIndirectTemplateSpecialization(int i) {
// IWYU: IndirectTemplate is...*indirect.h
// IWYU: IndirectClass is...*indirect.h
(void)sizeof(t[i]); // requires full type
// IWYU: IndirectTemplate needs a declaration
// IWYU: IndirectTemplate is...*indirect.h
// IWYU: IndirectClass is...*indirect.h
(void)sizeof(&(t[i])); // requires full type
// IWYU: IndirectTemplate is...*indirect.h
// IWYU: IndirectClass is...*indirect.h
return &(t[i]);
}
// IWYU: IndirectClass needs a declaration
IndirectClass *b;
// IWYU: IndirectTemplate needs a declaration
// IWYU: IndirectClass needs a declaration
IndirectTemplate<IndirectClass> *t;
};
@ -38,6 +57,6 @@ tests/cxx/array.cc should remove these lines:
- #include "tests/cxx/direct.h" // lines XX-XX
The full include-list for tests/cxx/array.cc:
#include "tests/cxx/indirect.h" // for IndirectClass
#include "tests/cxx/indirect.h" // for IndirectClass, IndirectTemplate
***** IWYU_SUMMARY */

View File

@ -25,5 +25,18 @@ class IndirectClass {
static int statica;
};
template <typename T>
class IndirectTemplate {
public:
void Method() const {
}
int a;
static void StaticMethod() {
}
private:
T t;
};
#endif // INCLUDE_WHAT_YOU_USE_TESTS_CXX_INDIRECT_H_

View File

@ -58,6 +58,68 @@ void ViaMacro(const IndirectClass& ic) {
}
// Because any member expression instantiates the template, the template
// specialization used as a member expression base requires full type info for
// all the template type arguments that are full-type-used for member
// initializers, non-static data members or nested typedef instantiation,
// regardless of whether those types are part of the member expression or not.
// IWYU: IndirectTemplate needs a declaration
// IWYU: IndirectClass needs a declaration
int RefFn(const IndirectTemplate<IndirectClass>& ic) {
// IWYU: IndirectTemplate is...*indirect.h
// IWYU: IndirectClass is...*indirect.h
ic.Method();
// IWYU: IndirectTemplate is...*indirect.h
// IWYU: IndirectClass is...*indirect.h
return ic.a;
}
// IWYU: IndirectTemplate needs a declaration
// IWYU: IndirectClass needs a declaration
int PtrFn(const IndirectTemplate<IndirectClass>* ic) {
// IWYU: IndirectTemplate is...*indirect.h
// IWYU: IndirectClass is...*indirect.h
ic->Method();
// IWYU: IndirectTemplate is...*indirect.h
// IWYU: IndirectClass is...*indirect.h
return ic->a;
}
void TemplateStaticFn() {
// IWYU: IndirectTemplate is...*indirect.h
// IWYU: IndirectClass needs a declaration
// IWYU: IndirectClass is...*indirect.h
IndirectTemplate<IndirectClass>::StaticMethod();
}
// IWYU: IndirectTemplate needs a declaration
// IWYU: IndirectClass needs a declaration
void ViaMacro(const IndirectTemplate<IndirectClass>& ic) {
// We should figure out we need IndirectTemplate and IndirectClass
// because of the 'ic.', even if the member-expr itself is in another file
// due to the macro.
// IWYU: IndirectTemplate is...*indirect.h
// IWYU: IndirectClass is...*indirect.h
ic.CALL_METHOD;
// Likewise, we 'own' this member expr because we own the dot.
// IWYU: IndirectTemplate is...*indirect.h
// IWYU: IndirectClass is...*indirect.h
IC.Method();
// IWYU: IndirectTemplate is...*indirect.h
// IWYU: IndirectClass is...*indirect.h
IC.CALL_METHOD;
IC.
// IWYU: IndirectTemplate is...*indirect.h
// IWYU: IndirectClass is...*indirect.h
CALL_METHOD;
// But this member-expr is entirely in the macro, so we don't own it.
IC_CALL_METHOD;
}
/**** IWYU_SUMMARY
tests/cxx/member_expr.cc should add these lines:
@ -67,7 +129,7 @@ tests/cxx/member_expr.cc should remove these lines:
- #include "tests/cxx/direct.h" // lines XX-XX
The full include-list for tests/cxx/member_expr.cc:
#include "tests/cxx/indirect.h" // for IndirectClass
#include "tests/cxx/indirect.h" // for IndirectClass, IndirectTemplate
#include "tests/cxx/member_expr-d1.h" // for CALL_METHOD, IC, IC_CALL_METHOD
***** IWYU_SUMMARY */

View File

@ -47,6 +47,47 @@ void PointerArithmetic() {
p1 += 100;
}
// IWYU: IndirectTemplate is...*indirect.h
// IWYU: IndirectClass needs a declaration
// IWYU: IndirectClass is...*indirect.h
IndirectTemplate<IndirectClass> itc1, itc2;
void PointerArithmeticWithTemplates() {
// IWYU: IndirectTemplate needs a declaration
// IWYU: IndirectClass needs a declaration
IndirectTemplate<IndirectClass>* p1 = &itc1;
// IWYU: IndirectTemplate needs a declaration
// IWYU: IndirectClass needs a declaration
IndirectTemplate<IndirectClass>* p2 = &itc2;
// All the pointer arithmetic below require the full type.
// Pointer minus pointer
// IWYU: IndirectTemplate is...*indirect.h
// IWYU: IndirectClass is...*indirect.h
long x = p2 - p1;
// Pointer minus offset
// IWYU: IndirectTemplate is...*indirect.h
// IWYU: IndirectClass is...*indirect.h
void* p3 = p1 - 20;
// Pointer decrement
// IWYU: IndirectTemplate is...*indirect.h
// IWYU: IndirectClass is...*indirect.h
p1 -= 10;
// Pointer plus offset
// IWYU: IndirectTemplate is...*indirect.h
// IWYU: IndirectClass is...*indirect.h
p3 = p1 + 100;
// Pointer increment
// IWYU: IndirectTemplate is...*indirect.h
// IWYU: IndirectClass is...*indirect.h
p1 += 100;
}
// Make sure pointer arithmetic with builtins does not yield IWYU warnings.
void BuiltinPointerArithmetic() {
char c = 0;
@ -71,6 +112,6 @@ tests/cxx/pointer_arith.cc should remove these lines:
- #include "tests/cxx/direct.h" // lines XX-XX
The full include-list for tests/cxx/pointer_arith.cc:
#include "tests/cxx/indirect.h" // for IndirectClass
#include "tests/cxx/indirect.h" // for IndirectClass, IndirectTemplate
***** IWYU_SUMMARY */