[clang] [clang]get non-injected-class before finding instantiated decl (PR #70886)

2023-11-06 Thread Congcong Cai via cfe-commits

HerrCai0907 wrote:

ping @erichkeane @AaronBallman @shafik @cor3ntin 

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


[clang-tools-extra] [clang-tidy] Improve performance-enum-size to exclude empty enums (PR #71640)

2023-11-08 Thread Congcong Cai via cfe-commits

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

LGTM

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


[clang-tools-extra] [clang-tidy] Improve performance-enum-size to exclude empty enums (PR #71640)

2023-11-08 Thread Congcong Cai via cfe-commits

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


[clang-tools-extra] [clang-tidy] Improve performance-enum-size to exclude empty enums (PR #71640)

2023-11-08 Thread Congcong Cai via cfe-commits


@@ -23,6 +23,10 @@ namespace clang::tidy::performance {
 
 namespace {
 
+AST_MATCHER(EnumDecl, hasEnumerators) {
+  return Node.enumerator_begin() != Node.enumerator_end();

HerrCai0907 wrote:

```suggestion
  return !Node.enumerators().empty();
```

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


[clang-tools-extra] [clang-tidy][misleading-indentation]ignore false-positives for line started with empty macro (PR #75061)

2023-12-11 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 created 
https://github.com/llvm/llvm-project/pull/75061

Fixes: #71767

>From e710a0568c73e87296b4df03ac6746766bd87c3a Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Mon, 11 Dec 2023 23:32:32 +0800
Subject: [PATCH] [clang-tidy][misleading-indentation]ignore false-positives
 for line started with empty macro

Fixes: #71767
---
 .../MisleadingIndentationCheck.cpp| 24 ---
 .../readability/MisleadingIndentationCheck.h  |  3 ++-
 clang-tools-extra/docs/ReleaseNotes.rst   |  4 
 .../readability/misleading-indentation.cpp|  9 +++
 4 files changed, 36 insertions(+), 4 deletions(-)

diff --git 
a/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp
index 2c011f5c0e6904..a096c0e383130a 100644
--- a/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp
@@ -7,6 +7,7 @@
 
//===--===//
 
 #include "MisleadingIndentationCheck.h"
+#include "../utils/LexerUtils.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 
@@ -51,8 +52,22 @@ void MisleadingIndentationCheck::danglingElseCheck(const 
SourceManager &SM,
 diag(ElseLoc, "different indentation for 'if' and corresponding 'else'");
 }
 
-void MisleadingIndentationCheck::missingBracesCheck(const SourceManager &SM,
-const CompoundStmt *CStmt) 
{
+static bool isAtStartOfLineIncludingEmptyMacro(SourceLocation NextLoc,
+   const SourceManager &SM,
+   const LangOptions &LangOpts) {
+  NextLoc.dump(SM);
+  const SourceLocation BeforeLoc =
+  utils::lexer::getPreviousTokenAndStart(NextLoc, SM, LangOpts).second;
+  if (SM.getExpansionLineNumber(BeforeLoc) ==
+  SM.getExpansionLineNumber(NextLoc)) {
+return false;
+  }
+  return true;
+}
+
+void MisleadingIndentationCheck::missingBracesCheck(
+const SourceManager &SM, const CompoundStmt *CStmt,
+const LangOptions &LangOpts) {
   const static StringRef StmtNames[] = {"if", "for", "while"};
   for (unsigned int I = 0; I < CStmt->size() - 1; I++) {
 const Stmt *CurrentStmt = CStmt->body_begin()[I];
@@ -92,6 +107,8 @@ void MisleadingIndentationCheck::missingBracesCheck(const 
SourceManager &SM,
 
 if (NextLoc.isInvalid() || NextLoc.isMacroID())
   continue;
+if (!isAtStartOfLineIncludingEmptyMacro(NextLoc, SM, LangOpts))
+  continue;
 
 if (SM.getExpansionColumnNumber(InnerLoc) ==
 SM.getExpansionColumnNumber(NextLoc)) {
@@ -117,7 +134,8 @@ void MisleadingIndentationCheck::check(const 
MatchFinder::MatchResult &Result) {
 danglingElseCheck(*Result.SourceManager, Result.Context, If);
 
   if (const auto *CStmt = Result.Nodes.getNodeAs("compound"))
-missingBracesCheck(*Result.SourceManager, CStmt);
+missingBracesCheck(*Result.SourceManager, CStmt,
+   Result.Context->getLangOpts());
 }
 
 } // namespace clang::tidy::readability
diff --git 
a/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.h 
b/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.h
index c336abbc7c4a91..9c92fc1e18b6f3 100644
--- a/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.h
+++ b/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.h
@@ -32,7 +32,8 @@ class MisleadingIndentationCheck : public ClangTidyCheck {
 private:
   void danglingElseCheck(const SourceManager &SM, ASTContext *Context,
  const IfStmt *If);
-  void missingBracesCheck(const SourceManager &SM, const CompoundStmt *CStmt);
+  void missingBracesCheck(const SourceManager &SM, const CompoundStmt *CStmt,
+  const LangOptions &LangOpts);
 };
 
 } // namespace clang::tidy::readability
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 6d91748e4cef18..85438ce795b34c 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -453,6 +453,10 @@ Changes in existing checks
   ` check to ignore
   false-positives in initializer list of record.
 
+- Improved :doc:`readability-misleading-indentation
+  ` check to ignore
+  false-positives for line started with empty macro.
+
 - Improved :doc:`readability-static-accessed-through-instance
   ` check to
   identify calls to static member functions with out-of-class inline 
definitions.
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/readability/misleading-indentation.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/readability/misleading-indentation.cpp
index aea0618d120db6..5d4d60f5f1a351 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/readability/mis

[clang] [clang-tools-extra] [clang-tidy] Improve performance of misc-const-correctness (PR #72705)

2023-12-11 Thread Congcong Cai via cfe-commits


@@ -15,6 +15,76 @@
 namespace clang {
 using namespace ast_matchers;
 
+static bool canResolveToExprImpl(const Expr *Node, const Expr *Target) {
+
+  const auto IgnoreDerivedToBase = [](const Expr *E, auto Matcher) {
+if (Matcher(E))
+  return true;
+if (const auto *Cast = dyn_cast(E)) {
+  if ((Cast->getCastKind() == CK_DerivedToBase ||
+   Cast->getCastKind() == CK_UncheckedDerivedToBase) &&
+  Matcher(Cast->getSubExpr()))
+return true;
+}
+return false;
+  };
+
+  const auto EvalCommaExpr = [](const Expr *E, auto Matcher) {
+const Expr *Result = E;
+while (const auto *BOComma =
+   dyn_cast_or_null(Result->IgnoreParens())) {
+  if (!BOComma->isCommaOp())
+break;
+  Result = BOComma->getRHS();
+}
+
+return Result != E && Matcher(Result);
+  };
+
+  // The 'ConditionalOperatorM' matches on ` ?  : `.
+  // This matching must be recursive because `` can be anything resolving
+  // to the `InnerMatcher`, for example another conditional operator.
+  // The edge-case `BaseClass &b =  ? DerivedVar1 : DerivedVar2;`
+  // is handled, too. The implicit cast happens outside of the conditional.
+  // This is matched by `IgnoreDerivedToBase(canResolveToExpr(InnerMatcher))`
+  // below.
+  const auto ConditionalOperatorM = [Target](const Expr *E, auto Matcher) {

HerrCai0907 wrote:

useless arguments `Matcher`

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


[clang] [clang-tools-extra] [clang-tidy] Improve performance of misc-const-correctness (PR #72705)

2023-12-11 Thread Congcong Cai via cfe-commits


@@ -15,6 +15,76 @@
 namespace clang {
 using namespace ast_matchers;
 
+static bool canResolveToExprImpl(const Expr *Node, const Expr *Target) {

HerrCai0907 wrote:

This function is hard to understand because it used matcher style naming but 
logic operator style code.

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


[clang-tools-extra] [clang] [clang-tidy] Improve performance of misc-const-correctness (PR #72705)

2023-12-11 Thread Congcong Cai via cfe-commits


@@ -15,6 +15,76 @@
 namespace clang {
 using namespace ast_matchers;
 
+static bool canResolveToExprImpl(const Expr *Node, const Expr *Target) {
+
+  const auto IgnoreDerivedToBase = [](const Expr *E, auto Matcher) {
+if (Matcher(E))
+  return true;
+if (const auto *Cast = dyn_cast(E)) {
+  if ((Cast->getCastKind() == CK_DerivedToBase ||
+   Cast->getCastKind() == CK_UncheckedDerivedToBase) &&
+  Matcher(Cast->getSubExpr()))
+return true;
+}
+return false;
+  };
+
+  const auto EvalCommaExpr = [](const Expr *E, auto Matcher) {
+const Expr *Result = E;
+while (const auto *BOComma =
+   dyn_cast_or_null(Result->IgnoreParens())) {
+  if (!BOComma->isCommaOp())
+break;
+  Result = BOComma->getRHS();
+}
+
+return Result != E && Matcher(Result);
+  };
+
+  // The 'ConditionalOperatorM' matches on ` ?  : `.
+  // This matching must be recursive because `` can be anything resolving
+  // to the `InnerMatcher`, for example another conditional operator.
+  // The edge-case `BaseClass &b =  ? DerivedVar1 : DerivedVar2;`
+  // is handled, too. The implicit cast happens outside of the conditional.
+  // This is matched by `IgnoreDerivedToBase(canResolveToExpr(InnerMatcher))`
+  // below.
+  const auto ConditionalOperatorM = [Target](const Expr *E, auto Matcher) {
+if (const auto *OP = dyn_cast(E)) {
+  if (const auto *TE = OP->getTrueExpr()->IgnoreParens())
+if (canResolveToExprImpl(TE, Target))
+  return true;
+  if (const auto *FE = OP->getFalseExpr()->IgnoreParens())
+if (canResolveToExprImpl(FE, Target))
+  return true;
+}
+return false;
+  };
+
+  const auto ElvisOperator = [Target](const Expr *E, auto Matcher) {

HerrCai0907 wrote:

useless arguments `Matcher`

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


[clang-tools-extra] [clang-tidy][NFC][DOC] Add missing HICPP rule id's (PR #72553)

2023-11-17 Thread Congcong Cai via cfe-commits
=?utf-8?q?Bj=C3=B6rn?= Svensson 
Message-ID:
In-Reply-To: 


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


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


[clang-tools-extra] [clang-tidy] Fix crash in modernize-use-trailing-return-type (PR #70709)

2023-10-30 Thread Congcong Cai via cfe-commits

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

LGTM except release note

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


[clang-tools-extra] [clang-tidy] Fix crash in modernize-use-trailing-return-type (PR #70709)

2023-10-30 Thread Congcong Cai via cfe-commits

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


[clang] [clang]get non-injected-class before finding instantiated decl (PR #70886)

2023-10-31 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 created 
https://github.com/llvm/llvm-project/pull/70886

Fixes #21483 

>From 73471336857b84c69e51d4561838e588c7162bfa Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Tue, 31 Oct 2023 17:27:35 +0800
Subject: [PATCH 1/2] [clang]get non-injected-class before finding instantiated
 decl

---
 clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp 
b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 78a7892a35a320b..83dac8ece06b20b 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -6191,6 +6191,9 @@ NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, 
NamedDecl *D,
   }
 
   if (CXXRecordDecl *Record = dyn_cast(D)) {
+if (Record->isInjectedClassName())
+  Record = cast(Record->getDeclContext());
+
 if (!Record->isDependentContext())
   return D;
 

>From ebda1b76c090b52273d2fe72fbc30744a5fb892d Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Wed, 1 Nov 2023 10:59:12 +0800
Subject: [PATCH 2/2] add test

---
 clang/docs/ReleaseNotes.rst   |  2 ++
 clang/test/SemaCXX/friend.cpp | 16 
 2 files changed, 18 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index bc28bb567f6932a..a8ae5fb2596c580 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -533,6 +533,8 @@ Bug Fixes in This Version
   Fixes (`#67687 `_)
 - Fix crash from constexpr evaluator evaluating uninitialized arrays as rvalue.
   Fixes (`#67317 `_)
+- Fix an issue when do name lookup which scope resolution operator in friend 
function.
+  Fixes (`#21483 `_)
 
 Bug Fixes to Compiler Builtins
 ^^
diff --git a/clang/test/SemaCXX/friend.cpp b/clang/test/SemaCXX/friend.cpp
index 367d6a6c1807c92..7b8e50322c4f8f9 100644
--- a/clang/test/SemaCXX/friend.cpp
+++ b/clang/test/SemaCXX/friend.cpp
@@ -429,3 +429,19 @@ namespace qualified_friend_no_match {
 friend void Y::f(double); // expected-error {{friend declaration of 'f' 
does not match any declaration in 'qualified_friend_no_match::Y'}}
   };
 }
+
+namespace gh21483 {
+template 
+struct B {
+  struct mixin {
+int type;
+friend void foo(B::mixin it) {
+  (void)it.mixin::type;
+}
+  };
+};
+
+void bar() {
+  foo(B::mixin{});
+}
+}
\ No newline at end of file

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


[clang] [clang]get non-injected-class before finding instantiated decl (PR #70886)

2023-11-01 Thread Congcong Cai via cfe-commits


@@ -429,3 +429,19 @@ namespace qualified_friend_no_match {
 friend void Y::f(double); // expected-error {{friend declaration of 'f' 
does not match any declaration in 'qualified_friend_no_match::Y'}}
   };
 }
+
+namespace gh21483 {
+template 
+struct B {
+  struct mixin {
+int type;
+friend void foo(B::mixin it) {
+  (void)it.mixin::type;

HerrCai0907 wrote:

`::` is scope resolution operator.
A more clear example is 
```c++
struct T {
int a;
};
struct T1 : public T {
int a;
};

void foo () {
T1{}.T::a;
T1{}.T1::a;
}
```
https://godbolt.org/z/K1b1jodse

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


[clang] [clang]get non-injected-class before finding instantiated decl (PR #70886)

2023-11-01 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/70886

>From 73471336857b84c69e51d4561838e588c7162bfa Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Tue, 31 Oct 2023 17:27:35 +0800
Subject: [PATCH 1/3] [clang]get non-injected-class before finding instantiated
 decl

---
 clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp 
b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 78a7892a35a320b..83dac8ece06b20b 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -6191,6 +6191,9 @@ NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, 
NamedDecl *D,
   }
 
   if (CXXRecordDecl *Record = dyn_cast(D)) {
+if (Record->isInjectedClassName())
+  Record = cast(Record->getDeclContext());
+
 if (!Record->isDependentContext())
   return D;
 

>From ebda1b76c090b52273d2fe72fbc30744a5fb892d Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Wed, 1 Nov 2023 10:59:12 +0800
Subject: [PATCH 2/3] add test

---
 clang/docs/ReleaseNotes.rst   |  2 ++
 clang/test/SemaCXX/friend.cpp | 16 
 2 files changed, 18 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index bc28bb567f6932a..a8ae5fb2596c580 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -533,6 +533,8 @@ Bug Fixes in This Version
   Fixes (`#67687 `_)
 - Fix crash from constexpr evaluator evaluating uninitialized arrays as rvalue.
   Fixes (`#67317 `_)
+- Fix an issue when do name lookup which scope resolution operator in friend 
function.
+  Fixes (`#21483 `_)
 
 Bug Fixes to Compiler Builtins
 ^^
diff --git a/clang/test/SemaCXX/friend.cpp b/clang/test/SemaCXX/friend.cpp
index 367d6a6c1807c92..7b8e50322c4f8f9 100644
--- a/clang/test/SemaCXX/friend.cpp
+++ b/clang/test/SemaCXX/friend.cpp
@@ -429,3 +429,19 @@ namespace qualified_friend_no_match {
 friend void Y::f(double); // expected-error {{friend declaration of 'f' 
does not match any declaration in 'qualified_friend_no_match::Y'}}
   };
 }
+
+namespace gh21483 {
+template 
+struct B {
+  struct mixin {
+int type;
+friend void foo(B::mixin it) {
+  (void)it.mixin::type;
+}
+  };
+};
+
+void bar() {
+  foo(B::mixin{});
+}
+}
\ No newline at end of file

>From 8d1f90df3571cdeccc2cf0f1cb14091afd7593ad Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Wed, 1 Nov 2023 23:52:59 +0800
Subject: [PATCH 3/3] rename test

---
 clang/test/SemaCXX/friend.cpp | 10 +-
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/clang/test/SemaCXX/friend.cpp b/clang/test/SemaCXX/friend.cpp
index 7b8e50322c4f8f9..8272fabb97f4da3 100644
--- a/clang/test/SemaCXX/friend.cpp
+++ b/clang/test/SemaCXX/friend.cpp
@@ -433,15 +433,15 @@ namespace qualified_friend_no_match {
 namespace gh21483 {
 template 
 struct B {
-  struct mixin {
-int type;
-friend void foo(B::mixin it) {
-  (void)it.mixin::type;
+  struct T {
+int v;
+friend void foo(B::T t) {
+  (void)t.T::v;
 }
   };
 };
 
 void bar() {
-  foo(B::mixin{});
+  foo(B::T{});
 }
 }
\ No newline at end of file

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


[clang-tools-extra] [NFC][clang-tidy]refactor isAssignmentToMemberOf in PreferMemberInitializerCheck (PR #71006)

2023-11-01 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 created 
https://github.com/llvm/llvm-project/pull/71006

isAssignmentToMemberOf will return `std::make_pair(nullptr, nullptr)` when 
failed.
Using `std::optional` can describe failed case clearly.

>From 69b7688f56f7fe96031201eef1c3804f391c5abe Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Thu, 2 Nov 2023 08:49:22 +0800
Subject: [PATCH] [NFC][clang-tidy]refactor isAssignmentToMemberOf in
 PreferMemberInitializerCheck

---
 .../PreferMemberInitializerCheck.cpp  | 29 ++-
 1 file changed, 15 insertions(+), 14 deletions(-)

diff --git 
a/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
 
b/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
index b6daf8b936bde0f..f0070265e6a734a 100644
--- 
a/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
+++ 
b/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
@@ -118,45 +118,45 @@ static void updateAssignmentLevel(
   }
 }
 
-static std::pair
+static std::optional>
 isAssignmentToMemberOf(const CXXRecordDecl *Rec, const Stmt *S,
const CXXConstructorDecl *Ctor) {
   if (const auto *BO = dyn_cast(S)) {
 if (BO->getOpcode() != BO_Assign)
-  return std::make_pair(nullptr, nullptr);
+  return {};
 
 const auto *ME = dyn_cast(BO->getLHS()->IgnoreParenImpCasts());
 if (!ME)
-  return std::make_pair(nullptr, nullptr);
+  return {};
 
 const auto *Field = dyn_cast(ME->getMemberDecl());
 if (!Field)
-  return std::make_pair(nullptr, nullptr);
+  return {};
 
 if (!isa(ME->getBase()))
-  return std::make_pair(nullptr, nullptr);
+  return {};
 const Expr *Init = BO->getRHS()->IgnoreParenImpCasts();
 return std::make_pair(Field, Init);
   }
   if (const auto *COCE = dyn_cast(S)) {
 if (COCE->getOperator() != OO_Equal)
-  return std::make_pair(nullptr, nullptr);
+  return {};
 
 const auto *ME =
 dyn_cast(COCE->getArg(0)->IgnoreParenImpCasts());
 if (!ME)
-  return std::make_pair(nullptr, nullptr);
+  return {};
 
 const auto *Field = dyn_cast(ME->getMemberDecl());
 if (!Field)
-  return std::make_pair(nullptr, nullptr);
+  return {};
 
 if (!isa(ME->getBase()))
-  return std::make_pair(nullptr, nullptr);
+  return {};
 const Expr *Init = COCE->getArg(1)->IgnoreParenImpCasts();
 return std::make_pair(Field, Init);
   }
-  return std::make_pair(nullptr, nullptr);
+  return {};
 }
 
 PreferMemberInitializerCheck::PreferMemberInitializerCheck(
@@ -216,11 +216,12 @@ void PreferMemberInitializerCheck::check(
 return;
 }
 
-const FieldDecl *Field = nullptr;
-const Expr *InitValue = nullptr;
-std::tie(Field, InitValue) = isAssignmentToMemberOf(Class, S, Ctor);
-if (!Field)
+std::optional>
+AssignmentToMember = isAssignmentToMemberOf(Class, S, Ctor);
+if (!AssignmentToMember)
   continue;
+const FieldDecl *Field = AssignmentToMember.value().first;
+const Expr *InitValue = AssignmentToMember.value().second;
 updateAssignmentLevel(Field, InitValue, Ctor, AssignedFields);
 if (!canAdvanceAssignment(AssignedFields[Field]))
   continue;

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


[clang-tools-extra] [NFC][clang-tidy]refactor isAssignmentToMemberOf in PreferMemberInitializerCheck (PR #71006)

2023-11-02 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/71006

>From 69b7688f56f7fe96031201eef1c3804f391c5abe Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Thu, 2 Nov 2023 08:49:22 +0800
Subject: [PATCH 1/2] [NFC][clang-tidy]refactor isAssignmentToMemberOf in
 PreferMemberInitializerCheck

---
 .../PreferMemberInitializerCheck.cpp  | 29 ++-
 1 file changed, 15 insertions(+), 14 deletions(-)

diff --git 
a/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
 
b/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
index b6daf8b936bde0f..f0070265e6a734a 100644
--- 
a/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
+++ 
b/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
@@ -118,45 +118,45 @@ static void updateAssignmentLevel(
   }
 }
 
-static std::pair
+static std::optional>
 isAssignmentToMemberOf(const CXXRecordDecl *Rec, const Stmt *S,
const CXXConstructorDecl *Ctor) {
   if (const auto *BO = dyn_cast(S)) {
 if (BO->getOpcode() != BO_Assign)
-  return std::make_pair(nullptr, nullptr);
+  return {};
 
 const auto *ME = dyn_cast(BO->getLHS()->IgnoreParenImpCasts());
 if (!ME)
-  return std::make_pair(nullptr, nullptr);
+  return {};
 
 const auto *Field = dyn_cast(ME->getMemberDecl());
 if (!Field)
-  return std::make_pair(nullptr, nullptr);
+  return {};
 
 if (!isa(ME->getBase()))
-  return std::make_pair(nullptr, nullptr);
+  return {};
 const Expr *Init = BO->getRHS()->IgnoreParenImpCasts();
 return std::make_pair(Field, Init);
   }
   if (const auto *COCE = dyn_cast(S)) {
 if (COCE->getOperator() != OO_Equal)
-  return std::make_pair(nullptr, nullptr);
+  return {};
 
 const auto *ME =
 dyn_cast(COCE->getArg(0)->IgnoreParenImpCasts());
 if (!ME)
-  return std::make_pair(nullptr, nullptr);
+  return {};
 
 const auto *Field = dyn_cast(ME->getMemberDecl());
 if (!Field)
-  return std::make_pair(nullptr, nullptr);
+  return {};
 
 if (!isa(ME->getBase()))
-  return std::make_pair(nullptr, nullptr);
+  return {};
 const Expr *Init = COCE->getArg(1)->IgnoreParenImpCasts();
 return std::make_pair(Field, Init);
   }
-  return std::make_pair(nullptr, nullptr);
+  return {};
 }
 
 PreferMemberInitializerCheck::PreferMemberInitializerCheck(
@@ -216,11 +216,12 @@ void PreferMemberInitializerCheck::check(
 return;
 }
 
-const FieldDecl *Field = nullptr;
-const Expr *InitValue = nullptr;
-std::tie(Field, InitValue) = isAssignmentToMemberOf(Class, S, Ctor);
-if (!Field)
+std::optional>
+AssignmentToMember = isAssignmentToMemberOf(Class, S, Ctor);
+if (!AssignmentToMember)
   continue;
+const FieldDecl *Field = AssignmentToMember.value().first;
+const Expr *InitValue = AssignmentToMember.value().second;
 updateAssignmentLevel(Field, InitValue, Ctor, AssignedFields);
 if (!canAdvanceAssignment(AssignedFields[Field]))
   continue;

>From 02201dba276c8d11f324af01ef01d06a04324a4d Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Thu, 2 Nov 2023 18:44:01 +0800
Subject: [PATCH 2/2] refactor

---
 .../PreferMemberInitializerCheck.cpp  | 19 ---
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git 
a/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
 
b/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
index f0070265e6a734a..23d7303371529da 100644
--- 
a/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
+++ 
b/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
@@ -118,7 +118,12 @@ static void updateAssignmentLevel(
   }
 }
 
-static std::optional>
+struct AssignmentPair {
+  const FieldDecl *Field;
+  const Expr *Init;
+};
+
+static std::optional
 isAssignmentToMemberOf(const CXXRecordDecl *Rec, const Stmt *S,
const CXXConstructorDecl *Ctor) {
   if (const auto *BO = dyn_cast(S)) {
@@ -136,7 +141,7 @@ isAssignmentToMemberOf(const CXXRecordDecl *Rec, const Stmt 
*S,
 if (!isa(ME->getBase()))
   return {};
 const Expr *Init = BO->getRHS()->IgnoreParenImpCasts();
-return std::make_pair(Field, Init);
+return AssignmentPair{Field, Init};
   }
   if (const auto *COCE = dyn_cast(S)) {
 if (COCE->getOperator() != OO_Equal)
@@ -154,7 +159,7 @@ isAssignmentToMemberOf(const CXXRecordDecl *Rec, const Stmt 
*S,
 if (!isa(ME->getBase()))
   return {};
 const Expr *Init = COCE->getArg(1)->IgnoreParenImpCasts();
-return std::make_pair(Field, Init);
+return AssignmentPair{Field, Init};
   }
   return {};
 }
@@ -216,12 +221,12 @@ void PreferMemberInitializerCheck::check(
 return;
 }
 
-std::optional>
-Assignme

[clang-tools-extra] [NFC][clang-tidy]refactor isAssignmentToMemberOf in PreferMemberInitializerCheck (PR #71006)

2023-11-02 Thread Congcong Cai via cfe-commits

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


[clang-tools-extra] [clang-tidy] Improve modernize-make-shared check (PR #70600)

2023-11-02 Thread Congcong Cai via cfe-commits

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


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


[clang-tools-extra] [clang-tidy][misleading-indentation]ignore false-positives for line started with empty macro (PR #75061)

2023-12-26 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/75061

>From c3938da3a94ed9a9cc86d006d4b39dc5b73e7114 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Mon, 11 Dec 2023 23:32:32 +0800
Subject: [PATCH] [clang-tidy][misleading-indentation]ignore false-positives
 for line started with empty macro

Fixes: #71767
---
 .../MisleadingIndentationCheck.cpp| 22 ---
 .../readability/MisleadingIndentationCheck.h  |  3 ++-
 clang-tools-extra/docs/ReleaseNotes.rst   |  4 
 .../readability/misleading-indentation.cpp|  9 
 4 files changed, 34 insertions(+), 4 deletions(-)

diff --git 
a/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp
index 2c011f5c0e6904..e32f79589a059b 100644
--- a/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp
@@ -7,6 +7,7 @@
 
//===--===//
 
 #include "MisleadingIndentationCheck.h"
+#include "../utils/LexerUtils.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 
@@ -51,8 +52,20 @@ void MisleadingIndentationCheck::danglingElseCheck(const 
SourceManager &SM,
 diag(ElseLoc, "different indentation for 'if' and corresponding 'else'");
 }
 
-void MisleadingIndentationCheck::missingBracesCheck(const SourceManager &SM,
-const CompoundStmt *CStmt) 
{
+static bool isAtStartOfLineIncludingEmptyMacro(SourceLocation NextLoc,
+   const SourceManager &SM,
+   const LangOptions &LangOpts) {
+  const SourceLocation BeforeLoc =
+  utils::lexer::getPreviousTokenAndStart(NextLoc, SM, LangOpts).second;
+  if (BeforeLoc.isInvalid())
+return false;
+  return SM.getExpansionLineNumber(BeforeLoc) !=
+ SM.getExpansionLineNumber(NextLoc);
+}
+
+void MisleadingIndentationCheck::missingBracesCheck(
+const SourceManager &SM, const CompoundStmt *CStmt,
+const LangOptions &LangOpts) {
   const static StringRef StmtNames[] = {"if", "for", "while"};
   for (unsigned int I = 0; I < CStmt->size() - 1; I++) {
 const Stmt *CurrentStmt = CStmt->body_begin()[I];
@@ -92,6 +105,8 @@ void MisleadingIndentationCheck::missingBracesCheck(const 
SourceManager &SM,
 
 if (NextLoc.isInvalid() || NextLoc.isMacroID())
   continue;
+if (!isAtStartOfLineIncludingEmptyMacro(NextLoc, SM, LangOpts))
+  continue;
 
 if (SM.getExpansionColumnNumber(InnerLoc) ==
 SM.getExpansionColumnNumber(NextLoc)) {
@@ -117,7 +132,8 @@ void MisleadingIndentationCheck::check(const 
MatchFinder::MatchResult &Result) {
 danglingElseCheck(*Result.SourceManager, Result.Context, If);
 
   if (const auto *CStmt = Result.Nodes.getNodeAs("compound"))
-missingBracesCheck(*Result.SourceManager, CStmt);
+missingBracesCheck(*Result.SourceManager, CStmt,
+   Result.Context->getLangOpts());
 }
 
 } // namespace clang::tidy::readability
diff --git 
a/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.h 
b/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.h
index c336abbc7c4a91..9c92fc1e18b6f3 100644
--- a/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.h
+++ b/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.h
@@ -32,7 +32,8 @@ class MisleadingIndentationCheck : public ClangTidyCheck {
 private:
   void danglingElseCheck(const SourceManager &SM, ASTContext *Context,
  const IfStmt *If);
-  void missingBracesCheck(const SourceManager &SM, const CompoundStmt *CStmt);
+  void missingBracesCheck(const SourceManager &SM, const CompoundStmt *CStmt,
+  const LangOptions &LangOpts);
 };
 
 } // namespace clang::tidy::readability
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index fe7f40d95fe6ca..571808a51596a8 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -465,6 +465,10 @@ Changes in existing checks
   `AllowPointerConditions` options. It also now provides more consistent
   suggestions when parentheses are added to the return value.
 
+- Improved :doc:`readability-misleading-indentation
+  ` check to ignore
+  false-positives for line started with empty macro.
+
 - Improved :doc:`readability-non-const-parameter
   ` check to ignore
   false-positives in initializer list of record.
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/readability/misleading-indentation.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/readability/misleading-indentation.cpp
index aea0618d120db6..5d4d60f5f1a351 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/readability

[clang] [clang]get non-injected-class before finding instantiated decl (PR #70886)

2023-12-26 Thread Congcong Cai via cfe-commits

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


[clang-tools-extra] [clang-tidy][misleading-indentation]ignore false-positives for line started with empty macro (PR #75061)

2023-12-27 Thread Congcong Cai via cfe-commits

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


[clang] [clang]Transform uninstantiated ExceptionSpec in `TemplateInstantiator` (PR #77073)

2024-01-05 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 created 
https://github.com/llvm/llvm-project/pull/77073

Fixes: #77071
`SubstituteDeducedTypeTransform` will transform type and it will visit 
uninstantiated `ExceptionSpecInfo`, which will cause odd behavior

>From 04ef8fd566491024c8163ad3b901f8c4cebaf890 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Fri, 5 Jan 2024 18:15:24 +0800
Subject: [PATCH] [clang]Transform uninstantiated ExceptionSpec in
 TemplateInstantiator

Fixes: #77071
SubstituteDeducedTypeTransform will transform type and it will visit 
uninstantiated ExceptionSpecInfo which will odd behavior
---
 clang/docs/ReleaseNotes.rst   |  3 ++-
 clang/include/clang/AST/Type.h|  2 ++
 clang/lib/AST/Type.cpp|  7 +++
 clang/lib/Sema/SemaTemplateDeduction.cpp  | 11 +++
 clang/lib/Sema/SemaTemplateInstantiate.cpp|  4 +---
 .../SemaCXX/dependent-noexcept-uninstantiated.cpp |  4 +++-
 6 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ee211c16a48ac8..f750587ad12271 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -607,7 +607,8 @@ Bug Fixes in This Version
 - Clang will correctly evaluate ``noexcept`` expression for template functions
   of template classes. Fixes
   (`#68543 `_,
-  `#42496 `_)
+  `#42496 `_,
+  `#77071 `_)
 - Fixed an issue when a shift count larger than ``__INT64_MAX__``, in a right
   shift operation, could result in missing warnings about
   ``shift count >= width of type`` or internal compiler error.
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index b3ae66e6e769d0..0fcf331dd79bc6 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -4228,6 +4228,8 @@ class FunctionProtoType final
 ExceptionSpecInfo() = default;
 
 ExceptionSpecInfo(ExceptionSpecificationType EST) : Type(EST) {}
+
+void instantiate();
   };
 
   /// Extra information about a function prototype. ExtProtoInfo is not
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 160a725939ccd4..a894d3289eb185 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -3414,6 +3414,13 @@ StringRef FunctionType::getNameForCallConv(CallingConv 
CC) {
   llvm_unreachable("Invalid calling convention.");
 }
 
+void FunctionProtoType::ExceptionSpecInfo::instantiate() {
+  assert(Type == EST_Uninstantiated);
+  NoexceptExpr =
+  cast(SourceTemplate->getType())->getNoexceptExpr();
+  Type = EST_DependentNoexcept;
+}
+
 FunctionProtoType::FunctionProtoType(QualType result, ArrayRef 
params,
  QualType canonical,
  const ExtProtoInfo &epi)
diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp 
b/clang/lib/Sema/SemaTemplateDeduction.cpp
index 699e0985e595b6..015b0abaf0e5ee 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -4737,6 +4737,7 @@ namespace {
 QualType Replacement;
 bool ReplacementIsPack;
 bool UseTypeSugar;
+using inherited = TreeTransform;
 
   public:
 SubstituteDeducedTypeTransform(Sema &SemaRef, DependentAuto DA)
@@ -4797,6 +4798,16 @@ namespace {
   // Lambdas never need to be transformed.
   return E;
 }
+bool TransformExceptionSpec(SourceLocation Loc,
+FunctionProtoType::ExceptionSpecInfo &ESI,
+SmallVectorImpl &Exceptions,
+bool &Changed) {
+  if (ESI.Type == EST_Uninstantiated) {
+ESI.instantiate();
+Changed = true;
+  }
+  return inherited::TransformExceptionSpec(Loc, ESI, Exceptions, Changed);
+}
 
 QualType Apply(TypeLoc TL) {
   // Create some scratch storage for the transformed type locations.
diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp 
b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index df6b40999e645c..2c452404459a5d 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -1630,9 +1630,7 @@ bool TemplateInstantiator::TransformExceptionSpec(
 SourceLocation Loc, FunctionProtoType::ExceptionSpecInfo &ESI,
 SmallVectorImpl &Exceptions, bool &Changed) {
   if (ESI.Type == EST_Uninstantiated) {
-ESI.NoexceptExpr = cast(ESI.SourceTemplate->getType())
-   ->getNoexceptExpr();
-ESI.Type = EST_DependentNoexcept;
+ESI.instantiate();
 Changed = true;
   }
   return inherited::TransformExceptionSpec(Loc, ESI, Exceptions, Changed);
diff --git a/clang/test/SemaCXX/dependent-noexcept-uninstantiated.cpp 
b/clang/test/SemaC

[clang] [clang]Transform uninstantiated ExceptionSpec in `TemplateInstantiator` (PR #77073)

2024-01-05 Thread Congcong Cai via cfe-commits

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


[clang] [clang]Transform uninstantiated ExceptionSpec in `TemplateInstantiator` (PR #77073)

2024-01-05 Thread Congcong Cai via cfe-commits

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


[clang] [clang-tools-extra] [clang-tidy] Improve performance of misc-const-correctness (PR #72705)

2024-01-08 Thread Congcong Cai via cfe-commits

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

LGTM

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


[clang-tools-extra] [clang-tidy]unused using decls only check cpp files (PR #77335)

2024-01-08 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 created 
https://github.com/llvm/llvm-project/pull/77335

using decls can be used in cpp

>From 39fae7adcdc97b4781911097f5f09c5bf8266d1c Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Tue, 9 Jan 2024 00:25:44 +0800
Subject: [PATCH] [clang-tidy]unused using decls only check cpp files

---
 clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h | 5 -
 clang-tools-extra/docs/ReleaseNotes.rst   | 2 +-
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h 
b/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h
index fa2a8799d098c3..aab9a57b8ba01d 100644
--- a/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h
+++ b/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h
@@ -26,7 +26,10 @@ class UnusedUsingDeclsCheck : public ClangTidyCheck {
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
   void onEndOfTranslationUnit() override;
-
+  bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
+return LangOpts.CPlusPlus;
+  }
+  
 private:
   void removeFromFoundDecls(const Decl *D);
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 1bd5a72126c10b..d7f46cede0370c 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -382,7 +382,7 @@ Changes in existing checks
 
 - Improved :doc:`misc-unused-using-decls
   ` check to avoid false positive 
when
-  using in elaborated type.
+  using in elaborated type and only check cpp files.
 
 - Improved :doc:`modernize-avoid-bind
   ` check to

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


[clang-tools-extra] [clang-tidy]unused using decls only check cpp files (PR #77335)

2024-01-08 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/77335

>From 832bb78fd8cac372ec5560bb280f11e11be8d2e6 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Tue, 9 Jan 2024 00:41:33 +0800
Subject: [PATCH] [clang-tidy]unused using decls only check cpp files

---
 clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h | 3 +++
 clang-tools-extra/docs/ReleaseNotes.rst   | 2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h 
b/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h
index fa2a8799d098c3..498b3ffd2678c3 100644
--- a/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h
+++ b/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h
@@ -26,6 +26,9 @@ class UnusedUsingDeclsCheck : public ClangTidyCheck {
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
   void onEndOfTranslationUnit() override;
+  bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
+return LangOpts.CPlusPlus;
+  }
 
 private:
   void removeFromFoundDecls(const Decl *D);
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 1bd5a72126c10b..d7f46cede0370c 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -382,7 +382,7 @@ Changes in existing checks
 
 - Improved :doc:`misc-unused-using-decls
   ` check to avoid false positive 
when
-  using in elaborated type.
+  using in elaborated type and only check cpp files.
 
 - Improved :doc:`modernize-avoid-bind
   ` check to

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


[clang-tools-extra] [clang-tidy]unused using decls only check cpp files (PR #77335)

2024-01-08 Thread Congcong Cai via cfe-commits

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


[clang] [clang]use correct this scope to evaluate noexcept expr (PR #77416)

2024-01-08 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 created 
https://github.com/llvm/llvm-project/pull/77416

Fixes: #77411
When substituting deduced type, noexcept expr in method should be instantiated 
and evaluated.
ThisScrope should be switched to method context instead of origin sema context

>From 0637a482c881f5ce31fd1debbc8dcc40a05c66ba Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Tue, 9 Jan 2024 14:53:25 +0800
Subject: [PATCH] [clang]use correct this scope to evaluate noexcept expr when
 deduce template

Fixes: #77411
When substituting deduced type, noexcept expr in function decl should be 
instantiated and evaluated.
ThisScrope should be switched to method context
---
 clang/docs/ReleaseNotes.rst   |  3 ++-
 clang/lib/Sema/TreeTransform.h|  6 ++
 .../test/SemaCXX/cxx1z-noexcept-function-type.cpp | 15 +++
 3 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 803eb2f7c74cf6..2695a0e30f58e0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -610,7 +610,8 @@ Bug Fixes in This Version
   of template classes. Fixes
   (`#68543 `_,
   `#42496 `_,
-  `#77071 `_)
+  `#77071 `_,
+  `#77411 `_)
 - Fixed an issue when a shift count larger than ``__INT64_MAX__``, in a right
   shift operation, could result in missing warnings about
   ``shift count >= width of type`` or internal compiler error.
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index c8c5a51bf9f94e..fb7e15b5bcbb02 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -6192,6 +6192,12 @@ bool TreeTransform::TransformExceptionSpec(
 
   // Instantiate a dynamic noexcept expression, if any.
   if (isComputedNoexcept(ESI.Type)) {
+// Update this scrope because ContextDecl in Sema will be used in 
TransformExpr.
+auto *Method = dyn_cast_or_null(ESI.SourceTemplate);
+Sema::CXXThisScopeRAII ThisScope(
+SemaRef, Method ? Method->getParent() : nullptr,
+Method ? Method->getMethodQualifiers() : Qualifiers{},
+Method != nullptr);
 EnterExpressionEvaluationContext Unevaluated(
 getSema(), Sema::ExpressionEvaluationContext::ConstantEvaluated);
 ExprResult NoexceptExpr = getDerived().TransformExpr(ESI.NoexceptExpr);
diff --git a/clang/test/SemaCXX/cxx1z-noexcept-function-type.cpp 
b/clang/test/SemaCXX/cxx1z-noexcept-function-type.cpp
index 11b1093f9064f6..5e56f19477d6ca 100644
--- a/clang/test/SemaCXX/cxx1z-noexcept-function-type.cpp
+++ b/clang/test/SemaCXX/cxx1z-noexcept-function-type.cpp
@@ -64,6 +64,21 @@ namespace DependentDefaultCtorExceptionSpec {
   struct A { multimap Map; } a;
 
   static_assert(noexcept(A()));
+
+  template  struct NoexceptWithThis {
+int ca;
+template  auto foo(T) noexcept(ca) { return true; }
+// expected-error@-1 {{noexcept specifier argument is not a constant 
expression}}
+// expected-note@-2 {{in instantiation of exception specification}}
+// expected-note@-3 {{implicit use of 'this' pointer is only allowed 
within the evaluation of a call to a 'constexpr' member function}}
+  };
+  struct InstantiateFromAnotherClass {
+template (&B::foo))> // expected-note {{in instantiation of function 
template specialization}}
+InstantiateFromAnotherClass(B *) {} // expected-note {{in instantiation of 
default argument}}
+  };
+  NoexceptWithThis f{};
+  // Don't crash here.
+  InstantiateFromAnotherClass b{&f}; // expected-note {{while substituting 
deduced template arguments into function template}}
 }
 
 #endif

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


[clang] [clang]use correct this scope to evaluate noexcept expr (PR #77416)

2024-01-09 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/77416

>From 0637a482c881f5ce31fd1debbc8dcc40a05c66ba Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Tue, 9 Jan 2024 14:53:25 +0800
Subject: [PATCH 1/3] [clang]use correct this scope to evaluate noexcept expr
 when deduce template

Fixes: #77411
When substituting deduced type, noexcept expr in function decl should be 
instantiated and evaluated.
ThisScrope should be switched to method context
---
 clang/docs/ReleaseNotes.rst   |  3 ++-
 clang/lib/Sema/TreeTransform.h|  6 ++
 .../test/SemaCXX/cxx1z-noexcept-function-type.cpp | 15 +++
 3 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 803eb2f7c74cf6..2695a0e30f58e0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -610,7 +610,8 @@ Bug Fixes in This Version
   of template classes. Fixes
   (`#68543 `_,
   `#42496 `_,
-  `#77071 `_)
+  `#77071 `_,
+  `#77411 `_)
 - Fixed an issue when a shift count larger than ``__INT64_MAX__``, in a right
   shift operation, could result in missing warnings about
   ``shift count >= width of type`` or internal compiler error.
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index c8c5a51bf9f94e..fb7e15b5bcbb02 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -6192,6 +6192,12 @@ bool TreeTransform::TransformExceptionSpec(
 
   // Instantiate a dynamic noexcept expression, if any.
   if (isComputedNoexcept(ESI.Type)) {
+// Update this scrope because ContextDecl in Sema will be used in 
TransformExpr.
+auto *Method = dyn_cast_or_null(ESI.SourceTemplate);
+Sema::CXXThisScopeRAII ThisScope(
+SemaRef, Method ? Method->getParent() : nullptr,
+Method ? Method->getMethodQualifiers() : Qualifiers{},
+Method != nullptr);
 EnterExpressionEvaluationContext Unevaluated(
 getSema(), Sema::ExpressionEvaluationContext::ConstantEvaluated);
 ExprResult NoexceptExpr = getDerived().TransformExpr(ESI.NoexceptExpr);
diff --git a/clang/test/SemaCXX/cxx1z-noexcept-function-type.cpp 
b/clang/test/SemaCXX/cxx1z-noexcept-function-type.cpp
index 11b1093f9064f6..5e56f19477d6ca 100644
--- a/clang/test/SemaCXX/cxx1z-noexcept-function-type.cpp
+++ b/clang/test/SemaCXX/cxx1z-noexcept-function-type.cpp
@@ -64,6 +64,21 @@ namespace DependentDefaultCtorExceptionSpec {
   struct A { multimap Map; } a;
 
   static_assert(noexcept(A()));
+
+  template  struct NoexceptWithThis {
+int ca;
+template  auto foo(T) noexcept(ca) { return true; }
+// expected-error@-1 {{noexcept specifier argument is not a constant 
expression}}
+// expected-note@-2 {{in instantiation of exception specification}}
+// expected-note@-3 {{implicit use of 'this' pointer is only allowed 
within the evaluation of a call to a 'constexpr' member function}}
+  };
+  struct InstantiateFromAnotherClass {
+template (&B::foo))> // expected-note {{in instantiation of function 
template specialization}}
+InstantiateFromAnotherClass(B *) {} // expected-note {{in instantiation of 
default argument}}
+  };
+  NoexceptWithThis f{};
+  // Don't crash here.
+  InstantiateFromAnotherClass b{&f}; // expected-note {{while substituting 
deduced template arguments into function template}}
 }
 
 #endif

>From 80b07ef334b9fdb0699d156e58eb13c8835d835a Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Tue, 9 Jan 2024 15:54:54 +0800
Subject: [PATCH 2/3] fix format

---
 clang/lib/Sema/TreeTransform.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index fb7e15b5bcbb02..9dc0c55518ae21 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -6192,7 +6192,8 @@ bool TreeTransform::TransformExceptionSpec(
 
   // Instantiate a dynamic noexcept expression, if any.
   if (isComputedNoexcept(ESI.Type)) {
-// Update this scrope because ContextDecl in Sema will be used in 
TransformExpr.
+// Update this scrope because ContextDecl in Sema will be used in
+// TransformExpr.
 auto *Method = dyn_cast_or_null(ESI.SourceTemplate);
 Sema::CXXThisScopeRAII ThisScope(
 SemaRef, Method ? Method->getParent() : nullptr,

>From edaee6a67f296dae7dd6ab5defbeab32a1d34724 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Tue, 9 Jan 2024 16:27:24 +0800
Subject: [PATCH 3/3] avoid use deprecated API

---
 clang/lib/Sema/TreeTransform.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 9dc

[clang] [clang]use correct this scope to evaluate noexcept expr (PR #77416)

2024-01-09 Thread Congcong Cai via cfe-commits

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


[clang] [clang]not lookup name containing a dependent type (PR #77587)

2024-01-10 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 created 
https://github.com/llvm/llvm-project/pull/77587

Fixes: #77583
bcd51aaaf8bde4b0ae7a4155d9ce3dec78fe2598 fixed part of template instantiation 
dependent name issues but still missing some cases This patch want to enhance 
the dependent name check

>From f6b9afa26fabb5f9dcea5615c92914bed93ef474 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Wed, 10 Jan 2024 19:27:31 +0800
Subject: [PATCH] [clang]not lookup name containing a dependent type

Fixes: #77583
bcd51aaaf8bde4b0ae7a4155d9ce3dec78fe2598 fixed part of template instantiation 
dependent name issues but still missing some cases
This patch want to enhance the dependent name check
---
 clang/lib/Sema/SemaExprMember.cpp  |  3 ++-
 clang/test/SemaCXX/conversion-function.cpp | 14 +++---
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Sema/SemaExprMember.cpp 
b/clang/lib/Sema/SemaExprMember.cpp
index 2abec3d86a27d9..32998ae60eafe2 100644
--- a/clang/lib/Sema/SemaExprMember.cpp
+++ b/clang/lib/Sema/SemaExprMember.cpp
@@ -782,7 +782,8 @@ Sema::BuildMemberReferenceExpr(Expr *Base, QualType 
BaseType,
const Scope *S,
ActOnMemberAccessExtraArgs *ExtraArgs) {
   if (BaseType->isDependentType() ||
-  (SS.isSet() && isDependentScopeSpecifier(SS)))
+  (SS.isSet() && isDependentScopeSpecifier(SS)) ||
+  NameInfo.getName().isDependentName())
 return ActOnDependentMemberExpr(Base, BaseType,
 IsArrow, OpLoc,
 SS, TemplateKWLoc, FirstQualifierInScope,
diff --git a/clang/test/SemaCXX/conversion-function.cpp 
b/clang/test/SemaCXX/conversion-function.cpp
index b6e6142d179066..220ae78f2d8246 100644
--- a/clang/test/SemaCXX/conversion-function.cpp
+++ b/clang/test/SemaCXX/conversion-function.cpp
@@ -475,13 +475,21 @@ struct S {
 
 #if __cplusplus >= 201103L
 namespace dependent_conversion_function_id_lookup {
-  template struct A {
+  struct A1 {
+operator int();
+  };
+  template struct C {
+template  using Lookup = decltype(T{}.operator U());
+  };
+  C v{};
+
+  template struct A2 {
 operator T();
   };
-  template struct B : A {
+  template struct B : A2 {
 template using Lookup = decltype(&B::operator U);
   };
   using Result = B::Lookup;
-  using Result = int (A::*)();
+  using Result = int (A2::*)();
 }
 #endif

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


[clang-tools-extra] Add new check: do not return 0; at the end of main() in C++ (PR #77586)

2024-01-10 Thread Congcong Cai via cfe-commits


@@ -0,0 +1,30 @@
+//===--- DonotreturnzerocheckCheck.h - clang-tidy ---*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef 
LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_DONOTRETURNZEROCHECKCHECK_H
+#define 
LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_DONOTRETURNZEROCHECKCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::readability {
+
+/// FIXME: Write a short description.

HerrCai0907 wrote:

Copy description from docs

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


[clang-tools-extra] Add new check: do not return 0; at the end of main() in C++ (PR #77586)

2024-01-10 Thread Congcong Cai via cfe-commits


@@ -0,0 +1,50 @@
+//===--- DonotreturnzerocheckCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "DonotreturnzerocheckCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+bool isCPlusPlusOrC99(const LangOptions &LangOpts) {
+  return LangOpts.CPlusPlus || LangOpts.C99;
+}
+
+void DonotreturnzerocheckCheck::registerMatchers(MatchFinder *Finder) {
+  // FIXME: Add matchers.
+  Finder->addMatcher(
+  functionDecl(isMain(), returns(asString("int"))).bind("main"), this);
+}
+
+void DonotreturnzerocheckCheck::check(const MatchFinder::MatchResult &Result) {
+  // FIXME: Add callback implementation.

HerrCai0907 wrote:

clean code

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


[clang-tools-extra] Add new check: do not return 0; at the end of main() in C++ (PR #77586)

2024-01-10 Thread Congcong Cai via cfe-commits


@@ -0,0 +1,50 @@
+//===--- DonotreturnzerocheckCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "DonotreturnzerocheckCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+bool isCPlusPlusOrC99(const LangOptions &LangOpts) {
+  return LangOpts.CPlusPlus || LangOpts.C99;
+}
+
+void DonotreturnzerocheckCheck::registerMatchers(MatchFinder *Finder) {
+  // FIXME: Add matchers.
+  Finder->addMatcher(
+  functionDecl(isMain(), returns(asString("int"))).bind("main"), this);
+}
+
+void DonotreturnzerocheckCheck::check(const MatchFinder::MatchResult &Result) {
+  // FIXME: Add callback implementation.
+  const auto *MatchedDecl = Result.Nodes.getNodeAs("main");
+  if (isCPlusPlusOrC99(Result.Context->getLangOpts())) {

HerrCai0907 wrote:

move it to `bool isLanguageVersionSupported(const LangOptions &LangOpts) const 
override`

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


[clang-tools-extra] Add new check: do not return 0; at the end of main() in C++ (PR #77586)

2024-01-10 Thread Congcong Cai via cfe-commits


@@ -0,0 +1,50 @@
+//===--- DonotreturnzerocheckCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "DonotreturnzerocheckCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+bool isCPlusPlusOrC99(const LangOptions &LangOpts) {
+  return LangOpts.CPlusPlus || LangOpts.C99;
+}
+
+void DonotreturnzerocheckCheck::registerMatchers(MatchFinder *Finder) {
+  // FIXME: Add matchers.
+  Finder->addMatcher(
+  functionDecl(isMain(), returns(asString("int"))).bind("main"), this);
+}
+
+void DonotreturnzerocheckCheck::check(const MatchFinder::MatchResult &Result) {
+  // FIXME: Add callback implementation.
+  const auto *MatchedDecl = Result.Nodes.getNodeAs("main");
+  if (isCPlusPlusOrC99(Result.Context->getLangOpts())) {
+SourceLocation ReturnLoc;
+if (MatchedDecl->hasBody()) {
+  const CompoundStmt *Body = 
dyn_cast(MatchedDecl->getBody());
+  if (Body && !Body->body_empty()) {
+const Stmt *LastStmt = Body->body_back();
+if (const auto *Return = dyn_cast(LastStmt)) {
+  ReturnLoc = Return->getReturnLoc();
+}

HerrCai0907 wrote:

redundant brace

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


[clang] [clang]not lookup name containing a dependent type (PR #77587)

2024-01-11 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/77587

>From f6b9afa26fabb5f9dcea5615c92914bed93ef474 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Wed, 10 Jan 2024 19:27:31 +0800
Subject: [PATCH 1/2] [clang]not lookup name containing a dependent type

Fixes: #77583
bcd51aaaf8bde4b0ae7a4155d9ce3dec78fe2598 fixed part of template instantiation 
dependent name issues but still missing some cases
This patch want to enhance the dependent name check
---
 clang/lib/Sema/SemaExprMember.cpp  |  3 ++-
 clang/test/SemaCXX/conversion-function.cpp | 14 +++---
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Sema/SemaExprMember.cpp 
b/clang/lib/Sema/SemaExprMember.cpp
index 2abec3d86a27d9..32998ae60eafe2 100644
--- a/clang/lib/Sema/SemaExprMember.cpp
+++ b/clang/lib/Sema/SemaExprMember.cpp
@@ -782,7 +782,8 @@ Sema::BuildMemberReferenceExpr(Expr *Base, QualType 
BaseType,
const Scope *S,
ActOnMemberAccessExtraArgs *ExtraArgs) {
   if (BaseType->isDependentType() ||
-  (SS.isSet() && isDependentScopeSpecifier(SS)))
+  (SS.isSet() && isDependentScopeSpecifier(SS)) ||
+  NameInfo.getName().isDependentName())
 return ActOnDependentMemberExpr(Base, BaseType,
 IsArrow, OpLoc,
 SS, TemplateKWLoc, FirstQualifierInScope,
diff --git a/clang/test/SemaCXX/conversion-function.cpp 
b/clang/test/SemaCXX/conversion-function.cpp
index b6e6142d179066..220ae78f2d8246 100644
--- a/clang/test/SemaCXX/conversion-function.cpp
+++ b/clang/test/SemaCXX/conversion-function.cpp
@@ -475,13 +475,21 @@ struct S {
 
 #if __cplusplus >= 201103L
 namespace dependent_conversion_function_id_lookup {
-  template struct A {
+  struct A1 {
+operator int();
+  };
+  template struct C {
+template  using Lookup = decltype(T{}.operator U());
+  };
+  C v{};
+
+  template struct A2 {
 operator T();
   };
-  template struct B : A {
+  template struct B : A2 {
 template using Lookup = decltype(&B::operator U);
   };
   using Result = B::Lookup;
-  using Result = int (A::*)();
+  using Result = int (A2::*)();
 }
 #endif

>From e48e981d65502948972d091c612ee7a354abb3d0 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Thu, 11 Jan 2024 18:39:31 +0800
Subject: [PATCH 2/2] add release note

---
 clang/docs/ReleaseNotes.rst|  4 ++-
 clang/test/SemaCXX/conversion-function.cpp | 33 +++---
 2 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 37f8bbc89d8949..8e403530a19502 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -708,7 +708,9 @@ Bug Fixes in This Version
 - Clang now emits correct source location for code-coverage regions in `if 
constexpr`
   and `if consteval` branches.
   Fixes (`#54419 `_)
-
+- Fix an issue where clang cannot find conversion function with template
+  parameter when instantiation of template class.
+  Fixes (`#77583 `_)
 
 Bug Fixes to Compiler Builtins
 ^^
diff --git a/clang/test/SemaCXX/conversion-function.cpp 
b/clang/test/SemaCXX/conversion-function.cpp
index 220ae78f2d8246..749e2fc1b452b6 100644
--- a/clang/test/SemaCXX/conversion-function.cpp
+++ b/clang/test/SemaCXX/conversion-function.cpp
@@ -475,21 +475,22 @@ struct S {
 
 #if __cplusplus >= 201103L
 namespace dependent_conversion_function_id_lookup {
-  struct A1 {
-operator int();
-  };
-  template struct C {
-template  using Lookup = decltype(T{}.operator U());
-  };
-  C v{};
-
-  template struct A2 {
-operator T();
-  };
-  template struct B : A2 {
-template using Lookup = decltype(&B::operator U);
-  };
-  using Result = B::Lookup;
-  using Result = int (A2::*)();
+namespace gh77583 {
+struct A1 {
+  operator int();
+};
+template struct C {
+  template  using Lookup = decltype(T{}.operator U());
+};
+C v{};
+}
+template struct A2 {
+  operator T();
+};
+template struct B : A2 {
+  template using Lookup = decltype(&B::operator U);
+};
+using Result = B::Lookup;
+using Result = int (A2::*)();
 }
 #endif

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


[clang] [clang]not lookup name containing a dependent type (PR #77587)

2024-01-11 Thread Congcong Cai via cfe-commits

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


[clang-tools-extra] [clang-tidy]bugprone-unused-return-value ignore `++` and `--` operator overloading (PR #84922)

2024-03-14 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/84922

>From d760b280a79be973642a0549db0e5a8838c397fd Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Tue, 12 Mar 2024 22:33:18 +0800
Subject: [PATCH 1/3] [clang-tidy]bugprone-unused-return-value ignore `++` and
 `--` operator overloading

Fixes: #84705
---
 .../bugprone/UnusedReturnValueCheck.cpp   | 28 +--
 .../unused-return-value-avoid-assignment.cpp  |  8 ++
 2 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
index 243fe47c2036b6..83b332fba1e2da 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
@@ -165,20 +165,20 @@ void 
UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
 
 void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) {
   auto MatchedDirectCallExpr = expr(
-  callExpr(
-  callee(functionDecl(
-  // Don't match void overloads of checked functions.
-  unless(returns(voidType())),
-  // Don't match copy or move assignment operator.
-  unless(cxxMethodDecl(isOperatorOverloading(
-  {OO_Equal, OO_PlusEqual, OO_MinusEqual, OO_StarEqual,
-   OO_SlashEqual, OO_PercentEqual, OO_CaretEqual, OO_AmpEqual,
-   OO_PipeEqual, OO_LessLessEqual, OO_GreaterGreaterEqual}))),
-  anyOf(
-  isInstantiatedFrom(
-  matchers::matchesAnyListedName(CheckedFunctions)),
-  returns(hasCanonicalType(hasDeclaration(namedDecl(
-  
matchers::matchesAnyListedName(CheckedReturnTypes)
+  callExpr(callee(functionDecl(
+   // Don't match void overloads of checked functions.
+   unless(returns(voidType())),
+   // Don't match copy or move assignment operator.
+   unless(cxxMethodDecl(isOperatorOverloading(
+   {OO_Equal, OO_PlusEqual, OO_MinusEqual, OO_StarEqual,
+OO_SlashEqual, OO_PercentEqual, OO_CaretEqual,
+OO_AmpEqual, OO_PipeEqual, OO_LessLessEqual,
+OO_GreaterGreaterEqual, OO_PlusPlus, OO_MinusMinus}))),
+   anyOf(isInstantiatedFrom(
+ matchers::matchesAnyListedName(CheckedFunctions)),
+ returns(hasCanonicalType(hasDeclaration(
+ namedDecl(matchers::matchesAnyListedName(
+ CheckedReturnTypes)
   .bind("match"));
 
   auto CheckCastToVoid =
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
index b4a41004adf894..5809da9ec27b38 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
@@ -10,6 +10,10 @@ struct S {
   S &operator=(S const &);
   S &operator=(S &&);
   S &operator+=(S);
+  S &operator++();
+  S &operator++(int);
+  S &operator--();
+  S &operator--(int);
 };
 
 S returnValue();
@@ -27,4 +31,8 @@ void bar() {
   a.operator=(returnRef());
 
   a += returnRef();
+  a++;
+  ++a;
+  a--;
+  --a;
 }

>From 784df684a08f4b4da97d8db9ba3bf708dafbf626 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Wed, 13 Mar 2024 14:31:36 +0800
Subject: [PATCH 2/3] better version

---
 .../bugprone/UnusedReturnValueCheck.cpp   | 43 +++
 .../unused-return-value-avoid-assignment.cpp  |  4 ++
 2 files changed, 30 insertions(+), 17 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
index 83b332fba1e2da..846505317c1c7a 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
@@ -31,9 +31,20 @@ AST_MATCHER_P(FunctionDecl, isInstantiatedFrom, 
Matcher,
   Finder, Builder);
 }
 
-AST_MATCHER_P(CXXMethodDecl, isOperatorOverloading,
-  llvm::SmallVector, Kinds) {
-  return llvm::is_contained(Kinds, Node.getOverloadedOperator());
+constexpr std::initializer_list
+AssignmentOverloadedOperatorKinds = {
+OO_Equal,  OO_PlusEqual, OO_MinusEqual,  OO_StarEqual,
+OO_SlashEqual, OO_PercentEqual,  OO_CaretEqual,  OO_AmpEqual,
+OO_PipeEqual,  OO_LessLessEqual, OO_GreaterGreaterEqual, OO_PlusPlus,
+OO_MinusMinus};
+
+AST_MATCHER(CXXOperatorCallExpr, isAssignmentOver

[clang-tools-extra] [clang-tidy]bugprone-unused-return-value ignore `++` and `--` operator overloading (PR #84922)

2024-03-14 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/84922

>From d760b280a79be973642a0549db0e5a8838c397fd Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Tue, 12 Mar 2024 22:33:18 +0800
Subject: [PATCH 1/4] [clang-tidy]bugprone-unused-return-value ignore `++` and
 `--` operator overloading

Fixes: #84705
---
 .../bugprone/UnusedReturnValueCheck.cpp   | 28 +--
 .../unused-return-value-avoid-assignment.cpp  |  8 ++
 2 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
index 243fe47c2036b6..83b332fba1e2da 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
@@ -165,20 +165,20 @@ void 
UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
 
 void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) {
   auto MatchedDirectCallExpr = expr(
-  callExpr(
-  callee(functionDecl(
-  // Don't match void overloads of checked functions.
-  unless(returns(voidType())),
-  // Don't match copy or move assignment operator.
-  unless(cxxMethodDecl(isOperatorOverloading(
-  {OO_Equal, OO_PlusEqual, OO_MinusEqual, OO_StarEqual,
-   OO_SlashEqual, OO_PercentEqual, OO_CaretEqual, OO_AmpEqual,
-   OO_PipeEqual, OO_LessLessEqual, OO_GreaterGreaterEqual}))),
-  anyOf(
-  isInstantiatedFrom(
-  matchers::matchesAnyListedName(CheckedFunctions)),
-  returns(hasCanonicalType(hasDeclaration(namedDecl(
-  
matchers::matchesAnyListedName(CheckedReturnTypes)
+  callExpr(callee(functionDecl(
+   // Don't match void overloads of checked functions.
+   unless(returns(voidType())),
+   // Don't match copy or move assignment operator.
+   unless(cxxMethodDecl(isOperatorOverloading(
+   {OO_Equal, OO_PlusEqual, OO_MinusEqual, OO_StarEqual,
+OO_SlashEqual, OO_PercentEqual, OO_CaretEqual,
+OO_AmpEqual, OO_PipeEqual, OO_LessLessEqual,
+OO_GreaterGreaterEqual, OO_PlusPlus, OO_MinusMinus}))),
+   anyOf(isInstantiatedFrom(
+ matchers::matchesAnyListedName(CheckedFunctions)),
+ returns(hasCanonicalType(hasDeclaration(
+ namedDecl(matchers::matchesAnyListedName(
+ CheckedReturnTypes)
   .bind("match"));
 
   auto CheckCastToVoid =
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
index b4a41004adf894..5809da9ec27b38 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
@@ -10,6 +10,10 @@ struct S {
   S &operator=(S const &);
   S &operator=(S &&);
   S &operator+=(S);
+  S &operator++();
+  S &operator++(int);
+  S &operator--();
+  S &operator--(int);
 };
 
 S returnValue();
@@ -27,4 +31,8 @@ void bar() {
   a.operator=(returnRef());
 
   a += returnRef();
+  a++;
+  ++a;
+  a--;
+  --a;
 }

>From 784df684a08f4b4da97d8db9ba3bf708dafbf626 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Wed, 13 Mar 2024 14:31:36 +0800
Subject: [PATCH 2/4] better version

---
 .../bugprone/UnusedReturnValueCheck.cpp   | 43 +++
 .../unused-return-value-avoid-assignment.cpp  |  4 ++
 2 files changed, 30 insertions(+), 17 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
index 83b332fba1e2da..846505317c1c7a 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
@@ -31,9 +31,20 @@ AST_MATCHER_P(FunctionDecl, isInstantiatedFrom, 
Matcher,
   Finder, Builder);
 }
 
-AST_MATCHER_P(CXXMethodDecl, isOperatorOverloading,
-  llvm::SmallVector, Kinds) {
-  return llvm::is_contained(Kinds, Node.getOverloadedOperator());
+constexpr std::initializer_list
+AssignmentOverloadedOperatorKinds = {
+OO_Equal,  OO_PlusEqual, OO_MinusEqual,  OO_StarEqual,
+OO_SlashEqual, OO_PercentEqual,  OO_CaretEqual,  OO_AmpEqual,
+OO_PipeEqual,  OO_LessLessEqual, OO_GreaterGreaterEqual, OO_PlusPlus,
+OO_MinusMinus};
+
+AST_MATCHER(CXXOperatorCallExpr, isAssignmentOver

[clang-tools-extra] [clang-tidy]bugprone-unused-return-value ignore `++` and `--` operator overloading (PR #84922)

2024-03-14 Thread Congcong Cai via cfe-commits


@@ -169,16 +176,13 @@ void UnusedReturnValueCheck::registerMatchers(MatchFinder 
*Finder) {
   callee(functionDecl(
   // Don't match void overloads of checked functions.
   unless(returns(voidType())),
-  // Don't match copy or move assignment operator.
-  unless(cxxMethodDecl(isOperatorOverloading(
-  {OO_Equal, OO_PlusEqual, OO_MinusEqual, OO_StarEqual,
-   OO_SlashEqual, OO_PercentEqual, OO_CaretEqual, OO_AmpEqual,
-   OO_PipeEqual, OO_LessLessEqual, OO_GreaterGreaterEqual}))),
   anyOf(
   isInstantiatedFrom(
   matchers::matchesAnyListedName(CheckedFunctions)),
   returns(hasCanonicalType(hasDeclaration(namedDecl(
-  
matchers::matchesAnyListedName(CheckedReturnTypes)
+  
matchers::matchesAnyListedName(CheckedReturnTypes,
+  // Don't match copy or move assignment operator.
+  unless(callee(functionDecl(isAssignmentOverloadedOperatorMethod()

HerrCai0907 wrote:

Fixed. I refactor it several times and forget to move it back.

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


[clang-tools-extra] [clang-tidy]bugprone-unused-return-value ignore `++` and `--` operator overloading (PR #84922)

2024-03-14 Thread Congcong Cai via cfe-commits

HerrCai0907 wrote:

> Should this check really ignore postfix increment/decrement? Or maybe this 
> check ignores it and a new check diagnoses `it++` vs `++it` (where possible). 
> What are your thoughts on this?

@5chmidti  What does `a new check diagnoses `it++` vs `++it` (where possible)` 
mean?

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


[clang-tools-extra] [clang-tidy]bugprone-unused-return-value ignore `++` and `--` operator overloading (PR #84922)

2024-03-14 Thread Congcong Cai via cfe-commits

HerrCai0907 wrote:

It should be another check in maybe performance, since this check wants to 
detect unused return instead of iterator.

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


[clang-tools-extra] [clang-tidy]bugprone-unused-return-value ignore `++` and `--` operator overloading (PR #84922)

2024-03-18 Thread Congcong Cai via cfe-commits

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


[clang-tools-extra] [clang-tidy] add new check readability-enum-initial-value (PR #86129)

2024-03-21 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 created 
https://github.com/llvm/llvm-project/pull/86129

Fixes: #85243.


>From 4e0845a143a820d4a68ffbdced206654c7593359 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Fri, 15 Mar 2024 08:07:47 +0800
Subject: [PATCH] [clang-tidy] add new check readability-enum-initial-value

Fixes: #85243.
---
 .../clang-tidy/readability/CMakeLists.txt |  1 +
 .../readability/EnumInitialValueCheck.cpp | 82 +++
 .../readability/EnumInitialValueCheck.h   | 31 +++
 .../readability/ReadabilityTidyModule.cpp |  3 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  6 ++
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../checks/readability/enum-initial-value.rst | 45 ++
 .../checkers/readability/enum-initial-value.c | 27 ++
 .../readability/enum-initial-value.cpp| 27 ++
 9 files changed, 223 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/enum-initial-value.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value.c
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index 5728c9970fb65d..dd772d69202548 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -17,6 +17,7 @@ add_clang_library(clangTidyReadabilityModule
   DeleteNullPointerCheck.cpp
   DuplicateIncludeCheck.cpp
   ElseAfterReturnCheck.cpp
+  EnumInitialValueCheck.cpp
   FunctionCognitiveComplexityCheck.cpp
   FunctionSizeCheck.cpp
   IdentifierLengthCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
new file mode 100644
index 00..78d5101d439dde
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
@@ -0,0 +1,82 @@
+//===--- EnumInitialValueCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "EnumInitialValueCheck.h"
+#include "../utils/LexerUtils.h"
+#include "clang/AST/Decl.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Basic/Diagnostic.h"
+#include "llvm/ADT/SmallString.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+namespace {
+
+AST_MATCHER(EnumDecl, isNoneEnumeratorsInitialized) {
+  return llvm::all_of(Node.enumerators(), [](EnumConstantDecl const *ECD) {
+return ECD->getInitExpr() == nullptr;
+  });
+}
+
+AST_MATCHER(EnumDecl, isOnlyFirstEnumeratorsInitialized) {
+  for (EnumConstantDecl const *ECD : Node.enumerators())
+if (ECD == *Node.enumerator_begin()) {
+  if (ECD->getInitExpr() == nullptr)
+return false;
+} else {
+  if (ECD->getInitExpr() != nullptr)
+return false;
+}
+  return true;
+}
+
+AST_MATCHER(EnumDecl, isAllEnumeratorsInitialized) {
+  return llvm::all_of(Node.enumerators(), [](EnumConstantDecl const *ECD) {
+return ECD->getInitExpr() != nullptr;
+  });
+}
+
+} // namespace
+
+void EnumInitialValueCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(enumDecl(unless(anyOf(isNoneEnumeratorsInitialized(),
+   isOnlyFirstEnumeratorsInitialized(),
+   isAllEnumeratorsInitialized(
+ .bind("enum"),
+ this);
+}
+
+void EnumInitialValueCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *Enum = Result.Nodes.getNodeAs("enum");
+  assert(Enum != nullptr);
+  SourceLocation Loc = Enum->getBeginLoc();
+  if (Loc.isInvalid() || Loc.isMacroID())
+return;
+  DiagnosticBuilder Diag =
+  diag(Loc, "inital value in enum %0 has readability issue, "
+"explicit initialization of all of enumerators")
+  << Enum->getName();
+  for (EnumConstantDecl const *ECD : Enum->enumerators())
+if (ECD->getInitExpr() == nullptr) {
+  SourceLocation ECDLoc = ECD->getEndLoc();
+  if (ECDLoc.isInvalid() || ECDLoc.isMacroID())
+continue;
+  std::optional Next = utils::lexer::findNextTokenSkippingComments(
+  ECDLoc, *Result.SourceManager, getLangOpts());
+  if (!Next.has_value() || Next->getLocation().isMacroID())
+continue;
+  llvm::SmallString<

[clang-tools-extra] [clang-tidy] add new check readability-enum-initial-value (PR #86129)

2024-03-21 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/86129

>From 4e0845a143a820d4a68ffbdced206654c7593359 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Fri, 15 Mar 2024 08:07:47 +0800
Subject: [PATCH 1/2] [clang-tidy] add new check readability-enum-initial-value

Fixes: #85243.
---
 .../clang-tidy/readability/CMakeLists.txt |  1 +
 .../readability/EnumInitialValueCheck.cpp | 82 +++
 .../readability/EnumInitialValueCheck.h   | 31 +++
 .../readability/ReadabilityTidyModule.cpp |  3 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  6 ++
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../checks/readability/enum-initial-value.rst | 45 ++
 .../checkers/readability/enum-initial-value.c | 27 ++
 .../readability/enum-initial-value.cpp| 27 ++
 9 files changed, 223 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/enum-initial-value.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value.c
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index 5728c9970fb65d..dd772d69202548 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -17,6 +17,7 @@ add_clang_library(clangTidyReadabilityModule
   DeleteNullPointerCheck.cpp
   DuplicateIncludeCheck.cpp
   ElseAfterReturnCheck.cpp
+  EnumInitialValueCheck.cpp
   FunctionCognitiveComplexityCheck.cpp
   FunctionSizeCheck.cpp
   IdentifierLengthCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
new file mode 100644
index 00..78d5101d439dde
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
@@ -0,0 +1,82 @@
+//===--- EnumInitialValueCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "EnumInitialValueCheck.h"
+#include "../utils/LexerUtils.h"
+#include "clang/AST/Decl.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Basic/Diagnostic.h"
+#include "llvm/ADT/SmallString.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+namespace {
+
+AST_MATCHER(EnumDecl, isNoneEnumeratorsInitialized) {
+  return llvm::all_of(Node.enumerators(), [](EnumConstantDecl const *ECD) {
+return ECD->getInitExpr() == nullptr;
+  });
+}
+
+AST_MATCHER(EnumDecl, isOnlyFirstEnumeratorsInitialized) {
+  for (EnumConstantDecl const *ECD : Node.enumerators())
+if (ECD == *Node.enumerator_begin()) {
+  if (ECD->getInitExpr() == nullptr)
+return false;
+} else {
+  if (ECD->getInitExpr() != nullptr)
+return false;
+}
+  return true;
+}
+
+AST_MATCHER(EnumDecl, isAllEnumeratorsInitialized) {
+  return llvm::all_of(Node.enumerators(), [](EnumConstantDecl const *ECD) {
+return ECD->getInitExpr() != nullptr;
+  });
+}
+
+} // namespace
+
+void EnumInitialValueCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(enumDecl(unless(anyOf(isNoneEnumeratorsInitialized(),
+   isOnlyFirstEnumeratorsInitialized(),
+   isAllEnumeratorsInitialized(
+ .bind("enum"),
+ this);
+}
+
+void EnumInitialValueCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *Enum = Result.Nodes.getNodeAs("enum");
+  assert(Enum != nullptr);
+  SourceLocation Loc = Enum->getBeginLoc();
+  if (Loc.isInvalid() || Loc.isMacroID())
+return;
+  DiagnosticBuilder Diag =
+  diag(Loc, "inital value in enum %0 has readability issue, "
+"explicit initialization of all of enumerators")
+  << Enum->getName();
+  for (EnumConstantDecl const *ECD : Enum->enumerators())
+if (ECD->getInitExpr() == nullptr) {
+  SourceLocation ECDLoc = ECD->getEndLoc();
+  if (ECDLoc.isInvalid() || ECDLoc.isMacroID())
+continue;
+  std::optional Next = utils::lexer::findNextTokenSkippingComments(
+  ECDLoc, *Result.SourceManager, getLangOpts());
+  if (!Next.has_value() || Next->getLocation().isMacroID())
+continue;
+  llvm::SmallString<8> Str{" = "}

[clang-tools-extra] [clang-tidy] add new check readability-enum-initial-value (PR #86129)

2024-03-21 Thread Congcong Cai via cfe-commits


@@ -0,0 +1,82 @@
+//===--- EnumInitialValueCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "EnumInitialValueCheck.h"
+#include "../utils/LexerUtils.h"
+#include "clang/AST/Decl.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Basic/Diagnostic.h"
+#include "llvm/ADT/SmallString.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+namespace {
+
+AST_MATCHER(EnumDecl, isNoneEnumeratorsInitialized) {
+  return llvm::all_of(Node.enumerators(), [](EnumConstantDecl const *ECD) {
+return ECD->getInitExpr() == nullptr;
+  });
+}
+
+AST_MATCHER(EnumDecl, isOnlyFirstEnumeratorsInitialized) {
+  for (EnumConstantDecl const *ECD : Node.enumerators())
+if (ECD == *Node.enumerator_begin()) {
+  if (ECD->getInitExpr() == nullptr)
+return false;
+} else {
+  if (ECD->getInitExpr() != nullptr)
+return false;
+}
+  return true;
+}
+
+AST_MATCHER(EnumDecl, isAllEnumeratorsInitialized) {
+  return llvm::all_of(Node.enumerators(), [](EnumConstantDecl const *ECD) {
+return ECD->getInitExpr() != nullptr;
+  });
+}
+
+} // namespace
+
+void EnumInitialValueCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(enumDecl(unless(anyOf(isNoneEnumeratorsInitialized(),
+   isOnlyFirstEnumeratorsInitialized(),
+   isAllEnumeratorsInitialized(

HerrCai0907 wrote:

I will add some options to make it possible to let it strict.
But I don't find the code guideline about "all enums are initialized but with 
consecutive values, in such case those inits could be removed and only first 
could be left". Inferring the usage of enum is hard and impossible, I think 
adding this check will create lots of false positive.
For example 
```c++
enum A {
a = 0,
b = 1,
c = 2,
}
```
can also be a bitmap.

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


[clang-tools-extra] [clang-tidy] add new check readability-enum-initial-value (PR #86129)

2024-03-26 Thread Congcong Cai via cfe-commits


@@ -0,0 +1,82 @@
+//===--- EnumInitialValueCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "EnumInitialValueCheck.h"
+#include "../utils/LexerUtils.h"
+#include "clang/AST/Decl.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Basic/Diagnostic.h"
+#include "llvm/ADT/SmallString.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+namespace {
+
+AST_MATCHER(EnumDecl, isNoneEnumeratorsInitialized) {
+  return llvm::all_of(Node.enumerators(), [](EnumConstantDecl const *ECD) {
+return ECD->getInitExpr() == nullptr;
+  });
+}
+
+AST_MATCHER(EnumDecl, isOnlyFirstEnumeratorsInitialized) {
+  for (EnumConstantDecl const *ECD : Node.enumerators())
+if (ECD == *Node.enumerator_begin()) {
+  if (ECD->getInitExpr() == nullptr)
+return false;
+} else {
+  if (ECD->getInitExpr() != nullptr)
+return false;
+}
+  return true;
+}
+
+AST_MATCHER(EnumDecl, isAllEnumeratorsInitialized) {
+  return llvm::all_of(Node.enumerators(), [](EnumConstantDecl const *ECD) {
+return ECD->getInitExpr() != nullptr;
+  });
+}
+
+} // namespace
+
+void EnumInitialValueCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(enumDecl(unless(anyOf(isNoneEnumeratorsInitialized(),
+   isOnlyFirstEnumeratorsInitialized(),
+   isAllEnumeratorsInitialized(
+ .bind("enum"),
+ this);
+}
+
+void EnumInitialValueCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *Enum = Result.Nodes.getNodeAs("enum");
+  assert(Enum != nullptr);
+  SourceLocation Loc = Enum->getBeginLoc();
+  if (Loc.isInvalid() || Loc.isMacroID())
+return;
+  DiagnosticBuilder Diag =
+  diag(Loc, "inital value in enum %0 has readability issue, "
+"explicit initialization of all of enumerators")
+  << Enum->getName();
+  for (EnumConstantDecl const *ECD : Enum->enumerators())
+if (ECD->getInitExpr() == nullptr) {
+  SourceLocation ECDLoc = ECD->getEndLoc();
+  if (ECDLoc.isInvalid() || ECDLoc.isMacroID())
+continue;
+  std::optional Next = utils::lexer::findNextTokenSkippingComments(

HerrCai0907 wrote:

fix in the end location of enum constant will insert the string before enum 
constant.
`a3,` will be changed to `= 3a3,`.

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


[clang-tools-extra] [clang-tidy] add new check readability-enum-initial-value (PR #86129)

2024-03-26 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/86129

>From 4e0845a143a820d4a68ffbdced206654c7593359 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Fri, 15 Mar 2024 08:07:47 +0800
Subject: [PATCH 1/5] [clang-tidy] add new check readability-enum-initial-value

Fixes: #85243.
---
 .../clang-tidy/readability/CMakeLists.txt |  1 +
 .../readability/EnumInitialValueCheck.cpp | 82 +++
 .../readability/EnumInitialValueCheck.h   | 31 +++
 .../readability/ReadabilityTidyModule.cpp |  3 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  6 ++
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../checks/readability/enum-initial-value.rst | 45 ++
 .../checkers/readability/enum-initial-value.c | 27 ++
 .../readability/enum-initial-value.cpp| 27 ++
 9 files changed, 223 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/enum-initial-value.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value.c
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index 5728c9970fb65d..dd772d69202548 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -17,6 +17,7 @@ add_clang_library(clangTidyReadabilityModule
   DeleteNullPointerCheck.cpp
   DuplicateIncludeCheck.cpp
   ElseAfterReturnCheck.cpp
+  EnumInitialValueCheck.cpp
   FunctionCognitiveComplexityCheck.cpp
   FunctionSizeCheck.cpp
   IdentifierLengthCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
new file mode 100644
index 00..78d5101d439dde
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
@@ -0,0 +1,82 @@
+//===--- EnumInitialValueCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "EnumInitialValueCheck.h"
+#include "../utils/LexerUtils.h"
+#include "clang/AST/Decl.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Basic/Diagnostic.h"
+#include "llvm/ADT/SmallString.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+namespace {
+
+AST_MATCHER(EnumDecl, isNoneEnumeratorsInitialized) {
+  return llvm::all_of(Node.enumerators(), [](EnumConstantDecl const *ECD) {
+return ECD->getInitExpr() == nullptr;
+  });
+}
+
+AST_MATCHER(EnumDecl, isOnlyFirstEnumeratorsInitialized) {
+  for (EnumConstantDecl const *ECD : Node.enumerators())
+if (ECD == *Node.enumerator_begin()) {
+  if (ECD->getInitExpr() == nullptr)
+return false;
+} else {
+  if (ECD->getInitExpr() != nullptr)
+return false;
+}
+  return true;
+}
+
+AST_MATCHER(EnumDecl, isAllEnumeratorsInitialized) {
+  return llvm::all_of(Node.enumerators(), [](EnumConstantDecl const *ECD) {
+return ECD->getInitExpr() != nullptr;
+  });
+}
+
+} // namespace
+
+void EnumInitialValueCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(enumDecl(unless(anyOf(isNoneEnumeratorsInitialized(),
+   isOnlyFirstEnumeratorsInitialized(),
+   isAllEnumeratorsInitialized(
+ .bind("enum"),
+ this);
+}
+
+void EnumInitialValueCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *Enum = Result.Nodes.getNodeAs("enum");
+  assert(Enum != nullptr);
+  SourceLocation Loc = Enum->getBeginLoc();
+  if (Loc.isInvalid() || Loc.isMacroID())
+return;
+  DiagnosticBuilder Diag =
+  diag(Loc, "inital value in enum %0 has readability issue, "
+"explicit initialization of all of enumerators")
+  << Enum->getName();
+  for (EnumConstantDecl const *ECD : Enum->enumerators())
+if (ECD->getInitExpr() == nullptr) {
+  SourceLocation ECDLoc = ECD->getEndLoc();
+  if (ECDLoc.isInvalid() || ECDLoc.isMacroID())
+continue;
+  std::optional Next = utils::lexer::findNextTokenSkippingComments(
+  ECDLoc, *Result.SourceManager, getLangOpts());
+  if (!Next.has_value() || Next->getLocation().isMacroID())
+continue;
+  llvm::SmallString<8> Str{" = "}

[clang-tools-extra] [clang-tidy] add new check readability-enum-initial-value (PR #86129)

2024-03-27 Thread Congcong Cai via cfe-commits


@@ -0,0 +1,193 @@
+//===--- EnumInitialValueCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "EnumInitialValueCheck.h"
+#include "../utils/LexerUtils.h"
+#include "clang/AST/Decl.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/SourceLocation.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallString.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+EnumInitialValueCheck::EnumInitialValueCheck(StringRef Name,
+ ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  AllowExplicitZeroFirstInitialValue(
+  Options.get("AllowExplicitZeroFirstInitialValue", true)),
+  AllowExplicitLinearInitialValues(
+  Options.get("AllowExplicitLinearInitialValues", true)) {}
+
+void EnumInitialValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+  Options.store(Opts, "AllowExplicitZeroFirstInitialValue",
+AllowExplicitZeroFirstInitialValue);
+  Options.store(Opts, "AllowExplicitLinearInitialValues",
+AllowExplicitLinearInitialValues);
+}
+
+namespace {
+
+bool isNoneEnumeratorsInitialized(const EnumDecl &Node) {
+  return llvm::all_of(Node.enumerators(), [](const EnumConstantDecl *ECD) {
+return ECD->getInitExpr() == nullptr;
+  });
+}
+
+bool isOnlyFirstEnumeratorsInitialized(const EnumDecl &Node) {
+  bool IsFirst = true;
+  for (const EnumConstantDecl *ECD : Node.enumerators())
+if (IsFirst) {
+  IsFirst = false;
+  if (ECD->getInitExpr() == nullptr)
+return false;
+} else {
+  if (ECD->getInitExpr() != nullptr)
+return false;
+}

HerrCai0907 wrote:

I think simplify it to `if ((IsFirst && ECD->getInitExpr() == nullptr) || 
(!IsFirst && ECD->getInitExpr() != nullptr))` is enough. `if (IsFirst == 
(ECD->getInitExpr() == nullptr))` make code hard to understand. And after 
optimization, the result should be the same.

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


[clang-tools-extra] [clang-tidy] add new check readability-enum-initial-value (PR #86129)

2024-03-27 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/86129

>From 4e0845a143a820d4a68ffbdced206654c7593359 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Fri, 15 Mar 2024 08:07:47 +0800
Subject: [PATCH 1/6] [clang-tidy] add new check readability-enum-initial-value

Fixes: #85243.
---
 .../clang-tidy/readability/CMakeLists.txt |  1 +
 .../readability/EnumInitialValueCheck.cpp | 82 +++
 .../readability/EnumInitialValueCheck.h   | 31 +++
 .../readability/ReadabilityTidyModule.cpp |  3 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  6 ++
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../checks/readability/enum-initial-value.rst | 45 ++
 .../checkers/readability/enum-initial-value.c | 27 ++
 .../readability/enum-initial-value.cpp| 27 ++
 9 files changed, 223 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/enum-initial-value.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value.c
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index 5728c9970fb65d..dd772d69202548 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -17,6 +17,7 @@ add_clang_library(clangTidyReadabilityModule
   DeleteNullPointerCheck.cpp
   DuplicateIncludeCheck.cpp
   ElseAfterReturnCheck.cpp
+  EnumInitialValueCheck.cpp
   FunctionCognitiveComplexityCheck.cpp
   FunctionSizeCheck.cpp
   IdentifierLengthCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
new file mode 100644
index 00..78d5101d439dde
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
@@ -0,0 +1,82 @@
+//===--- EnumInitialValueCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "EnumInitialValueCheck.h"
+#include "../utils/LexerUtils.h"
+#include "clang/AST/Decl.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Basic/Diagnostic.h"
+#include "llvm/ADT/SmallString.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+namespace {
+
+AST_MATCHER(EnumDecl, isNoneEnumeratorsInitialized) {
+  return llvm::all_of(Node.enumerators(), [](EnumConstantDecl const *ECD) {
+return ECD->getInitExpr() == nullptr;
+  });
+}
+
+AST_MATCHER(EnumDecl, isOnlyFirstEnumeratorsInitialized) {
+  for (EnumConstantDecl const *ECD : Node.enumerators())
+if (ECD == *Node.enumerator_begin()) {
+  if (ECD->getInitExpr() == nullptr)
+return false;
+} else {
+  if (ECD->getInitExpr() != nullptr)
+return false;
+}
+  return true;
+}
+
+AST_MATCHER(EnumDecl, isAllEnumeratorsInitialized) {
+  return llvm::all_of(Node.enumerators(), [](EnumConstantDecl const *ECD) {
+return ECD->getInitExpr() != nullptr;
+  });
+}
+
+} // namespace
+
+void EnumInitialValueCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(enumDecl(unless(anyOf(isNoneEnumeratorsInitialized(),
+   isOnlyFirstEnumeratorsInitialized(),
+   isAllEnumeratorsInitialized(
+ .bind("enum"),
+ this);
+}
+
+void EnumInitialValueCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *Enum = Result.Nodes.getNodeAs("enum");
+  assert(Enum != nullptr);
+  SourceLocation Loc = Enum->getBeginLoc();
+  if (Loc.isInvalid() || Loc.isMacroID())
+return;
+  DiagnosticBuilder Diag =
+  diag(Loc, "inital value in enum %0 has readability issue, "
+"explicit initialization of all of enumerators")
+  << Enum->getName();
+  for (EnumConstantDecl const *ECD : Enum->enumerators())
+if (ECD->getInitExpr() == nullptr) {
+  SourceLocation ECDLoc = ECD->getEndLoc();
+  if (ECDLoc.isInvalid() || ECDLoc.isMacroID())
+continue;
+  std::optional Next = utils::lexer::findNextTokenSkippingComments(
+  ECDLoc, *Result.SourceManager, getLangOpts());
+  if (!Next.has_value() || Next->getLocation().isMacroID())
+continue;
+  llvm::SmallString<8> Str{" = "}

[clang-tools-extra] [clang-tidy] add new check readability-enum-initial-value (PR #86129)

2024-03-27 Thread Congcong Cai via cfe-commits

HerrCai0907 wrote:

> Try getting rid of findNextTokenSkippingComments, and maybe just jump to 
> begin() + token width

Do you think it is a good idea to use 
`ECD->getLocation().getLocWithOffset(ECD->getName().size())`?

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


[clang-tools-extra] [clang-tidy] add new check readability-enum-initial-value (PR #86129)

2024-03-27 Thread Congcong Cai via cfe-commits

HerrCai0907 wrote:

> > Try getting rid of findNextTokenSkippingComments, and maybe just jump to 
> > begin() + token width
> 
> Do you think it is a good idea to use 
> `ECD->getLocation().getLocWithOffset(ECD->getName().size())`?

It doesn't work for macro. I think the most precise way is to fix it is using 
`findNextTokenSkippingComments`. Why should we avoid to use 
`findNextTokenSkippingComments`? Is it for performance reasons?

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


[clang-tools-extra] [clang-tidy] add new check readability-enum-initial-value (PR #86129)

2024-03-30 Thread Congcong Cai via cfe-commits


@@ -0,0 +1,199 @@
+//===--- EnumInitialValueCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "EnumInitialValueCheck.h"
+#include "../utils/LexerUtils.h"
+#include "clang/AST/Decl.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/SourceLocation.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallString.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+static bool isNoneEnumeratorsInitialized(const EnumDecl &Node) {
+  return llvm::all_of(Node.enumerators(), [](const EnumConstantDecl *ECD) {
+return ECD->getInitExpr() == nullptr;
+  });
+}
+
+static bool isOnlyFirstEnumeratorInitialized(const EnumDecl &Node) {
+  bool IsFirst = true;
+  for (const EnumConstantDecl *ECD : Node.enumerators()) {
+if ((IsFirst && ECD->getInitExpr() == nullptr) ||
+(!IsFirst && ECD->getInitExpr() != nullptr))
+  return false;
+IsFirst = false;
+  }
+  return !IsFirst;
+}
+
+static bool areAllEnumeratorsInitialized(const EnumDecl &Node) {
+  return llvm::all_of(Node.enumerators(), [](const EnumConstantDecl *ECD) {
+return ECD->getInitExpr() != nullptr;
+  });
+}
+
+/// Check if \p Enumerator is initialized with a (potentially negated) \c
+/// IntegerLiteral.
+static bool isInitializedByLiteral(const EnumConstantDecl *Enumerator) {
+  const Expr *const Init = Enumerator->getInitExpr();
+  if (!Init)
+return false;
+  return Init->isIntegerConstantExpr(Enumerator->getASTContext());
+}
+
+static void cleanInitialValue(DiagnosticBuilder &Diag,
+  const EnumConstantDecl *ECD,
+  const SourceManager &SM,
+  const LangOptions &LangOpts) {
+  std::optional EqualToken = 
utils::lexer::findNextTokenSkippingComments(
+  ECD->getLocation(), SM, LangOpts);
+  if (!EqualToken.has_value())
+return;
+  SourceLocation EqualLoc{EqualToken->getLocation()};
+  if (EqualLoc.isInvalid() || EqualLoc.isMacroID())
+return;
+  SourceRange InitExprRange = ECD->getInitExpr()->getSourceRange();
+  if (InitExprRange.isInvalid() || InitExprRange.getBegin().isMacroID() ||
+  InitExprRange.getEnd().isMacroID())
+return;
+  Diag << FixItHint::CreateRemoval(EqualLoc)
+   << FixItHint::CreateRemoval(InitExprRange);
+  return;
+}
+
+namespace {
+
+AST_MATCHER(EnumDecl, isMacro) {
+  SourceLocation Loc = Node.getBeginLoc();
+  return Loc.isMacroID();
+}
+
+AST_MATCHER(EnumDecl, hasConsistentInitialValues) {
+  return isNoneEnumeratorsInitialized(Node) ||
+ isOnlyFirstEnumeratorInitialized(Node) ||
+ areAllEnumeratorsInitialized(Node);
+}
+
+AST_MATCHER(EnumDecl, hasZeroInitialValueForFirstEnumerator) {
+  EnumDecl::enumerator_range Enumerators = Node.enumerators();
+  if (Enumerators.empty())
+return false;
+  const EnumConstantDecl *ECD = *Enumerators.begin();
+  return isOnlyFirstEnumeratorInitialized(Node) &&
+ isInitializedByLiteral(ECD) && ECD->getInitVal().isZero();
+}
+
+/// Excludes bitfields because enumerators initialized with the result of a
+/// bitwise operator on enumeration values or any other expr that is not a
+/// potentially negative integer literal.
+/// Enumerations where it is not directly clear if they are used with
+/// bitmask, evident when enumerators are only initialized with (potentially
+/// negative) integer literals, are ignored. This is also the case when all
+/// enumerators are powers of two (e.g., 0, 1, 2).
+AST_MATCHER(EnumDecl, hasSequentialInitialValues) {
+  EnumDecl::enumerator_range Enumerators = Node.enumerators();
+  if (Enumerators.empty())
+return false;
+  const EnumConstantDecl *const FirstEnumerator = *Node.enumerator_begin();
+  llvm::APSInt PrevValue = FirstEnumerator->getInitVal();
+  if (!isInitializedByLiteral(FirstEnumerator))
+return false;
+  bool AllEnumeratorsArePowersOfTwo = true;
+  for (const EnumConstantDecl *Enumerator : llvm::drop_begin(Enumerators)) {
+const llvm::APSInt NewValue = Enumerator->getInitVal();
+if (NewValue != ++PrevValue)
+  return false;
+if (!isInitializedByLiteral(Enumerator))
+  return false;
+PrevValue = NewValue;
+AllEnumeratorsArePowersOfTwo &= NewValue.isPowerOf2();
+  }
+  return !AllEnumeratorsArePowersOfTwo;
+}
+
+} // namespace
+
+EnumInitialValueCheck::EnumInitialValueCheck(StringRef Name,
+ ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  AllowExplicitZeroFirstInitialValue(
+  Options.get("AllowExplicitZeroFirstInitialValue", tru

[clang-tools-extra] [clang-tidy] add new check readability-enum-initial-value (PR #86129)

2024-03-30 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/86129

>From 4e0845a143a820d4a68ffbdced206654c7593359 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Fri, 15 Mar 2024 08:07:47 +0800
Subject: [PATCH 1/7] [clang-tidy] add new check readability-enum-initial-value

Fixes: #85243.
---
 .../clang-tidy/readability/CMakeLists.txt |  1 +
 .../readability/EnumInitialValueCheck.cpp | 82 +++
 .../readability/EnumInitialValueCheck.h   | 31 +++
 .../readability/ReadabilityTidyModule.cpp |  3 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  6 ++
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../checks/readability/enum-initial-value.rst | 45 ++
 .../checkers/readability/enum-initial-value.c | 27 ++
 .../readability/enum-initial-value.cpp| 27 ++
 9 files changed, 223 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/enum-initial-value.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value.c
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index 5728c9970fb65d..dd772d69202548 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -17,6 +17,7 @@ add_clang_library(clangTidyReadabilityModule
   DeleteNullPointerCheck.cpp
   DuplicateIncludeCheck.cpp
   ElseAfterReturnCheck.cpp
+  EnumInitialValueCheck.cpp
   FunctionCognitiveComplexityCheck.cpp
   FunctionSizeCheck.cpp
   IdentifierLengthCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
new file mode 100644
index 00..78d5101d439dde
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
@@ -0,0 +1,82 @@
+//===--- EnumInitialValueCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "EnumInitialValueCheck.h"
+#include "../utils/LexerUtils.h"
+#include "clang/AST/Decl.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Basic/Diagnostic.h"
+#include "llvm/ADT/SmallString.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+namespace {
+
+AST_MATCHER(EnumDecl, isNoneEnumeratorsInitialized) {
+  return llvm::all_of(Node.enumerators(), [](EnumConstantDecl const *ECD) {
+return ECD->getInitExpr() == nullptr;
+  });
+}
+
+AST_MATCHER(EnumDecl, isOnlyFirstEnumeratorsInitialized) {
+  for (EnumConstantDecl const *ECD : Node.enumerators())
+if (ECD == *Node.enumerator_begin()) {
+  if (ECD->getInitExpr() == nullptr)
+return false;
+} else {
+  if (ECD->getInitExpr() != nullptr)
+return false;
+}
+  return true;
+}
+
+AST_MATCHER(EnumDecl, isAllEnumeratorsInitialized) {
+  return llvm::all_of(Node.enumerators(), [](EnumConstantDecl const *ECD) {
+return ECD->getInitExpr() != nullptr;
+  });
+}
+
+} // namespace
+
+void EnumInitialValueCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(enumDecl(unless(anyOf(isNoneEnumeratorsInitialized(),
+   isOnlyFirstEnumeratorsInitialized(),
+   isAllEnumeratorsInitialized(
+ .bind("enum"),
+ this);
+}
+
+void EnumInitialValueCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *Enum = Result.Nodes.getNodeAs("enum");
+  assert(Enum != nullptr);
+  SourceLocation Loc = Enum->getBeginLoc();
+  if (Loc.isInvalid() || Loc.isMacroID())
+return;
+  DiagnosticBuilder Diag =
+  diag(Loc, "inital value in enum %0 has readability issue, "
+"explicit initialization of all of enumerators")
+  << Enum->getName();
+  for (EnumConstantDecl const *ECD : Enum->enumerators())
+if (ECD->getInitExpr() == nullptr) {
+  SourceLocation ECDLoc = ECD->getEndLoc();
+  if (ECDLoc.isInvalid() || ECDLoc.isMacroID())
+continue;
+  std::optional Next = utils::lexer::findNextTokenSkippingComments(
+  ECDLoc, *Result.SourceManager, getLangOpts());
+  if (!Next.has_value() || Next->getLocation().isMacroID())
+continue;
+  llvm::SmallString<8> Str{" = "}

[clang-tools-extra] [clang-tidy] add new check readability-enum-initial-value (PR #86129)

2024-03-30 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/86129

>From 4e0845a143a820d4a68ffbdced206654c7593359 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Fri, 15 Mar 2024 08:07:47 +0800
Subject: [PATCH 1/8] [clang-tidy] add new check readability-enum-initial-value

Fixes: #85243.
---
 .../clang-tidy/readability/CMakeLists.txt |  1 +
 .../readability/EnumInitialValueCheck.cpp | 82 +++
 .../readability/EnumInitialValueCheck.h   | 31 +++
 .../readability/ReadabilityTidyModule.cpp |  3 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  6 ++
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../checks/readability/enum-initial-value.rst | 45 ++
 .../checkers/readability/enum-initial-value.c | 27 ++
 .../readability/enum-initial-value.cpp| 27 ++
 9 files changed, 223 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/enum-initial-value.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value.c
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index 5728c9970fb65d..dd772d69202548 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -17,6 +17,7 @@ add_clang_library(clangTidyReadabilityModule
   DeleteNullPointerCheck.cpp
   DuplicateIncludeCheck.cpp
   ElseAfterReturnCheck.cpp
+  EnumInitialValueCheck.cpp
   FunctionCognitiveComplexityCheck.cpp
   FunctionSizeCheck.cpp
   IdentifierLengthCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
new file mode 100644
index 00..78d5101d439dde
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/EnumInitialValueCheck.cpp
@@ -0,0 +1,82 @@
+//===--- EnumInitialValueCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "EnumInitialValueCheck.h"
+#include "../utils/LexerUtils.h"
+#include "clang/AST/Decl.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Basic/Diagnostic.h"
+#include "llvm/ADT/SmallString.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+namespace {
+
+AST_MATCHER(EnumDecl, isNoneEnumeratorsInitialized) {
+  return llvm::all_of(Node.enumerators(), [](EnumConstantDecl const *ECD) {
+return ECD->getInitExpr() == nullptr;
+  });
+}
+
+AST_MATCHER(EnumDecl, isOnlyFirstEnumeratorsInitialized) {
+  for (EnumConstantDecl const *ECD : Node.enumerators())
+if (ECD == *Node.enumerator_begin()) {
+  if (ECD->getInitExpr() == nullptr)
+return false;
+} else {
+  if (ECD->getInitExpr() != nullptr)
+return false;
+}
+  return true;
+}
+
+AST_MATCHER(EnumDecl, isAllEnumeratorsInitialized) {
+  return llvm::all_of(Node.enumerators(), [](EnumConstantDecl const *ECD) {
+return ECD->getInitExpr() != nullptr;
+  });
+}
+
+} // namespace
+
+void EnumInitialValueCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(enumDecl(unless(anyOf(isNoneEnumeratorsInitialized(),
+   isOnlyFirstEnumeratorsInitialized(),
+   isAllEnumeratorsInitialized(
+ .bind("enum"),
+ this);
+}
+
+void EnumInitialValueCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *Enum = Result.Nodes.getNodeAs("enum");
+  assert(Enum != nullptr);
+  SourceLocation Loc = Enum->getBeginLoc();
+  if (Loc.isInvalid() || Loc.isMacroID())
+return;
+  DiagnosticBuilder Diag =
+  diag(Loc, "inital value in enum %0 has readability issue, "
+"explicit initialization of all of enumerators")
+  << Enum->getName();
+  for (EnumConstantDecl const *ECD : Enum->enumerators())
+if (ECD->getInitExpr() == nullptr) {
+  SourceLocation ECDLoc = ECD->getEndLoc();
+  if (ECDLoc.isInvalid() || ECDLoc.isMacroID())
+continue;
+  std::optional Next = utils::lexer::findNextTokenSkippingComments(
+  ECDLoc, *Result.SourceManager, getLangOpts());
+  if (!Next.has_value() || Next->getLocation().isMacroID())
+continue;
+  llvm::SmallString<8> Str{" = "}

[clang-tools-extra] [clang-tidy] add new check readability-enum-initial-value (PR #86129)

2024-03-31 Thread Congcong Cai via cfe-commits


@@ -0,0 +1,200 @@
+//===--- EnumInitialValueCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "EnumInitialValueCheck.h"
+#include "../utils/LexerUtils.h"
+#include "clang/AST/Decl.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/SourceLocation.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallString.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+static bool isNoneEnumeratorsInitialized(const EnumDecl &Node) {
+  return llvm::all_of(Node.enumerators(), [](const EnumConstantDecl *ECD) {
+return ECD->getInitExpr() == nullptr;
+  });
+}
+
+static bool isOnlyFirstEnumeratorInitialized(const EnumDecl &Node) {
+  bool IsFirst = true;
+  for (const EnumConstantDecl *ECD : Node.enumerators()) {
+if ((IsFirst && ECD->getInitExpr() == nullptr) ||
+(!IsFirst && ECD->getInitExpr() != nullptr))
+  return false;
+IsFirst = false;
+  }
+  return !IsFirst;
+}
+
+static bool areAllEnumeratorsInitialized(const EnumDecl &Node) {
+  return llvm::all_of(Node.enumerators(), [](const EnumConstantDecl *ECD) {
+return ECD->getInitExpr() != nullptr;
+  });
+}
+
+/// Check if \p Enumerator is initialized with a (potentially negated) \c
+/// IntegerLiteral.
+static bool isInitializedByLiteral(const EnumConstantDecl *Enumerator) {
+  const Expr *const Init = Enumerator->getInitExpr();
+  if (!Init)
+return false;
+  return Init->isIntegerConstantExpr(Enumerator->getASTContext());
+}
+
+static void cleanInitialValue(DiagnosticBuilder &Diag,
+  const EnumConstantDecl *ECD,
+  const SourceManager &SM,
+  const LangOptions &LangOpts) {
+  const SourceRange InitExprRange = ECD->getInitExpr()->getSourceRange();
+  if (InitExprRange.isInvalid() || InitExprRange.getBegin().isMacroID() ||
+  InitExprRange.getEnd().isMacroID())
+return;
+  std::optional EqualToken = 
utils::lexer::findNextTokenSkippingComments(
+  ECD->getLocation(), SM, LangOpts);
+  if (!EqualToken.has_value() ||
+  EqualToken.value().getKind() != tok::TokenKind::equal)
+return;
+  const SourceLocation EqualLoc{EqualToken->getLocation()};
+  if (EqualLoc.isInvalid() || EqualLoc.isMacroID())
+return;
+  Diag << FixItHint::CreateRemoval(EqualLoc)
+   << FixItHint::CreateRemoval(InitExprRange);
+  return;
+}
+
+namespace {
+
+AST_MATCHER(EnumDecl, isMacro) {
+  SourceLocation Loc = Node.getBeginLoc();
+  return Loc.isMacroID();
+}
+
+AST_MATCHER(EnumDecl, hasConsistentInitialValues) {
+  return isNoneEnumeratorsInitialized(Node) ||
+ isOnlyFirstEnumeratorInitialized(Node) ||
+ areAllEnumeratorsInitialized(Node);
+}
+
+AST_MATCHER(EnumDecl, hasZeroInitialValueForFirstEnumerator) {
+  const EnumDecl::enumerator_range Enumerators = Node.enumerators();
+  if (Enumerators.empty())
+return false;
+  const EnumConstantDecl *ECD = *Enumerators.begin();
+  return isOnlyFirstEnumeratorInitialized(Node) &&
+ isInitializedByLiteral(ECD) && ECD->getInitVal().isZero();
+}
+
+/// Excludes bitfields because enumerators initialized with the result of a
+/// bitwise operator on enumeration values or any other expr that is not a
+/// potentially negative integer literal.
+/// Enumerations where it is not directly clear if they are used with
+/// bitmask, evident when enumerators are only initialized with (potentially
+/// negative) integer literals, are ignored. This is also the case when all
+/// enumerators are powers of two (e.g., 0, 1, 2).
+AST_MATCHER(EnumDecl, hasSequentialInitialValues) {
+  const EnumDecl::enumerator_range Enumerators = Node.enumerators();
+  if (Enumerators.empty())
+return false;
+  const EnumConstantDecl *const FirstEnumerator = *Node.enumerator_begin();
+  llvm::APSInt PrevValue = FirstEnumerator->getInitVal();
+  if (!isInitializedByLiteral(FirstEnumerator))
+return false;
+  bool AllEnumeratorsArePowersOfTwo = true;
+  for (const EnumConstantDecl *Enumerator : llvm::drop_begin(Enumerators)) {
+const llvm::APSInt NewValue = Enumerator->getInitVal();
+if (NewValue != ++PrevValue)
+  return false;
+if (!isInitializedByLiteral(Enumerator))
+  return false;
+PrevValue = NewValue;
+AllEnumeratorsArePowersOfTwo &= NewValue.isPowerOf2();
+  }
+  return !AllEnumeratorsArePowersOfTwo;
+}
+
+} // namespace
+
+EnumInitialValueCheck::EnumInitialValueCheck(StringRef Name,
+ ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  AllowExplici

[clang-tools-extra] [clang-tidy] add new check readability-enum-initial-value (PR #86129)

2024-04-01 Thread Congcong Cai via cfe-commits

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


[clang] [llvm] [clang-tools-extra] [clang] reject to capture variable in `RequiresExprBodyDecl` (PR #78598)

2024-02-06 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/78598

>From 8fa3dc43e770025308da47f6aff309fa58c47fc3 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Thu, 18 Jan 2024 23:12:23 +0800
Subject: [PATCH 1/4] [clang] reject to capture variable in
 `RequiresExprBodyDecl`

Expression in `RequiresExprBodyDecl` is resolved as constants and should not be 
captured.
Fixes: #69307, #76593.
---
 clang/lib/Sema/SemaExpr.cpp   | 21 ++--
 ...uires-expression-with-capture-variable.cpp | 25 +++
 2 files changed, 38 insertions(+), 8 deletions(-)
 create mode 100644 
clang/test/SemaCXX/requires-expression-with-capture-variable.cpp

diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 6413a48f809ac..580e759f63495 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -19704,6 +19704,12 @@ static void buildLambdaCaptureFixit(Sema &Sema, 
LambdaScopeInfo *LSI,
   }
 }
 
+static DeclContext *ignoreReuquiresBodyDecl(DeclContext *DC) {
+  if (isa_and_present(DC))
+return DC->getParent();
+  return DC;
+}
+
 bool Sema::tryCaptureVariable(
 ValueDecl *Var, SourceLocation ExprLoc, TryCaptureKind Kind,
 SourceLocation EllipsisLoc, bool BuildAndDiagnose, QualType &CaptureType,
@@ -19711,15 +19717,15 @@ bool Sema::tryCaptureVariable(
   // An init-capture is notionally from the context surrounding its
   // declaration, but its parent DC is the lambda class.
   DeclContext *VarDC = Var->getDeclContext();
-  DeclContext *DC = CurContext;
-
   // tryCaptureVariable is called every time a DeclRef is formed,
   // it can therefore have non-negigible impact on performances.
   // For local variables and when there is no capturing scope,
   // we can bailout early.
-  if (CapturingFunctionScopes == 0 && (!BuildAndDiagnose || VarDC == DC))
+  if (CapturingFunctionScopes == 0 && (!BuildAndDiagnose || VarDC == 
CurContext))
 return true;
 
+  DeclContext *DC = ignoreReuquiresBodyDecl(CurContext);
+
   const auto *VD = dyn_cast(Var);
   if (VD) {
 if (VD->isInitCapture())
@@ -19789,11 +19795,10 @@ bool Sema::tryCaptureVariable(
 
 // Only block literals, captured statements, and lambda expressions can
 // capture; other scopes don't work.
-DeclContext *ParentDC =
-!IsInScopeDeclarationContext
-? DC->getParent()
-: getParentOfCapturingContextOrNull(DC, Var, ExprLoc,
-BuildAndDiagnose, *this);
+DeclContext *ParentDC = IsInScopeDeclarationContext
+? getParentOfCapturingContextOrNull(
+  DC, Var, ExprLoc, BuildAndDiagnose, 
*this)
+: DC->getParent();
 // We need to check for the parent *first* because, if we *have*
 // private-captured a global variable, we need to recursively capture it in
 // intermediate blocks, lambdas, etc.
diff --git a/clang/test/SemaCXX/requires-expression-with-capture-variable.cpp 
b/clang/test/SemaCXX/requires-expression-with-capture-variable.cpp
new file mode 100644
index 0..d01a54133f6c3
--- /dev/null
+++ b/clang/test/SemaCXX/requires-expression-with-capture-variable.cpp
@@ -0,0 +1,25 @@
+// RUN: %clang -fsyntax-only -std=c++20 -Xclang -verify %s
+
+// expected-no-diagnostics
+
+auto GH69307_Func_1() {
+  constexpr auto b = 1;
+  return [&](auto c) -> int
+   requires requires { b + c; }
+  { return 1; };
+};
+auto GH69307_Func_Ret = GH69307_Func_1()(1);
+
+auto GH69307_Lambda_1 = []() {
+  return [&](auto c) -> int
+   requires requires { c; }
+  { return 1; };
+};
+auto GH69307_Lambda_1_Ret = GH69307_Lambda_1()(1);
+
+auto GH69307_Lambda_2 = [](auto c) {
+  return [&]() -> int
+   requires requires { c; }
+  { return 1; };
+};
+auto GH69307_Lambda_2_Ret = GH69307_Lambda_2(1)();

>From a99f16ff51702ff249cdf8a47de0cf24a08f694a Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Sat, 20 Jan 2024 13:40:12 +0800
Subject: [PATCH 2/4] another test

---
 clang/test/SemaCXX/warn-unused-lambda-capture-cxx20.cpp | 7 +++
 clang/test/SemaCXX/warn-unused-lambda-capture.cpp   | 4 
 2 files changed, 11 insertions(+)
 create mode 100644 clang/test/SemaCXX/warn-unused-lambda-capture-cxx20.cpp

diff --git a/clang/test/SemaCXX/warn-unused-lambda-capture-cxx20.cpp 
b/clang/test/SemaCXX/warn-unused-lambda-capture-cxx20.cpp
new file mode 100644
index 0..b787edb188ed8
--- /dev/null
+++ b/clang/test/SemaCXX/warn-unused-lambda-capture-cxx20.cpp
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -fsyntax-only -Wunused-lambda-capture 
-Wused-but-marked-unused -Wno-uninitialized -verify -std=c++20 %s
+
+void test() {
+  int i;
+  auto explicit_by_value_unused_requires = [i] (auto) requires requires { i; } 
{}; // expected-warning{{lambda capture 'i' is not required to be captured for 
this use}}
+  explicit_by_value_unused_requires(1);
+}
diff --git a/clang/

[clang] [llvm] [clang-tools-extra] [clang] reject to capture variable in `RequiresExprBodyDecl` (PR #78598)

2024-02-06 Thread Congcong Cai via cfe-commits


@@ -19706,22 +19706,29 @@ static void buildLambdaCaptureFixit(Sema &Sema, 
LambdaScopeInfo *LSI,
   }
 }
 
+static DeclContext *ignoreReuquiresBodyDecl(DeclContext *DC) {

HerrCai0907 wrote:

I think inline it will be better since we only call it once.

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


[clang] [clang-tools-extra] [llvm] [clang] reject to capture variable in `RequiresExprBodyDecl` (PR #78598)

2024-02-06 Thread Congcong Cai via cfe-commits


@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -fsyntax-only -Wunused-lambda-capture 
-Wused-but-marked-unused -Wno-uninitialized -verify -std=c++20 %s
+
+void test() {
+  int i;
+  auto explicit_by_value_unused_requires = [i] (auto) requires requires { i; } 
{}; // expected-warning{{lambda capture 'i' is not required to be captured for 
this use}}

HerrCai0907 wrote:

That would be interesting. I will try to do it later in festival.

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


[clang-tools-extra] [clang-tidy] fix incorrect hint for InitListExpr in prefer-member-initializer (PR #81560)

2024-02-12 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 created 
https://github.com/llvm/llvm-project/pull/81560

Fixes: #77684.

>From 35dba54b1d6d158118c34d0f1bd8038b64c9bda4 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Tue, 13 Feb 2024 09:11:06 +0800
Subject: [PATCH] [clang-tidy] fix incorrect hint for InitListExpr in
 prefer-member-initializer

Fixes: #77684.
---
 .../PreferMemberInitializerCheck.cpp  |  5 
 clang-tools-extra/docs/ReleaseNotes.rst   |  3 ++-
 .../prefer-member-initializer.cpp | 23 +++
 3 files changed, 30 insertions(+), 1 deletion(-)

diff --git 
a/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
 
b/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
index de96c3dc4efef7..7ede900d7ba7b1 100644
--- 
a/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
+++ 
b/clang-tools-extra/clang-tidy/cppcoreguidelines/PreferMemberInitializerCheck.cpp
@@ -203,6 +203,7 @@ void PreferMemberInitializerCheck::check(
 SourceLocation InsertPos;
 SourceRange ReplaceRange;
 bool AddComma = false;
+bool AddBracket = false;
 bool InvalidFix = false;
 unsigned Index = Field->getFieldIndex();
 const CXXCtorInitializer *LastInListInit = nullptr;
@@ -216,6 +217,7 @@ void PreferMemberInitializerCheck::check(
 else {
   ReplaceRange = Init->getInit()->getSourceRange();
 }
+AddBracket = isa(Init->getInit());
 break;
   }
   if (Init->isMemberInitializer() &&
@@ -279,6 +281,9 @@ void PreferMemberInitializerCheck::check(
 if (HasInitAlready) {
   if (InsertPos.isValid())
 Diag << FixItHint::CreateInsertion(InsertPos, NewInit);
+  else if (AddBracket)
+Diag << FixItHint::CreateReplacement(ReplaceRange,
+ ("{" + NewInit + "}").str());
   else
 Diag << FixItHint::CreateReplacement(ReplaceRange, NewInit);
 } else {
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index ee68c8f49b3df2..f8bc85d78315bf 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -127,7 +127,8 @@ Changes in existing checks
   
`_,
   which was deprecated since :program:`clang-tidy` 17. This rule is now covered
   by :doc:`cppcoreguidelines-use-default-member-init
-  `.
+  ` and fixes
+  incorrect hints when using list-initialization.
 
 - Improved :doc:`google-build-namespaces
   ` check by replacing the local
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/prefer-member-initializer.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/prefer-member-initializer.cpp
index 8086caa2aa6f2c..63b4919608ff18 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/prefer-member-initializer.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/prefer-member-initializer.cpp
@@ -616,3 +616,26 @@ class Foo {
 #undef INVALID_HANDLE_VALUE
 #undef RGB
 }
+
+namespace GH77684 {
+struct S1 {
+// CHECK-MESSAGES: :[[@LINE+1]]:16: warning: 'M' should be initialized in a 
member initializer of the constructor 
[cppcoreguidelines-prefer-member-initializer]
+  S1() : M{} { M = 0; }
+// CHECK-FIXES: {{ S1.+M\{0\} }}
+  int M;
+};
+struct S2 {
+// CHECK-MESSAGES: :[[@LINE+1]]:17: warning: 'M' should be initialized in a 
member initializer of the constructor 
[cppcoreguidelines-prefer-member-initializer]
+  S2() : M{2} { M = 1; }
+// CHECK-FIXES: {{ S2.+M\{1\} }}
+  int M;
+};
+struct T { int a; int b; int c; };
+T v;
+struct S3 {
+// CHECK-MESSAGES: :[[@LINE+1]]:21: warning: 'M' should be initialized in a 
member initializer of the constructor 
[cppcoreguidelines-prefer-member-initializer]
+  S3() : M{1,2,3} { M = v; }
+// CHECK-FIXES: {{ S3.+M\{v\} }}
+  T M;
+};
+}

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


[clang-tools-extra] [clang-tidy] ignore local variable with [maybe_unused] attribute in bugprone-unused-local-non-trivial-variable (PR #81563)

2024-02-12 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 created 
https://github.com/llvm/llvm-project/pull/81563

Fixes: #81419.

>From fefe52614837d14858d056783dca8b08745de9d4 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Tue, 13 Feb 2024 09:47:52 +0800
Subject: [PATCH] [clang-tidy] ignore local variable with [maybe_unused]
 attribute in bugprone-unused-local-non-trivial-variable

Fixes: #81419.
---
 .../bugprone/UnusedLocalNonTrivialVariableCheck.cpp   | 1 +
 clang-tools-extra/docs/ReleaseNotes.rst   | 4 
 .../checks/bugprone/unused-local-non-trivial-variable.rst | 1 +
 .../checkers/bugprone/unused-local-non-trivial-variable.cpp   | 1 +
 4 files changed, 7 insertions(+)

diff --git 
a/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp
index 1b763d291082b6..37baae7a6f0c3a 100644
--- 
a/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp
+++ 
b/clang-tools-extra/clang-tidy/bugprone/UnusedLocalNonTrivialVariableCheck.cpp
@@ -60,6 +60,7 @@ void 
UnusedLocalNonTrivialVariableCheck::registerMatchers(MatchFinder *Finder) {
   varDecl(isLocalVarDecl(), unless(isReferenced()),
   unless(isExceptionVariable()), hasLocalStorage(), isDefinition(),
   unless(hasType(isReferenceType())), unless(hasType(isTrivial())),
+  unless(hasAttr(attr::Kind::Unused)),
   hasType(hasUnqualifiedDesugaredType(
   anyOf(recordType(hasDeclaration(namedDecl(
 matchesAnyListedName(IncludeTypes),
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index ee68c8f49b3df2..f2fba9aa1450d6 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -121,6 +121,10 @@ Changes in existing checks
   ` check by incorporating
   better support for ``const`` loop boundaries.
 
+- Improved :doc:`bugprone-unused-local-non-trivial-variable
+  ` check by
+  ignoring local variable with ``[maybe_unused]`` attribute.
+
 - Cleaned up :doc:`cppcoreguidelines-prefer-member-initializer
   `
   by removing enforcement of rule `C.48
diff --git 
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/unused-local-non-trivial-variable.rst
 
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/unused-local-non-trivial-variable.rst
index 7531f19f3ebc15..9f283de78fbdec 100644
--- 
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/unused-local-non-trivial-variable.rst
+++ 
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/unused-local-non-trivial-variable.rst
@@ -11,6 +11,7 @@ The following types of variables are excluded from this check:
 * exception variables in catch clauses
 * static or thread local
 * structured bindings
+* variables with ``[[maybe_unused]]`` attribute
 
 This check can be configured to warn on all non-trivial variables by setting
 `IncludeTypes` to `.*`, and excluding specific types using `ExcludeTypes`.
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-local-non-trivial-variable.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-local-non-trivial-variable.cpp
index 19f2344de4a650..3fdc24b94a6cb2 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-local-non-trivial-variable.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-local-non-trivial-variable.cpp
@@ -77,6 +77,7 @@ T qux(T Generic) {
 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: unused local variable 
'TemplateType' of type 'async::Future' 
[bugprone-unused-local-non-trivial-variable]
 a::Future AliasTemplateType;
 // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: unused local variable 
'AliasTemplateType' of type 'a::Future' (aka 'Future') 
[bugprone-unused-local-non-trivial-variable]
+[[maybe_unused]] async::Future MaybeUnused;
 return Generic;
 }
 

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


[clang-tools-extra] [clang-tidy] ignore local variable with [maybe_unused] attribute in bugprone-unused-local-non-trivial-variable (PR #81563)

2024-02-13 Thread Congcong Cai via cfe-commits

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


[clang-tools-extra] [clang-tidy] bugprone-assert-side-effect can detect side effect from non-const reference parameters (PR #84095)

2024-03-05 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 created 
https://github.com/llvm/llvm-project/pull/84095

Fixes: #84092

>From 160add4d95f34608a3d423482ad50fb45f6db522 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Wed, 6 Mar 2024 06:55:30 +0800
Subject: [PATCH 1/2] [clang-tidy][NFC]refactor AssertSideEffectCheck logic

---
 .../clang-tidy/bugprone/AssertSideEffectCheck.cpp | 11 ++-
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp
index 43bedd4f73ef44..25ca42bfd1bede 100644
--- a/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp
@@ -60,16 +60,17 @@ AST_MATCHER_P2(Expr, hasSideEffect, bool, 
CheckFunctionCalls,
   }
 
   if (const auto *CExpr = dyn_cast(E)) {
-bool Result = CheckFunctionCalls;
+if (!CheckFunctionCalls)
+  return false;
 if (const auto *FuncDecl = CExpr->getDirectCallee()) {
   if (FuncDecl->getDeclName().isIdentifier() &&
   IgnoredFunctionsMatcher.matches(*FuncDecl, Finder,
   Builder)) // exceptions come here
-Result = false;
-  else if (const auto *MethodDecl = dyn_cast(FuncDecl))
-Result &= !MethodDecl->isConst();
+return false;
+  if (const auto *MethodDecl = dyn_cast(FuncDecl))
+return !MethodDecl->isConst();
 }
-return Result;
+return true;
   }
 
   return isa(E) || isa(E) || isa(E);

>From ce40ee88c708c8dcce8e5b2b9a1d000465e80c24 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Wed, 6 Mar 2024 07:44:15 +0800
Subject: [PATCH 2/2] [clang-tidy] bugprone-assert-side-effect can detect side
 effect from non-const reference parameters

Fixes: #84092
---
 .../bugprone/AssertSideEffectCheck.cpp| 11 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  4 
 .../checkers/bugprone/assert-side-effect.cpp  | 24 +++
 3 files changed, 39 insertions(+)

diff --git a/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp
index 25ca42bfd1bede..2111047b9dff7a 100644
--- a/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp
@@ -13,6 +13,7 @@
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Lex/Lexer.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Casting.h"
@@ -67,6 +68,16 @@ AST_MATCHER_P2(Expr, hasSideEffect, bool, CheckFunctionCalls,
   IgnoredFunctionsMatcher.matches(*FuncDecl, Finder,
   Builder)) // exceptions come here
 return false;
+  for (size_t I = 0; I < FuncDecl->getNumParams(); I++) {
+const ParmVarDecl *P = FuncDecl->getParamDecl(I);
+const Expr *ArgExpr =
+I < CExpr->getNumArgs() ? CExpr->getArg(I) : nullptr;
+QualType PT = P->getType().getCanonicalType();
+if (PT->isReferenceType() &&
+!PT.getNonReferenceType().isConstQualified() && ArgExpr &&
+!ArgExpr->isXValue())
+  return true;
+  }
   if (const auto *MethodDecl = dyn_cast(FuncDecl))
 return !MethodDecl->isConst();
 }
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 0d2467210fc664..0d4d5ddb5c8763 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -122,6 +122,10 @@ New check aliases
 Changes in existing checks
 ^^
 
+- Improved :doc:`bugprone-assert-side-effect
+  ` check by detection side
+  effect from method call with non-const reference parameters.
+
 - Improved :doc:`bugprone-non-zero-enum-to-bool-conversion
   ` check by
   eliminating false positives resulting from direct usage of bitwise operators
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/assert-side-effect.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/assert-side-effect.cpp
index c11638aa823aae..5cdc1afb3d9099 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/assert-side-effect.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/assert-side-effect.cpp
@@ -108,3 +108,27 @@ int main() {
 
   return 0;
 }
+
+namespace parameter_anaylysis {
+
+struct S {
+  bool value(int) const;
+  bool leftValueRef(int &) const;
+  bool constRef(int const &) const;
+  bool rightValueRef(int &&) const;
+};
+
+void foo() {
+  S s{};
+  int i = 0;
+  assert(s.value(0));
+  assert(s.value(i));
+  assert(s.leftValueRef(i));
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: side effect in assert() 
condition discarded in release builds
+  assert(s.constRef(0));
+  assert(s.constRef(i));
+  as

[clang-tools-extra] [clang-tidy] bugprone-assert-side-effect can detect side effect from non-const reference parameters (PR #84095)

2024-03-05 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/84095

>From 160add4d95f34608a3d423482ad50fb45f6db522 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Wed, 6 Mar 2024 06:55:30 +0800
Subject: [PATCH 1/3] [clang-tidy][NFC]refactor AssertSideEffectCheck logic

---
 .../clang-tidy/bugprone/AssertSideEffectCheck.cpp | 11 ++-
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp
index 43bedd4f73ef44..25ca42bfd1bede 100644
--- a/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp
@@ -60,16 +60,17 @@ AST_MATCHER_P2(Expr, hasSideEffect, bool, 
CheckFunctionCalls,
   }
 
   if (const auto *CExpr = dyn_cast(E)) {
-bool Result = CheckFunctionCalls;
+if (!CheckFunctionCalls)
+  return false;
 if (const auto *FuncDecl = CExpr->getDirectCallee()) {
   if (FuncDecl->getDeclName().isIdentifier() &&
   IgnoredFunctionsMatcher.matches(*FuncDecl, Finder,
   Builder)) // exceptions come here
-Result = false;
-  else if (const auto *MethodDecl = dyn_cast(FuncDecl))
-Result &= !MethodDecl->isConst();
+return false;
+  if (const auto *MethodDecl = dyn_cast(FuncDecl))
+return !MethodDecl->isConst();
 }
-return Result;
+return true;
   }
 
   return isa(E) || isa(E) || isa(E);

>From ce40ee88c708c8dcce8e5b2b9a1d000465e80c24 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Wed, 6 Mar 2024 07:44:15 +0800
Subject: [PATCH 2/3] [clang-tidy] bugprone-assert-side-effect can detect side
 effect from non-const reference parameters

Fixes: #84092
---
 .../bugprone/AssertSideEffectCheck.cpp| 11 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  4 
 .../checkers/bugprone/assert-side-effect.cpp  | 24 +++
 3 files changed, 39 insertions(+)

diff --git a/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp
index 25ca42bfd1bede..2111047b9dff7a 100644
--- a/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp
@@ -13,6 +13,7 @@
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Lex/Lexer.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Casting.h"
@@ -67,6 +68,16 @@ AST_MATCHER_P2(Expr, hasSideEffect, bool, CheckFunctionCalls,
   IgnoredFunctionsMatcher.matches(*FuncDecl, Finder,
   Builder)) // exceptions come here
 return false;
+  for (size_t I = 0; I < FuncDecl->getNumParams(); I++) {
+const ParmVarDecl *P = FuncDecl->getParamDecl(I);
+const Expr *ArgExpr =
+I < CExpr->getNumArgs() ? CExpr->getArg(I) : nullptr;
+QualType PT = P->getType().getCanonicalType();
+if (PT->isReferenceType() &&
+!PT.getNonReferenceType().isConstQualified() && ArgExpr &&
+!ArgExpr->isXValue())
+  return true;
+  }
   if (const auto *MethodDecl = dyn_cast(FuncDecl))
 return !MethodDecl->isConst();
 }
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 0d2467210fc664..0d4d5ddb5c8763 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -122,6 +122,10 @@ New check aliases
 Changes in existing checks
 ^^
 
+- Improved :doc:`bugprone-assert-side-effect
+  ` check by detection side
+  effect from method call with non-const reference parameters.
+
 - Improved :doc:`bugprone-non-zero-enum-to-bool-conversion
   ` check by
   eliminating false positives resulting from direct usage of bitwise operators
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/assert-side-effect.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/assert-side-effect.cpp
index c11638aa823aae..5cdc1afb3d9099 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/assert-side-effect.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/assert-side-effect.cpp
@@ -108,3 +108,27 @@ int main() {
 
   return 0;
 }
+
+namespace parameter_anaylysis {
+
+struct S {
+  bool value(int) const;
+  bool leftValueRef(int &) const;
+  bool constRef(int const &) const;
+  bool rightValueRef(int &&) const;
+};
+
+void foo() {
+  S s{};
+  int i = 0;
+  assert(s.value(0));
+  assert(s.value(i));
+  assert(s.leftValueRef(i));
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: side effect in assert() 
condition discarded in release builds
+  assert(s.constRef(0));
+  assert(s.constRef(i));
+  assert(s.rightVal

[clang-tools-extra] [clang-tidy] bugprone-assert-side-effect can detect side effect from non-const reference parameters (PR #84095)

2024-03-06 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/84095

>From 160add4d95f34608a3d423482ad50fb45f6db522 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Wed, 6 Mar 2024 06:55:30 +0800
Subject: [PATCH 1/4] [clang-tidy][NFC]refactor AssertSideEffectCheck logic

---
 .../clang-tidy/bugprone/AssertSideEffectCheck.cpp | 11 ++-
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp
index 43bedd4f73ef44..25ca42bfd1bede 100644
--- a/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp
@@ -60,16 +60,17 @@ AST_MATCHER_P2(Expr, hasSideEffect, bool, 
CheckFunctionCalls,
   }
 
   if (const auto *CExpr = dyn_cast(E)) {
-bool Result = CheckFunctionCalls;
+if (!CheckFunctionCalls)
+  return false;
 if (const auto *FuncDecl = CExpr->getDirectCallee()) {
   if (FuncDecl->getDeclName().isIdentifier() &&
   IgnoredFunctionsMatcher.matches(*FuncDecl, Finder,
   Builder)) // exceptions come here
-Result = false;
-  else if (const auto *MethodDecl = dyn_cast(FuncDecl))
-Result &= !MethodDecl->isConst();
+return false;
+  if (const auto *MethodDecl = dyn_cast(FuncDecl))
+return !MethodDecl->isConst();
 }
-return Result;
+return true;
   }
 
   return isa(E) || isa(E) || isa(E);

>From ce40ee88c708c8dcce8e5b2b9a1d000465e80c24 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Wed, 6 Mar 2024 07:44:15 +0800
Subject: [PATCH 2/4] [clang-tidy] bugprone-assert-side-effect can detect side
 effect from non-const reference parameters

Fixes: #84092
---
 .../bugprone/AssertSideEffectCheck.cpp| 11 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  4 
 .../checkers/bugprone/assert-side-effect.cpp  | 24 +++
 3 files changed, 39 insertions(+)

diff --git a/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp
index 25ca42bfd1bede..2111047b9dff7a 100644
--- a/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp
@@ -13,6 +13,7 @@
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Lex/Lexer.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Casting.h"
@@ -67,6 +68,16 @@ AST_MATCHER_P2(Expr, hasSideEffect, bool, CheckFunctionCalls,
   IgnoredFunctionsMatcher.matches(*FuncDecl, Finder,
   Builder)) // exceptions come here
 return false;
+  for (size_t I = 0; I < FuncDecl->getNumParams(); I++) {
+const ParmVarDecl *P = FuncDecl->getParamDecl(I);
+const Expr *ArgExpr =
+I < CExpr->getNumArgs() ? CExpr->getArg(I) : nullptr;
+QualType PT = P->getType().getCanonicalType();
+if (PT->isReferenceType() &&
+!PT.getNonReferenceType().isConstQualified() && ArgExpr &&
+!ArgExpr->isXValue())
+  return true;
+  }
   if (const auto *MethodDecl = dyn_cast(FuncDecl))
 return !MethodDecl->isConst();
 }
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 0d2467210fc664..0d4d5ddb5c8763 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -122,6 +122,10 @@ New check aliases
 Changes in existing checks
 ^^
 
+- Improved :doc:`bugprone-assert-side-effect
+  ` check by detection side
+  effect from method call with non-const reference parameters.
+
 - Improved :doc:`bugprone-non-zero-enum-to-bool-conversion
   ` check by
   eliminating false positives resulting from direct usage of bitwise operators
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/assert-side-effect.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/assert-side-effect.cpp
index c11638aa823aae..5cdc1afb3d9099 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/assert-side-effect.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/assert-side-effect.cpp
@@ -108,3 +108,27 @@ int main() {
 
   return 0;
 }
+
+namespace parameter_anaylysis {
+
+struct S {
+  bool value(int) const;
+  bool leftValueRef(int &) const;
+  bool constRef(int const &) const;
+  bool rightValueRef(int &&) const;
+};
+
+void foo() {
+  S s{};
+  int i = 0;
+  assert(s.value(0));
+  assert(s.value(i));
+  assert(s.leftValueRef(i));
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: side effect in assert() 
condition discarded in release builds
+  assert(s.constRef(0));
+  assert(s.constRef(i));
+  assert(s.rightVal

[clang-tools-extra] [clang-tidy] bugprone-assert-side-effect can detect side effect from non-const reference parameters (PR #84095)

2024-03-06 Thread Congcong Cai via cfe-commits

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


[clang-tools-extra] [clang-tidy][missing-std-forward]report diagnotics for using forward in lambda body (PR #83930)

2024-03-07 Thread Congcong Cai via cfe-commits

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


[clang-tools-extra] [clang-tidy]avoid bugprone-unused-return-value false negative for function with the same prefix as the default argument (PR #84333)

2024-03-07 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 created 
https://github.com/llvm/llvm-project/pull/84333

String without `$` end causes incorrectly match results, For example 
`std::unique` can match `std::unique_ptr::unique_ptr`.
It causes false negative.
Fixes: #84314.


>From f96e0c7a37b0ef60e4a208725a40d0585c5743f9 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Thu, 7 Mar 2024 23:28:40 +0800
Subject: [PATCH] [clang-tidy]avoid bugprone-unused-return-value false negative
 for function with the same prefix as the default argument

string without `$` end will cause incorrectly match results.
Fixes: #84314.
---
 .../bugprone/UnusedReturnValueCheck.cpp   | 184 +-
 clang-tools-extra/docs/ReleaseNotes.rst   |   4 +-
 .../checkers/bugprone/unused-return-value.cpp |  14 ++
 3 files changed, 109 insertions(+), 93 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
index b4bf85c912c3ca..1252b2f23805a1 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
@@ -34,102 +34,102 @@ 
UnusedReturnValueCheck::UnusedReturnValueCheck(llvm::StringRef Name,
ClangTidyContext *Context)
 : ClangTidyCheck(Name, Context),
   CheckedFunctions(utils::options::parseStringList(
-  Options.get("CheckedFunctions", "::std::async;"
-  "::std::launder;"
-  "::std::remove;"
-  "::std::remove_if;"
-  "::std::unique;"
-  "::std::unique_ptr::release;"
-  "::std::basic_string::empty;"
-  "::std::vector::empty;"
-  "::std::back_inserter;"
-  "::std::distance;"
-  "::std::find;"
-  "::std::find_if;"
-  "::std::inserter;"
-  "::std::lower_bound;"
-  "::std::make_pair;"
-  "::std::map::count;"
-  "::std::map::find;"
-  "::std::map::lower_bound;"
-  "::std::multimap::equal_range;"
-  "::std::multimap::upper_bound;"
-  "::std::set::count;"
-  "::std::set::find;"
-  "::std::setfill;"
-  "::std::setprecision;"
-  "::std::setw;"
-  "::std::upper_bound;"
-  "::std::vector::at;"
+  Options.get("CheckedFunctions", "::std::async$;"
+  "::std::launder$;"
+  "::std::remove$;"
+  "::std::remove_if$;"
+  "::std::unique$;"
+  "::std::unique_ptr::release$;"
+  "::std::basic_string::empty$;"
+  "::std::vector::empty$;"
+  "::std::back_inserter$;"
+  "::std::distance$;"
+  "::std::find$;"
+  "::std::find_if$;"
+  "::std::inserter$;"
+  "::std::lower_bound$;"
+  "::std::make_pair$;"
+  "::std::map::count$;"
+  "::std::map::find$;"
+  "::std::map::lower_bound$;"
+  "::std::multimap::equal_range$;"
+  "::std::multimap::upper_bound$;"
+  "::std::set::count$;"
+  "::std::set::find$;"
+  "::std::setfill$;"
+  "::std::setprecision$;"
+  "::std::setw$;"
+  "::std::upper_bound$;"
+  "::std::vector::at$;"
   // C st

[clang-tools-extra] [clang-tidy]avoid bugprone-unused-return-value false positive for function with the same prefix as the default argument (PR #84333)

2024-03-07 Thread Congcong Cai via cfe-commits

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


[clang-tools-extra] [clang-tidy]avoid bugprone-unused-return-value false positive for function with the same prefix as the default argument (PR #84333)

2024-03-07 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/84333

>From aaa9ea1db21b5de5d8be454ce1b1d05b219cccfc Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Thu, 7 Mar 2024 23:28:40 +0800
Subject: [PATCH] [clang-tidy]avoid bugprone-unused-return-value false negative
 for function with the same prefix as the default argument

string without `$` end will cause incorrectly match results.
Fixes: #84314.
---
 .../bugprone/UnusedReturnValueCheck.cpp   | 184 +-
 clang-tools-extra/docs/ReleaseNotes.rst   |   4 +-
 .../checkers/bugprone/unused-return-value.cpp |  14 ++
 3 files changed, 109 insertions(+), 93 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
index b4bf85c912c3ca..1252b2f23805a1 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
@@ -34,102 +34,102 @@ 
UnusedReturnValueCheck::UnusedReturnValueCheck(llvm::StringRef Name,
ClangTidyContext *Context)
 : ClangTidyCheck(Name, Context),
   CheckedFunctions(utils::options::parseStringList(
-  Options.get("CheckedFunctions", "::std::async;"
-  "::std::launder;"
-  "::std::remove;"
-  "::std::remove_if;"
-  "::std::unique;"
-  "::std::unique_ptr::release;"
-  "::std::basic_string::empty;"
-  "::std::vector::empty;"
-  "::std::back_inserter;"
-  "::std::distance;"
-  "::std::find;"
-  "::std::find_if;"
-  "::std::inserter;"
-  "::std::lower_bound;"
-  "::std::make_pair;"
-  "::std::map::count;"
-  "::std::map::find;"
-  "::std::map::lower_bound;"
-  "::std::multimap::equal_range;"
-  "::std::multimap::upper_bound;"
-  "::std::set::count;"
-  "::std::set::find;"
-  "::std::setfill;"
-  "::std::setprecision;"
-  "::std::setw;"
-  "::std::upper_bound;"
-  "::std::vector::at;"
+  Options.get("CheckedFunctions", "::std::async$;"
+  "::std::launder$;"
+  "::std::remove$;"
+  "::std::remove_if$;"
+  "::std::unique$;"
+  "::std::unique_ptr::release$;"
+  "::std::basic_string::empty$;"
+  "::std::vector::empty$;"
+  "::std::back_inserter$;"
+  "::std::distance$;"
+  "::std::find$;"
+  "::std::find_if$;"
+  "::std::inserter$;"
+  "::std::lower_bound$;"
+  "::std::make_pair$;"
+  "::std::map::count$;"
+  "::std::map::find$;"
+  "::std::map::lower_bound$;"
+  "::std::multimap::equal_range$;"
+  "::std::multimap::upper_bound$;"
+  "::std::set::count$;"
+  "::std::set::find$;"
+  "::std::setfill$;"
+  "::std::setprecision$;"
+  "::std::setw$;"
+  "::std::upper_bound$;"
+  "::std::vector::at$;"
   // C standard library
-  "::bsearch;"
-  "::ferror;"
- 

[clang-tools-extra] [clang-tidy] Add support for lambdas in cppcoreguidelines-owning-memory (PR #77246)

2024-03-07 Thread Congcong Cai via cfe-commits

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

LGTM

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


[clang-tools-extra] [clang-tidy]avoid bugprone-unused-return-value false positive for function with the same prefix as the default argument (PR #84333)

2024-03-07 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/84333

>From aaa9ea1db21b5de5d8be454ce1b1d05b219cccfc Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Thu, 7 Mar 2024 23:28:40 +0800
Subject: [PATCH 1/2] [clang-tidy]avoid bugprone-unused-return-value false
 negative for function with the same prefix as the default argument

string without `$` end will cause incorrectly match results.
Fixes: #84314.
---
 .../bugprone/UnusedReturnValueCheck.cpp   | 184 +-
 clang-tools-extra/docs/ReleaseNotes.rst   |   4 +-
 .../checkers/bugprone/unused-return-value.cpp |  14 ++
 3 files changed, 109 insertions(+), 93 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
index b4bf85c912c3ca..1252b2f23805a1 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
@@ -34,102 +34,102 @@ 
UnusedReturnValueCheck::UnusedReturnValueCheck(llvm::StringRef Name,
ClangTidyContext *Context)
 : ClangTidyCheck(Name, Context),
   CheckedFunctions(utils::options::parseStringList(
-  Options.get("CheckedFunctions", "::std::async;"
-  "::std::launder;"
-  "::std::remove;"
-  "::std::remove_if;"
-  "::std::unique;"
-  "::std::unique_ptr::release;"
-  "::std::basic_string::empty;"
-  "::std::vector::empty;"
-  "::std::back_inserter;"
-  "::std::distance;"
-  "::std::find;"
-  "::std::find_if;"
-  "::std::inserter;"
-  "::std::lower_bound;"
-  "::std::make_pair;"
-  "::std::map::count;"
-  "::std::map::find;"
-  "::std::map::lower_bound;"
-  "::std::multimap::equal_range;"
-  "::std::multimap::upper_bound;"
-  "::std::set::count;"
-  "::std::set::find;"
-  "::std::setfill;"
-  "::std::setprecision;"
-  "::std::setw;"
-  "::std::upper_bound;"
-  "::std::vector::at;"
+  Options.get("CheckedFunctions", "::std::async$;"
+  "::std::launder$;"
+  "::std::remove$;"
+  "::std::remove_if$;"
+  "::std::unique$;"
+  "::std::unique_ptr::release$;"
+  "::std::basic_string::empty$;"
+  "::std::vector::empty$;"
+  "::std::back_inserter$;"
+  "::std::distance$;"
+  "::std::find$;"
+  "::std::find_if$;"
+  "::std::inserter$;"
+  "::std::lower_bound$;"
+  "::std::make_pair$;"
+  "::std::map::count$;"
+  "::std::map::find$;"
+  "::std::map::lower_bound$;"
+  "::std::multimap::equal_range$;"
+  "::std::multimap::upper_bound$;"
+  "::std::set::count$;"
+  "::std::set::find$;"
+  "::std::setfill$;"
+  "::std::setprecision$;"
+  "::std::setw$;"
+  "::std::upper_bound$;"
+  "::std::vector::at$;"
   // C standard library
-  "::bsearch;"
-  "::ferror;"
- 

[clang-tools-extra] [clang-tidy]avoid bugprone-unused-return-value false positive for function with the same prefix as the default argument (PR #84333)

2024-03-08 Thread Congcong Cai via cfe-commits

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


[clang-tools-extra] [clang-tidy]avoid bugprone-unused-return-value false positive for assignment operator overloading (PR #84489)

2024-03-08 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 created 
https://github.com/llvm/llvm-project/pull/84489

Fixes: #84480
We assuem assignemnt at most of time, operator overloading means the value is 
assigned to the other variable, then clang-tidy should suppress warning even if 
this operator overloading match the regex.


>From b87bb6b65099228ff7ded13da059215d59d794d5 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Fri, 8 Mar 2024 22:15:20 +0800
Subject: [PATCH] [clang-tidy]avoid bugprone-unused-return-value false positive
 for assignment operator overloading

Fixes: #84480
We assuem assignemnt at most of time, operator overloading means the value is 
assigned to the other variable, then clang-tidy should suppress warning even if 
this operator overloading match the regex.
---
 .../bugprone/UnusedReturnValueCheck.cpp   | 24 +++
 clang-tools-extra/docs/ReleaseNotes.rst   |  4 ++--
 2 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
index 1252b2f23805a1..2167d381c42b03 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
@@ -11,6 +11,7 @@
 #include "../utils/OptionsUtils.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
 
 using namespace clang::ast_matchers;
 using namespace clang::ast_matchers::internal;
@@ -157,16 +158,19 @@ void 
UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
 }
 
 void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) {
-  auto MatchedDirectCallExpr =
-  expr(callExpr(callee(functionDecl(
-// Don't match void overloads of checked functions.
-unless(returns(voidType())),
-
anyOf(isInstantiatedFrom(matchers::matchesAnyListedName(
-  CheckedFunctions)),
-  returns(hasCanonicalType(hasDeclaration(
-  namedDecl(matchers::matchesAnyListedName(
-  CheckedReturnTypes)
-   .bind("match"));
+  auto MatchedDirectCallExpr = expr(
+  callExpr(callee(functionDecl(
+   // Don't match void overloads of checked functions.
+   unless(returns(voidType())),
+   // Don't match copy or move assignment operator.
+   unless(cxxMethodDecl(anyOf(isCopyAssignmentOperator(),
+  isMoveAssignmentOperator(,
+   anyOf(isInstantiatedFrom(
+ matchers::matchesAnyListedName(CheckedFunctions)),
+ returns(hasCanonicalType(hasDeclaration(
+ namedDecl(matchers::matchesAnyListedName(
+ CheckedReturnTypes)
+  .bind("match"));
 
   auto CheckCastToVoid =
   AllowCastToVoid ? castExpr(unless(hasCastKind(CK_ToVoid))) : castExpr();
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index b5f025ce467a15..c7121fe07e0ad3 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -152,9 +152,9 @@ Changes in existing checks
 
 - Improved :doc:`bugprone-unused-return-value
   ` check by updating the
-  parameter `CheckedFunctions` to support regexp and avoiding false postive for
+  parameter `CheckedFunctions` to support regexp, avoiding false positive for
   function with the same prefix as the default argument, e.g. 
``std::unique_ptr``
-  and ``std::unique``.
+  and ``std::unique``, avoiding false positive for assignment operator 
overloading.
 
 - Improved :doc:`bugprone-use-after-move
   ` check to also handle

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


[clang-tools-extra] [clang-tidy]avoid bugprone-unused-return-value false positive for assignment operator overloading (PR #84489)

2024-03-08 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/84489

>From 265db5ee772772bef4099cc97b69995cfa67b3f2 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Fri, 8 Mar 2024 22:15:20 +0800
Subject: [PATCH] [clang-tidy]avoid bugprone-unused-return-value false positive
 for assignment operator overloading

Fixes: #84480
We assuem assignemnt at most of time, operator overloading means the value is 
assigned to the other variable, then clang-tidy should suppress warning even if 
this operator overloading match the regex.
---
 .../bugprone/UnusedReturnValueCheck.cpp   | 24 --
 clang-tools-extra/docs/ReleaseNotes.rst   |  4 +--
 .../unused-return-value-avoid-assignment.cpp  | 31 +++
 3 files changed, 47 insertions(+), 12 deletions(-)
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp

diff --git a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
index 1252b2f23805a1..2167d381c42b03 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
@@ -11,6 +11,7 @@
 #include "../utils/OptionsUtils.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
 
 using namespace clang::ast_matchers;
 using namespace clang::ast_matchers::internal;
@@ -157,16 +158,19 @@ void 
UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
 }
 
 void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) {
-  auto MatchedDirectCallExpr =
-  expr(callExpr(callee(functionDecl(
-// Don't match void overloads of checked functions.
-unless(returns(voidType())),
-
anyOf(isInstantiatedFrom(matchers::matchesAnyListedName(
-  CheckedFunctions)),
-  returns(hasCanonicalType(hasDeclaration(
-  namedDecl(matchers::matchesAnyListedName(
-  CheckedReturnTypes)
-   .bind("match"));
+  auto MatchedDirectCallExpr = expr(
+  callExpr(callee(functionDecl(
+   // Don't match void overloads of checked functions.
+   unless(returns(voidType())),
+   // Don't match copy or move assignment operator.
+   unless(cxxMethodDecl(anyOf(isCopyAssignmentOperator(),
+  isMoveAssignmentOperator(,
+   anyOf(isInstantiatedFrom(
+ matchers::matchesAnyListedName(CheckedFunctions)),
+ returns(hasCanonicalType(hasDeclaration(
+ namedDecl(matchers::matchesAnyListedName(
+ CheckedReturnTypes)
+  .bind("match"));
 
   auto CheckCastToVoid =
   AllowCastToVoid ? castExpr(unless(hasCastKind(CK_ToVoid))) : castExpr();
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index b5f025ce467a15..c7121fe07e0ad3 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -152,9 +152,9 @@ Changes in existing checks
 
 - Improved :doc:`bugprone-unused-return-value
   ` check by updating the
-  parameter `CheckedFunctions` to support regexp and avoiding false postive for
+  parameter `CheckedFunctions` to support regexp, avoiding false positive for
   function with the same prefix as the default argument, e.g. 
``std::unique_ptr``
-  and ``std::unique``.
+  and ``std::unique``, avoiding false positive for assignment operator 
overloading.
 
 - Improved :doc:`bugprone-use-after-move
   ` check to also handle
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
new file mode 100644
index 00..8bd3c30e71b51a
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
@@ -0,0 +1,31 @@
+// RUN: %check_clang_tidy %s bugprone-unused-return-value %t \
+// RUN: -config='{CheckOptions: \
+// RUN:  {bugprone-unused-return-value.CheckedFunctions: "::*"}}' \
+// RUN: --
+
+struct S {
+  S(){};
+  S(S const &);
+  S(S &&);
+  S &operator=(S const &);
+  S &operator=(S &&);
+};
+
+S returnValue();
+S const &returnRef();
+
+void bar() {
+  returnValue();
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this 
function should not be disregarded; neglecting it may lead to errors
+
+  S a{};
+  a = returnValue();
+  // CHECK-NOT: [[@LINE-1]]:3: warning
+  a.operator=(returnValue());
+  // CHECK-NOT: [[@LINE-1]]:3: warning
+
+ 

[clang-tools-extra] [clang-tidy]avoid bugprone-unused-return-value false positive for assignment operator overloading (PR #84489)

2024-03-08 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/84489

>From 265db5ee772772bef4099cc97b69995cfa67b3f2 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Fri, 8 Mar 2024 22:15:20 +0800
Subject: [PATCH 1/2] [clang-tidy]avoid bugprone-unused-return-value false
 positive for assignment operator overloading

Fixes: #84480
We assuem assignemnt at most of time, operator overloading means the value is 
assigned to the other variable, then clang-tidy should suppress warning even if 
this operator overloading match the regex.
---
 .../bugprone/UnusedReturnValueCheck.cpp   | 24 --
 clang-tools-extra/docs/ReleaseNotes.rst   |  4 +--
 .../unused-return-value-avoid-assignment.cpp  | 31 +++
 3 files changed, 47 insertions(+), 12 deletions(-)
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp

diff --git a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
index 1252b2f23805a1..2167d381c42b03 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
@@ -11,6 +11,7 @@
 #include "../utils/OptionsUtils.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
 
 using namespace clang::ast_matchers;
 using namespace clang::ast_matchers::internal;
@@ -157,16 +158,19 @@ void 
UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
 }
 
 void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) {
-  auto MatchedDirectCallExpr =
-  expr(callExpr(callee(functionDecl(
-// Don't match void overloads of checked functions.
-unless(returns(voidType())),
-
anyOf(isInstantiatedFrom(matchers::matchesAnyListedName(
-  CheckedFunctions)),
-  returns(hasCanonicalType(hasDeclaration(
-  namedDecl(matchers::matchesAnyListedName(
-  CheckedReturnTypes)
-   .bind("match"));
+  auto MatchedDirectCallExpr = expr(
+  callExpr(callee(functionDecl(
+   // Don't match void overloads of checked functions.
+   unless(returns(voidType())),
+   // Don't match copy or move assignment operator.
+   unless(cxxMethodDecl(anyOf(isCopyAssignmentOperator(),
+  isMoveAssignmentOperator(,
+   anyOf(isInstantiatedFrom(
+ matchers::matchesAnyListedName(CheckedFunctions)),
+ returns(hasCanonicalType(hasDeclaration(
+ namedDecl(matchers::matchesAnyListedName(
+ CheckedReturnTypes)
+  .bind("match"));
 
   auto CheckCastToVoid =
   AllowCastToVoid ? castExpr(unless(hasCastKind(CK_ToVoid))) : castExpr();
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index b5f025ce467a15..c7121fe07e0ad3 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -152,9 +152,9 @@ Changes in existing checks
 
 - Improved :doc:`bugprone-unused-return-value
   ` check by updating the
-  parameter `CheckedFunctions` to support regexp and avoiding false postive for
+  parameter `CheckedFunctions` to support regexp, avoiding false positive for
   function with the same prefix as the default argument, e.g. 
``std::unique_ptr``
-  and ``std::unique``.
+  and ``std::unique``, avoiding false positive for assignment operator 
overloading.
 
 - Improved :doc:`bugprone-use-after-move
   ` check to also handle
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
new file mode 100644
index 00..8bd3c30e71b51a
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
@@ -0,0 +1,31 @@
+// RUN: %check_clang_tidy %s bugprone-unused-return-value %t \
+// RUN: -config='{CheckOptions: \
+// RUN:  {bugprone-unused-return-value.CheckedFunctions: "::*"}}' \
+// RUN: --
+
+struct S {
+  S(){};
+  S(S const &);
+  S(S &&);
+  S &operator=(S const &);
+  S &operator=(S &&);
+};
+
+S returnValue();
+S const &returnRef();
+
+void bar() {
+  returnValue();
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this 
function should not be disregarded; neglecting it may lead to errors
+
+  S a{};
+  a = returnValue();
+  // CHECK-NOT: [[@LINE-1]]:3: warning
+  a.operator=(returnValue());
+  // CHECK-NOT: [[@LINE-1]]:3: warning

[clang-tools-extra] [clang-tidy]avoid bugprone-unused-return-value false positive for assignment operator overloading (PR #84489)

2024-03-08 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/84489

>From 265db5ee772772bef4099cc97b69995cfa67b3f2 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Fri, 8 Mar 2024 22:15:20 +0800
Subject: [PATCH 1/3] [clang-tidy]avoid bugprone-unused-return-value false
 positive for assignment operator overloading

Fixes: #84480
We assuem assignemnt at most of time, operator overloading means the value is 
assigned to the other variable, then clang-tidy should suppress warning even if 
this operator overloading match the regex.
---
 .../bugprone/UnusedReturnValueCheck.cpp   | 24 --
 clang-tools-extra/docs/ReleaseNotes.rst   |  4 +--
 .../unused-return-value-avoid-assignment.cpp  | 31 +++
 3 files changed, 47 insertions(+), 12 deletions(-)
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp

diff --git a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
index 1252b2f23805a1..2167d381c42b03 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
@@ -11,6 +11,7 @@
 #include "../utils/OptionsUtils.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
 
 using namespace clang::ast_matchers;
 using namespace clang::ast_matchers::internal;
@@ -157,16 +158,19 @@ void 
UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
 }
 
 void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) {
-  auto MatchedDirectCallExpr =
-  expr(callExpr(callee(functionDecl(
-// Don't match void overloads of checked functions.
-unless(returns(voidType())),
-
anyOf(isInstantiatedFrom(matchers::matchesAnyListedName(
-  CheckedFunctions)),
-  returns(hasCanonicalType(hasDeclaration(
-  namedDecl(matchers::matchesAnyListedName(
-  CheckedReturnTypes)
-   .bind("match"));
+  auto MatchedDirectCallExpr = expr(
+  callExpr(callee(functionDecl(
+   // Don't match void overloads of checked functions.
+   unless(returns(voidType())),
+   // Don't match copy or move assignment operator.
+   unless(cxxMethodDecl(anyOf(isCopyAssignmentOperator(),
+  isMoveAssignmentOperator(,
+   anyOf(isInstantiatedFrom(
+ matchers::matchesAnyListedName(CheckedFunctions)),
+ returns(hasCanonicalType(hasDeclaration(
+ namedDecl(matchers::matchesAnyListedName(
+ CheckedReturnTypes)
+  .bind("match"));
 
   auto CheckCastToVoid =
   AllowCastToVoid ? castExpr(unless(hasCastKind(CK_ToVoid))) : castExpr();
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index b5f025ce467a15..c7121fe07e0ad3 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -152,9 +152,9 @@ Changes in existing checks
 
 - Improved :doc:`bugprone-unused-return-value
   ` check by updating the
-  parameter `CheckedFunctions` to support regexp and avoiding false postive for
+  parameter `CheckedFunctions` to support regexp, avoiding false positive for
   function with the same prefix as the default argument, e.g. 
``std::unique_ptr``
-  and ``std::unique``.
+  and ``std::unique``, avoiding false positive for assignment operator 
overloading.
 
 - Improved :doc:`bugprone-use-after-move
   ` check to also handle
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
new file mode 100644
index 00..8bd3c30e71b51a
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
@@ -0,0 +1,31 @@
+// RUN: %check_clang_tidy %s bugprone-unused-return-value %t \
+// RUN: -config='{CheckOptions: \
+// RUN:  {bugprone-unused-return-value.CheckedFunctions: "::*"}}' \
+// RUN: --
+
+struct S {
+  S(){};
+  S(S const &);
+  S(S &&);
+  S &operator=(S const &);
+  S &operator=(S &&);
+};
+
+S returnValue();
+S const &returnRef();
+
+void bar() {
+  returnValue();
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: the value returned by this 
function should not be disregarded; neglecting it may lead to errors
+
+  S a{};
+  a = returnValue();
+  // CHECK-NOT: [[@LINE-1]]:3: warning
+  a.operator=(returnValue());
+  // CHECK-NOT: [[@LINE-1]]:3: warning

[clang-tools-extra] [clang-tidy]avoid bugprone-unused-return-value false positive for assignment operator overloading (PR #84489)

2024-03-09 Thread Congcong Cai via cfe-commits

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


[clang-tools-extra] [clang-tidy]bugprone-unused-return-value ignore `++` and `--` operator overloading (PR #84922)

2024-03-12 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 created 
https://github.com/llvm/llvm-project/pull/84922

Fixes: #84705


>From d760b280a79be973642a0549db0e5a8838c397fd Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Tue, 12 Mar 2024 22:33:18 +0800
Subject: [PATCH] [clang-tidy]bugprone-unused-return-value ignore `++` and `--`
 operator overloading

Fixes: #84705
---
 .../bugprone/UnusedReturnValueCheck.cpp   | 28 +--
 .../unused-return-value-avoid-assignment.cpp  |  8 ++
 2 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
index 243fe47c2036b6..83b332fba1e2da 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
@@ -165,20 +165,20 @@ void 
UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
 
 void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) {
   auto MatchedDirectCallExpr = expr(
-  callExpr(
-  callee(functionDecl(
-  // Don't match void overloads of checked functions.
-  unless(returns(voidType())),
-  // Don't match copy or move assignment operator.
-  unless(cxxMethodDecl(isOperatorOverloading(
-  {OO_Equal, OO_PlusEqual, OO_MinusEqual, OO_StarEqual,
-   OO_SlashEqual, OO_PercentEqual, OO_CaretEqual, OO_AmpEqual,
-   OO_PipeEqual, OO_LessLessEqual, OO_GreaterGreaterEqual}))),
-  anyOf(
-  isInstantiatedFrom(
-  matchers::matchesAnyListedName(CheckedFunctions)),
-  returns(hasCanonicalType(hasDeclaration(namedDecl(
-  
matchers::matchesAnyListedName(CheckedReturnTypes)
+  callExpr(callee(functionDecl(
+   // Don't match void overloads of checked functions.
+   unless(returns(voidType())),
+   // Don't match copy or move assignment operator.
+   unless(cxxMethodDecl(isOperatorOverloading(
+   {OO_Equal, OO_PlusEqual, OO_MinusEqual, OO_StarEqual,
+OO_SlashEqual, OO_PercentEqual, OO_CaretEqual,
+OO_AmpEqual, OO_PipeEqual, OO_LessLessEqual,
+OO_GreaterGreaterEqual, OO_PlusPlus, OO_MinusMinus}))),
+   anyOf(isInstantiatedFrom(
+ matchers::matchesAnyListedName(CheckedFunctions)),
+ returns(hasCanonicalType(hasDeclaration(
+ namedDecl(matchers::matchesAnyListedName(
+ CheckedReturnTypes)
   .bind("match"));
 
   auto CheckCastToVoid =
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
index b4a41004adf894..5809da9ec27b38 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
@@ -10,6 +10,10 @@ struct S {
   S &operator=(S const &);
   S &operator=(S &&);
   S &operator+=(S);
+  S &operator++();
+  S &operator++(int);
+  S &operator--();
+  S &operator--(int);
 };
 
 S returnValue();
@@ -27,4 +31,8 @@ void bar() {
   a.operator=(returnRef());
 
   a += returnRef();
+  a++;
+  ++a;
+  a--;
+  --a;
 }

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


[clang-tools-extra] [clang-tidy]bugprone-unused-return-value ignore `++` and `--` operator overloading (PR #84922)

2024-03-12 Thread Congcong Cai via cfe-commits

HerrCai0907 wrote:

> Please mention changes in Release Notes.

release notes already includes this change.

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


[clang-tools-extra] [clang-tidy]bugprone-unused-return-value ignore `++` and `--` operator overloading (PR #84922)

2024-03-12 Thread Congcong Cai via cfe-commits

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


[clang-tools-extra] [clang-tidy]bugprone-unused-return-value ignore `++` and `--` operator overloading (PR #84922)

2024-03-12 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/84922

>From d760b280a79be973642a0549db0e5a8838c397fd Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Tue, 12 Mar 2024 22:33:18 +0800
Subject: [PATCH 1/2] [clang-tidy]bugprone-unused-return-value ignore `++` and
 `--` operator overloading

Fixes: #84705
---
 .../bugprone/UnusedReturnValueCheck.cpp   | 28 +--
 .../unused-return-value-avoid-assignment.cpp  |  8 ++
 2 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
index 243fe47c2036b6..83b332fba1e2da 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
@@ -165,20 +165,20 @@ void 
UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
 
 void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) {
   auto MatchedDirectCallExpr = expr(
-  callExpr(
-  callee(functionDecl(
-  // Don't match void overloads of checked functions.
-  unless(returns(voidType())),
-  // Don't match copy or move assignment operator.
-  unless(cxxMethodDecl(isOperatorOverloading(
-  {OO_Equal, OO_PlusEqual, OO_MinusEqual, OO_StarEqual,
-   OO_SlashEqual, OO_PercentEqual, OO_CaretEqual, OO_AmpEqual,
-   OO_PipeEqual, OO_LessLessEqual, OO_GreaterGreaterEqual}))),
-  anyOf(
-  isInstantiatedFrom(
-  matchers::matchesAnyListedName(CheckedFunctions)),
-  returns(hasCanonicalType(hasDeclaration(namedDecl(
-  
matchers::matchesAnyListedName(CheckedReturnTypes)
+  callExpr(callee(functionDecl(
+   // Don't match void overloads of checked functions.
+   unless(returns(voidType())),
+   // Don't match copy or move assignment operator.
+   unless(cxxMethodDecl(isOperatorOverloading(
+   {OO_Equal, OO_PlusEqual, OO_MinusEqual, OO_StarEqual,
+OO_SlashEqual, OO_PercentEqual, OO_CaretEqual,
+OO_AmpEqual, OO_PipeEqual, OO_LessLessEqual,
+OO_GreaterGreaterEqual, OO_PlusPlus, OO_MinusMinus}))),
+   anyOf(isInstantiatedFrom(
+ matchers::matchesAnyListedName(CheckedFunctions)),
+ returns(hasCanonicalType(hasDeclaration(
+ namedDecl(matchers::matchesAnyListedName(
+ CheckedReturnTypes)
   .bind("match"));
 
   auto CheckCastToVoid =
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
index b4a41004adf894..5809da9ec27b38 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-return-value-avoid-assignment.cpp
@@ -10,6 +10,10 @@ struct S {
   S &operator=(S const &);
   S &operator=(S &&);
   S &operator+=(S);
+  S &operator++();
+  S &operator++(int);
+  S &operator--();
+  S &operator--(int);
 };
 
 S returnValue();
@@ -27,4 +31,8 @@ void bar() {
   a.operator=(returnRef());
 
   a += returnRef();
+  a++;
+  ++a;
+  a--;
+  --a;
 }

>From 784df684a08f4b4da97d8db9ba3bf708dafbf626 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Wed, 13 Mar 2024 14:31:36 +0800
Subject: [PATCH 2/2] better version

---
 .../bugprone/UnusedReturnValueCheck.cpp   | 43 +++
 .../unused-return-value-avoid-assignment.cpp  |  4 ++
 2 files changed, 30 insertions(+), 17 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
index 83b332fba1e2da..846505317c1c7a 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UnusedReturnValueCheck.cpp
@@ -31,9 +31,20 @@ AST_MATCHER_P(FunctionDecl, isInstantiatedFrom, 
Matcher,
   Finder, Builder);
 }
 
-AST_MATCHER_P(CXXMethodDecl, isOperatorOverloading,
-  llvm::SmallVector, Kinds) {
-  return llvm::is_contained(Kinds, Node.getOverloadedOperator());
+constexpr std::initializer_list
+AssignmentOverloadedOperatorKinds = {
+OO_Equal,  OO_PlusEqual, OO_MinusEqual,  OO_StarEqual,
+OO_SlashEqual, OO_PercentEqual,  OO_CaretEqual,  OO_AmpEqual,
+OO_PipeEqual,  OO_LessLessEqual, OO_GreaterGreaterEqual, OO_PlusPlus,
+OO_MinusMinus};
+
+AST_MATCHER(CXXOperatorCallExpr, isAssignmentOver

[clang-tools-extra] [clang-tidy]bugprone-unused-return-value ignore `++` and `--` operator overloading (PR #84922)

2024-03-12 Thread Congcong Cai via cfe-commits


@@ -165,20 +165,20 @@ void 
UnusedReturnValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
 
 void UnusedReturnValueCheck::registerMatchers(MatchFinder *Finder) {
   auto MatchedDirectCallExpr = expr(
-  callExpr(
-  callee(functionDecl(
-  // Don't match void overloads of checked functions.
-  unless(returns(voidType())),
-  // Don't match copy or move assignment operator.
-  unless(cxxMethodDecl(isOperatorOverloading(
-  {OO_Equal, OO_PlusEqual, OO_MinusEqual, OO_StarEqual,
-   OO_SlashEqual, OO_PercentEqual, OO_CaretEqual, OO_AmpEqual,
-   OO_PipeEqual, OO_LessLessEqual, OO_GreaterGreaterEqual}))),
-  anyOf(
-  isInstantiatedFrom(
-  matchers::matchesAnyListedName(CheckedFunctions)),
-  returns(hasCanonicalType(hasDeclaration(namedDecl(
-  
matchers::matchesAnyListedName(CheckedReturnTypes)
+  callExpr(callee(functionDecl(

HerrCai0907 wrote:

I refactor it via `cxxOperatorCallExpr`.

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


[clang-tools-extra] [clang-tidy] bugprone-unused-return-value config now supports regexes (PR #82952)

2024-02-25 Thread Congcong Cai via cfe-commits

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


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


[clang] [clang] reject to capture variable in `RequiresExprBodyDecl` (PR #78598)

2024-02-26 Thread Congcong Cai via cfe-commits

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


[clang-tools-extra] [clang-tidy] Fix `cppcoreguidelines-missing-std-forward` false positive for deleted functions (PR #83055)

2024-02-27 Thread Congcong Cai via cfe-commits

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


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


[clang-tools-extra] [clang-tidy] Improve `google-explicit-constructor` checks handling of `explicit(bool)` (PR #82689)

2024-03-02 Thread Congcong Cai via cfe-commits

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


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


[clang-tools-extra] [clang-tidy] Let `bugprone-use-after-move` also handle calls to `std::forward` (PR #82673)

2024-03-03 Thread Congcong Cai via cfe-commits

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

LGTM

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


[clang-tools-extra] [clang-tidy] Add bugprone-suspicious-stringview-data-usage check (PR #83716)

2024-03-03 Thread Congcong Cai via cfe-commits

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

LGTM

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


[clang-tools-extra] [clang-tidy] fix false positive in cppcoreguidelines-missing-std-forward (PR #77056)

2024-03-04 Thread Congcong Cai via cfe-commits

HerrCai0907 wrote:

I agree to fix false positive reported in #68105. But for capturing in lambda 
be reference and do forward in lambda, It is just like create a record by ref 
instead of using forward and do forward in some member function. I don't think 
it is an expected false positive.
```c++
template
void lambda_value_reference_capture_list_ref_2(T&& t) {
[&t] { T other = std::forward(t); };
}
```

@jcsxky @PiotrZSL Could you think again about this PR?

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


[clang-tools-extra] [clang-tidy][missing-std-forward]report diagnotics for using forward in lambda body (PR #83930)

2024-03-04 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 created 
https://github.com/llvm/llvm-project/pull/83930

Fixes: #83845
Capturing variable in lambda by reference and doing forward in lambda body are 
like constructing a struct by reference and using std::forward in some member 
function. It is dangerous if the lifetime of lambda expression is longer then 
current function. And it goes against what this check is intended to check.
This PR wants to revert this behavior introduced in #77056 partially.


>From bb5062f3044e2f0a830a3d80864192f70f7eb95b Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Tue, 5 Mar 2024 07:39:50 +0800
Subject: [PATCH] [clang-tidy][missing-std-forward]report diagnotics for using
 forward in lambda body

Fixes: #83845
Capturing variable in lambda by reference and doing forward in lambda body are 
like constructing a struct by reference and using std::forward in some member 
function. It is dangerous if the lifetime of lambda expression is longer then 
current function. And it goes against what this check is intended to check.
This PR wants to revert this behavior introduced in #77056 partially.
---
 .../MissingStdForwardCheck.cpp| 57 ++-
 clang-tools-extra/docs/ReleaseNotes.rst   |  4 +-
 .../cppcoreguidelines/missing-std-forward.cpp | 49 
 3 files changed, 34 insertions(+), 76 deletions(-)

diff --git 
a/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp 
b/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp
index c633683570f748..ecfad9cd44bfb1 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp
@@ -9,7 +9,6 @@
 #include "MissingStdForwardCheck.h"
 #include "../utils/Matchers.h"
 #include "clang/AST/ASTContext.h"
-#include "clang/AST/ExprConcepts.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 
 using namespace clang::ast_matchers;
@@ -53,68 +52,22 @@ AST_MATCHER(ParmVarDecl, isTemplateTypeParameter) {
  FuncTemplate->getTemplateParameters()->getDepth();
 }
 
-AST_MATCHER_P(NamedDecl, hasSameNameAsBoundNode, std::string, BindingID) {
-  IdentifierInfo *II = Node.getIdentifier();
-  if (nullptr == II)
-return false;
-  StringRef Name = II->getName();
-
-  return Builder->removeBindings(
-  [this, Name](const ast_matchers::internal::BoundNodesMap &Nodes) {
-const DynTypedNode &BN = Nodes.getNode(this->BindingID);
-if (const auto *ND = BN.get()) {
-  if (!isa(ND))
-return true;
-  return ND->getName() != Name;
-}
-return true;
-  });
-}
-
-AST_MATCHER_P(LambdaCapture, hasCaptureKind, LambdaCaptureKind, Kind) {
-  return Node.getCaptureKind() == Kind;
-}
-
-AST_MATCHER_P(LambdaExpr, hasCaptureDefaultKind, LambdaCaptureDefault, Kind) {
-  return Node.getCaptureDefault() == Kind;
-}
-
 } // namespace
 
 void MissingStdForwardCheck::registerMatchers(MatchFinder *Finder) {
-  auto RefToParmImplicit = allOf(
-  equalsBoundNode("var"), hasInitializer(ignoringParenImpCasts(
-  declRefExpr(to(equalsBoundNode("param"));
-  auto RefToParm = capturesVar(
-  varDecl(anyOf(hasSameNameAsBoundNode("param"), RefToParmImplicit)));
-  auto HasRefToParm = hasAnyCapture(RefToParm);
-
-  auto CaptureInRef =
-  allOf(hasCaptureDefaultKind(LambdaCaptureDefault::LCD_ByRef),
-unless(hasAnyCapture(
-capturesVar(varDecl(hasSameNameAsBoundNode("param"));
-  auto CaptureInCopy = allOf(
-  hasCaptureDefaultKind(LambdaCaptureDefault::LCD_ByCopy), HasRefToParm);
-  auto CaptureByRefExplicit = hasAnyCapture(
-  allOf(hasCaptureKind(LambdaCaptureKind::LCK_ByRef), RefToParm));
-
-  auto CapturedInBody =
-  lambdaExpr(anyOf(CaptureInRef, CaptureInCopy, CaptureByRefExplicit));
-  auto CapturedInCaptureList = hasAnyCapture(capturesVar(
-  
varDecl(hasInitializer(ignoringParenImpCasts(equalsBoundNode("call"));
-
   auto CapturedInLambda = hasDeclContext(cxxRecordDecl(
   isLambda(),
-  hasParent(lambdaExpr(forCallable(equalsBoundNode("func")),
-   anyOf(CapturedInCaptureList, CapturedInBody);
+  hasParent(
+  lambdaExpr(forCallable(equalsBoundNode("func")),
+ hasAnyCapture(capturesVar(varDecl(hasInitializer(
+ 
ignoringParenImpCasts(equalsBoundNode("call"));
 
   auto ToParam = hasAnyParameter(parmVarDecl(equalsBoundNode("param")));
 
   auto ForwardCallMatcher = callExpr(
   callExpr().bind("call"), argumentCountIs(1),
   hasArgument(
-  0, declRefExpr(to(
- varDecl(optionally(equalsBoundNode("param"))).bind("var",
+  0, declRefExpr(to(varDecl(equalsBoundNode("param")).bind("var",
   forCallable(anyOf(equalsBoundNode("func"), CapturedInLambda)),
   callee(unresolvedLookupExpr(hasAnyDeclaration(
   

[clang-tools-extra] [clang-tidy] fix false positive in cppcoreguidelines-missing-std-forward (PR #77056)

2024-03-04 Thread Congcong Cai via cfe-commits

HerrCai0907 wrote:

> Capture by reference makes t in parameter same with it in lambda body

That is definitely wrong.
It will get an error for this code if address sanitize is enabled.

```c++
#include 
#include 

template 
auto f(T &&t) {
  return [&]() {
return std::forward(t);
  };
}

struct S {
  int v;
};

int main() {
  auto fn = f(S{.v = 1000});

  std::cout << fn().v << "\n";
}
```

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


[clang-tools-extra] [clang-tidy] fix false negative in cppcoreguidelines-missing-std-forward (PR #83987)

2024-03-05 Thread Congcong Cai via cfe-commits

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


[clang-tools-extra] [clang-tidy] fix false negative in cppcoreguidelines-missing-std-forward (PR #83987)

2024-03-05 Thread Congcong Cai via cfe-commits


@@ -156,7 +156,8 @@ Changes in existing checks
 
 - Improved :doc:`cppcoreguidelines-missing-std-forward
   ` check by no longer
-  giving false positives for deleted functions.
+  giving false positives for deleted functions and fix false negative when one
+  parameter is forwarded, but some other parameter isn't.

HerrCai0907 wrote:

```suggestion
  giving false positives for deleted functions and fix false negative when some
  parameters are forwarded, but other aren't.
```

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


[clang-tools-extra] [clang-tidy] fix false negative in cppcoreguidelines-missing-std-forward (PR #83987)

2024-03-05 Thread Congcong Cai via cfe-commits

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

LGTM since it is helpful to improve this check.
But I still take a negative view about use `std::forward` in lambda body.

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


[clang-tools-extra] [clang-tidy] Add AllowStringArrays option to modernize-avoid-c-arrays (PR #71701)

2024-01-31 Thread Congcong Cai via cfe-commits

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


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


[clang-tools-extra] [clang-tidy] Fix warnings caused by "new check" template (PR #80537)

2024-02-03 Thread Congcong Cai via cfe-commits
Danny =?utf-8?q?M=C3=B6sch?= 
Message-ID:
In-Reply-To: 


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


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


[llvm] [clang-tools-extra] [clang] [clang] reject to capture variable in `RequiresExprBodyDecl` (PR #78598)

2024-02-03 Thread Congcong Cai via cfe-commits

HerrCai0907 wrote:

ping @erichkeane @cor3ntin 

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


  1   2   3   4   5   6   7   8   9   10   >