[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 15:50:38 PDT 2026
https://github.com/AnonMiraj updated https://github.com/llvm/llvm-project/pull/209694
>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 1/3] 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);
>From fd7a20179d9455d771fec81d0364c3fc472718b4 Mon Sep 17 00:00:00 2001
From: Anonmiraj <ezzibrahimx at gmail.com>
Date: Wed, 15 Jul 2026 10:53:16 +0300
Subject: [PATCH 2/3] fix formatting
---
clang/lib/Lex/PPDirectives.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 418f450ba470b..43805a91cf9d8 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -520,8 +520,7 @@ void Preprocessor::SuggestTypoedDirective(const Token &Tok,
// 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)
+ 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
>From be3ef404b9b911089f54c71ca9f97c4433e5fcfb Mon Sep 17 00:00:00 2001
From: Anonmiraj <ezzibrahimx at gmail.com>
Date: Thu, 16 Jul 2026 01:49:52 +0300
Subject: [PATCH 3/3] address reviews
---
clang/lib/Lex/PPDirectives.cpp | 28 +++++++++++++---------------
1 file changed, 13 insertions(+), 15 deletions(-)
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 43805a91cf9d8..b14a4bdd736db 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -325,8 +325,8 @@ static bool warnByDefaultOnWrongCase(StringRef Include) {
///
/// \returns a similar string if exists. If no similar string exists,
/// returns std::nullopt.
-static std::optional<StringRef>
-findSimilarStr(StringRef LHS, const std::vector<StringRef> &Candidates) {
+static std::optional<StringRef> findSimilarStr(StringRef LHS,
+ ArrayRef<StringRef> Candidates) {
// We need to check if `Candidates` has the exact case-insensitive string
// because the Levenshtein distance match does not care about it.
for (StringRef C : Candidates) {
@@ -515,25 +515,23 @@ 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"
- };
- if (LangOpts.C23 || LangOpts.CPlusPlus23)
- Candidates.insert(Candidates.end(), {"elifdef", "elifndef"});
+ // A known directive (e.g. #include or #define inside a skipped conditional
+ // block) is not a typo of a conditional; don't scan it.
+ if (getIdentifierInfo(Directive)->getPPKeywordID() != tok::pp_not_keyword)
+ return;
+
+ static constexpr StringRef AllCandidates[] = {
+ "if", "ifdef", "ifndef", "elif", "else", "endif", "elifdef", "elifndef"};
+ ArrayRef<StringRef> Candidates(AllCandidates);
+ // #elifdef/#elifndef are only suggested in C23/C++23 and later.
+ if (!LangOpts.C23 && !LangOpts.CPlusPlus23)
+ Candidates = Candidates.drop_back(2);
if (std::optional<StringRef> Sugg = findSimilarStr(Directive, Candidates)) {
// Directive cannot be coming from macro.
More information about the cfe-commits
mailing list