Author: mitchell
Date: 2025-12-21T11:56:18+08:00
New Revision: 14b9478b328e4d8b91feef3b5a0b3b4152182b34

URL: 
https://github.com/llvm/llvm-project/commit/14b9478b328e4d8b91feef3b5a0b3b4152182b34
DIFF: 
https://github.com/llvm/llvm-project/commit/14b9478b328e4d8b91feef3b5a0b3b4152182b34.diff

LOG: [clang-tidy][NFC] Refactor `bugprone-argument-comment` [1/3] (#172521)

This is a necessary step to land #171757

Part of https://github.com/llvm/llvm-project/issues/170921

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp
index cb898beb53c48..16a0fb1b037bf 100644
--- a/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp
@@ -142,37 +142,37 @@ getCommentsBeforeLoc(ASTContext *Ctx, SourceLocation Loc) 
{
   return Comments;
 }
 
-static bool isLikelyTypo(llvm::ArrayRef<ParmVarDecl *> Params,
-                         StringRef ArgName, unsigned ArgIndex) {
+template <typename NamedDeclRange>
+static bool isLikelyTypo(const NamedDeclRange &Candidates, StringRef ArgName,
+                         StringRef TargetName) {
   const std::string ArgNameLowerStr = ArgName.lower();
   const StringRef ArgNameLower = ArgNameLowerStr;
   // The threshold is arbitrary.
   const unsigned UpperBound = ((ArgName.size() + 2) / 3) + 1;
-  const unsigned ThisED = ArgNameLower.edit_distance(
-      Params[ArgIndex]->getIdentifier()->getName().lower(),
-      /*AllowReplacements=*/true, UpperBound);
+  const unsigned ThisED =
+      ArgNameLower.edit_distance(TargetName.lower(),
+                                 /*AllowReplacements=*/true, UpperBound);
   if (ThisED >= UpperBound)
     return false;
 
-  for (unsigned I = 0, E = Params.size(); I != E; ++I) {
-    if (I == ArgIndex)
-      continue;
-    const IdentifierInfo *II = Params[I]->getIdentifier();
+  return llvm::all_of(Candidates, [&](const auto &Candidate) {
+    const IdentifierInfo *II = Candidate->getIdentifier();
     if (!II)
-      continue;
+      return true;
+
+    // Skip the target itself.
+    if (II->getName() == TargetName)
+      return true;
 
     const unsigned Threshold = 2;
-    // Other parameters must be an edit distance at least Threshold more away
-    // from this parameter. This gives us greater confidence that this is a
-    // typo of this parameter and not one with a similar name.
+    // Other candidates must be an edit distance at least Threshold more away
+    // from this candidate. This gives us greater confidence that this is a
+    // typo of this candidate and not one with a similar name.
     const unsigned OtherED = ArgNameLower.edit_distance(
         II->getName().lower(),
         /*AllowReplacements=*/true, ThisED + Threshold);
-    if (OtherED < ThisED + Threshold)
-      return false;
-  }
-
-  return true;
+    return OtherED >= ThisED + Threshold;
+  });
 }
 
 static bool sameName(StringRef InComment, StringRef InDecl, bool StrictMode) {
@@ -318,7 +318,7 @@ void ArgumentCommentCheck::checkCallArgs(ASTContext *Ctx,
               diag(Comment.first, "argument name '%0' in comment does not "
                                   "match parameter name %1")
               << Matches[2] << II;
-          if (isLikelyTypo(Callee->parameters(), Matches[2], I)) {
+          if (isLikelyTypo(Callee->parameters(), Matches[2], II->getName())) {
             Diag << FixItHint::CreateReplacement(
                 Comment.first, (Matches[1] + II->getName() + 
Matches[3]).str());
           }
@@ -334,8 +334,8 @@ void ArgumentCommentCheck::checkCallArgs(ASTContext *Ctx,
 
     // If the argument comments are missing for literals add them.
     if (Comments.empty() && shouldAddComment(Args[I])) {
-      const std::string ArgComment =
-          (llvm::Twine("/*") + II->getName() + "=*/").str();
+      llvm::SmallString<32> ArgComment;
+      (llvm::Twine("/*") + II->getName() + "=*/").toStringRef(ArgComment);
       const DiagnosticBuilder Diag =
           diag(Args[I]->getBeginLoc(),
                "argument comment missing for literal argument %0")


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to