From 06f7c21cbc54de15926c942a8c68fa496b4fe9e6 Mon Sep 17 00:00:00 2001 From: Bolshakov Date: Wed, 13 Jul 2022 12:31:13 +0300 Subject: [PATCH] Test template instantiation reporting --- tests/cxx/array.cc | 21 ++++++++++++- tests/cxx/indirect.h | 13 ++++++++ tests/cxx/member_expr.cc | 64 +++++++++++++++++++++++++++++++++++++- tests/cxx/pointer_arith.cc | 43 ++++++++++++++++++++++++- 4 files changed, 138 insertions(+), 3 deletions(-) diff --git a/tests/cxx/array.cc b/tests/cxx/array.cc index a2debce..aec7848 100644 --- a/tests/cxx/array.cc +++ b/tests/cxx/array.cc @@ -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 *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 *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 */ diff --git a/tests/cxx/indirect.h b/tests/cxx/indirect.h index aebf274..a23d697 100644 --- a/tests/cxx/indirect.h +++ b/tests/cxx/indirect.h @@ -25,5 +25,18 @@ class IndirectClass { static int statica; }; +template +class IndirectTemplate { + public: + void Method() const { + } + int a; + static void StaticMethod() { + } + + private: + T t; +}; + #endif // INCLUDE_WHAT_YOU_USE_TESTS_CXX_INDIRECT_H_ diff --git a/tests/cxx/member_expr.cc b/tests/cxx/member_expr.cc index a808782..0ec420a 100644 --- a/tests/cxx/member_expr.cc +++ b/tests/cxx/member_expr.cc @@ -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& 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* 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::StaticMethod(); +} + +// IWYU: IndirectTemplate needs a declaration +// IWYU: IndirectClass needs a declaration +void ViaMacro(const IndirectTemplate& 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 */ diff --git a/tests/cxx/pointer_arith.cc b/tests/cxx/pointer_arith.cc index 30f16cb..3d4e81c 100644 --- a/tests/cxx/pointer_arith.cc +++ b/tests/cxx/pointer_arith.cc @@ -47,6 +47,47 @@ void PointerArithmetic() { p1 += 100; } +// IWYU: IndirectTemplate is...*indirect.h +// IWYU: IndirectClass needs a declaration +// IWYU: IndirectClass is...*indirect.h +IndirectTemplate itc1, itc2; + +void PointerArithmeticWithTemplates() { + // IWYU: IndirectTemplate needs a declaration + // IWYU: IndirectClass needs a declaration + IndirectTemplate* p1 = &itc1; + // IWYU: IndirectTemplate needs a declaration + // IWYU: IndirectClass needs a declaration + IndirectTemplate* 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 */