Issue 1249 in include-what-you-use: iwyu removes some headers incorrectly

0 views
Skip to first unread message

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

unread,
Apr 24, 2023, 1:46:01 PM4/24/23
to include-wh...@googlegroups.com
New issue 1249 by iven: iwyu removes some headers incorrectly
https://github.com/include-what-you-use/include-what-you-use/issues/1249

I have these three files:

FontMgr.h:
```c++
class IFontMgr {
public:
void getFont();
};
```

main.h:
```c++
#include <memory>

class IFontMgr;

class FontRenderer {
void render() const;

private:
std::shared_ptr<IFontMgr> fontMgr_;
};
```

main.cpp:
```c++
#include "main.h"
#include "FontMgr.h"

void FontRenderer::render() const {
fontMgr_->getFont();
}
```

Running iwyu(main branch & clang_16 branch):
```bash
$ include-what-you-use -stdlib=libc++ main.cpp
```

I get:
```
main.h should add these lines:
#include <__memory/shared_ptr.h> // for shared_ptr

main.h should remove these lines:
- #include <memory> // lines 1-1

The full include-list for main.h:
#include <__memory/shared_ptr.h> // for shared_ptr
class IFontMgr; // lines 3-3
---

main.cpp should add these lines:

main.cpp should remove these lines:
- #include "FontMgr.h" // lines 2-2

The full include-list for main.cpp:
#include "main.h"
---
```

`FontMgr.h` is removed by iwyu, and `main.cpp` cannot be compiled by clang, I get errors like:
```
error: member access into incomplete type 'element_type' (aka 'IFontMgr')
```

This happens not only when calling `shared_ptr<IFontMgr>->method()`, but also `optional<IFontMgr>->method()`.


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

unread,
Apr 24, 2023, 6:02:39 PM4/24/23
to include-wh...@googlegroups.com
Comment #1 on issue 1249 by kilroyd: iwyu removes some headers incorrectly
https://github.com/include-what-you-use/include-what-you-use/issues/1249

On the master branch, the above works for me (MacOS)

```
$ ../iwyu-build/bin/include-what-you-use -stdlib=libc++ main.cc

(main.h has correct #includes/fwd-decls)

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

clang-16 branch doesn't have the libc++ support, so the missing `__memory` mappings are expected. I can't explain the `FontMgr.h` removal. Are you sure you're using `include-what-you-use` from the build output, and not an older installed version?


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

unread,
Apr 24, 2023, 10:52:57 PM4/24/23
to include-wh...@googlegroups.com
Comment #2 on issue 1249 by iven: iwyu removes some headers incorrectly
https://github.com/include-what-you-use/include-what-you-use/issues/1249

I think I'm using the latest version (Ubuntu 22.04.1, clang installed from `deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-16 main`):
```bash
$ include-what-you-use --version
include-what-you-use 0.21 (git:1646cfb) based on Ubuntu clang version 16.0.2 (++20230414073413+b5aa566a7e53-1~exp1~20230414073428.73)
```
Iwyu source code not modified, built with:
```
$ mkdir build && cd build
$ cmake -G Ninja -DCMAKE_PREFIX_PATH=/usr/lib/llvm-16 ../include-what-you-use
$ ninja -j 50
$ sudo cp bin/include-what-you-use /usr/lib/llvm-16/bin/
```


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

unread,
Apr 24, 2023, 10:54:50 PM4/24/23
to include-wh...@googlegroups.com
Comment #2 on issue 1249 by iven: iwyu removes some headers incorrectly
https://github.com/include-what-you-use/include-what-you-use/issues/1249

I think I'm using the latest version (Ubuntu 22.04.1, clang installed from `deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-16 main`):
```bash
$ include-what-you-use --version
include-what-you-use 0.21 (git:14e9b20) based on Ubuntu clang version 16.0.2 (++20230414073413+b5aa566a7e53-1~exp1~20230414073428.73)

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

unread,
Apr 24, 2023, 10:57:36 PM4/24/23
to include-wh...@googlegroups.com
Also the '__memory' mapping does not seem to work here, iwyu suggests to add `<__memory/shared_ptr.h>`.


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

unread,
Apr 27, 2023, 3:51:49 AM4/27/23
to include-wh...@googlegroups.com
Comment #3 on issue 1249 by iven: iwyu removes some headers incorrectly
https://github.com/include-what-you-use/include-what-you-use/issues/1249

I increased log verbose and got:
```
--- Calculating IWYU violations for main.h ---
Ignoring fwd-decl use of FontRenderer (main.h:5:7): dfn is present: main.h:5:7
Ignoring fwd-decl use of FontRenderer (main.h:5:7): dfn is present: main.h:5:7
Ignoring use of FontRenderer (main.h:5:7): definition is present: main.h:5:7
Ignoring use of FontRenderer (main.h:5:7): definition is present: main.h:5:7
Ignoring use of std::shared_ptr::~shared_ptr<_Tp> (main.h:5:7): member of class
Ignoring use of std::shared_ptr::shared_ptr<_Tp> (main.h:5:7): member of class
Mapped /usr/lib/llvm-16/include/c++/v1/__memory/shared_ptr.h to <__memory/shared_ptr.h> for std::shared_ptr (only candidate)
Noting fwd-decl use of IFontMgr (main.h:9:19) is declared at main.h:3:7
Ignoring fwd-decl use of IFontMgr (main.h:9:19): have earlier fwd-decl at main.h:3:7
main.h:9:8: warning: std::shared_ptr is defined in <__memory/shared_ptr.h>, which isn't directly #included.

main.h should add these lines:
#include <__memory/shared_ptr.h> // for shared_ptr

main.h should remove these lines:
- #include <memory> // lines 1-1

The full include-list for main.h:
#include <__memory/shared_ptr.h> // for shared_ptr
class IFontMgr; // lines 3-3
---
--- Calculating IWYU violations for main.cpp ---
Ignoring use of std::shared_ptr::operator-> (main.cpp:5:11): member of class
Ignoring use of std::shared_ptr::operator-> (main.cpp:5:11): member of class
Ignoring use of std::shared_ptr::operator-> (main.cpp:5:11): member of class
Ignoring use of IFontMgr::getFont (main.cpp:5:3): member of class
Marked use of include-file (from "main.h") at <invalid loc>
Mapped main.h to "main.h" for FontRenderer (only candidate)
Mapped /usr/lib/llvm-16/include/c++/v1/__memory/shared_ptr.h to <__memory/shared_ptr.h> for std::shared_ptr (only candidate)
Mapped main.h to "main.h" for FontRenderer (only candidate)
Ignoring full use of FontRenderer (main.cpp:4:6): #including dfn from "main.h"
Ignoring full use of FontRenderer (main.cpp:5:3): #including dfn from "main.h"
Ignoring full use of (<invalid loc>): #including dfn from "main.h"
main.cpp:5:3: warning: std::shared_ptr is defined in <__memory/shared_ptr.h>, which isn't directly #included.

main.cpp should add these lines:

main.cpp should remove these lines:
- #include "FontMgr.h" // lines 2-2

The full include-list for main.cpp:
#include "main.h"
---
```

Seems that the use of FontMgr is ignored because of "member of class".


Reply all
Reply to author
Forward
0 new messages