Issue 1250 in include-what-you-use: Incorrect include with function template

0 views
Skip to first unread message

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

unread,
Apr 25, 2023, 4:53:00 PM4/25/23
to include-wh...@googlegroups.com
New issue 1250 by carlosgalvezp: Incorrect include with function template
https://github.com/include-what-you-use/include-what-you-use/issues/1250

Hi,

Consider the following case:

```cpp
// bar.h
#pragma once
#include <iterator>
```

```cpp
// foo.h
#pragma once

#include "bar.h"

template <typename T>
T get_begin(T const(&x)[10])
{
return *std::begin(x);
}
```

```cpp
// foo.cpp
#include "foo.h"

int main()
{
int x[10]{};
return get_begin(x);
}
```

If I run IWYU, on `foo.cpp`, I get:

```
$ include-what-you-use foo.cpp

foo.h should add these lines:

foo.h should remove these lines:
- #include "bar.h" // lines 3-3

The full include-list for foo.h:
---

foo.cpp should add these lines:
#include <iterator> // for begin

foo.cpp should remove these lines:

The full include-list for foo.cpp:
#include "foo.h"
#include <iterator> // for begin
---
```

This is incorrect - `foo.h` should be the one including `iterator`. Currently, if N headers include `foo.h`, they are asked to include `iterator` just because they are calling a function that internally uses something from it. Implementation details are leaked to clients - if `foo.h` no longer needs `iterator`, suddenly all clients need to remove it. This creates a lot of code churn.

This behavior does not happen if the function `get_begin` is a regular (non-template) function.


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

unread,
Apr 26, 2023, 3:46:17 PM4/26/23
to include-wh...@googlegroups.com
Comment #2 on issue 1250 by carlosgalvezp: Incorrect include with function template
https://github.com/include-what-you-use/include-what-you-use/issues/1250

@bolshakov-a If I do that and remove `#include bar.h` it actually asks me to remove it:

```
foo.h should add these lines:

foo.h should remove these lines:
- #include <iterator> // lines 3-3

The full include-list for foo.h:
---

(foo.cpp has correct #includes/fwd-decls)
```

Which is even worse since the code no longer compiles. I now remember the sequence of events:

- I had code with the correct includes on IWYU 0.18.
- I bump to IWYU 0.20, which asked to remove `iterator` from `foo.h`.
- When `iterator` was removed from `foo.h`, IWYU started asking to include it in all the clients of `foo.h`, which led me to this opening this ticket.


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

unread,
Apr 26, 2023, 3:46:28 PM4/26/23
to include-wh...@googlegroups.com

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

unread,
Apr 26, 2023, 4:15:19 PM4/26/23
to include-wh...@googlegroups.com
Comment #3 on issue 1250 by bolshakov-a: Incorrect include with function template
https://github.com/include-what-you-use/include-what-you-use/issues/1250

IWYU has some heuristics about whether template provide a declaration or not (`PublicHeaderIntendsToProvide`). If you `#include` a header directly inside template containing header, it is considered as "provided". On the template declaration side, IWYU reports `#include` for overloaded function set only if all the set occurs to be in one header (if I remember correctly). It may be undesired behaviour, of course.

Maybe, I'll take a look some day, but it would be helpful to know which standard library implementation you use and whether you've updated it along with IWYU or not.


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

unread,
Apr 26, 2023, 4:21:47 PM4/26/23
to include-wh...@googlegroups.com
Comment #4 on issue 1250 by carlosgalvezp: Incorrect include with function template
https://github.com/include-what-you-use/include-what-you-use/issues/1250

Thanks for the clarification! I still don't quite understand why IWYU would want to remove the correct `iterator` include in the first place... (let me know if you'd like a separate ticket for just that).

> which standard library implementation you use and whether you've updated it along with IWYU or not.
I'm using libstdc++ from GCC 7.5, which I have kept constant for both versions of IWYU.


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

unread,
Apr 26, 2023, 4:21:54 PM4/26/23
to include-wh...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages