[PATCH] D128715: [clang-tidy] Fix confusable identifiers interaction with DeclContext

serge via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 28 01:37:44 PDT 2022


serge-sans-paille created this revision.
serge-sans-paille added a reviewer: aaron.ballman.
Herald added subscribers: carlosgalvezp, xazax.hun.
Herald added a project: All.
serge-sans-paille requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

Properly checks enclosing DeclContext, and add the related test case.

Fix #56221


https://reviews.llvm.org/D128715

Files:
  clang-tools-extra/clang-tidy/misc/ConfusableIdentifierCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/misc/confusable-identifiers.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/misc/confusable-identifiers.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/misc/confusable-identifiers.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/misc/confusable-identifiers.cpp
@@ -18,8 +18,26 @@
 int fi;
 // CHECK-MESSAGES: :[[#@LINE-1]]:5: note: other declaration found here
 
+bool f0(const char *q1, const char *ql) {
+  // CHECK-MESSAGES: :[[#@LINE-1]]:21: warning: q1 is confusable with ql [misc-confusable-identifiers]
+  // CHECK-MESSAGES: :[[#@LINE-2]]:37: note: other declaration found here
+  return q1 < ql;
+}
+
 // should not print anything
 namespace ns {
 struct Foo {};
 } // namespace ns
 auto f = ns::Foo();
+
+struct Test {
+  void f1(const char *pl);
+};
+
+bool f2(const char *p1, const char *ql) {
+  return p1 < ql;
+}
+
+bool f3(const char *q0, const char *q1) {
+  return q0 < q1;
+}
Index: clang-tools-extra/clang-tidy/misc/ConfusableIdentifierCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/misc/ConfusableIdentifierCheck.cpp
+++ clang-tools-extra/clang-tidy/misc/ConfusableIdentifierCheck.cpp
@@ -99,10 +99,9 @@
     if (IdentifierInfo *II = ND->getIdentifier()) {
       StringRef NDName = II->getName();
       llvm::SmallVector<const NamedDecl *> &Mapped = Mapper[skeleton(NDName)];
-      const DeclContext *NDDecl = ND->getDeclContext();
       for (const NamedDecl *OND : Mapped) {
-        if (!NDDecl->isDeclInLexicalTraversal(OND) &&
-            !OND->getDeclContext()->isDeclInLexicalTraversal(ND))
+        if (!ND->getDeclContext()->Encloses(OND->getDeclContext()) &&
+            !OND->getDeclContext()->Encloses(ND->getDeclContext()))
           continue;
         if (OND->getIdentifier()->getName() != NDName) {
           diag(OND->getLocation(), "%0 is confusable with %1")


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128715.440529.patch
Type: text/x-patch
Size: 1916 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220628/b44935f0/attachment.bin>


More information about the cfe-commits mailing list