[clang-tools-extra] [clang-tidy] Fix false positive in readability-redundant-preprocessor for builtin checks (PR #181734)
Aditya Singh via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 18 05:54:46 PST 2026
================
@@ -69,6 +72,44 @@ class RedundantPreprocessorCallbacks : public PPCallbacks {
}
private:
+ /// Extract the condition text following the 'if' keyword from source.
+ /// Loc points to the 'if' keyword token, so we find its end and read
+ /// forward through the source buffer to the end of the directive,
+ /// handling backslash line continuations.
+ StringRef getConditionText(SourceLocation Loc) {
+ const SourceManager &SM = PP.getSourceManager();
+ const SourceLocation SpellingLoc = SM.getSpellingLoc(Loc);
+
+ const SourceLocation AfterIf =
+ Lexer::getLocForEndOfToken(SpellingLoc, 0, SM, PP.getLangOpts());
+ if (AfterIf.isInvalid())
+ return {};
+
+ bool Invalid = false;
+ auto [FID, Offset] = SM.getDecomposedLoc(AfterIf);
+ const StringRef Buffer = SM.getBufferData(FID, &Invalid);
+ if (Invalid || Offset >= Buffer.size())
+ return {};
+
+ // Read to end-of-line, handling backslash line continuations.
----------------
Aditya26189 wrote:
I tried Lexer::getRawToken but It doesn't know where a #if line ends and tok::eod only exists inside the preprocessor, not in raw lexing mode. Hence, Out of 12 warnings its showing only 4.
The buffer reads the raw source bytes directly, which gives me the exact original text with correct spacing and explicit newline boundaries.
https://github.com/llvm/llvm-project/pull/181734
More information about the cfe-commits
mailing list