llvm / llvm/llvm-project

Missing `-Wrange-loop-construct` when class is annotated with `[[clang::trivial_abi]]`

Open
#182,177 2 comments 0 reactions 0 assignees View on GitHub
clang:diagnostics false-negative
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Compile this with `-Wrange-loop-construct`:
```c++
struct [[clang::trivial_abi]] Ptr {
Ptr();
Ptr(const Ptr&);
Ptr(Ptr&&) noexcept;
Ptr& operator=(const Ptr&);
Ptr& operator=(Ptr&&) noexcept;
~Ptr();

int& operator*() const;
};

void f() {
Ptr ptrs[1];
for (const auto p : ptrs)
*p = 0;
}
```
There is no warning. But there is one if we remove the `[[clang::trivial_abi]]` attribute:
```
:14:21: warning: loop variable 'p' creates a copy from type 'const Ptr' [-Wrange-loop-construct]
14 | for (const auto p : ptrs)
| ^
:14:10: note: use reference type 'const Ptr &' to prevent copying
14 | for (const auto p : ptrs)
| ^~~~~~~~~~~~~~
| &
```
I don't see why the warning should disappear. The attribute simply means that the class can be passed in registers, but this does not make the copy trivial.

Contributor guide

Open the contributing guide

Research direction

Start by compiling the provided C++ reproducer with Clang and -Wrange-loop-construct, then trace the implementation of that diagnostic and how [[clang::trivial_abi]] affects its analysis. Done means the warning and reference suggestion are produced for the annotated class, with a regression test covering the reproducer.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.