[clang-tidy] readability-redundant-preprocessor false positive on builtin preprocessor checks
#if __has_builtin(__remove_cvref)
# if __has_cpp_attribute(no_unique_address)
# endif
#endif
(Godbolt) generates
1 warning generated.
<source>:2:4: warning: nested redundant #if; consider removing it [readability-redundant-preprocessor]
2 | # if __has_cpp_attribute(no_unique_address)
| ^
<source>:1:2: note: previous #if was here
1 | #if __has_builtin(__remove_cvref)
| ^
1 warning generated.
which is obviously wrong.
@llvm/issue-subscribers-clang-tidy
What I found is that ConditionRange point to ')' character only, and we compare later ')' with ')', oryginal issue is probably somewhere in clang preprocessor, but still easiest fix would be simply to take as a begining of range a token after 'if'.
Following up on @PiotrZSL's finding - the issue is that ConditionRange points to incomplete token locations after macro expansion. For builtin expressions like __has_builtin(__remove_cvref), the preprocessor's ExpandBuiltinMacro replaces the entire expression with a single numeric token, causing the condition range to collapse.
The fix reads the condition text directly from the source buffer by:
- Using
Lexer::getLocForEndOfToken()on theLocparameter (which points to theifkeyword token) to find where the condition starts - Reading forward to end-of-line, handling
\continuations - This recovers the original source text regardless of macro expansion
I have this implemented and tested. Ready to submit a PR if this approach looks good!