Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include cleaner prefers <assert.h> over <cassert> for assert() in C++ files #62635

Closed
sam-mccall opened this issue May 10, 2023 · 2 comments
Closed

Comments

@sam-mccall
Copy link
Collaborator

sam-mccall commented May 10, 2023

TL;DR: assert.h is getting the PreferredHeader signal due to name match.

// test.cpp
#include <bits/stdc++.h> // assuming libstdc++
void x() { assert(42); }

Clangd correctly produces the diagnostic No header providing "assert" is directly included.
The suggested fix is to insert #include <assert.h>, but we should prefer <cassert>.

<cassert> is listed first in StdSymbolMap.inc, so would in general be preferred, but because the symbol name matches the header name the PreferredHeader signal kicks in.

@llvmbot
Copy link
Collaborator

llvmbot commented May 10, 2023

@llvm/issue-subscribers-clang-include-cleaner

@hokein
Copy link
Collaborator

hokein commented May 11, 2023

The <cassert> should have the PreferredHeader hint applied as well as it is the first candidate, https://github.com/llvm/llvm-project/blob/main/clang-tools-extra/include-cleaner/lib/FindHeaders.cpp#L105-L108.

The both headers have the same ranking signal, but the <cassert> is the first one in the Headers sequence, we should select it. However the sequence is re-arranged when we perform the deduplications here, we sort the array by name (so assert.h become the first one!)

@hokein hokein closed this as completed in 48967c6 Jun 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants