Issue 240048
Summary [include-cleaner] Inherited member operators are attributed to the base class, so misc-include-cleaner disagrees between libstdc++ and libc++
Labels libc++
Assignees
Reporter Spikhalskiy
    `misc-include-cleaner` gives different results for the same file on Linux and macOS:

```cpp
// foo.h
#pragma once
#include <memory>
struct Foo { int x; };
std::shared_ptr<Foo> make_foo();

// main.cpp
#include "foo.h"
#include <memory>
int f() { return make_foo()->x; }
```

With libstdc++ (Linux) it reports `included header memory is not used directly`; with libc++ (macOS) it reports nothing. `make_foo().get()->x` is clean on both.

The difference is where each library declares the operator. libc++ declares `operator->` and `operator*` on `std::shared_ptr`. libstdc++ inherits them from `std::__shared_ptr_access` in `bits/shared_ptr_base.h`. `TraverseCXXOperatorCallExpr` in `clang-tools-extra/include-cleaner/lib/WalkAST.cpp` reports `getMemberProvider(S->getArg(0)->getType())`, and for an inherited operator that argument is a derived-to-base implicit cast, so the use goes to the base class. `VisitMemberExpr` strips the cast with `IgnoreImpCasts()`, which is why `.get()` is attributed to `std::shared_ptr` on both.

Without a standard library:

```cpp
// ptr.h
template <class T> struct PtrBase { T *operator->() const; T *get() const; };
template <class T> struct Ptr : PtrBase<T> {};
```

`make_bar()->x` reports `ptr.h` as unused when `PtrBase` lives in a header that `ptr.h` includes; `make_bar().get()->x` does not.

Fix: use `S->getArg(0)->IgnoreImpCasts()->getType()` in `TraverseCXXOperatorCallExpr`, matching `VisitMemberExpr`. Seen with clang-tidy 22.1.8; the code is the same on `main`.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to