[clang] [clang] Do not compute typo-correction suggestions for disabled diagnostics (PR #209694)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 15 00:49:46 PDT 2026
https://github.com/AnonMiraj created https://github.com/llvm/llvm-project/pull/209694
While benchmarking I noticed that Boost.MPL compiles with ~1.2% more instructions
after #140629.
clang spends a fair amount of time computing typo-correction
suggestions for diagnostics that are never emitted, for example Boost doesn't contain any
typos, so all of this work produces nothing.
We can easily avoid this overhead by checking `isIgnored()` before computing
suggestions, and by not treating known non-conditional directives as typos.
You can see the improvement here:
https://llvm-compile-time-tracker.com/compare.php?from=49de424f45389cb757c3cc8c50daf38d024e2314&to=89a68cd24f9fabf15897d7b20b77bb5b0bfb9c16&stat=instructions%3Au
>From 89a68cd24f9fabf15897d7b20b77bb5b0bfb9c16 Mon Sep 17 00:00:00 2001
From: Anonmiraj <ezzibrahimx at gmail.com>
Date: Wed, 15 Jul 2026 06:44:23 +0300
Subject: [PATCH] Do not compute typo-correction suggestions for disabled
diagnostics
---
clang/lib/Lex/PPDirectives.cpp | 15 +++++++++++++++
clang/lib/Sema/SemaDeclAttr.cpp | 6 ++++++
2 files changed, 21 insertions(+)
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index eb21a510dcf83..418f450ba470b 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -515,6 +515,21 @@ void Preprocessor::SuggestTypoedDirective(const Token &Tok,
// directives.
if (getLangOpts().AsmPreprocessor) return;
+ // A known non-conditional directive (e.g. #include or #define inside a
+ // skipped conditional block) is not a typo of a conditional don't scan
+ // it. #elifdef/#elifndef stay eligible so that pre-C23/C++23 code still
+ // gets the "did you mean #elif" suggestion.
+ if (tok::PPKeywordKind K = getIdentifierInfo(Directive)->getPPKeywordID();
+ K != tok::pp_not_keyword && K != tok::pp_elifdef &&
+ K != tok::pp_elifndef)
+ return;
+
+ // The scan only feeds this diagnostic; skip it when the diagnostic is
+ // disabled at this location (e.g. -w).
+ if (getDiagnostics().isIgnored(diag::warn_pp_invalid_directive,
+ Tok.getLocation()))
+ return;
+
std::vector<StringRef> Candidates = {
"if", "ifdef", "ifndef", "elif", "else", "endif"
};
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 2159c586e5738..f781dc489ed2b 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -8637,6 +8637,12 @@ void Sema::checkUnusedDeclAttributes(Declarator &D) {
void Sema::DiagnoseUnknownAttribute(const ParsedAttr &AL) {
SourceRange NR = AL.getNormalizedRange();
+ // Skip the expensive spelling-list typo-correction scan when the
+ // diagnostics it feeds are disabled at this location.
+ if (Diags.isIgnored(diag::warn_unknown_attribute_ignored, NR.getBegin()) &&
+ Diags.isIgnored(diag::warn_unknown_attribute_ignored_suggestion,
+ NR.getBegin()))
+ return;
StringRef ScopeName = AL.getNormalizedScopeName();
std::optional<StringRef> CorrectedScopeName =
AL.tryGetCorrectedScopeName(ScopeName);
More information about the cfe-commits
mailing list