Issue 202945
Summary Enable -Wunused-template by default (add to -Wall)
Labels new issue
Assignees
Reporter flash1729
    `-Wunused-template` already exists in Clang, but it's `DefaultIgnore` and commented out of the `Unused` diagnostic group in `clang/include/clang/Basic/DiagnosticGroups.td`, so it isn't part of `-Wall`. This issue tracks cleaning up the remaining in-tree occurrences and then enabling the warning.

### Why it helps
A `static` (or anonymous-namespace) function template defined in a header has internal linkage, so every translation unit that includes the header gets its own copy. When such a template is referenced from another inline function or template in a header, the definitions across TUs refer to different entities, which is a latent ODR violation (ill-formed, no diagnostic required). `-Wunused-template` surfaces these, and enabling it keeps the pattern from coming back.

### Status
Building LLVM + Clang with the flag enabled:

- Started at 652 raw warnings (46 unique sites) across ~24 files.
- After the cleanup below, the tree builds completely clean (0 warnings).
- libc++/libc++abi/libunwind already build clean (handled in D144667), so the historical "clean up libc++ first" note is stale. The remaining work is in LLVM core and Clang.

### Approach
Three kinds of warning, each with its own fix:

1. `static` or anonymous-namespace function template in a header: remove the internal linkage (drop `static`, or move it out of the anonymous namespace into a named one). Templates are implicitly inline, so nothing is added. This is the ODR fix.
2. Template in a `.cpp` that's never instantiated: dead code, delete it.
3. Template used only inside `assert()` or `LLVM_DEBUG()` (compiled out in release): mark `[[maybe_unused]]`.

### Cleanup PRs (per area, all NFC)

- [ ] `[Object][ELF]` Remove internal linkage from header function templates (NFC)
- [ ] `[Mips]` Remove unused function templates (NFC)
- [ ] `[JITLink][ORC]` Clean up unused and assert-only function templates (NFC)
- [ ] `[VPlan]` Remove internal linkage from `findUserOf` templates (NFC)
- [ ] `[IR]` Remove unused and mark debug-only verifier templates (NFC)
- [ ] `[clang]` Fix `-Wunused-template` in AST/analysis helpers (NFC)
- [ ] `[clang]` Clean up unused/assert-only frontend helpers (NFC)
- [ ] `[FunctionAttrs]` Remove unused legacy-PM `runImpl` template (NFC)
- [ ] `[OpenMP]` Remove unused `isStrictSubset` template (NFC)
- [ ] Assorted library helpers — Support / Bitcode / Coverage / ProfileData / MC / llvm-rc (each NFC)

### Final step

- [ ] `[Clang]` Enable `-Wunused-template` under `-Wall` — uncomment `UnusedTemplate` in `DiagnosticGroups.td` and update tests

### Prior art
- [D29877](https://reviews.llvm.org/D29877): original implementation of `-Wunused-template` (2017).
- [D144667](https://reviews.llvm.org/D144667): `[libc++] Enable -Wunused-template` (landed).
- [D143524](https://reviews.llvm.org/D143524): earlier attempt to enable it by default (didn't land, bandwidth).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to