summaryrefslogtreecommitdiff
path: root/clang/lib/AST/CommentSema.cpp
diff options
context:
space:
mode:
authorOleksandr T. <oleksandr.tarasiuk@outlook.com>2025-05-22 17:03:32 +0300
committerGitHub <noreply@github.com>2025-05-22 17:03:32 +0300
commit50127ac054a08321b64f4ac24bda5cad20e20ac1 (patch)
treeee8eaf7fa55099db7c898b27b2eb12c5fb3754c4 /clang/lib/AST/CommentSema.cpp
parent6eb4adf7460f6b677f372b19758540ec890ef06a (diff)
[Clang] add typo correction for unknown attribute names (#140629)
This patch enhances Clang's diagnosis for unknown attributes by providing typo correction suggestions for known attributes. ```cpp [[gmu::deprected]] // expected-warning {{unknown attribute 'gmu::deprected' ignored; did you mean 'gnu::deprecated'?}} int f1(void) { return 0; } [[deprected]] // expected-warning {{unknown attribute 'deprected' ignored; did you mean 'deprecated'?}} int f2(void) { return 0; } ```
Diffstat (limited to 'clang/lib/AST/CommentSema.cpp')
-rw-r--r--clang/lib/AST/CommentSema.cpp97
1 files changed, 26 insertions, 71 deletions
diff --git a/clang/lib/AST/CommentSema.cpp b/clang/lib/AST/CommentSema.cpp
index bd2206bb8a3b..fb745fc560d2 100644
--- a/clang/lib/AST/CommentSema.cpp
+++ b/clang/lib/AST/CommentSema.cpp
@@ -13,6 +13,7 @@
#include "clang/AST/DeclTemplate.h"
#include "clang/Basic/DiagnosticComment.h"
#include "clang/Basic/LLVM.h"
+#include "clang/Basic/SimpleTypoCorrection.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/Preprocessor.h"
#include "llvm/ADT/StringSwitch.h"
@@ -975,69 +976,22 @@ unsigned Sema::resolveParmVarReference(StringRef Name,
return ParamCommandComment::InvalidParamIndex;
}
-namespace {
-class SimpleTypoCorrector {
- const NamedDecl *BestDecl;
-
- StringRef Typo;
- const unsigned MaxEditDistance;
-
- unsigned BestEditDistance;
- unsigned BestIndex;
- unsigned NextIndex;
-
-public:
- explicit SimpleTypoCorrector(StringRef Typo)
- : BestDecl(nullptr), Typo(Typo), MaxEditDistance((Typo.size() + 2) / 3),
- BestEditDistance(MaxEditDistance + 1), BestIndex(0), NextIndex(0) {}
-
- void addDecl(const NamedDecl *ND);
-
- const NamedDecl *getBestDecl() const {
- if (BestEditDistance > MaxEditDistance)
- return nullptr;
-
- return BestDecl;
- }
+unsigned
+Sema::correctTypoInParmVarReference(StringRef Typo,
+ ArrayRef<const ParmVarDecl *> ParamVars) {
+ SimpleTypoCorrection STC(Typo);
+ for (unsigned i = 0, e = ParamVars.size(); i != e; ++i) {
+ const ParmVarDecl *Param = ParamVars[i];
+ if (!Param)
+ continue;
- unsigned getBestDeclIndex() const {
- assert(getBestDecl());
- return BestIndex;
+ STC.add(Param->getIdentifier());
}
-};
-
-void SimpleTypoCorrector::addDecl(const NamedDecl *ND) {
- unsigned CurrIndex = NextIndex++;
-
- const IdentifierInfo *II = ND->getIdentifier();
- if (!II)
- return;
- StringRef Name = II->getName();
- unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
- if (MinPossibleEditDistance > 0 &&
- Typo.size() / MinPossibleEditDistance < 3)
- return;
+ if (STC.hasCorrection())
+ return STC.getCorrectionIndex();
- unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
- if (EditDistance < BestEditDistance) {
- BestEditDistance = EditDistance;
- BestDecl = ND;
- BestIndex = CurrIndex;
- }
-}
-} // end anonymous namespace
-
-unsigned Sema::correctTypoInParmVarReference(
- StringRef Typo,
- ArrayRef<const ParmVarDecl *> ParamVars) {
- SimpleTypoCorrector Corrector(Typo);
- for (unsigned i = 0, e = ParamVars.size(); i != e; ++i)
- Corrector.addDecl(ParamVars[i]);
- if (Corrector.getBestDecl())
- return Corrector.getBestDeclIndex();
- else
- return ParamCommandComment::InvalidParamIndex;
+ return ParamCommandComment::InvalidParamIndex;
}
namespace {
@@ -1079,16 +1033,18 @@ bool Sema::resolveTParamReference(
namespace {
void CorrectTypoInTParamReferenceHelper(
- const TemplateParameterList *TemplateParameters,
- SimpleTypoCorrector &Corrector) {
+ const TemplateParameterList *TemplateParameters,
+ SimpleTypoCorrection &STC) {
for (unsigned i = 0, e = TemplateParameters->size(); i != e; ++i) {
const NamedDecl *Param = TemplateParameters->getParam(i);
- Corrector.addDecl(Param);
+ if (!Param)
+ continue;
+
+ STC.add(Param->getIdentifier());
if (const TemplateTemplateParmDecl *TTP =
dyn_cast<TemplateTemplateParmDecl>(Param))
- CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(),
- Corrector);
+ CorrectTypoInTParamReferenceHelper(TTP->getTemplateParameters(), STC);
}
}
} // end anonymous namespace
@@ -1096,13 +1052,12 @@ void CorrectTypoInTParamReferenceHelper(
StringRef Sema::correctTypoInTParamReference(
StringRef Typo,
const TemplateParameterList *TemplateParameters) {
- SimpleTypoCorrector Corrector(Typo);
- CorrectTypoInTParamReferenceHelper(TemplateParameters, Corrector);
- if (const NamedDecl *ND = Corrector.getBestDecl()) {
- const IdentifierInfo *II = ND->getIdentifier();
- assert(II && "SimpleTypoCorrector should not return this decl");
- return II->getName();
- }
+ SimpleTypoCorrection STC(Typo);
+ CorrectTypoInTParamReferenceHelper(TemplateParameters, STC);
+
+ if (auto CorrectedTParamReference = STC.getCorrection())
+ return *CorrectedTParamReference;
+
return StringRef();
}