Issue 965 in include-what-you-use: iwyu removes include when declaration is only used in uninstantiated template function

2 views
Skip to first unread message

notifi...@include-what-you-use.org

unread,
Nov 3, 2021, 1:01:32 PM11/3/21
to include-wh...@googlegroups.com
New issue 965 by tberghammer: iwyu removes include when declaration is only used in uninstantiated template function
https://github.com/include-what-you-use/include-what-you-use/issues/965

```
// a.h
struct Foo {};

// b.h
#include "a.h"
template <typename T> int size() { return sizeof(T); }
template <int X> int foo() { return X + size<Foo>(); }

// b.cc
#include "b.h"

// c.cc
#include "b.h"
int bar() { return foo<5>(); }
```

```
/include-what-you-use b.cc

b.h should add these lines:
struct Foo;

b.h should remove these lines:
- #include "a.h" // lines 1-1

The full include-list for b.h:
struct Foo;
---

(b.cc has correct #includes/fwd-decls)
```

IWYU decides that we don't need the full definition of Foo in b.h because we never instantiated size<Foo>() but it means that when trying to compile c.cc it will fail to compile as we won't have the full definition of Foo.

P.S.: size<T>() is a simplification on std::make_unique<T> what fails the same way as the one above


notifi...@include-what-you-use.org

unread,
Nov 10, 2021, 3:34:18 PM11/10/21
to include-wh...@googlegroups.com
Comment #0 on issue 965 by kimgr: iwyu removes include when declaration is only used in uninstantiated template function
https://github.com/include-what-you-use/include-what-you-use/issues/965

Thanks, that does look like something IWYU should be able to catch (try `-Xiwyu -v7` to increase verbosity level, and you'll see that it's traversing the uninstantiated template, but fails to report the full use of the template argument type for `sizeof`).


notifi...@include-what-you-use.org

unread,
Nov 15, 2021, 5:59:55 AM11/15/21
to include-wh...@googlegroups.com
Comment #2 on issue 965 by tberghammer: iwyu removes include when declaration is only used in uninstantiated template function
https://github.com/include-what-you-use/include-what-you-use/issues/965

Is your expectation is that when we traverse the uninstantiated template of `size` then we will report a full use of `T` and then when we hit `size<Foo>` we look up the fact that `size<T>` will require the full use of `T` and based on that require a full use of `Foo` as well?

At the moment the first step (reporting a full use of `T` inside `size`) doesn't happen but I expect that to be something fairly easy to fix.


Reply all
Reply to author
Forward
0 new messages