[clang-tools-extra] [clang-tidy] Improved modernize-use-using by fixing a false-negative (PR #82947)

2024-03-26 Thread Piotr Zegar via cfe-commits

https://github.com/PiotrZSL closed 
https://github.com/llvm/llvm-project/pull/82947
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Improved modernize-use-using by fixing a false-negative (PR #82947)

2024-03-23 Thread via cfe-commits

github-actions[bot] wrote:



:white_check_mark: With the latest revision this PR passed the Python code 
formatter.

https://github.com/llvm/llvm-project/pull/82947
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Improved modernize-use-using by fixing a false-negative (PR #82947)

2024-03-23 Thread via cfe-commits

github-actions[bot] wrote:



:white_check_mark: With the latest revision this PR passed the C/C++ code 
formatter.

https://github.com/llvm/llvm-project/pull/82947
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Improved modernize-use-using by fixing a false-negative (PR #82947)

2024-03-23 Thread Félix-Antoine Constantin via cfe-commits

https://github.com/felix642 updated 
https://github.com/llvm/llvm-project/pull/82947

From 72f2b398d6f41372dc84579a83d273398884c869 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?=
 
Date: Sun, 25 Feb 2024 20:20:59 -0500
Subject: [PATCH] [clang-tidy] Improved modernize-use-using by fixing a
 false-negative

The check needs a parent decl to match but if the typedef is in a function,
the parent is a declStmt which is not a decl by itself.
Improved the matcher to match on either a decl or a declstmt and extract
the decl from the stmt in the latter case.

fixes #72179
---
 .../clang-tidy/modernize/UseUsingCheck.cpp| 24 +--
 clang-tools-extra/docs/ReleaseNotes.rst   |  3 ++
 .../checkers/modernize/use-using.cpp  | 43 ++-
 3 files changed, 66 insertions(+), 4 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
index bb05f206c717ce..24eefdb082eb32 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
@@ -8,6 +8,7 @@
 
 #include "UseUsingCheck.h"
 #include "clang/AST/ASTContext.h"
+#include "clang/AST/DeclGroup.h"
 #include "clang/Lex/Lexer.h"
 
 using namespace clang::ast_matchers;
@@ -24,6 +25,7 @@ static constexpr llvm::StringLiteral ExternCDeclName = 
"extern-c-decl";
 static constexpr llvm::StringLiteral ParentDeclName = "parent-decl";
 static constexpr llvm::StringLiteral TagDeclName = "tag-decl";
 static constexpr llvm::StringLiteral TypedefName = "typedef";
+static constexpr llvm::StringLiteral DeclStmtName = "decl-stmt";
 
 UseUsingCheck::UseUsingCheck(StringRef Name, ClangTidyContext *Context)
 : ClangTidyCheck(Name, Context),
@@ -41,7 +43,8 @@ void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
   unless(isInstantiated()),
   optionally(hasAncestor(
   linkageSpecDecl(isExternCLinkage()).bind(ExternCDeclName))),
-  hasParent(decl().bind(ParentDeclName)))
+  anyOf(hasParent(decl().bind(ParentDeclName)),
+hasParent(declStmt().bind(DeclStmtName
   .bind(TypedefName),
   this);
 
@@ -51,17 +54,32 @@ void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
   tagDecl(
   anyOf(allOf(unless(anyOf(isImplicit(),
classTemplateSpecializationDecl())),
-  hasParent(decl().bind(ParentDeclName))),
+  anyOf(hasParent(decl().bind(ParentDeclName)),
+hasParent(declStmt().bind(DeclStmtName,
 // We want the parent of the ClassTemplateDecl, not the parent
 // of the specialization.
 classTemplateSpecializationDecl(hasAncestor(classTemplateDecl(
-hasParent(decl().bind(ParentDeclName)))
+anyOf(hasParent(decl().bind(ParentDeclName)),
+  hasParent(declStmt().bind(DeclStmtName
   .bind(TagDeclName),
   this);
 }
 
 void UseUsingCheck::check(const MatchFinder::MatchResult ) {
   const auto *ParentDecl = Result.Nodes.getNodeAs(ParentDeclName);
+
+  if (!ParentDecl) {
+const auto *ParentDeclStmt = 
Result.Nodes.getNodeAs(DeclStmtName);
+if (ParentDeclStmt) {
+  if (ParentDeclStmt->isSingleDecl())
+ParentDecl = ParentDeclStmt->getSingleDecl();
+  else
+ParentDecl =
+ParentDeclStmt->getDeclGroup().getDeclGroup()
+[ParentDeclStmt->getDeclGroup().getDeclGroup().size() - 1];
+}
+  }
+
   if (!ParentDecl)
 return;
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index a604e9276668ae..14c7064930bbbc 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -235,6 +235,9 @@ Changes in existing checks
   analyzed, se the check now handles the common patterns
   `const auto e = (*vector_ptr)[i]` and `const auto e = vector_ptr->at(i);`.
 
+- Improved :doc:`modernize-use-using `
+  check by adding support for detection of typedefs declared on function level.
+
 - Improved :doc:`readability-implicit-bool-conversion
   ` check to provide
   valid fix suggestions for ``static_cast`` without a preceding space and
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
index 462bc984fd3ad5..925e5f9c1ca54e 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy %s modernize-use-using %t -- -- -I 
%S/Inputs/use-using/
+// RUN: %check_clang_tidy %s modernize-use-using %t -- -- 
-fno-delayed-template-parsing -I %S/Inputs/use-using/
 
 typedef int Type;
 // CHECK-MESSAGES: :[[@LINE-1]]:1: 

[clang-tools-extra] [clang-tidy] Improved modernize-use-using by fixing a false-negative (PR #82947)

2024-03-17 Thread Félix-Antoine Constantin via cfe-commits

felix642 wrote:

@PiotrZSL I've squashed my changed. When you'll have time, I think we can merge 
this PR. 

https://github.com/llvm/llvm-project/pull/82947
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Improved modernize-use-using by fixing a false-negative (PR #82947)

2024-03-17 Thread Félix-Antoine Constantin via cfe-commits

https://github.com/felix642 updated 
https://github.com/llvm/llvm-project/pull/82947

From e67122cb2c17395e863725cc25d99afe6c7bb48c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?=
 
Date: Sun, 25 Feb 2024 20:20:59 -0500
Subject: [PATCH] [clang-tidy] Improved modernize-use-using by fixing a
 false-negative

The check needs a parent decl to match but if the typedef is in a function,
the parent is a declStmt which is not a decl by itself.
Improved the matcher to match on either a decl or a declstmt and extract
the decl from the stmt in the latter case.

fixes #72179
---
 .../clang-tidy/modernize/UseUsingCheck.cpp| 24 +--
 clang-tools-extra/docs/ReleaseNotes.rst   |  3 ++
 .../checkers/modernize/use-using.cpp  | 43 ++-
 3 files changed, 66 insertions(+), 4 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
index bb05f206c717cea..24eefdb082eb32a 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
@@ -8,6 +8,7 @@
 
 #include "UseUsingCheck.h"
 #include "clang/AST/ASTContext.h"
+#include "clang/AST/DeclGroup.h"
 #include "clang/Lex/Lexer.h"
 
 using namespace clang::ast_matchers;
@@ -24,6 +25,7 @@ static constexpr llvm::StringLiteral ExternCDeclName = 
"extern-c-decl";
 static constexpr llvm::StringLiteral ParentDeclName = "parent-decl";
 static constexpr llvm::StringLiteral TagDeclName = "tag-decl";
 static constexpr llvm::StringLiteral TypedefName = "typedef";
+static constexpr llvm::StringLiteral DeclStmtName = "decl-stmt";
 
 UseUsingCheck::UseUsingCheck(StringRef Name, ClangTidyContext *Context)
 : ClangTidyCheck(Name, Context),
@@ -41,7 +43,8 @@ void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
   unless(isInstantiated()),
   optionally(hasAncestor(
   linkageSpecDecl(isExternCLinkage()).bind(ExternCDeclName))),
-  hasParent(decl().bind(ParentDeclName)))
+  anyOf(hasParent(decl().bind(ParentDeclName)),
+hasParent(declStmt().bind(DeclStmtName
   .bind(TypedefName),
   this);
 
@@ -51,17 +54,32 @@ void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
   tagDecl(
   anyOf(allOf(unless(anyOf(isImplicit(),
classTemplateSpecializationDecl())),
-  hasParent(decl().bind(ParentDeclName))),
+  anyOf(hasParent(decl().bind(ParentDeclName)),
+hasParent(declStmt().bind(DeclStmtName,
 // We want the parent of the ClassTemplateDecl, not the parent
 // of the specialization.
 classTemplateSpecializationDecl(hasAncestor(classTemplateDecl(
-hasParent(decl().bind(ParentDeclName)))
+anyOf(hasParent(decl().bind(ParentDeclName)),
+  hasParent(declStmt().bind(DeclStmtName
   .bind(TagDeclName),
   this);
 }
 
 void UseUsingCheck::check(const MatchFinder::MatchResult ) {
   const auto *ParentDecl = Result.Nodes.getNodeAs(ParentDeclName);
+
+  if (!ParentDecl) {
+const auto *ParentDeclStmt = 
Result.Nodes.getNodeAs(DeclStmtName);
+if (ParentDeclStmt) {
+  if (ParentDeclStmt->isSingleDecl())
+ParentDecl = ParentDeclStmt->getSingleDecl();
+  else
+ParentDecl =
+ParentDeclStmt->getDeclGroup().getDeclGroup()
+[ParentDeclStmt->getDeclGroup().getDeclGroup().size() - 1];
+}
+  }
+
   if (!ParentDecl)
 return;
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 44680f79de6f543..7e4c75b5c3a2948 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -224,6 +224,9 @@ Changes in existing checks
   analyzed, se the check now handles the common patterns
   `const auto e = (*vector_ptr)[i]` and `const auto e = vector_ptr->at(i);`.
 
+- Improved :doc:`modernize-use-using `
+  check by adding support for detection of typedefs declared on function level.
+
 - Improved :doc:`readability-implicit-bool-conversion
   ` check to provide
   valid fix suggestions for ``static_cast`` without a preceding space and
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
index 462bc984fd3ad56..925e5f9c1ca54e8 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy %s modernize-use-using %t -- -- -I 
%S/Inputs/use-using/
+// RUN: %check_clang_tidy %s modernize-use-using %t -- -- 
-fno-delayed-template-parsing -I %S/Inputs/use-using/
 
 typedef int Type;
 // CHECK-MESSAGES: 

[clang-tools-extra] [clang-tidy] Improved modernize-use-using by fixing a false-negative (PR #82947)

2024-03-10 Thread Félix-Antoine Constantin via cfe-commits

https://github.com/felix642 ready_for_review 
https://github.com/llvm/llvm-project/pull/82947
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Improved modernize-use-using by fixing a false-negative (PR #82947)

2024-03-10 Thread Félix-Antoine Constantin via cfe-commits

https://github.com/felix642 updated 
https://github.com/llvm/llvm-project/pull/82947

From f1d957413bcc81e9ced06ad40570859769f4c1c1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?=
 
Date: Sun, 25 Feb 2024 20:20:59 -0500
Subject: [PATCH 1/6] [clang-tidy] Improved modernize-use-using by fixing a
 false-negative

The check needs a parent decl to match but if the typedef is in a function,
the parent is a declStmt which is not a decl by itself.
Improved the matcher to match on either a decl or a declstmt and extract
the decl from the stmt in the latter case.

fixes #72179
---
 .../clang-tidy/modernize/UseUsingCheck.cpp | 16 +---
 clang-tools-extra/docs/ReleaseNotes.rst|  3 +++
 .../checkers/modernize/use-using.cpp   | 18 ++
 3 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
index bb05f206c717ce..50a07fc02e31b4 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
@@ -24,6 +24,7 @@ static constexpr llvm::StringLiteral ExternCDeclName = 
"extern-c-decl";
 static constexpr llvm::StringLiteral ParentDeclName = "parent-decl";
 static constexpr llvm::StringLiteral TagDeclName = "tag-decl";
 static constexpr llvm::StringLiteral TypedefName = "typedef";
+static constexpr llvm::StringLiteral DeclStmtName = "decl-stmt";
 
 UseUsingCheck::UseUsingCheck(StringRef Name, ClangTidyContext *Context)
 : ClangTidyCheck(Name, Context),
@@ -41,7 +42,8 @@ void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
   unless(isInstantiated()),
   optionally(hasAncestor(
   linkageSpecDecl(isExternCLinkage()).bind(ExternCDeclName))),
-  hasParent(decl().bind(ParentDeclName)))
+  anyOf(hasParent(decl().bind(ParentDeclName)),
+hasParent(declStmt().bind(DeclStmtName
   .bind(TypedefName),
   this);
 
@@ -51,17 +53,25 @@ void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
   tagDecl(
   anyOf(allOf(unless(anyOf(isImplicit(),
classTemplateSpecializationDecl())),
-  hasParent(decl().bind(ParentDeclName))),
+  anyOf(hasParent(decl().bind(ParentDeclName)),
+hasParent(declStmt().bind(DeclStmtName,
 // We want the parent of the ClassTemplateDecl, not the parent
 // of the specialization.
 classTemplateSpecializationDecl(hasAncestor(classTemplateDecl(
-hasParent(decl().bind(ParentDeclName)))
+anyOf(hasParent(decl().bind(ParentDeclName)),
+  hasParent(declStmt().bind(DeclStmtName
   .bind(TagDeclName),
   this);
 }
 
 void UseUsingCheck::check(const MatchFinder::MatchResult ) {
   const auto *ParentDecl = Result.Nodes.getNodeAs(ParentDeclName);
+
+  if (!ParentDecl) {
+const auto *ParentDeclStmt = 
Result.Nodes.getNodeAs(DeclStmtName);
+ParentDecl = ParentDeclStmt->getSingleDecl();
+  }
+
   if (!ParentDecl)
 return;
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 44680f79de6f54..479b9b86444184 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -224,6 +224,9 @@ Changes in existing checks
   analyzed, se the check now handles the common patterns
   `const auto e = (*vector_ptr)[i]` and `const auto e = vector_ptr->at(i);`.
 
+- Improved :doc:`modernize-use-using `
+  check by fixing false-negative in functions.
+
 - Improved :doc:`readability-implicit-bool-conversion
   ` check to provide
   valid fix suggestions for ``static_cast`` without a preceding space and
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
index 462bc984fd3ad5..230c7c94cda1cf 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
@@ -342,3 +342,21 @@ typedef int InExternCPP;
 // CHECK-FIXES: using InExternCPP = int;
 
 }
+
+namespace ISSUE_72179
+{  
+  void foo()
+  {
+typedef int a;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use 'using' instead of 
'typedef' [modernize-use-using]
+// CHECK-FIXES: using a = int;
+
+  }
+
+  void foo2()
+  {
+typedef struct { int a; union { int b; }; } c;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use 'using' instead of 
'typedef' [modernize-use-using]
+// CHECK-FIXES: using c = struct { int a; union { int b; }; };
+  }
+}

From e7dcbbcf92e896354e9a52da259251a7d3a74369 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?=
 
Date: Sun, 25 Feb 2024 22:14:22 -0500

[clang-tools-extra] [clang-tidy] Improved modernize-use-using by fixing a false-negative (PR #82947)

2024-03-10 Thread Félix-Antoine Constantin via cfe-commits

https://github.com/felix642 converted_to_draft 
https://github.com/llvm/llvm-project/pull/82947
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Improved modernize-use-using by fixing a false-negative (PR #82947)

2024-03-10 Thread Félix-Antoine Constantin via cfe-commits

felix642 wrote:

Thank you for the quick review @PiotrZSL. I've implemented the proposed changes 
and rebased on top of main. We can wait a few days to see if someone else has 
some comments to add and then we should be ready to merge 

https://github.com/llvm/llvm-project/pull/82947
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Improved modernize-use-using by fixing a false-negative (PR #82947)

2024-03-10 Thread Félix-Antoine Constantin via cfe-commits

https://github.com/felix642 ready_for_review 
https://github.com/llvm/llvm-project/pull/82947
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Improved modernize-use-using by fixing a false-negative (PR #82947)

2024-03-10 Thread Félix-Antoine Constantin via cfe-commits

https://github.com/felix642 updated 
https://github.com/llvm/llvm-project/pull/82947

From f1d957413bcc81e9ced06ad40570859769f4c1c1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?=
 
Date: Sun, 25 Feb 2024 20:20:59 -0500
Subject: [PATCH 1/5] [clang-tidy] Improved modernize-use-using by fixing a
 false-negative

The check needs a parent decl to match but if the typedef is in a function,
the parent is a declStmt which is not a decl by itself.
Improved the matcher to match on either a decl or a declstmt and extract
the decl from the stmt in the latter case.

fixes #72179
---
 .../clang-tidy/modernize/UseUsingCheck.cpp | 16 +---
 clang-tools-extra/docs/ReleaseNotes.rst|  3 +++
 .../checkers/modernize/use-using.cpp   | 18 ++
 3 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
index bb05f206c717ce..50a07fc02e31b4 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
@@ -24,6 +24,7 @@ static constexpr llvm::StringLiteral ExternCDeclName = 
"extern-c-decl";
 static constexpr llvm::StringLiteral ParentDeclName = "parent-decl";
 static constexpr llvm::StringLiteral TagDeclName = "tag-decl";
 static constexpr llvm::StringLiteral TypedefName = "typedef";
+static constexpr llvm::StringLiteral DeclStmtName = "decl-stmt";
 
 UseUsingCheck::UseUsingCheck(StringRef Name, ClangTidyContext *Context)
 : ClangTidyCheck(Name, Context),
@@ -41,7 +42,8 @@ void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
   unless(isInstantiated()),
   optionally(hasAncestor(
   linkageSpecDecl(isExternCLinkage()).bind(ExternCDeclName))),
-  hasParent(decl().bind(ParentDeclName)))
+  anyOf(hasParent(decl().bind(ParentDeclName)),
+hasParent(declStmt().bind(DeclStmtName
   .bind(TypedefName),
   this);
 
@@ -51,17 +53,25 @@ void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
   tagDecl(
   anyOf(allOf(unless(anyOf(isImplicit(),
classTemplateSpecializationDecl())),
-  hasParent(decl().bind(ParentDeclName))),
+  anyOf(hasParent(decl().bind(ParentDeclName)),
+hasParent(declStmt().bind(DeclStmtName,
 // We want the parent of the ClassTemplateDecl, not the parent
 // of the specialization.
 classTemplateSpecializationDecl(hasAncestor(classTemplateDecl(
-hasParent(decl().bind(ParentDeclName)))
+anyOf(hasParent(decl().bind(ParentDeclName)),
+  hasParent(declStmt().bind(DeclStmtName
   .bind(TagDeclName),
   this);
 }
 
 void UseUsingCheck::check(const MatchFinder::MatchResult ) {
   const auto *ParentDecl = Result.Nodes.getNodeAs(ParentDeclName);
+
+  if (!ParentDecl) {
+const auto *ParentDeclStmt = 
Result.Nodes.getNodeAs(DeclStmtName);
+ParentDecl = ParentDeclStmt->getSingleDecl();
+  }
+
   if (!ParentDecl)
 return;
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 44680f79de6f54..479b9b86444184 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -224,6 +224,9 @@ Changes in existing checks
   analyzed, se the check now handles the common patterns
   `const auto e = (*vector_ptr)[i]` and `const auto e = vector_ptr->at(i);`.
 
+- Improved :doc:`modernize-use-using `
+  check by fixing false-negative in functions.
+
 - Improved :doc:`readability-implicit-bool-conversion
   ` check to provide
   valid fix suggestions for ``static_cast`` without a preceding space and
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
index 462bc984fd3ad5..230c7c94cda1cf 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
@@ -342,3 +342,21 @@ typedef int InExternCPP;
 // CHECK-FIXES: using InExternCPP = int;
 
 }
+
+namespace ISSUE_72179
+{  
+  void foo()
+  {
+typedef int a;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use 'using' instead of 
'typedef' [modernize-use-using]
+// CHECK-FIXES: using a = int;
+
+  }
+
+  void foo2()
+  {
+typedef struct { int a; union { int b; }; } c;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use 'using' instead of 
'typedef' [modernize-use-using]
+// CHECK-FIXES: using c = struct { int a; union { int b; }; };
+  }
+}

From e7dcbbcf92e896354e9a52da259251a7d3a74369 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?=
 
Date: Sun, 25 Feb 2024 22:14:22 -0500

[clang-tools-extra] [clang-tidy] Improved modernize-use-using by fixing a false-negative (PR #82947)

2024-03-10 Thread Félix-Antoine Constantin via cfe-commits

https://github.com/felix642 updated 
https://github.com/llvm/llvm-project/pull/82947

From d1cbed0e2e83bd3544067fd25d7e811f1ab3f095 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?=
 
Date: Sun, 25 Feb 2024 20:20:59 -0500
Subject: [PATCH 1/4] [clang-tidy] Improved modernize-use-using by fixing a
 false-negative

The check needs a parent decl to match but if the typedef is in a function,
the parent is a declStmt which is not a decl by itself.
Improved the matcher to match on either a decl or a declstmt and extract
the decl from the stmt in the latter case.

fixes #72179
---
 .../clang-tidy/modernize/UseUsingCheck.cpp | 16 +---
 clang-tools-extra/docs/ReleaseNotes.rst|  3 +++
 .../checkers/modernize/use-using.cpp   | 18 ++
 3 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
index bb05f206c717ce..50a07fc02e31b4 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
@@ -24,6 +24,7 @@ static constexpr llvm::StringLiteral ExternCDeclName = 
"extern-c-decl";
 static constexpr llvm::StringLiteral ParentDeclName = "parent-decl";
 static constexpr llvm::StringLiteral TagDeclName = "tag-decl";
 static constexpr llvm::StringLiteral TypedefName = "typedef";
+static constexpr llvm::StringLiteral DeclStmtName = "decl-stmt";
 
 UseUsingCheck::UseUsingCheck(StringRef Name, ClangTidyContext *Context)
 : ClangTidyCheck(Name, Context),
@@ -41,7 +42,8 @@ void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
   unless(isInstantiated()),
   optionally(hasAncestor(
   linkageSpecDecl(isExternCLinkage()).bind(ExternCDeclName))),
-  hasParent(decl().bind(ParentDeclName)))
+  anyOf(hasParent(decl().bind(ParentDeclName)),
+hasParent(declStmt().bind(DeclStmtName
   .bind(TypedefName),
   this);
 
@@ -51,17 +53,25 @@ void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
   tagDecl(
   anyOf(allOf(unless(anyOf(isImplicit(),
classTemplateSpecializationDecl())),
-  hasParent(decl().bind(ParentDeclName))),
+  anyOf(hasParent(decl().bind(ParentDeclName)),
+hasParent(declStmt().bind(DeclStmtName,
 // We want the parent of the ClassTemplateDecl, not the parent
 // of the specialization.
 classTemplateSpecializationDecl(hasAncestor(classTemplateDecl(
-hasParent(decl().bind(ParentDeclName)))
+anyOf(hasParent(decl().bind(ParentDeclName)),
+  hasParent(declStmt().bind(DeclStmtName
   .bind(TagDeclName),
   this);
 }
 
 void UseUsingCheck::check(const MatchFinder::MatchResult ) {
   const auto *ParentDecl = Result.Nodes.getNodeAs(ParentDeclName);
+
+  if (!ParentDecl) {
+const auto *ParentDeclStmt = 
Result.Nodes.getNodeAs(DeclStmtName);
+ParentDecl = ParentDeclStmt->getSingleDecl();
+  }
+
   if (!ParentDecl)
 return;
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 6fd01ed9d471c5..2b36ae4acb9f1d 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -183,6 +183,9 @@ Changes in existing checks
   ` check to also remove any trailing
   whitespace when deleting the ``virtual`` keyword.
 
+- Improved :doc:`modernize-use-using `
+  check by fixing false-negative in functions.
+
 - Improved :doc:`readability-implicit-bool-conversion
   ` check to provide
   valid fix suggestions for ``static_cast`` without a preceding space and
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
index 462bc984fd3ad5..230c7c94cda1cf 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
@@ -342,3 +342,21 @@ typedef int InExternCPP;
 // CHECK-FIXES: using InExternCPP = int;
 
 }
+
+namespace ISSUE_72179
+{  
+  void foo()
+  {
+typedef int a;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use 'using' instead of 
'typedef' [modernize-use-using]
+// CHECK-FIXES: using a = int;
+
+  }
+
+  void foo2()
+  {
+typedef struct { int a; union { int b; }; } c;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use 'using' instead of 
'typedef' [modernize-use-using]
+// CHECK-FIXES: using c = struct { int a; union { int b; }; };
+  }
+}

From 0b5c78508fbf49229d576171d7af6c6bb16b4367 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?=
 
Date: Sun, 25 Feb 2024 22:14:22 -0500
Subject: [PATCH 2/4] fixup! [clang-tidy] 

[clang-tools-extra] [clang-tidy] Improved modernize-use-using by fixing a false-negative (PR #82947)

2024-03-09 Thread Piotr Zegar via cfe-commits
=?utf-8?q?Félix-Antoine?= Constantin,
=?utf-8?q?Félix-Antoine?= Constantin
Message-ID:
In-Reply-To: 


https://github.com/PiotrZSL edited 
https://github.com/llvm/llvm-project/pull/82947
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Improved modernize-use-using by fixing a false-negative (PR #82947)

2024-03-09 Thread Piotr Zegar via cfe-commits
=?utf-8?q?Félix-Antoine?= Constantin,
=?utf-8?q?Félix-Antoine?= Constantin
Message-ID:
In-Reply-To: 



@@ -342,3 +342,21 @@ typedef int InExternCPP;
 // CHECK-FIXES: using InExternCPP = int;
 
 }
+
+namespace ISSUE_72179
+{  
+  void foo()
+  {
+typedef int a;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use 'using' instead of 
'typedef' [modernize-use-using]
+// CHECK-FIXES: using a = int;
+
+  }
+
+  void foo2()
+  {
+typedef struct { int a; union { int b; }; } c;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use 'using' instead of 
'typedef' [modernize-use-using]
+// CHECK-FIXES: using c = struct { int a; union { int b; }; };
+  }
+}

PiotrZSL wrote:

add test with lambda, template function, normal function in template class.

https://github.com/llvm/llvm-project/pull/82947
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Improved modernize-use-using by fixing a false-negative (PR #82947)

2024-03-09 Thread Piotr Zegar via cfe-commits
=?utf-8?q?F=C3=A9lix-Antoine?= Constantin,
=?utf-8?q?F=C3=A9lix-Antoine?= Constantin
Message-ID:
In-Reply-To: 


https://github.com/PiotrZSL approved this pull request.

Overall fine.

https://github.com/llvm/llvm-project/pull/82947
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Improved modernize-use-using by fixing a false-negative (PR #82947)

2024-03-09 Thread Piotr Zegar via cfe-commits
=?utf-8?q?Félix-Antoine?= Constantin,
=?utf-8?q?Félix-Antoine?= Constantin
Message-ID:
In-Reply-To: 



@@ -183,6 +183,9 @@ Changes in existing checks
   ` check to also remove any trailing
   whitespace when deleting the ``virtual`` keyword.
 
+- Improved :doc:`modernize-use-using `
+  check by fixing false-negative in functions.

PiotrZSL wrote:

maybe: `by adding support for detection of typedefs declared on function level.`

https://github.com/llvm/llvm-project/pull/82947
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Improved modernize-use-using by fixing a false-negative (PR #82947)

2024-03-09 Thread Félix-Antoine Constantin via cfe-commits

https://github.com/felix642 updated 
https://github.com/llvm/llvm-project/pull/82947

From d1cbed0e2e83bd3544067fd25d7e811f1ab3f095 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?=
 
Date: Sun, 25 Feb 2024 20:20:59 -0500
Subject: [PATCH 1/3] [clang-tidy] Improved modernize-use-using by fixing a
 false-negative

The check needs a parent decl to match but if the typedef is in a function,
the parent is a declStmt which is not a decl by itself.
Improved the matcher to match on either a decl or a declstmt and extract
the decl from the stmt in the latter case.

fixes #72179
---
 .../clang-tidy/modernize/UseUsingCheck.cpp | 16 +---
 clang-tools-extra/docs/ReleaseNotes.rst|  3 +++
 .../checkers/modernize/use-using.cpp   | 18 ++
 3 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
index bb05f206c717ce..50a07fc02e31b4 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
@@ -24,6 +24,7 @@ static constexpr llvm::StringLiteral ExternCDeclName = 
"extern-c-decl";
 static constexpr llvm::StringLiteral ParentDeclName = "parent-decl";
 static constexpr llvm::StringLiteral TagDeclName = "tag-decl";
 static constexpr llvm::StringLiteral TypedefName = "typedef";
+static constexpr llvm::StringLiteral DeclStmtName = "decl-stmt";
 
 UseUsingCheck::UseUsingCheck(StringRef Name, ClangTidyContext *Context)
 : ClangTidyCheck(Name, Context),
@@ -41,7 +42,8 @@ void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
   unless(isInstantiated()),
   optionally(hasAncestor(
   linkageSpecDecl(isExternCLinkage()).bind(ExternCDeclName))),
-  hasParent(decl().bind(ParentDeclName)))
+  anyOf(hasParent(decl().bind(ParentDeclName)),
+hasParent(declStmt().bind(DeclStmtName
   .bind(TypedefName),
   this);
 
@@ -51,17 +53,25 @@ void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
   tagDecl(
   anyOf(allOf(unless(anyOf(isImplicit(),
classTemplateSpecializationDecl())),
-  hasParent(decl().bind(ParentDeclName))),
+  anyOf(hasParent(decl().bind(ParentDeclName)),
+hasParent(declStmt().bind(DeclStmtName,
 // We want the parent of the ClassTemplateDecl, not the parent
 // of the specialization.
 classTemplateSpecializationDecl(hasAncestor(classTemplateDecl(
-hasParent(decl().bind(ParentDeclName)))
+anyOf(hasParent(decl().bind(ParentDeclName)),
+  hasParent(declStmt().bind(DeclStmtName
   .bind(TagDeclName),
   this);
 }
 
 void UseUsingCheck::check(const MatchFinder::MatchResult ) {
   const auto *ParentDecl = Result.Nodes.getNodeAs(ParentDeclName);
+
+  if (!ParentDecl) {
+const auto *ParentDeclStmt = 
Result.Nodes.getNodeAs(DeclStmtName);
+ParentDecl = ParentDeclStmt->getSingleDecl();
+  }
+
   if (!ParentDecl)
 return;
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 6fd01ed9d471c5..2b36ae4acb9f1d 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -183,6 +183,9 @@ Changes in existing checks
   ` check to also remove any trailing
   whitespace when deleting the ``virtual`` keyword.
 
+- Improved :doc:`modernize-use-using `
+  check by fixing false-negative in functions.
+
 - Improved :doc:`readability-implicit-bool-conversion
   ` check to provide
   valid fix suggestions for ``static_cast`` without a preceding space and
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
index 462bc984fd3ad5..230c7c94cda1cf 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
@@ -342,3 +342,21 @@ typedef int InExternCPP;
 // CHECK-FIXES: using InExternCPP = int;
 
 }
+
+namespace ISSUE_72179
+{  
+  void foo()
+  {
+typedef int a;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use 'using' instead of 
'typedef' [modernize-use-using]
+// CHECK-FIXES: using a = int;
+
+  }
+
+  void foo2()
+  {
+typedef struct { int a; union { int b; }; } c;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use 'using' instead of 
'typedef' [modernize-use-using]
+// CHECK-FIXES: using c = struct { int a; union { int b; }; };
+  }
+}

From 0b5c78508fbf49229d576171d7af6c6bb16b4367 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?=
 
Date: Sun, 25 Feb 2024 22:14:22 -0500
Subject: [PATCH 2/3] fixup! [clang-tidy] 

[clang-tools-extra] [clang-tidy] Improved modernize-use-using by fixing a false-negative (PR #82947)

2024-02-26 Thread Félix-Antoine Constantin via cfe-commits

https://github.com/felix642 converted_to_draft 
https://github.com/llvm/llvm-project/pull/82947
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Improved modernize-use-using by fixing a false-negative (PR #82947)

2024-02-25 Thread Félix-Antoine Constantin via cfe-commits

https://github.com/felix642 updated 
https://github.com/llvm/llvm-project/pull/82947

From d1cbed0e2e83bd3544067fd25d7e811f1ab3f095 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?=
 
Date: Sun, 25 Feb 2024 20:20:59 -0500
Subject: [PATCH 1/2] [clang-tidy] Improved modernize-use-using by fixing a
 false-negative

The check needs a parent decl to match but if the typedef is in a function,
the parent is a declStmt which is not a decl by itself.
Improved the matcher to match on either a decl or a declstmt and extract
the decl from the stmt in the latter case.

fixes #72179
---
 .../clang-tidy/modernize/UseUsingCheck.cpp | 16 +---
 clang-tools-extra/docs/ReleaseNotes.rst|  3 +++
 .../checkers/modernize/use-using.cpp   | 18 ++
 3 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
index bb05f206c717ce..50a07fc02e31b4 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
@@ -24,6 +24,7 @@ static constexpr llvm::StringLiteral ExternCDeclName = 
"extern-c-decl";
 static constexpr llvm::StringLiteral ParentDeclName = "parent-decl";
 static constexpr llvm::StringLiteral TagDeclName = "tag-decl";
 static constexpr llvm::StringLiteral TypedefName = "typedef";
+static constexpr llvm::StringLiteral DeclStmtName = "decl-stmt";
 
 UseUsingCheck::UseUsingCheck(StringRef Name, ClangTidyContext *Context)
 : ClangTidyCheck(Name, Context),
@@ -41,7 +42,8 @@ void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
   unless(isInstantiated()),
   optionally(hasAncestor(
   linkageSpecDecl(isExternCLinkage()).bind(ExternCDeclName))),
-  hasParent(decl().bind(ParentDeclName)))
+  anyOf(hasParent(decl().bind(ParentDeclName)),
+hasParent(declStmt().bind(DeclStmtName
   .bind(TypedefName),
   this);
 
@@ -51,17 +53,25 @@ void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
   tagDecl(
   anyOf(allOf(unless(anyOf(isImplicit(),
classTemplateSpecializationDecl())),
-  hasParent(decl().bind(ParentDeclName))),
+  anyOf(hasParent(decl().bind(ParentDeclName)),
+hasParent(declStmt().bind(DeclStmtName,
 // We want the parent of the ClassTemplateDecl, not the parent
 // of the specialization.
 classTemplateSpecializationDecl(hasAncestor(classTemplateDecl(
-hasParent(decl().bind(ParentDeclName)))
+anyOf(hasParent(decl().bind(ParentDeclName)),
+  hasParent(declStmt().bind(DeclStmtName
   .bind(TagDeclName),
   this);
 }
 
 void UseUsingCheck::check(const MatchFinder::MatchResult ) {
   const auto *ParentDecl = Result.Nodes.getNodeAs(ParentDeclName);
+
+  if (!ParentDecl) {
+const auto *ParentDeclStmt = 
Result.Nodes.getNodeAs(DeclStmtName);
+ParentDecl = ParentDeclStmt->getSingleDecl();
+  }
+
   if (!ParentDecl)
 return;
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 6fd01ed9d471c5..2b36ae4acb9f1d 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -183,6 +183,9 @@ Changes in existing checks
   ` check to also remove any trailing
   whitespace when deleting the ``virtual`` keyword.
 
+- Improved :doc:`modernize-use-using `
+  check by fixing false-negative in functions.
+
 - Improved :doc:`readability-implicit-bool-conversion
   ` check to provide
   valid fix suggestions for ``static_cast`` without a preceding space and
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
index 462bc984fd3ad5..230c7c94cda1cf 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
@@ -342,3 +342,21 @@ typedef int InExternCPP;
 // CHECK-FIXES: using InExternCPP = int;
 
 }
+
+namespace ISSUE_72179
+{  
+  void foo()
+  {
+typedef int a;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use 'using' instead of 
'typedef' [modernize-use-using]
+// CHECK-FIXES: using a = int;
+
+  }
+
+  void foo2()
+  {
+typedef struct { int a; union { int b; }; } c;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use 'using' instead of 
'typedef' [modernize-use-using]
+// CHECK-FIXES: using c = struct { int a; union { int b; }; };
+  }
+}

From 0b5c78508fbf49229d576171d7af6c6bb16b4367 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?=
 
Date: Sun, 25 Feb 2024 22:14:22 -0500
Subject: [PATCH 2/2] fixup! [clang-tidy] 

[clang-tools-extra] [clang-tidy] Improved modernize-use-using by fixing a false-negative (PR #82947)

2024-02-25 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-tidy

Author: Félix-Antoine Constantin (felix642)


Changes

The check needs a parent decl to match but if the typedef is in a function, the 
parent is a declStmt which is not a decl by itself. Improved the matcher to 
match on either a decl or a declstmt and extract the decl from the stmt in the 
latter case.

fixes #72179

CC: @FabianWolff @PiotrZSL @njames93 

---
Full diff: https://github.com/llvm/llvm-project/pull/82947.diff


3 Files Affected:

- (modified) clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp (+13-3) 
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+3) 
- (modified) clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp 
(+18) 


``diff
diff --git a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
index bb05f206c717ce..50a07fc02e31b4 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
@@ -24,6 +24,7 @@ static constexpr llvm::StringLiteral ExternCDeclName = 
"extern-c-decl";
 static constexpr llvm::StringLiteral ParentDeclName = "parent-decl";
 static constexpr llvm::StringLiteral TagDeclName = "tag-decl";
 static constexpr llvm::StringLiteral TypedefName = "typedef";
+static constexpr llvm::StringLiteral DeclStmtName = "decl-stmt";
 
 UseUsingCheck::UseUsingCheck(StringRef Name, ClangTidyContext *Context)
 : ClangTidyCheck(Name, Context),
@@ -41,7 +42,8 @@ void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
   unless(isInstantiated()),
   optionally(hasAncestor(
   linkageSpecDecl(isExternCLinkage()).bind(ExternCDeclName))),
-  hasParent(decl().bind(ParentDeclName)))
+  anyOf(hasParent(decl().bind(ParentDeclName)),
+hasParent(declStmt().bind(DeclStmtName
   .bind(TypedefName),
   this);
 
@@ -51,17 +53,25 @@ void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
   tagDecl(
   anyOf(allOf(unless(anyOf(isImplicit(),
classTemplateSpecializationDecl())),
-  hasParent(decl().bind(ParentDeclName))),
+  anyOf(hasParent(decl().bind(ParentDeclName)),
+hasParent(declStmt().bind(DeclStmtName,
 // We want the parent of the ClassTemplateDecl, not the parent
 // of the specialization.
 classTemplateSpecializationDecl(hasAncestor(classTemplateDecl(
-hasParent(decl().bind(ParentDeclName)))
+anyOf(hasParent(decl().bind(ParentDeclName)),
+  hasParent(declStmt().bind(DeclStmtName
   .bind(TagDeclName),
   this);
 }
 
 void UseUsingCheck::check(const MatchFinder::MatchResult ) {
   const auto *ParentDecl = Result.Nodes.getNodeAs(ParentDeclName);
+
+  if (!ParentDecl) {
+const auto *ParentDeclStmt = 
Result.Nodes.getNodeAs(DeclStmtName);
+ParentDecl = ParentDeclStmt->getSingleDecl();
+  }
+
   if (!ParentDecl)
 return;
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 6fd01ed9d471c5..2b36ae4acb9f1d 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -183,6 +183,9 @@ Changes in existing checks
   ` check to also remove any trailing
   whitespace when deleting the ``virtual`` keyword.
 
+- Improved :doc:`modernize-use-using `
+  check by fixing false-negative in functions.
+
 - Improved :doc:`readability-implicit-bool-conversion
   ` check to provide
   valid fix suggestions for ``static_cast`` without a preceding space and
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
index 462bc984fd3ad5..230c7c94cda1cf 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
@@ -342,3 +342,21 @@ typedef int InExternCPP;
 // CHECK-FIXES: using InExternCPP = int;
 
 }
+
+namespace ISSUE_72179
+{  
+  void foo()
+  {
+typedef int a;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use 'using' instead of 
'typedef' [modernize-use-using]
+// CHECK-FIXES: using a = int;
+
+  }
+
+  void foo2()
+  {
+typedef struct { int a; union { int b; }; } c;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use 'using' instead of 
'typedef' [modernize-use-using]
+// CHECK-FIXES: using c = struct { int a; union { int b; }; };
+  }
+}

``




https://github.com/llvm/llvm-project/pull/82947
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Improved modernize-use-using by fixing a false-negative (PR #82947)

2024-02-25 Thread Félix-Antoine Constantin via cfe-commits

https://github.com/felix642 updated 
https://github.com/llvm/llvm-project/pull/82947

From d1cbed0e2e83bd3544067fd25d7e811f1ab3f095 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?=
 
Date: Sun, 25 Feb 2024 20:20:59 -0500
Subject: [PATCH] [clang-tidy] Improved modernize-use-using by fixing a
 false-negative

The check needs a parent decl to match but if the typedef is in a function,
the parent is a declStmt which is not a decl by itself.
Improved the matcher to match on either a decl or a declstmt and extract
the decl from the stmt in the latter case.

fixes #72179
---
 .../clang-tidy/modernize/UseUsingCheck.cpp | 16 +---
 clang-tools-extra/docs/ReleaseNotes.rst|  3 +++
 .../checkers/modernize/use-using.cpp   | 18 ++
 3 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
index bb05f206c717ce..50a07fc02e31b4 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
@@ -24,6 +24,7 @@ static constexpr llvm::StringLiteral ExternCDeclName = 
"extern-c-decl";
 static constexpr llvm::StringLiteral ParentDeclName = "parent-decl";
 static constexpr llvm::StringLiteral TagDeclName = "tag-decl";
 static constexpr llvm::StringLiteral TypedefName = "typedef";
+static constexpr llvm::StringLiteral DeclStmtName = "decl-stmt";
 
 UseUsingCheck::UseUsingCheck(StringRef Name, ClangTidyContext *Context)
 : ClangTidyCheck(Name, Context),
@@ -41,7 +42,8 @@ void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
   unless(isInstantiated()),
   optionally(hasAncestor(
   linkageSpecDecl(isExternCLinkage()).bind(ExternCDeclName))),
-  hasParent(decl().bind(ParentDeclName)))
+  anyOf(hasParent(decl().bind(ParentDeclName)),
+hasParent(declStmt().bind(DeclStmtName
   .bind(TypedefName),
   this);
 
@@ -51,17 +53,25 @@ void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
   tagDecl(
   anyOf(allOf(unless(anyOf(isImplicit(),
classTemplateSpecializationDecl())),
-  hasParent(decl().bind(ParentDeclName))),
+  anyOf(hasParent(decl().bind(ParentDeclName)),
+hasParent(declStmt().bind(DeclStmtName,
 // We want the parent of the ClassTemplateDecl, not the parent
 // of the specialization.
 classTemplateSpecializationDecl(hasAncestor(classTemplateDecl(
-hasParent(decl().bind(ParentDeclName)))
+anyOf(hasParent(decl().bind(ParentDeclName)),
+  hasParent(declStmt().bind(DeclStmtName
   .bind(TagDeclName),
   this);
 }
 
 void UseUsingCheck::check(const MatchFinder::MatchResult ) {
   const auto *ParentDecl = Result.Nodes.getNodeAs(ParentDeclName);
+
+  if (!ParentDecl) {
+const auto *ParentDeclStmt = 
Result.Nodes.getNodeAs(DeclStmtName);
+ParentDecl = ParentDeclStmt->getSingleDecl();
+  }
+
   if (!ParentDecl)
 return;
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 6fd01ed9d471c5..2b36ae4acb9f1d 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -183,6 +183,9 @@ Changes in existing checks
   ` check to also remove any trailing
   whitespace when deleting the ``virtual`` keyword.
 
+- Improved :doc:`modernize-use-using `
+  check by fixing false-negative in functions.
+
 - Improved :doc:`readability-implicit-bool-conversion
   ` check to provide
   valid fix suggestions for ``static_cast`` without a preceding space and
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
index 462bc984fd3ad5..230c7c94cda1cf 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
@@ -342,3 +342,21 @@ typedef int InExternCPP;
 // CHECK-FIXES: using InExternCPP = int;
 
 }
+
+namespace ISSUE_72179
+{  
+  void foo()
+  {
+typedef int a;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use 'using' instead of 
'typedef' [modernize-use-using]
+// CHECK-FIXES: using a = int;
+
+  }
+
+  void foo2()
+  {
+typedef struct { int a; union { int b; }; } c;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use 'using' instead of 
'typedef' [modernize-use-using]
+// CHECK-FIXES: using c = struct { int a; union { int b; }; };
+  }
+}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Improved modernize-use-using by fixing a false-negative (PR #82947)

2024-02-25 Thread Félix-Antoine Constantin via cfe-commits

https://github.com/felix642 created 
https://github.com/llvm/llvm-project/pull/82947

The check needs a parent decl to match but if the typedef is in a function, the 
parent is a declStmt which is not a decl by itself. Improved the matcher to 
match on either a decl or a declstmt and extract the decl from the stmt in the 
latter case.

fixes #72179

CC: @FabianWolff @PiotrZSL @njames93 

From 003ec9d5b75cdf6de3140612e1c4b42322a52413 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?=
 
Date: Sun, 25 Feb 2024 20:20:59 -0500
Subject: [PATCH] [clang-tidy] Improved modernize-use-using by fixing a
 false-positive

The check needs a parent decl to match but if the typedef is in a function,
the parent is a declStmt which is not a decl by itself.
Improved the matcher to match on either a decl or a declstmt and extract
the decl from the stmt in the latter case.

fixes #72179
---
 .../clang-tidy/modernize/UseUsingCheck.cpp | 16 +---
 clang-tools-extra/docs/ReleaseNotes.rst|  3 +++
 .../checkers/modernize/use-using.cpp   | 18 ++
 3 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
index bb05f206c717ce..50a07fc02e31b4 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
@@ -24,6 +24,7 @@ static constexpr llvm::StringLiteral ExternCDeclName = 
"extern-c-decl";
 static constexpr llvm::StringLiteral ParentDeclName = "parent-decl";
 static constexpr llvm::StringLiteral TagDeclName = "tag-decl";
 static constexpr llvm::StringLiteral TypedefName = "typedef";
+static constexpr llvm::StringLiteral DeclStmtName = "decl-stmt";
 
 UseUsingCheck::UseUsingCheck(StringRef Name, ClangTidyContext *Context)
 : ClangTidyCheck(Name, Context),
@@ -41,7 +42,8 @@ void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
   unless(isInstantiated()),
   optionally(hasAncestor(
   linkageSpecDecl(isExternCLinkage()).bind(ExternCDeclName))),
-  hasParent(decl().bind(ParentDeclName)))
+  anyOf(hasParent(decl().bind(ParentDeclName)),
+hasParent(declStmt().bind(DeclStmtName
   .bind(TypedefName),
   this);
 
@@ -51,17 +53,25 @@ void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
   tagDecl(
   anyOf(allOf(unless(anyOf(isImplicit(),
classTemplateSpecializationDecl())),
-  hasParent(decl().bind(ParentDeclName))),
+  anyOf(hasParent(decl().bind(ParentDeclName)),
+hasParent(declStmt().bind(DeclStmtName,
 // We want the parent of the ClassTemplateDecl, not the parent
 // of the specialization.
 classTemplateSpecializationDecl(hasAncestor(classTemplateDecl(
-hasParent(decl().bind(ParentDeclName)))
+anyOf(hasParent(decl().bind(ParentDeclName)),
+  hasParent(declStmt().bind(DeclStmtName
   .bind(TagDeclName),
   this);
 }
 
 void UseUsingCheck::check(const MatchFinder::MatchResult ) {
   const auto *ParentDecl = Result.Nodes.getNodeAs(ParentDeclName);
+
+  if (!ParentDecl) {
+const auto *ParentDeclStmt = 
Result.Nodes.getNodeAs(DeclStmtName);
+ParentDecl = ParentDeclStmt->getSingleDecl();
+  }
+
   if (!ParentDecl)
 return;
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 6fd01ed9d471c5..2b36ae4acb9f1d 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -183,6 +183,9 @@ Changes in existing checks
   ` check to also remove any trailing
   whitespace when deleting the ``virtual`` keyword.
 
+- Improved :doc:`modernize-use-using `
+  check by fixing false-negative in functions.
+
 - Improved :doc:`readability-implicit-bool-conversion
   ` check to provide
   valid fix suggestions for ``static_cast`` without a preceding space and
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
index 462bc984fd3ad5..230c7c94cda1cf 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
@@ -342,3 +342,21 @@ typedef int InExternCPP;
 // CHECK-FIXES: using InExternCPP = int;
 
 }
+
+namespace ISSUE_72179
+{  
+  void foo()
+  {
+typedef int a;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use 'using' instead of 
'typedef' [modernize-use-using]
+// CHECK-FIXES: using a = int;
+
+  }
+
+  void foo2()
+  {
+typedef struct { int a; union { int b; }; } c;
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use 'using' instead of 
'typedef'