[llvm-branch-commits] [clang-tools-extra] [clang-tidy] fix false positive in lambda expr for return-const-ref-from-parameter (PR #118990)

2024-12-06 Thread Congcong Cai via llvm-branch-commits

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

None

>From 2fb6e21f63a16b5bde5401a4e1cb68af1405a258 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Fri, 6 Dec 2024 23:45:56 +0800
Subject: [PATCH] [clang-tidy] fix false positive in lambda expr for
 return-const-ref-from-parameter

---
 .../ReturnConstRefFromParameterCheck.cpp  | 14 +--
 clang-tools-extra/docs/ReleaseNotes.rst   |  4 ++--
 .../return-const-ref-from-parameter.cpp   | 23 +++
 3 files changed, 31 insertions(+), 10 deletions(-)

diff --git 
a/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp
index a35fcd99d494af..295955a971d7e8 100644
--- a/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp
@@ -31,22 +31,20 @@ void 
ReturnConstRefFromParameterCheck::registerMatchers(MatchFinder *Finder) {
  qualType(lValueReferenceType(pointee(
   qualType(isConstQualified()
  .bind("type"))),
- hasDeclContext(functionDecl().bind("owner")),
+ hasDeclContext(functionDecl(
+ equalsBoundNode("func"),
+ hasReturnTypeLoc(loc(qualType(
+ 
hasCanonicalType(equalsBoundNode("type"))),
  unless(hasLifetimeBoundAttr()))
  .bind("param")))
   .bind("dref"));
-  const auto Func =
-  functionDecl(equalsBoundNode("owner"),
-   hasReturnTypeLoc(loc(
-   qualType(hasCanonicalType(equalsBoundNode("type"))
-  .bind("func");
 
   Finder->addMatcher(
   returnStmt(
+  hasAncestor(functionDecl().bind("func")),
   hasReturnValue(anyOf(
   DRef, ignoringParens(conditionalOperator(eachOf(
-hasTrueExpression(DRef), hasFalseExpression(DRef)),
-  hasAncestor(Func)),
+hasTrueExpression(DRef), 
hasFalseExpression(DRef))),
   this);
 }
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index e00f86f7d01447..b2b66dca6ccf85 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -183,8 +183,8 @@ Changes in existing checks
 - Improved :doc:`bugprone-return-const-ref-from-parameter
   ` check to
   diagnose potential dangling references when returning a ``const &`` parameter
-  by using the conditional operator ``cond ? var1 : var2`` and no longer giving
-  false positives for functions which contain lambda and ignore parameters
+  by using the conditional operator ``cond ? var1 : var2`` and fixing false
+  positives for functions which contain lambda and ignore parameters
   with ``[[clang::lifetimebound]]`` attribute.
   
 - Improved :doc:`bugprone-sizeof-expression
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/return-const-ref-from-parameter.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/return-const-ref-from-parameter.cpp
index 46cb9063beda97..a3297ca0f8084e 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/return-const-ref-from-parameter.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/return-const-ref-from-parameter.cpp
@@ -203,3 +203,26 @@ namespace use_lifetime_bound_attr {
 int const &f(int const &a [[clang::lifetimebound]]) { return a; }
 } // namespace use_lifetime_bound_attr
 } // namespace gh117696
+
+
+namespace lambda {
+using T = const int &;
+using K = const float &;
+T inner_valid_lambda(T a) {
+  [&]() -> T { return a; };
+  return a;
+  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: returning a constant reference 
parameter
+}
+T inner_invalid_lambda(T a) {
+  [&](T a) -> T { return a; };
+  // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: returning a constant reference 
parameter
+  return a;
+  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: returning a constant reference 
parameter
+}
+T inner_invalid_lambda2(T a) {
+  [&](K a) -> K { return a; };
+  // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: returning a constant reference 
parameter
+  return a;
+  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: returning a constant reference 
parameter
+}
+} // namespace lambda

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


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] fix false positive in lambda expr for return-const-ref-from-parameter (PR #118990)

2024-12-06 Thread Congcong Cai via llvm-branch-commits

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


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] fix wrong float to float conversion check when floating point type is not standard type (PR #122637)

2025-01-12 Thread Congcong Cai via llvm-branch-commits

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

compare type kind is the wrong way to compare floating point type compatibility.
more generic compatibility check is needed.

>From 31b602beadf57764e9138276cfd8b0a187ba9fb7 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Sun, 12 Jan 2025 21:47:02 +0800
Subject: [PATCH] [clang-tidy] fix wrong float to float conversion check when
 floating point type is not standard type

compare type kind is the wrong way to compare floating point type compatibility.
more generic compatibility check is needed.
---
 .../clang-tidy/bugprone/NarrowingConversionsCheck.cpp| 4 +++-
 clang-tools-extra/docs/ReleaseNotes.rst  | 5 +
 ...rrowing-conversions-narrowingfloatingpoint-option.cpp | 9 +
 3 files changed, 17 insertions(+), 1 deletion(-)

diff --git 
a/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp
index 408390ebc70b64..bafcd402ca8510 100644
--- a/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp
@@ -513,7 +513,9 @@ void NarrowingConversionsCheck::handleFloatingCast(const 
ASTContext &Context,
   return;
 }
 const BuiltinType *FromType = getBuiltinType(Rhs);
-if (ToType->getKind() < FromType->getKind())
+if (!llvm::APFloatBase::isRepresentableBy(
+Context.getFloatTypeSemantics(FromType->desugar()),
+Context.getFloatTypeSemantics(ToType->desugar(
   diagNarrowType(SourceLoc, Lhs, Rhs);
   }
 }
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 835a0269a2733c..f709902cfd45e7 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -206,6 +206,11 @@ Changes in existing checks
   ` check by fixing
   a crash when determining if an ``enable_if[_t]`` was found.
 
+- Improve :doc:`bugprone-narrowing-conversions
+  ` to avoid incorrect check
+  results when floating point type is not ``float``, ``double`` and
+  ``long double``.
+
 - Improved :doc:`bugprone-optional-value-conversion
   ` to support detecting
   conversion directly by ``std::make_unique`` and ``std::make_shared``.
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-narrowingfloatingpoint-option.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-narrowingfloatingpoint-option.cpp
index 9ded2f0923f4e6..180b789e45bb37 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-narrowingfloatingpoint-option.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-narrowingfloatingpoint-option.cpp
@@ -36,6 +36,15 @@ void narrow_double_to_float_not_ok(double d) {
   f = narrow_double_to_float_return();
 }
 
+float narrow_float16_to_float_return(_Float16 f) {
+  return f;
+}
+
+_Float16 narrow_float_to_float16_return(float f) {
+  return f;
+  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: narrowing conversion from 
'float' to '_Float16' [bugprone-narrowing-conversions]
+}
+
 void narrow_fp_constants() {
   float f;
   f = 0.5; // [dcl.init.list] 7.2 : in-range fp constant to narrower float is 
not a narrowing.

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


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] fix wrong float to float conversion check when floating point type is not standard type (PR #122637)

2025-01-12 Thread Congcong Cai via llvm-branch-commits

HerrCai0907 wrote:

> [!WARNING]
> This pull request is not mergeable via GitHub because a downstack PR is 
> open. Once all requirements are satisfied, merge this PR as a stack  href="https://app.graphite.dev/github/pr/llvm/llvm-project/122637?utm_source=stack-comment-downstack-mergeability-warning";
>  >on Graphite.
> https://graphite.dev/docs/merge-pull-requests";>Learn more

* **#122637** https://app.graphite.dev/github/pr/llvm/llvm-project/122637?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/122637?utm_source=stack-comment-view-in-graphite";
 target="_blank">(View in Graphite)
* **#122636** https://app.graphite.dev/github/pr/llvm/llvm-project/122636?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* `main`




This stack of pull requests is managed by https://graphite.dev?utm-source=stack-comment";>Graphite. Learn 
more about https://stacking.dev/?utm_source=stack-comment";>stacking.


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


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] fix wrong float to float conversion check when floating point type is not standard type (PR #122637)

2025-01-12 Thread Congcong Cai via llvm-branch-commits

https://github.com/HerrCai0907 ready_for_review 
https://github.com/llvm/llvm-project/pull/122637
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] fix wrong float to float conversion check when floating point type is not standard type (PR #122637)

2025-01-12 Thread Congcong Cai via llvm-branch-commits

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

>From ecfe551759c7b741e6cabe07daf60257c3c9eaeb Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Sun, 12 Jan 2025 21:47:02 +0800
Subject: [PATCH] [clang-tidy] fix wrong float to float conversion check when
 floating point type is not standard type

compare type kind is the wrong way to compare floating point type compatibility.
more generic compatibility check is needed.
---
 .../clang-tidy/bugprone/NarrowingConversionsCheck.cpp| 4 +++-
 clang-tools-extra/docs/ReleaseNotes.rst  | 5 +
 ...rrowing-conversions-narrowingfloatingpoint-option.cpp | 9 +
 3 files changed, 17 insertions(+), 1 deletion(-)

diff --git 
a/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp
index 408390ebc70b64..bafcd402ca8510 100644
--- a/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp
@@ -513,7 +513,9 @@ void NarrowingConversionsCheck::handleFloatingCast(const 
ASTContext &Context,
   return;
 }
 const BuiltinType *FromType = getBuiltinType(Rhs);
-if (ToType->getKind() < FromType->getKind())
+if (!llvm::APFloatBase::isRepresentableBy(
+Context.getFloatTypeSemantics(FromType->desugar()),
+Context.getFloatTypeSemantics(ToType->desugar(
   diagNarrowType(SourceLoc, Lhs, Rhs);
   }
 }
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 835a0269a2733c..f709902cfd45e7 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -206,6 +206,11 @@ Changes in existing checks
   ` check by fixing
   a crash when determining if an ``enable_if[_t]`` was found.
 
+- Improve :doc:`bugprone-narrowing-conversions
+  ` to avoid incorrect check
+  results when floating point type is not ``float``, ``double`` and
+  ``long double``.
+
 - Improved :doc:`bugprone-optional-value-conversion
   ` to support detecting
   conversion directly by ``std::make_unique`` and ``std::make_shared``.
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-narrowingfloatingpoint-option.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-narrowingfloatingpoint-option.cpp
index 9ded2f0923f4e6..180b789e45bb37 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-narrowingfloatingpoint-option.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-narrowingfloatingpoint-option.cpp
@@ -36,6 +36,15 @@ void narrow_double_to_float_not_ok(double d) {
   f = narrow_double_to_float_return();
 }
 
+float narrow_float16_to_float_return(_Float16 f) {
+  return f;
+}
+
+_Float16 narrow_float_to_float16_return(float f) {
+  return f;
+  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: narrowing conversion from 
'float' to '_Float16' [bugprone-narrowing-conversions]
+}
+
 void narrow_fp_constants() {
   float f;
   f = 0.5; // [dcl.init.list] 7.2 : in-range fp constant to narrower float is 
not a narrowing.

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


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] fix wrong float to float conversion check when floating point type is not standard type (PR #122637)

2025-01-12 Thread Congcong Cai via llvm-branch-commits

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

>From d138b3de06de410f063319ca08e33087e1bdeeb8 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Sun, 12 Jan 2025 21:47:02 +0800
Subject: [PATCH] [clang-tidy] fix wrong float to float conversion check when
 floating point type is not standard type

compare type kind is the wrong way to compare floating point type compatibility.
more generic compatibility check is needed.
---
 .../clang-tidy/bugprone/NarrowingConversionsCheck.cpp| 4 +++-
 clang-tools-extra/docs/ReleaseNotes.rst  | 5 +
 ...rrowing-conversions-narrowingfloatingpoint-option.cpp | 9 +
 3 files changed, 17 insertions(+), 1 deletion(-)

diff --git 
a/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp
index 408390ebc70b64..bafcd402ca8510 100644
--- a/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp
@@ -513,7 +513,9 @@ void NarrowingConversionsCheck::handleFloatingCast(const 
ASTContext &Context,
   return;
 }
 const BuiltinType *FromType = getBuiltinType(Rhs);
-if (ToType->getKind() < FromType->getKind())
+if (!llvm::APFloatBase::isRepresentableBy(
+Context.getFloatTypeSemantics(FromType->desugar()),
+Context.getFloatTypeSemantics(ToType->desugar(
   diagNarrowType(SourceLoc, Lhs, Rhs);
   }
 }
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 835a0269a2733c..f709902cfd45e7 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -206,6 +206,11 @@ Changes in existing checks
   ` check by fixing
   a crash when determining if an ``enable_if[_t]`` was found.
 
+- Improve :doc:`bugprone-narrowing-conversions
+  ` to avoid incorrect check
+  results when floating point type is not ``float``, ``double`` and
+  ``long double``.
+
 - Improved :doc:`bugprone-optional-value-conversion
   ` to support detecting
   conversion directly by ``std::make_unique`` and ``std::make_shared``.
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-narrowingfloatingpoint-option.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-narrowingfloatingpoint-option.cpp
index 9ded2f0923f4e6..180b789e45bb37 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-narrowingfloatingpoint-option.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-narrowingfloatingpoint-option.cpp
@@ -36,6 +36,15 @@ void narrow_double_to_float_not_ok(double d) {
   f = narrow_double_to_float_return();
 }
 
+float narrow_float16_to_float_return(_Float16 f) {
+  return f;
+}
+
+_Float16 narrow_float_to_float16_return(float f) {
+  return f;
+  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: narrowing conversion from 
'float' to '_Float16' [bugprone-narrowing-conversions]
+}
+
 void narrow_fp_constants() {
   float f;
   f = 0.5; // [dcl.init.list] 7.2 : in-range fp constant to narrower float is 
not a narrowing.

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


[llvm-branch-commits] [clang-tools-extra] [clang-tidy][NFC] refactor modernize-raw-string-literal fix hint (PR #122909)

2025-01-14 Thread Congcong Cai via llvm-branch-commits

HerrCai0907 wrote:

> [!WARNING]
> This pull request is not mergeable via GitHub because a downstack PR is 
> open. Once all requirements are satisfied, merge this PR as a stack  href="https://app.graphite.dev/github/pr/llvm/llvm-project/122909?utm_source=stack-comment-downstack-mergeability-warning";
>  >on Graphite.
> https://graphite.dev/docs/merge-pull-requests";>Learn more

* **#122909** https://app.graphite.dev/github/pr/llvm/llvm-project/122909?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/122909?utm_source=stack-comment-view-in-graphite";
 target="_blank">(View in Graphite)
* **#122901** https://app.graphite.dev/github/pr/llvm/llvm-project/122901?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* `main`




This stack of pull requests is managed by https://graphite.dev?utm-source=stack-comment";>Graphite. Learn 
more about https://stacking.dev/?utm_source=stack-comment";>stacking.


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


[llvm-branch-commits] [clang-tools-extra] [clang-tidy][NFC] refactor modernize-raw-string-literal fix hint (PR #122909)

2025-01-14 Thread Congcong Cai via llvm-branch-commits

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

None

>From c413bd7b5f8d8524335ad50a71bc986d8bdca2a8 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Tue, 14 Jan 2025 22:24:46 +0800
Subject: [PATCH] [clang-tidy][NFC] refactor modernize-raw-string-literal fix
 hint

---
 .../modernize/RawStringLiteralCheck.cpp   | 105 +++---
 .../modernize/RawStringLiteralCheck.h |   4 -
 2 files changed, 62 insertions(+), 47 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/modernize/RawStringLiteralCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/RawStringLiteralCheck.cpp
index bd9830278facb7..1618b5be7699d9 100644
--- a/clang-tools-extra/clang-tidy/modernize/RawStringLiteralCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/RawStringLiteralCheck.cpp
@@ -9,8 +9,11 @@
 #include "RawStringLiteralCheck.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Basic/LangOptions.h"
+#include "clang/Basic/SourceManager.h"
 #include "clang/Lex/Lexer.h"
 #include "llvm/ADT/StringRef.h"
+#include 
 
 using namespace clang::ast_matchers;
 
@@ -67,20 +70,6 @@ bool containsDelimiter(StringRef Bytes, const std::string 
&Delimiter) {
 : (")" + Delimiter + R"(")")) != StringRef::npos;
 }
 
-std::string asRawStringLiteral(const StringLiteral *Literal,
-   const std::string &DelimiterStem) {
-  const StringRef Bytes = Literal->getBytes();
-  std::string Delimiter;
-  for (int I = 0; containsDelimiter(Bytes, Delimiter); ++I) {
-Delimiter = (I == 0) ? DelimiterStem : DelimiterStem + std::to_string(I);
-  }
-
-  if (Delimiter.empty())
-return (R"(R"()" + Bytes + R"lit()")lit").str();
-
-  return (R"(R")" + Delimiter + "(" + Bytes + ")" + Delimiter + R"(")").str();
-}
-
 } // namespace
 
 RawStringLiteralCheck::RawStringLiteralCheck(StringRef Name,
@@ -120,43 +109,73 @@ void RawStringLiteralCheck::registerMatchers(MatchFinder 
*Finder) {
   stringLiteral(unless(hasParent(predefinedExpr(.bind("lit"), this);
 }
 
-void RawStringLiteralCheck::check(const MatchFinder::MatchResult &Result) {
-  const auto *Literal = Result.Nodes.getNodeAs("lit");
-  if (Literal->getBeginLoc().isMacroID())
-return;
-
-  if (containsEscapedCharacters(Result, Literal, DisallowedChars)) {
-std::string Replacement = asRawStringLiteral(Literal, DelimiterStem);
-if (ReplaceShorterLiterals ||
-Replacement.length() <=
-Lexer::MeasureTokenLength(Literal->getBeginLoc(),
-  *Result.SourceManager, getLangOpts()))
-  replaceWithRawStringLiteral(Result, Literal, Replacement);
-  }
-}
-
-void RawStringLiteralCheck::replaceWithRawStringLiteral(
-const MatchFinder::MatchResult &Result, const StringLiteral *Literal,
-std::string Replacement) {
-  DiagnosticBuilder Builder =
-  diag(Literal->getBeginLoc(),
-   "escaped string literal can be written as a raw string literal");
-  const SourceManager &SM = *Result.SourceManager;
+static std::optional
+createUserDefinedSuffix(const StringLiteral *Literal, const SourceManager &SM,
+const LangOptions &LangOpts) {
   const CharSourceRange TokenRange =
   CharSourceRange::getTokenRange(Literal->getSourceRange());
   Token T;
-  if (Lexer::getRawToken(Literal->getBeginLoc(), T, SM, getLangOpts()))
-return;
+  if (Lexer::getRawToken(Literal->getBeginLoc(), T, SM, LangOpts))
+return std::nullopt;
   const CharSourceRange CharRange =
-  Lexer::makeFileCharRange(TokenRange, SM, getLangOpts());
+  Lexer::makeFileCharRange(TokenRange, SM, LangOpts);
   if (T.hasUDSuffix()) {
-StringRef Text = Lexer::getSourceText(CharRange, SM, getLangOpts());
+StringRef Text = Lexer::getSourceText(CharRange, SM, LangOpts);
 const size_t UDSuffixPos = Text.find_last_of('"');
 if (UDSuffixPos == StringRef::npos)
-  return;
-Replacement += Text.slice(UDSuffixPos + 1, Text.size());
+  return std::nullopt;
+return Text.slice(UDSuffixPos + 1, Text.size());
+  }
+  return std::nullopt;
+}
+
+static std::string createRawStringLiteral(const StringLiteral *Literal,
+  const std::string &DelimiterStem,
+  const SourceManager &SM,
+  const LangOptions &LangOpts) {
+  const StringRef Bytes = Literal->getBytes();
+  std::string Delimiter;
+  for (int I = 0; containsDelimiter(Bytes, Delimiter); ++I) {
+Delimiter = (I == 0) ? DelimiterStem : DelimiterStem + std::to_string(I);
+  }
+
+  std::optional UserDefinedSuffix =
+  createUserDefinedSuffix(Literal, SM, LangOpts);
+
+  if (Delimiter.empty())
+return (R"(R"()" + Bytes + R"lit()")lit" + UserDefinedSuffix.value_or(""))
+.str();
+
+  return (R"(R")" + Delimiter + "(" + Bytes + ")" + Delimiter + R"(")" +
+  UserDefinedSuffix.valu

[llvm-branch-commits] [clang-tools-extra] [clang-tidy][NFC] refactor modernize-raw-string-literal fix hint (PR #122909)

2025-01-14 Thread Congcong Cai via llvm-branch-commits

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

>From 9e5c5eb96a65d9bdec47566c9bf5ae95c57107f0 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Tue, 14 Jan 2025 22:24:46 +0800
Subject: [PATCH] [clang-tidy][NFC] refactor modernize-raw-string-literal fix
 hint

---
 .../modernize/RawStringLiteralCheck.cpp   | 105 +++---
 .../modernize/RawStringLiteralCheck.h |   4 -
 2 files changed, 62 insertions(+), 47 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/modernize/RawStringLiteralCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/RawStringLiteralCheck.cpp
index 126463ae795eb6..24674a407cb369 100644
--- a/clang-tools-extra/clang-tidy/modernize/RawStringLiteralCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/RawStringLiteralCheck.cpp
@@ -9,8 +9,11 @@
 #include "RawStringLiteralCheck.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Basic/LangOptions.h"
+#include "clang/Basic/SourceManager.h"
 #include "clang/Lex/Lexer.h"
 #include "llvm/ADT/StringRef.h"
+#include 
 
 using namespace clang::ast_matchers;
 
@@ -67,20 +70,6 @@ bool containsDelimiter(StringRef Bytes, const std::string 
&Delimiter) {
 : (")" + Delimiter + R"(")")) != StringRef::npos;
 }
 
-std::string asRawStringLiteral(const StringLiteral *Literal,
-   const std::string &DelimiterStem) {
-  const StringRef Bytes = Literal->getBytes();
-  std::string Delimiter;
-  for (int I = 0; containsDelimiter(Bytes, Delimiter); ++I) {
-Delimiter = (I == 0) ? DelimiterStem : DelimiterStem + std::to_string(I);
-  }
-
-  if (Delimiter.empty())
-return (R"(R"()" + Bytes + R"lit()")lit").str();
-
-  return (R"(R")" + Delimiter + "(" + Bytes + ")" + Delimiter + R"(")").str();
-}
-
 } // namespace
 
 RawStringLiteralCheck::RawStringLiteralCheck(StringRef Name,
@@ -120,43 +109,73 @@ void RawStringLiteralCheck::registerMatchers(MatchFinder 
*Finder) {
   stringLiteral(unless(hasParent(predefinedExpr(.bind("lit"), this);
 }
 
-void RawStringLiteralCheck::check(const MatchFinder::MatchResult &Result) {
-  const auto *Literal = Result.Nodes.getNodeAs("lit");
-  if (Literal->getBeginLoc().isMacroID())
-return;
-
-  if (containsEscapedCharacters(Result, Literal, DisallowedChars)) {
-std::string Replacement = asRawStringLiteral(Literal, DelimiterStem);
-if (ReplaceShorterLiterals ||
-Replacement.length() <=
-Lexer::MeasureTokenLength(Literal->getBeginLoc(),
-  *Result.SourceManager, getLangOpts()))
-  replaceWithRawStringLiteral(Result, Literal, Replacement);
-  }
-}
-
-void RawStringLiteralCheck::replaceWithRawStringLiteral(
-const MatchFinder::MatchResult &Result, const StringLiteral *Literal,
-std::string Replacement) {
-  DiagnosticBuilder Builder =
-  diag(Literal->getBeginLoc(),
-   "escaped string literal can be written as a raw string literal");
-  const SourceManager &SM = *Result.SourceManager;
+static std::optional
+createUserDefinedSuffix(const StringLiteral *Literal, const SourceManager &SM,
+const LangOptions &LangOpts) {
   const CharSourceRange TokenRange =
   CharSourceRange::getTokenRange(Literal->getSourceRange());
   Token T;
-  if (Lexer::getRawToken(Literal->getBeginLoc(), T, SM, getLangOpts()))
-return;
+  if (Lexer::getRawToken(Literal->getBeginLoc(), T, SM, LangOpts))
+return std::nullopt;
   const CharSourceRange CharRange =
-  Lexer::makeFileCharRange(TokenRange, SM, getLangOpts());
+  Lexer::makeFileCharRange(TokenRange, SM, LangOpts);
   if (T.hasUDSuffix()) {
-const StringRef Text = Lexer::getSourceText(CharRange, SM, getLangOpts());
+StringRef Text = Lexer::getSourceText(CharRange, SM, LangOpts);
 const size_t UDSuffixPos = Text.find_last_of('"');
 if (UDSuffixPos == StringRef::npos)
-  return;
-Replacement += Text.slice(UDSuffixPos + 1, Text.size());
+  return std::nullopt;
+return Text.slice(UDSuffixPos + 1, Text.size());
+  }
+  return std::nullopt;
+}
+
+static std::string createRawStringLiteral(const StringLiteral *Literal,
+  const std::string &DelimiterStem,
+  const SourceManager &SM,
+  const LangOptions &LangOpts) {
+  const StringRef Bytes = Literal->getBytes();
+  std::string Delimiter;
+  for (int I = 0; containsDelimiter(Bytes, Delimiter); ++I) {
+Delimiter = (I == 0) ? DelimiterStem : DelimiterStem + std::to_string(I);
+  }
+
+  std::optional UserDefinedSuffix =
+  createUserDefinedSuffix(Literal, SM, LangOpts);
+
+  if (Delimiter.empty())
+return (R"(R"()" + Bytes + R"lit()")lit" + UserDefinedSuffix.value_or(""))
+.str();
+
+  return (R"(R")" + Delimiter + "(" + Bytes + ")" + Delimiter + R"(")" +
+  UserDefinedSuffix.valu

[llvm-branch-commits] [clang-tools-extra] [clang-tidy][NFC] refactor modernize-raw-string-literal fix hint (PR #122909)

2025-01-14 Thread Congcong Cai via llvm-branch-commits

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

>From 9e5c5eb96a65d9bdec47566c9bf5ae95c57107f0 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Tue, 14 Jan 2025 22:24:46 +0800
Subject: [PATCH] [clang-tidy][NFC] refactor modernize-raw-string-literal fix
 hint

---
 .../modernize/RawStringLiteralCheck.cpp   | 105 +++---
 .../modernize/RawStringLiteralCheck.h |   4 -
 2 files changed, 62 insertions(+), 47 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/modernize/RawStringLiteralCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/RawStringLiteralCheck.cpp
index 126463ae795eb6..24674a407cb369 100644
--- a/clang-tools-extra/clang-tidy/modernize/RawStringLiteralCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/RawStringLiteralCheck.cpp
@@ -9,8 +9,11 @@
 #include "RawStringLiteralCheck.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Basic/LangOptions.h"
+#include "clang/Basic/SourceManager.h"
 #include "clang/Lex/Lexer.h"
 #include "llvm/ADT/StringRef.h"
+#include 
 
 using namespace clang::ast_matchers;
 
@@ -67,20 +70,6 @@ bool containsDelimiter(StringRef Bytes, const std::string 
&Delimiter) {
 : (")" + Delimiter + R"(")")) != StringRef::npos;
 }
 
-std::string asRawStringLiteral(const StringLiteral *Literal,
-   const std::string &DelimiterStem) {
-  const StringRef Bytes = Literal->getBytes();
-  std::string Delimiter;
-  for (int I = 0; containsDelimiter(Bytes, Delimiter); ++I) {
-Delimiter = (I == 0) ? DelimiterStem : DelimiterStem + std::to_string(I);
-  }
-
-  if (Delimiter.empty())
-return (R"(R"()" + Bytes + R"lit()")lit").str();
-
-  return (R"(R")" + Delimiter + "(" + Bytes + ")" + Delimiter + R"(")").str();
-}
-
 } // namespace
 
 RawStringLiteralCheck::RawStringLiteralCheck(StringRef Name,
@@ -120,43 +109,73 @@ void RawStringLiteralCheck::registerMatchers(MatchFinder 
*Finder) {
   stringLiteral(unless(hasParent(predefinedExpr(.bind("lit"), this);
 }
 
-void RawStringLiteralCheck::check(const MatchFinder::MatchResult &Result) {
-  const auto *Literal = Result.Nodes.getNodeAs("lit");
-  if (Literal->getBeginLoc().isMacroID())
-return;
-
-  if (containsEscapedCharacters(Result, Literal, DisallowedChars)) {
-std::string Replacement = asRawStringLiteral(Literal, DelimiterStem);
-if (ReplaceShorterLiterals ||
-Replacement.length() <=
-Lexer::MeasureTokenLength(Literal->getBeginLoc(),
-  *Result.SourceManager, getLangOpts()))
-  replaceWithRawStringLiteral(Result, Literal, Replacement);
-  }
-}
-
-void RawStringLiteralCheck::replaceWithRawStringLiteral(
-const MatchFinder::MatchResult &Result, const StringLiteral *Literal,
-std::string Replacement) {
-  DiagnosticBuilder Builder =
-  diag(Literal->getBeginLoc(),
-   "escaped string literal can be written as a raw string literal");
-  const SourceManager &SM = *Result.SourceManager;
+static std::optional
+createUserDefinedSuffix(const StringLiteral *Literal, const SourceManager &SM,
+const LangOptions &LangOpts) {
   const CharSourceRange TokenRange =
   CharSourceRange::getTokenRange(Literal->getSourceRange());
   Token T;
-  if (Lexer::getRawToken(Literal->getBeginLoc(), T, SM, getLangOpts()))
-return;
+  if (Lexer::getRawToken(Literal->getBeginLoc(), T, SM, LangOpts))
+return std::nullopt;
   const CharSourceRange CharRange =
-  Lexer::makeFileCharRange(TokenRange, SM, getLangOpts());
+  Lexer::makeFileCharRange(TokenRange, SM, LangOpts);
   if (T.hasUDSuffix()) {
-const StringRef Text = Lexer::getSourceText(CharRange, SM, getLangOpts());
+StringRef Text = Lexer::getSourceText(CharRange, SM, LangOpts);
 const size_t UDSuffixPos = Text.find_last_of('"');
 if (UDSuffixPos == StringRef::npos)
-  return;
-Replacement += Text.slice(UDSuffixPos + 1, Text.size());
+  return std::nullopt;
+return Text.slice(UDSuffixPos + 1, Text.size());
+  }
+  return std::nullopt;
+}
+
+static std::string createRawStringLiteral(const StringLiteral *Literal,
+  const std::string &DelimiterStem,
+  const SourceManager &SM,
+  const LangOptions &LangOpts) {
+  const StringRef Bytes = Literal->getBytes();
+  std::string Delimiter;
+  for (int I = 0; containsDelimiter(Bytes, Delimiter); ++I) {
+Delimiter = (I == 0) ? DelimiterStem : DelimiterStem + std::to_string(I);
+  }
+
+  std::optional UserDefinedSuffix =
+  createUserDefinedSuffix(Literal, SM, LangOpts);
+
+  if (Delimiter.empty())
+return (R"(R"()" + Bytes + R"lit()")lit" + UserDefinedSuffix.value_or(""))
+.str();
+
+  return (R"(R")" + Delimiter + "(" + Bytes + ")" + Delimiter + R"(")" +
+  UserDefinedSuffix.valu

[llvm-branch-commits] [clang-tools-extra] [clang-tidy][NFC] refactor modernize-raw-string-literal fix hint (PR #122909)

2025-01-14 Thread Congcong Cai via llvm-branch-commits

https://github.com/HerrCai0907 ready_for_review 
https://github.com/llvm/llvm-project/pull/122909
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang-tools-extra] [clang-tidy][NFC] refactor modernize-raw-string-literal fix hint (PR #122909)

2025-01-17 Thread Congcong Cai via llvm-branch-commits

HerrCai0907 wrote:

### Merge activity

* **Jan 17, 8:39 AM EST**: A user started a stack merge that includes this pull 
request via 
[Graphite](https://app.graphite.dev/github/pr/llvm/llvm-project/122909).


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


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] use correct template type in ``std::min`` and ``std::max`` when operand is integer literal for readability-use-std-min-max (PR #122296)

2025-01-09 Thread Congcong Cai via llvm-branch-commits

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

None

>From 7958e403f1a7bbba383f8a4ed5c1dbdbadba18d4 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Thu, 9 Jan 2025 23:15:18 +0800
Subject: [PATCH] [clang-tidy] use correct template type in ``std::min`` and
 ``std::max`` when operand is integer literal for readability-use-std-min-max

---
 .../readability/UseStdMinMaxCheck.cpp | 35 ---
 clang-tools-extra/docs/ReleaseNotes.rst   |  4 +++
 .../checkers/readability/use-std-min-max.cpp  | 21 +++
 3 files changed, 48 insertions(+), 12 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/readability/UseStdMinMaxCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/UseStdMinMaxCheck.cpp
index 179173502a8d01..6f6b8a853a91e0 100644
--- a/clang-tools-extra/clang-tidy/readability/UseStdMinMaxCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/UseStdMinMaxCheck.cpp
@@ -79,6 +79,27 @@ static QualType getNonTemplateAlias(QualType QT) {
   return QT;
 }
 
+static QualType getReplacementCastType(const Expr *CondLhs, const Expr 
*CondRhs,
+   QualType ComparedType) {
+  QualType LhsType = CondLhs->getType();
+  QualType RhsType = CondRhs->getType();
+  QualType LhsCanonicalType =
+  LhsType.getCanonicalType().getNonReferenceType().getUnqualifiedType();
+  QualType RhsCanonicalType =
+  RhsType.getCanonicalType().getNonReferenceType().getUnqualifiedType();
+  QualType GlobalImplicitCastType;
+  if (LhsCanonicalType != RhsCanonicalType) {
+if (llvm::isa(CondRhs)) {
+  GlobalImplicitCastType = getNonTemplateAlias(LhsType);
+} else if (llvm::isa(CondLhs)) {
+  GlobalImplicitCastType = getNonTemplateAlias(RhsType);
+} else {
+  GlobalImplicitCastType = getNonTemplateAlias(ComparedType);
+}
+  }
+  return GlobalImplicitCastType;
+}
+
 static std::string createReplacement(const Expr *CondLhs, const Expr *CondRhs,
  const Expr *AssignLhs,
  const SourceManager &Source,
@@ -92,18 +113,8 @@ static std::string createReplacement(const Expr *CondLhs, 
const Expr *CondRhs,
   const llvm::StringRef AssignLhsStr = Lexer::getSourceText(
   Source.getExpansionRange(AssignLhs->getSourceRange()), Source, LO);
 
-  QualType GlobalImplicitCastType;
-  QualType LhsType = CondLhs->getType()
- .getCanonicalType()
- .getNonReferenceType()
- .getUnqualifiedType();
-  QualType RhsType = CondRhs->getType()
- .getCanonicalType()
- .getNonReferenceType()
- .getUnqualifiedType();
-  if (LhsType != RhsType) {
-GlobalImplicitCastType = getNonTemplateAlias(BO->getLHS()->getType());
-  }
+  QualType GlobalImplicitCastType =
+  getReplacementCastType(CondLhs, CondRhs, BO->getLHS()->getType());
 
   return (AssignLhsStr + " = " + FunctionName +
   (!GlobalImplicitCastType.isNull()
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 94e15639c4a92e..fd523da8dc5a1b 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -364,6 +364,10 @@ Changes in existing checks
   ` check to
   validate ``namespace`` aliases.
 
+- Improved :doc:`readability-use-std-min-max
+  ` check to use correct 
template
+  type in ``std::min`` and ``std::max`` when operand is integer literal.
+
 Removed checks
 ^^
 
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/readability/use-std-min-max.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/readability/use-std-min-max.cpp
index 9c0e2eabda348d..35ade8a7c6d37e 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/use-std-min-max.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/use-std-min-max.cpp
@@ -252,3 +252,24 @@ void testVectorSizeType() {
   if (value < v.size())
 value = v.size();
 }
+
+namespace gh121676 {
+
+void useLeft() {
+  using U16 = unsigned short;
+  U16 I = 0;
+  // CHECK-MESSAGES: :[[@LINE+2]]:3: warning: use `std::max` instead of `<` 
[readability-use-std-min-max]
+  // CHECK-FIXES: I = std::max(I, 16U);
+  if (I < 16U)
+I = 16U;
+}
+void useRight() {
+  using U16 = unsigned short;
+  U16 I = 0;
+  // CHECK-MESSAGES: :[[@LINE+2]]:3: warning: use `std::min` instead of `<` 
[readability-use-std-min-max]
+  // CHECK-FIXES: I = std::min(16U, I);
+  if (16U < I)
+I = 16U;
+}
+
+} // namespace gh121676

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


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] use correct template type in ``std::min`` and ``std::max`` when operand is integer literal for readability-use-std-min-max (PR #122296)

2025-01-09 Thread Congcong Cai via llvm-branch-commits

HerrCai0907 wrote:

> [!WARNING]
> This pull request is not mergeable via GitHub because a downstack PR is 
> open. Once all requirements are satisfied, merge this PR as a stack  href="https://app.graphite.dev/github/pr/llvm/llvm-project/122296?utm_source=stack-comment-downstack-mergeability-warning";
>  >on Graphite.
> https://graphite.dev/docs/merge-pull-requests";>Learn more

* **#122296** https://app.graphite.dev/github/pr/llvm/llvm-project/122296?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/122296?utm_source=stack-comment-view-in-graphite";
 target="_blank">(View in Graphite)
* **#122288** https://app.graphite.dev/github/pr/llvm/llvm-project/122288?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* `main`




This stack of pull requests is managed by https://graphite.dev?utm-source=stack-comment";>Graphite. Learn 
more about https://stacking.dev/?utm_source=stack-comment";>stacking.


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


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] use correct template type in ``std::min`` and ``std::max`` when operand is integer literal for readability-use-std-min-max (PR #122296)

2025-01-09 Thread Congcong Cai via llvm-branch-commits

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


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] use correct template type in ``std::min`` and ``std::max`` when operand is integer literal for readability-use-std-min-max (PR #122296)

2025-01-09 Thread Congcong Cai via llvm-branch-commits

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


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] use correct template type in ``std::min`` and ``std::max`` when operand is integer literal for readability-use-std-min-max (PR #122296)

2025-01-09 Thread Congcong Cai via llvm-branch-commits

https://github.com/HerrCai0907 ready_for_review 
https://github.com/llvm/llvm-project/pull/122296
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [YAML] fix output incorrect format for block scalar string (PR #131694)

2025-03-18 Thread Congcong Cai via llvm-branch-commits

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


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] `matchesAnyListedTypeName` support non canonical types (PR #134869)

2025-04-08 Thread Congcong Cai via llvm-branch-commits

HerrCai0907 wrote:

> [!WARNING]
> This pull request is not mergeable via GitHub because a downstack PR is 
> open. Once all requirements are satisfied, merge this PR as a stack  href="https://app.graphite.dev/github/pr/llvm/llvm-project/134869?utm_source=stack-comment-downstack-mergeability-warning";
>  >on Graphite.
> https://graphite.dev/docs/merge-pull-requests";>Learn more

* **#134870** https://app.graphite.dev/github/pr/llvm/llvm-project/134870?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#134869** https://app.graphite.dev/github/pr/llvm/llvm-project/134869?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/134869?utm_source=stack-comment-view-in-graphite";
 target="_blank">(View in Graphite)
* **#134868** https://app.graphite.dev/github/pr/llvm/llvm-project/134868?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* `main`




This stack of pull requests is managed by https://graphite.dev?utm-source=stack-comment";>Graphite. Learn 
more about https://stacking.dev/?utm_source=stack-comment";>stacking.


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


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] treat unsigned char and signed char as char type by default in bugprone-unintended-char-ostream-output (PR #134870)

2025-04-09 Thread Congcong Cai via llvm-branch-commits

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

>From 9d29fd3792be9ae10eb58cecaaba313a0eaf85f1 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Tue, 8 Apr 2025 15:27:54 +
Subject: [PATCH 1/2] [clang-tidy] treat unsigned char and signed char as char
 type by default in bugprone-unintended-char-ostream-output

Add `AllowedTypes` options to support custom defined char like type.
treat `unsigned char` and `signed char` as char like type by default.
The allowed types only effect when the var decl or explicit cast to this
non-canonical type names.
---
 .../UnintendedCharOstreamOutputCheck.cpp  | 19 -
 .../UnintendedCharOstreamOutputCheck.h|  1 +
 .../unintended-char-ostream-output.rst|  8 +++
 ...nded-char-ostream-output-allowed-types.cpp | 41 +++
 ...intended-char-ostream-output-cast-type.cpp | 11 +--
 .../unintended-char-ostream-output.cpp| 70 +--
 6 files changed, 106 insertions(+), 44 deletions(-)
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/unintended-char-ostream-output-allowed-types.cpp

diff --git 
a/clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.cpp
index 7250e4ccb8c69..57e1f744fcd7d 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.cpp
@@ -7,6 +7,8 @@
 
//===--===//
 
 #include "UnintendedCharOstreamOutputCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
 #include "clang/AST/Type.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/ASTMatchers/ASTMatchers.h"
@@ -35,10 +37,14 @@ AST_MATCHER(Type, isChar) {
 
 UnintendedCharOstreamOutputCheck::UnintendedCharOstreamOutputCheck(
 StringRef Name, ClangTidyContext *Context)
-: ClangTidyCheck(Name, Context), CastTypeName(Options.get("CastTypeName")) 
{
-}
+: ClangTidyCheck(Name, Context),
+  AllowedTypes(utils::options::parseStringList(
+  Options.get("AllowedTypes", "unsigned char;signed char"))),
+  CastTypeName(Options.get("CastTypeName")) {}
 void UnintendedCharOstreamOutputCheck::storeOptions(
 ClangTidyOptions::OptionMap &Opts) {
+  Options.store(Opts, "AllowedTypes",
+utils::options::serializeStringList(AllowedTypes));
   if (CastTypeName.has_value())
 Options.store(Opts, "CastTypeName", CastTypeName.value());
 }
@@ -50,13 +56,20 @@ void 
UnintendedCharOstreamOutputCheck::registerMatchers(MatchFinder *Finder) {
 // with char / unsigned char / signed char
 classTemplateSpecializationDecl(
 hasTemplateArgument(0, refersToType(isChar();
+  auto IsDeclRefExprFromAllowedTypes = declRefExpr(to(varDecl(
+  hasType(matchers::matchesAnyListedTypeName(AllowedTypes, false);
+  auto IsExplicitCastExprFromAllowedTypes = 
explicitCastExpr(hasDestinationType(
+  matchers::matchesAnyListedTypeName(AllowedTypes, false)));
   Finder->addMatcher(
   cxxOperatorCallExpr(
   hasOverloadedOperatorName("<<"),
   hasLHS(hasType(hasUnqualifiedDesugaredType(
   recordType(hasDeclaration(cxxRecordDecl(
   anyOf(BasicOstream, isDerivedFrom(BasicOstream,
-  hasRHS(hasType(hasUnqualifiedDesugaredType(isNumericChar()
+  hasRHS(expr(hasType(hasUnqualifiedDesugaredType(isNumericChar())),
+  unless(ignoringParenImpCasts(
+  anyOf(IsDeclRefExprFromAllowedTypes,
+IsExplicitCastExprFromAllowedTypes))
   .bind("x"),
   this);
 }
diff --git 
a/clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.h 
b/clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.h
index 61ea623d139ea..0759e3d1eb460 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.h
+++ b/clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.h
@@ -30,6 +30,7 @@ class UnintendedCharOstreamOutputCheck : public 
ClangTidyCheck {
   }
 
 private:
+  const std::vector AllowedTypes;
   const std::optional CastTypeName;
 };
 
diff --git 
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/unintended-char-ostream-output.rst
 
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/unintended-char-ostream-output.rst
index 95d02b3e2ddda..9ad08188d7fb2 100644
--- 
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/unintended-char-ostream-output.rst
+++ 
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/unintended-char-ostream-output.rst
@@ -42,6 +42,14 @@ Or cast to char to explicitly indicate that output should be 
a character.
 Options
 ---
 
+.. option:: AllowedTypes
+
+  A semicolon-separated list of type na

[llvm-branch-commits] [clang-tools-extra] [clang-tidy] `matchesAnyListedTypeName` support non canonical types (PR #134869)

2025-04-10 Thread Congcong Cai via llvm-branch-commits

https://github.com/HerrCai0907 ready_for_review 
https://github.com/llvm/llvm-project/pull/134869
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] `matchesAnyListedTypeName` support non canonical types (PR #134869)

2025-04-08 Thread Congcong Cai via llvm-branch-commits

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

None

>From 164409da8177cc5343a053a610095a6da335a126 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Tue, 8 Apr 2025 15:26:44 +
Subject: [PATCH] [clang-tidy] `matchesAnyListedTypeName` support non canonical
 types

---
 clang-tools-extra/clang-tidy/utils/Matchers.cpp |  7 ---
 clang-tools-extra/clang-tidy/utils/Matchers.h   | 13 ++---
 2 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/utils/Matchers.cpp 
b/clang-tools-extra/clang-tidy/utils/Matchers.cpp
index 7e89cae1c3316..742dc6fda8c92 100644
--- a/clang-tools-extra/clang-tidy/utils/Matchers.cpp
+++ b/clang-tools-extra/clang-tidy/utils/Matchers.cpp
@@ -18,8 +18,9 @@ bool NotIdenticalStatementsPredicate::operator()(
 }
 
 MatchesAnyListedTypeNameMatcher::MatchesAnyListedTypeNameMatcher(
-llvm::ArrayRef NameList)
-: NameMatchers(NameList.begin(), NameList.end()) {}
+llvm::ArrayRef NameList, bool CanonicalTypes)
+: NameMatchers(NameList.begin(), NameList.end()),
+  CanonicalTypes(CanonicalTypes) {}
 
 MatchesAnyListedTypeNameMatcher::~MatchesAnyListedTypeNameMatcher() = default;
 
@@ -32,7 +33,7 @@ bool MatchesAnyListedTypeNameMatcher::matches(
 
   PrintingPolicy PrintingPolicyWithSuppressedTag(
   Finder->getASTContext().getLangOpts());
-  PrintingPolicyWithSuppressedTag.PrintCanonicalTypes = true;
+  PrintingPolicyWithSuppressedTag.PrintCanonicalTypes = CanonicalTypes;
   PrintingPolicyWithSuppressedTag.SuppressElaboration = true;
   PrintingPolicyWithSuppressedTag.SuppressScope = false;
   PrintingPolicyWithSuppressedTag.SuppressTagKeyword = true;
diff --git a/clang-tools-extra/clang-tidy/utils/Matchers.h 
b/clang-tools-extra/clang-tidy/utils/Matchers.h
index 451c4ce92585b..2b6d377b8fd10 100644
--- a/clang-tools-extra/clang-tidy/utils/Matchers.h
+++ b/clang-tools-extra/clang-tidy/utils/Matchers.h
@@ -172,7 +172,8 @@ AST_MATCHER_P(Stmt, isStatementIdenticalToBoundNode, 
std::string, ID) {
 class MatchesAnyListedTypeNameMatcher
 : public ast_matchers::internal::MatcherInterface {
 public:
-  explicit MatchesAnyListedTypeNameMatcher(llvm::ArrayRef NameList);
+  explicit MatchesAnyListedTypeNameMatcher(llvm::ArrayRef NameList,
+   bool CanonicalTypes);
   ~MatchesAnyListedTypeNameMatcher() override;
   bool matches(
   const QualType &Node, ast_matchers::internal::ASTMatchFinder *Finder,
@@ -180,13 +181,19 @@ class MatchesAnyListedTypeNameMatcher
 
 private:
   std::vector NameMatchers;
+  bool CanonicalTypes;
 };
 
 // Returns a matcher that matches QualType against a list of provided regular.
 inline ::clang::ast_matchers::internal::Matcher
-matchesAnyListedTypeName(llvm::ArrayRef NameList) {
+matchesAnyListedTypeName(llvm::ArrayRef NameList,
+ bool CanonicalTypes) {
   return ::clang::ast_matchers::internal::makeMatcher(
-  new MatchesAnyListedTypeNameMatcher(NameList));
+  new MatchesAnyListedTypeNameMatcher(NameList, CanonicalTypes));
+}
+inline ::clang::ast_matchers::internal::Matcher
+matchesAnyListedTypeName(llvm::ArrayRef NameList) {
+  return matchesAnyListedTypeName(NameList, true);
 }
 
 } // namespace clang::tidy::matchers

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


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] treat unsigned char and signed char as char type by default in bugprone-unintended-char-ostream-output (PR #134870)

2025-04-08 Thread Congcong Cai via llvm-branch-commits

https://github.com/HerrCai0907 ready_for_review 
https://github.com/llvm/llvm-project/pull/134870
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] treat unsigned char and signed char as char type by default in bugprone-unintended-char-ostream-output (PR #134870)

2025-04-08 Thread Congcong Cai via llvm-branch-commits

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


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] treat unsigned char and signed char as char type by default in bugprone-unintended-char-ostream-output (PR #134870)

2025-04-08 Thread Congcong Cai via llvm-branch-commits

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

>From 9d29fd3792be9ae10eb58cecaaba313a0eaf85f1 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Tue, 8 Apr 2025 15:27:54 +
Subject: [PATCH] [clang-tidy] treat unsigned char and signed char as char type
 by default in bugprone-unintended-char-ostream-output

Add `AllowedTypes` options to support custom defined char like type.
treat `unsigned char` and `signed char` as char like type by default.
The allowed types only effect when the var decl or explicit cast to this
non-canonical type names.
---
 .../UnintendedCharOstreamOutputCheck.cpp  | 19 -
 .../UnintendedCharOstreamOutputCheck.h|  1 +
 .../unintended-char-ostream-output.rst|  8 +++
 ...nded-char-ostream-output-allowed-types.cpp | 41 +++
 ...intended-char-ostream-output-cast-type.cpp | 11 +--
 .../unintended-char-ostream-output.cpp| 70 +--
 6 files changed, 106 insertions(+), 44 deletions(-)
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/unintended-char-ostream-output-allowed-types.cpp

diff --git 
a/clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.cpp
index 7250e4ccb8c69..57e1f744fcd7d 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.cpp
@@ -7,6 +7,8 @@
 
//===--===//
 
 #include "UnintendedCharOstreamOutputCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
 #include "clang/AST/Type.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/ASTMatchers/ASTMatchers.h"
@@ -35,10 +37,14 @@ AST_MATCHER(Type, isChar) {
 
 UnintendedCharOstreamOutputCheck::UnintendedCharOstreamOutputCheck(
 StringRef Name, ClangTidyContext *Context)
-: ClangTidyCheck(Name, Context), CastTypeName(Options.get("CastTypeName")) 
{
-}
+: ClangTidyCheck(Name, Context),
+  AllowedTypes(utils::options::parseStringList(
+  Options.get("AllowedTypes", "unsigned char;signed char"))),
+  CastTypeName(Options.get("CastTypeName")) {}
 void UnintendedCharOstreamOutputCheck::storeOptions(
 ClangTidyOptions::OptionMap &Opts) {
+  Options.store(Opts, "AllowedTypes",
+utils::options::serializeStringList(AllowedTypes));
   if (CastTypeName.has_value())
 Options.store(Opts, "CastTypeName", CastTypeName.value());
 }
@@ -50,13 +56,20 @@ void 
UnintendedCharOstreamOutputCheck::registerMatchers(MatchFinder *Finder) {
 // with char / unsigned char / signed char
 classTemplateSpecializationDecl(
 hasTemplateArgument(0, refersToType(isChar();
+  auto IsDeclRefExprFromAllowedTypes = declRefExpr(to(varDecl(
+  hasType(matchers::matchesAnyListedTypeName(AllowedTypes, false);
+  auto IsExplicitCastExprFromAllowedTypes = 
explicitCastExpr(hasDestinationType(
+  matchers::matchesAnyListedTypeName(AllowedTypes, false)));
   Finder->addMatcher(
   cxxOperatorCallExpr(
   hasOverloadedOperatorName("<<"),
   hasLHS(hasType(hasUnqualifiedDesugaredType(
   recordType(hasDeclaration(cxxRecordDecl(
   anyOf(BasicOstream, isDerivedFrom(BasicOstream,
-  hasRHS(hasType(hasUnqualifiedDesugaredType(isNumericChar()
+  hasRHS(expr(hasType(hasUnqualifiedDesugaredType(isNumericChar())),
+  unless(ignoringParenImpCasts(
+  anyOf(IsDeclRefExprFromAllowedTypes,
+IsExplicitCastExprFromAllowedTypes))
   .bind("x"),
   this);
 }
diff --git 
a/clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.h 
b/clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.h
index 61ea623d139ea..0759e3d1eb460 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.h
+++ b/clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.h
@@ -30,6 +30,7 @@ class UnintendedCharOstreamOutputCheck : public 
ClangTidyCheck {
   }
 
 private:
+  const std::vector AllowedTypes;
   const std::optional CastTypeName;
 };
 
diff --git 
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/unintended-char-ostream-output.rst
 
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/unintended-char-ostream-output.rst
index 95d02b3e2ddda..9ad08188d7fb2 100644
--- 
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/unintended-char-ostream-output.rst
+++ 
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/unintended-char-ostream-output.rst
@@ -42,6 +42,14 @@ Or cast to char to explicitly indicate that output should be 
a character.
 Options
 ---
 
+.. option:: AllowedTypes
+
+  A semicolon-separated list of type names 

[llvm-branch-commits] [clang-tools-extra] [clang-tidy] treat unsigned char and signed char as char type by default in bugprone-unintended-char-ostream-output (PR #134870)

2025-04-08 Thread Congcong Cai via llvm-branch-commits

HerrCai0907 wrote:

> [!WARNING]
> This pull request is not mergeable via GitHub because a downstack PR is 
> open. Once all requirements are satisfied, merge this PR as a stack  href="https://app.graphite.dev/github/pr/llvm/llvm-project/134870?utm_source=stack-comment-downstack-mergeability-warning";
>  >on Graphite.
> https://graphite.dev/docs/merge-pull-requests";>Learn more

* **#134870** https://app.graphite.dev/github/pr/llvm/llvm-project/134870?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/134870?utm_source=stack-comment-view-in-graphite";
 target="_blank">(View in Graphite)
* **#134869** https://app.graphite.dev/github/pr/llvm/llvm-project/134869?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#134868** https://app.graphite.dev/github/pr/llvm/llvm-project/134868?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* `main`




This stack of pull requests is managed by https://graphite.dev?utm-source=stack-comment";>Graphite. Learn 
more about https://stacking.dev/?utm_source=stack-comment";>stacking.


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


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] treat unsigned char and signed char as char type by default in bugprone-unintended-char-ostream-output (PR #134870)

2025-04-10 Thread Congcong Cai via llvm-branch-commits

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

Add `AllowedTypes` options to support custom defined char like type.
treat `unsigned char` and `signed char` as char like type by default.
The allowed types only effect when the var decl or explicit cast to this
non-canonical type names.

>From f860128a64f171078eb9c61eca9eaf0152a04566 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Tue, 8 Apr 2025 15:27:54 +
Subject: [PATCH] [clang-tidy] treat unsigned char and signed char as char type
 by default in bugprone-unintended-char-ostream-output

Add `AllowedTypes` options to support custom defined char like type.
treat `unsigned char` and `signed char` as char like type by default.
The allowed types only effect when the var decl or explicit cast to this
non-canonical type names.
---
 .../UnintendedCharOstreamOutputCheck.cpp  | 19 -
 .../UnintendedCharOstreamOutputCheck.h|  1 +
 .../unintended-char-ostream-output.rst|  8 +++
 ...nded-char-ostream-output-allowed-types.cpp | 41 +++
 ...intended-char-ostream-output-cast-type.cpp | 11 +--
 .../unintended-char-ostream-output.cpp| 70 +--
 6 files changed, 106 insertions(+), 44 deletions(-)
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/unintended-char-ostream-output-allowed-types.cpp

diff --git 
a/clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.cpp
index 7250e4ccb8c69..57e1f744fcd7d 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.cpp
@@ -7,6 +7,8 @@
 
//===--===//
 
 #include "UnintendedCharOstreamOutputCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
 #include "clang/AST/Type.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/ASTMatchers/ASTMatchers.h"
@@ -35,10 +37,14 @@ AST_MATCHER(Type, isChar) {
 
 UnintendedCharOstreamOutputCheck::UnintendedCharOstreamOutputCheck(
 StringRef Name, ClangTidyContext *Context)
-: ClangTidyCheck(Name, Context), CastTypeName(Options.get("CastTypeName")) 
{
-}
+: ClangTidyCheck(Name, Context),
+  AllowedTypes(utils::options::parseStringList(
+  Options.get("AllowedTypes", "unsigned char;signed char"))),
+  CastTypeName(Options.get("CastTypeName")) {}
 void UnintendedCharOstreamOutputCheck::storeOptions(
 ClangTidyOptions::OptionMap &Opts) {
+  Options.store(Opts, "AllowedTypes",
+utils::options::serializeStringList(AllowedTypes));
   if (CastTypeName.has_value())
 Options.store(Opts, "CastTypeName", CastTypeName.value());
 }
@@ -50,13 +56,20 @@ void 
UnintendedCharOstreamOutputCheck::registerMatchers(MatchFinder *Finder) {
 // with char / unsigned char / signed char
 classTemplateSpecializationDecl(
 hasTemplateArgument(0, refersToType(isChar();
+  auto IsDeclRefExprFromAllowedTypes = declRefExpr(to(varDecl(
+  hasType(matchers::matchesAnyListedTypeName(AllowedTypes, false);
+  auto IsExplicitCastExprFromAllowedTypes = 
explicitCastExpr(hasDestinationType(
+  matchers::matchesAnyListedTypeName(AllowedTypes, false)));
   Finder->addMatcher(
   cxxOperatorCallExpr(
   hasOverloadedOperatorName("<<"),
   hasLHS(hasType(hasUnqualifiedDesugaredType(
   recordType(hasDeclaration(cxxRecordDecl(
   anyOf(BasicOstream, isDerivedFrom(BasicOstream,
-  hasRHS(hasType(hasUnqualifiedDesugaredType(isNumericChar()
+  hasRHS(expr(hasType(hasUnqualifiedDesugaredType(isNumericChar())),
+  unless(ignoringParenImpCasts(
+  anyOf(IsDeclRefExprFromAllowedTypes,
+IsExplicitCastExprFromAllowedTypes))
   .bind("x"),
   this);
 }
diff --git 
a/clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.h 
b/clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.h
index 61ea623d139ea..0759e3d1eb460 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.h
+++ b/clang-tools-extra/clang-tidy/bugprone/UnintendedCharOstreamOutputCheck.h
@@ -30,6 +30,7 @@ class UnintendedCharOstreamOutputCheck : public 
ClangTidyCheck {
   }
 
 private:
+  const std::vector AllowedTypes;
   const std::optional CastTypeName;
 };
 
diff --git 
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/unintended-char-ostream-output.rst
 
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/unintended-char-ostream-output.rst
index 95d02b3e2ddda..9ad08188d7fb2 100644
--- 
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/unintended-char-ostream-output.rst
+++ 
b/clang-tools-extra/docs/clang-tid

[llvm-branch-commits] [clang-tools-extra] [clang-tidy] treat unsigned char and signed char as char type by default in bugprone-unintended-char-ostream-output (PR #134870)

2025-04-12 Thread Congcong Cai via llvm-branch-commits

HerrCai0907 wrote:

### Merge activity

* **Apr 12, 11:58 PM EDT**: A user started a stack merge that includes this 
pull request via 
[Graphite](https://app.graphite.dev/github/pr/llvm/llvm-project/134870).


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


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] support to detect conversion in `make_optional` for `bugprone-optional-value-conversion` (PR #130417)

2025-03-08 Thread Congcong Cai via llvm-branch-commits

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

>From 1747df81cf0bf83838777a71ec577b083a66d85d Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Sat, 8 Mar 2025 21:50:19 +0800
Subject: [PATCH] [clang-tidy] support to detect conversion in `make_optional`
 for `bugprone-optional-value-conversion`

Fixes: #119554
---
 .../bugprone/OptionalValueConversionCheck.cpp  | 14 ++
 clang-tools-extra/docs/ReleaseNotes.rst|  4 
 ...ptional-value-conversion-construct-from-std.cpp | 13 +
 3 files changed, 31 insertions(+)

diff --git 
a/clang-tools-extra/clang-tidy/bugprone/OptionalValueConversionCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/OptionalValueConversionCheck.cpp
index 33e823ac07490..cb5a1c7bea801 100644
--- a/clang-tools-extra/clang-tidy/bugprone/OptionalValueConversionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/OptionalValueConversionCheck.cpp
@@ -12,6 +12,7 @@
 #include "../utils/OptionsUtils.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
 #include 
 
 using namespace clang::ast_matchers;
@@ -31,6 +32,7 @@ constexpr std::array MakeSmartPtrList{
 "::std::make_unique",
 "::std::make_shared",
 };
+constexpr StringRef MakeOptional = "::std::make_optional";
 
 } // namespace
 
@@ -86,6 +88,18 @@ void 
OptionalValueConversionCheck::registerMatchers(MatchFinder *Finder) {
callee(functionDecl(
matchers::matchesAnyListedName(MakeSmartPtrList),
hasTemplateArgument(0, 
refersToType(BindOptionalType,
+   hasArgument(0, OptionalDerefMatcher)),
+   callExpr(
+   // match first std::make_optional by limit argument count 
(1)
+   // and template count (1).
+   // 1. template< class T > constexpr
+   //std::optional> make_optional(T&& value);
+   // 2. template< class T, class... Args > constexpr
+   //std::optional make_optional(Args&&... args);
+   argumentCountIs(1),
+   callee(functionDecl(templateArgumentCountIs(1),
+   hasName(MakeOptional),
+   returns(BindOptionalType))),
hasArgument(0, OptionalDerefMatcher))),
unless(anyOf(hasAncestor(typeLoc()),
 hasAncestor(expr(matchers::hasUnevaluatedContext())
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index ce1418a2a7d58..a726015a708f7 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -115,6 +115,10 @@ Changes in existing checks
   no longer be needed and will be removed. Also fixing false positive from 
   const reference accessors to objects containing optional member.
 
+- Improved :doc:`bugprone-optional-value-conversion
+  ` check to detect
+  conversion in argument of ``std::make_optional``.
+
 - Improved :doc:`bugprone-unsafe-functions
   ` check to allow specifying
   additional C++ member functions to match.
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/optional-value-conversion-construct-from-std.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/optional-value-conversion-construct-from-std.cpp
index 768ab1ce014ce..305fd6890710d 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/optional-value-conversion-construct-from-std.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/optional-value-conversion-construct-from-std.cpp
@@ -27,9 +27,19 @@ class unique_ptr {};
 template 
 class shared_ptr {};
 
+template 
+class initializer_list {};
+
 template  unique_ptr make_unique(Args &&...args);
 template  shared_ptr make_shared(Args &&...args);
 
+template 
+constexpr std::optional<__decay(T)> make_optional(T &&value);
+template 
+constexpr std::optional make_optional(Args &&...args);
+template 
+constexpr std::optional make_optional(std::initializer_list il, Args 
&&...args);
+
 } // namespace std
 
 struct A {
@@ -45,9 +55,12 @@ void invalid() {
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: conversion from 
'std::optional' into 'int' and back into 'std::optional', remove 
potentially error-prone optional dereference 
[bugprone-optional-value-conversion]
   std::make_shared>(opt.value());
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: conversion from 
'std::optional' into 'int' and back into 'std::optional', remove 
potentially error-prone optional dereference 
[bugprone-optional-value-conversion]
+  std::make_optional(opt.value());
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: conversion from 
'std::optional' into 'int' and back into 'std::optional', remove 
potentially error-prone optional dereference 
[bugprone-optional-value-conversion]
 }
 
 void valid() {
   s

[llvm-branch-commits] [clang] [AstMatcher]`templateArgumentCountIs` support `FunctionDecl` (PR #130416)

2025-03-08 Thread Congcong Cai via llvm-branch-commits

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

`hasTemplateArgument` and `templateArgumentCountIs` are always used together. 
It is more convenient to make then support `FunctionDecl`.

>From 97ea31513d00893e797ff3c97ac441e54a11bbb8 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Sat, 8 Mar 2025 21:33:02 +0800
Subject: [PATCH] [AstMatcher]`templateArgumentCountIs` support `FunctionDecl`

`hasTemplateArgument` and `templateArgumentCountIs` are always used together. 
It is more convenient to make then support `FunctionDecl`.
---
 clang/include/clang/ASTMatchers/ASTMatchers.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h 
b/clang/include/clang/ASTMatchers/ASTMatchers.h
index 0f7e3a8a01762..03d522072f6c1 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -1090,6 +1090,7 @@ AST_POLYMORPHIC_MATCHER_P2(
 AST_POLYMORPHIC_MATCHER_P(
 templateArgumentCountIs,
 AST_POLYMORPHIC_SUPPORTED_TYPES(ClassTemplateSpecializationDecl,
+VarTemplateSpecializationDecl, 
FunctionDecl,
 TemplateSpecializationType),
 unsigned, N) {
   return internal::getTemplateSpecializationArgs(Node).size() == N;

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


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] support to detect conversion in `make_optional` for `bugprone-optional-value-conversion` (PR #130417)

2025-03-08 Thread Congcong Cai via llvm-branch-commits

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

Fixes: #119554

>From 51d2bd7e75426ee5c34bda94ff212d82da3354b7 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Sat, 8 Mar 2025 21:50:19 +0800
Subject: [PATCH] [clang-tidy] support to detect conversion in `make_optional`
 for `bugprone-optional-value-conversion`

Fixes: #119554
---
 .../bugprone/OptionalValueConversionCheck.cpp  | 18 ++
 clang-tools-extra/docs/ReleaseNotes.rst|  4 
 ...nal-value-conversion-construct-from-std.cpp | 13 +
 3 files changed, 35 insertions(+)

diff --git 
a/clang-tools-extra/clang-tidy/bugprone/OptionalValueConversionCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/OptionalValueConversionCheck.cpp
index 33e823ac07490..a3c6bebe3d17f 100644
--- a/clang-tools-extra/clang-tidy/bugprone/OptionalValueConversionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/OptionalValueConversionCheck.cpp
@@ -12,6 +12,7 @@
 #include "../utils/OptionsUtils.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
 #include 
 
 using namespace clang::ast_matchers;
@@ -27,10 +28,15 @@ AST_MATCHER_P(QualType, hasCleanType, Matcher, 
InnerMatcher) {
   Finder, Builder);
 }
 
+AST_MATCHER_P(FunctionDecl, hasReturnType, Matcher, InnerMatcher) {
+  return InnerMatcher.matches(Node.getReturnType(), Finder, Builder);
+}
+
 constexpr std::array MakeSmartPtrList{
 "::std::make_unique",
 "::std::make_shared",
 };
+constexpr StringRef MakeOptional = "::std::make_optional";
 
 } // namespace
 
@@ -86,6 +92,18 @@ void 
OptionalValueConversionCheck::registerMatchers(MatchFinder *Finder) {
callee(functionDecl(
matchers::matchesAnyListedName(MakeSmartPtrList),
hasTemplateArgument(0, 
refersToType(BindOptionalType,
+   hasArgument(0, OptionalDerefMatcher)),
+   callExpr(
+   // match first std::make_optional by limit argument count 
(1)
+   // and template count (1).
+   // 1. template< class T > constexpr
+   //std::optional> make_optional(T&& value);
+   // 2. template< class T, class... Args > constexpr
+   //std::optional make_optional(Args&&... args);
+   argumentCountIs(1),
+   callee(functionDecl(templateArgumentCountIs(1),
+   hasName(MakeOptional),
+   hasReturnType(BindOptionalType))),
hasArgument(0, OptionalDerefMatcher))),
unless(anyOf(hasAncestor(typeLoc()),
 hasAncestor(expr(matchers::hasUnevaluatedContext())
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index ce1418a2a7d58..a726015a708f7 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -115,6 +115,10 @@ Changes in existing checks
   no longer be needed and will be removed. Also fixing false positive from 
   const reference accessors to objects containing optional member.
 
+- Improved :doc:`bugprone-optional-value-conversion
+  ` check to detect
+  conversion in argument of ``std::make_optional``.
+
 - Improved :doc:`bugprone-unsafe-functions
   ` check to allow specifying
   additional C++ member functions to match.
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/optional-value-conversion-construct-from-std.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/optional-value-conversion-construct-from-std.cpp
index 768ab1ce014ce..305fd6890710d 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/optional-value-conversion-construct-from-std.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/optional-value-conversion-construct-from-std.cpp
@@ -27,9 +27,19 @@ class unique_ptr {};
 template 
 class shared_ptr {};
 
+template 
+class initializer_list {};
+
 template  unique_ptr make_unique(Args &&...args);
 template  shared_ptr make_shared(Args &&...args);
 
+template 
+constexpr std::optional<__decay(T)> make_optional(T &&value);
+template 
+constexpr std::optional make_optional(Args &&...args);
+template 
+constexpr std::optional make_optional(std::initializer_list il, Args 
&&...args);
+
 } // namespace std
 
 struct A {
@@ -45,9 +55,12 @@ void invalid() {
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: conversion from 
'std::optional' into 'int' and back into 'std::optional', remove 
potentially error-prone optional dereference 
[bugprone-optional-value-conversion]
   std::make_shared>(opt.value());
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: conversion from 
'std::optional' into 'int' and back into 'std::optional', remove 
potentially error-prone optional dereference 
[bugprone-optional-value-conversion]
+  std::make_optio

[llvm-branch-commits] [clang] [AstMatcher]`templateArgumentCountIs` support `FunctionDecl` (PR #130416)

2025-03-08 Thread Congcong Cai via llvm-branch-commits

HerrCai0907 wrote:

> [!WARNING]
> This pull request is not mergeable via GitHub because a downstack PR is 
> open. Once all requirements are satisfied, merge this PR as a stack  href="https://app.graphite.dev/github/pr/llvm/llvm-project/130416?utm_source=stack-comment-downstack-mergeability-warning";
>  >on Graphite.
> https://graphite.dev/docs/merge-pull-requests";>Learn more

* **#130417** https://app.graphite.dev/github/pr/llvm/llvm-project/130417?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#130416** https://app.graphite.dev/github/pr/llvm/llvm-project/130416?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/130416?utm_source=stack-comment-view-in-graphite";
 target="_blank">(View in Graphite)
* **#130415** https://app.graphite.dev/github/pr/llvm/llvm-project/130415?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* `main`




This stack of pull requests is managed by https://graphite.dev?utm-source=stack-comment";>Graphite. Learn 
more about https://stacking.dev/?utm_source=stack-comment";>stacking.


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


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] support to detect conversion in `make_optional` for `bugprone-optional-value-conversion` (PR #130417)

2025-03-08 Thread Congcong Cai via llvm-branch-commits

HerrCai0907 wrote:

> [!WARNING]
> This pull request is not mergeable via GitHub because a downstack PR is 
> open. Once all requirements are satisfied, merge this PR as a stack  href="https://app.graphite.dev/github/pr/llvm/llvm-project/130417?utm_source=stack-comment-downstack-mergeability-warning";
>  >on Graphite.
> https://graphite.dev/docs/merge-pull-requests";>Learn more

* **#130417** https://app.graphite.dev/github/pr/llvm/llvm-project/130417?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/130417?utm_source=stack-comment-view-in-graphite";
 target="_blank">(View in Graphite)
* **#130416** https://app.graphite.dev/github/pr/llvm/llvm-project/130416?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#130415** https://app.graphite.dev/github/pr/llvm/llvm-project/130415?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* `main`




This stack of pull requests is managed by https://graphite.dev?utm-source=stack-comment";>Graphite. Learn 
more about https://stacking.dev/?utm_source=stack-comment";>stacking.


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


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] support to detect conversion in `make_optional` for `bugprone-optional-value-conversion` (PR #130417)

2025-03-08 Thread Congcong Cai via llvm-branch-commits

https://github.com/HerrCai0907 ready_for_review 
https://github.com/llvm/llvm-project/pull/130417
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] [AstMatcher]`templateArgumentCountIs` support `FunctionDecl` (PR #130416)

2025-03-08 Thread Congcong Cai via llvm-branch-commits

https://github.com/HerrCai0907 ready_for_review 
https://github.com/llvm/llvm-project/pull/130416
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] support pointee mutation check in misc-const-correctness (PR #130494)

2025-03-09 Thread Congcong Cai via llvm-branch-commits

HerrCai0907 wrote:

> [!WARNING]
> This pull request is not mergeable via GitHub because a downstack PR is 
> open. Once all requirements are satisfied, merge this PR as a stack  href="https://app.graphite.dev/github/pr/llvm/llvm-project/130494?utm_source=stack-comment-downstack-mergeability-warning";
>  >on Graphite.
> https://graphite.dev/docs/merge-pull-requests";>Learn more

* **#130494** https://app.graphite.dev/github/pr/llvm/llvm-project/130494?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/130494?utm_source=stack-comment-view-in-graphite";
 target="_blank">(View in Graphite)
* **#130493** https://app.graphite.dev/github/pr/llvm/llvm-project/130493?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* `main`




This stack of pull requests is managed by https://graphite.dev?utm-source=stack-comment";>Graphite. Learn 
more about https://stacking.dev/?utm_source=stack-comment";>stacking.


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


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] support pointee mutation check in misc-const-correctness (PR #130494)

2025-03-09 Thread Congcong Cai via llvm-branch-commits

https://github.com/HerrCai0907 ready_for_review 
https://github.com/llvm/llvm-project/pull/130494
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] support pointee mutation check in misc-const-correctness (PR #130494)

2025-03-09 Thread Congcong Cai via llvm-branch-commits

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

None

>From 4c1a7915fca1933773d3e9f3cf0b8c07916f1dcf Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Sun, 9 Mar 2025 15:43:37 +
Subject: [PATCH] [clang-tidy] support pointee mutation check in
 misc-const-correctness

---
 .../clang-tidy/misc/ConstCorrectnessCheck.cpp | 156 --
 .../clang-tidy/misc/ConstCorrectnessCheck.h   |   3 +
 clang-tools-extra/docs/ReleaseNotes.rst   |   3 +-
 .../checks/misc/const-correctness.rst |  44 +
 .../const-correctness-pointer-as-pointers.cpp |  50 ++
 .../const-correctness-transform-values.cpp|   1 +
 .../const-correctness-values-before-cxx23.cpp |   1 +
 .../misc/const-correctness-values.cpp |   1 +
 .../misc/const-correctness-wrong-config.cpp   |   7 +-
 9 files changed, 207 insertions(+), 59 deletions(-)
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp

diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp 
b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
index dbe59233df699..023c834d5700f 100644
--- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
@@ -13,6 +13,8 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/ASTMatchers/ASTMatchers.h"
+#include "llvm/Support/Casting.h"
+#include 
 
 using namespace clang::ast_matchers;
 
@@ -39,34 +41,47 @@ ConstCorrectnessCheck::ConstCorrectnessCheck(StringRef Name,
 : ClangTidyCheck(Name, Context),
   AnalyzeValues(Options.get("AnalyzeValues", true)),
   AnalyzeReferences(Options.get("AnalyzeReferences", true)),
+  AnalyzePointers(Options.get("AnalyzePointers", true)),
   WarnPointersAsValues(Options.get("WarnPointersAsValues", false)),
+  WarnPointersAsPointers(Options.get("WarnPointersAsPointers", true)),
   TransformValues(Options.get("TransformValues", true)),
   TransformReferences(Options.get("TransformReferences", true)),
   TransformPointersAsValues(
   Options.get("TransformPointersAsValues", false)),
+  TransformPointersAsPointers(
+  Options.get("TransformPointersAsPointers", true)),
   AllowedTypes(
   utils::options::parseStringList(Options.get("AllowedTypes", ""))) {
-  if (AnalyzeValues == false && AnalyzeReferences == false)
+  if (AnalyzeValues == false && AnalyzeReferences == false &&
+  AnalyzePointers == false)
 this->configurationDiag(
 "The check 'misc-const-correctness' will not "
-"perform any analysis because both 'AnalyzeValues' and "
-"'AnalyzeReferences' are false.");
+"perform any analysis because both 'AnalyzeValues', "
+"'AnalyzeReferences' and 'AnalyzePointers' are false.");
 }
 
 void ConstCorrectnessCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
   Options.store(Opts, "AnalyzeValues", AnalyzeValues);
   Options.store(Opts, "AnalyzeReferences", AnalyzeReferences);
+  Options.store(Opts, "AnalyzePointers", AnalyzePointers);
   Options.store(Opts, "WarnPointersAsValues", WarnPointersAsValues);
+  Options.store(Opts, "WarnPointersAsPointers", WarnPointersAsPointers);
 
   Options.store(Opts, "TransformValues", TransformValues);
   Options.store(Opts, "TransformReferences", TransformReferences);
   Options.store(Opts, "TransformPointersAsValues", TransformPointersAsValues);
+  Options.store(Opts, "TransformPointersAsPointers",
+TransformPointersAsPointers);
   Options.store(Opts, "AllowedTypes",
 utils::options::serializeStringList(AllowedTypes));
 }
 
 void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) {
-  const auto ConstType = hasType(isConstQualified());
+  const auto ConstType = hasType(
+  qualType(isConstQualified(),
+   // pointee check will check the const pointer and const array
+   unless(pointerType()), unless(arrayType(;
+
   const auto ConstReference = hasType(references(isConstQualified()));
   const auto RValueReference = hasType(
   referenceType(anyOf(rValueReferenceType(), 
unless(isSpelledAsLValue();
@@ -124,6 +139,11 @@ void ConstCorrectnessCheck::check(const 
MatchFinder::MatchResult &Result) {
   const auto *LocalScope = Result.Nodes.getNodeAs("scope");
   const auto *Variable = Result.Nodes.getNodeAs("local-value");
   const auto *Function = Result.Nodes.getNodeAs("function-decl");
+  const auto *VarDeclStmt = Result.Nodes.getNodeAs("decl-stmt");
+  // It can not be guaranteed that the variable is declared isolated,
+  // therefore a transformation might effect the other variables as well and
+  // be incorrect.
+  const bool CanBeFixIt = VarDeclStmt != nullptr && 
VarDeclStmt->isSingleDecl();
 
   /// If the variable was declared in a template it might be analyzed multiple
   /// times. Only one of those i

[llvm-branch-commits] [clang] [AstMatcher]`templateArgumentCountIs` support `FunctionDecl` (PR #130416)

2025-03-11 Thread Congcong Cai via llvm-branch-commits

HerrCai0907 wrote:

dump-ast-matchers has some bug. I will fix it after 
https://github.com/llvm/llvm-project/pull/130726 merging

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


[llvm-branch-commits] [clang-tools-extra] [clang-tidy] support to detect conversion in `make_optional` for `bugprone-optional-value-conversion` (PR #130417)

2025-03-10 Thread Congcong Cai via llvm-branch-commits

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

>From 1706e3fc5819602febf9dfa554e98eb1e1fea365 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Sat, 8 Mar 2025 21:50:19 +0800
Subject: [PATCH] [clang-tidy] support to detect conversion in `make_optional`
 for `bugprone-optional-value-conversion`

Fixes: #119554
---
 .../bugprone/OptionalValueConversionCheck.cpp  | 14 ++
 clang-tools-extra/docs/ReleaseNotes.rst|  4 
 ...ptional-value-conversion-construct-from-std.cpp | 13 +
 3 files changed, 31 insertions(+)

diff --git 
a/clang-tools-extra/clang-tidy/bugprone/OptionalValueConversionCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/OptionalValueConversionCheck.cpp
index 33e823ac07490..cb5a1c7bea801 100644
--- a/clang-tools-extra/clang-tidy/bugprone/OptionalValueConversionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/OptionalValueConversionCheck.cpp
@@ -12,6 +12,7 @@
 #include "../utils/OptionsUtils.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
 #include 
 
 using namespace clang::ast_matchers;
@@ -31,6 +32,7 @@ constexpr std::array MakeSmartPtrList{
 "::std::make_unique",
 "::std::make_shared",
 };
+constexpr StringRef MakeOptional = "::std::make_optional";
 
 } // namespace
 
@@ -86,6 +88,18 @@ void 
OptionalValueConversionCheck::registerMatchers(MatchFinder *Finder) {
callee(functionDecl(
matchers::matchesAnyListedName(MakeSmartPtrList),
hasTemplateArgument(0, 
refersToType(BindOptionalType,
+   hasArgument(0, OptionalDerefMatcher)),
+   callExpr(
+   // match first std::make_optional by limit argument count 
(1)
+   // and template count (1).
+   // 1. template< class T > constexpr
+   //std::optional> make_optional(T&& value);
+   // 2. template< class T, class... Args > constexpr
+   //std::optional make_optional(Args&&... args);
+   argumentCountIs(1),
+   callee(functionDecl(templateArgumentCountIs(1),
+   hasName(MakeOptional),
+   returns(BindOptionalType))),
hasArgument(0, OptionalDerefMatcher))),
unless(anyOf(hasAncestor(typeLoc()),
 hasAncestor(expr(matchers::hasUnevaluatedContext())
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index fa68b6fabd549..5e2d87e0c2fa1 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -124,6 +124,10 @@ Changes in existing checks
   no longer be needed and will be removed. Also fixing false positive from
   const reference accessors to objects containing optional member.
 
+- Improved :doc:`bugprone-optional-value-conversion
+  ` check to detect
+  conversion in argument of ``std::make_optional``.
+
 - Improved :doc:`bugprone-unsafe-functions
   ` check to allow specifying
   additional C++ member functions to match.
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/optional-value-conversion-construct-from-std.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/optional-value-conversion-construct-from-std.cpp
index 768ab1ce014ce..305fd6890710d 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/optional-value-conversion-construct-from-std.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/optional-value-conversion-construct-from-std.cpp
@@ -27,9 +27,19 @@ class unique_ptr {};
 template 
 class shared_ptr {};
 
+template 
+class initializer_list {};
+
 template  unique_ptr make_unique(Args &&...args);
 template  shared_ptr make_shared(Args &&...args);
 
+template 
+constexpr std::optional<__decay(T)> make_optional(T &&value);
+template 
+constexpr std::optional make_optional(Args &&...args);
+template 
+constexpr std::optional make_optional(std::initializer_list il, Args 
&&...args);
+
 } // namespace std
 
 struct A {
@@ -45,9 +55,12 @@ void invalid() {
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: conversion from 
'std::optional' into 'int' and back into 'std::optional', remove 
potentially error-prone optional dereference 
[bugprone-optional-value-conversion]
   std::make_shared>(opt.value());
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: conversion from 
'std::optional' into 'int' and back into 'std::optional', remove 
potentially error-prone optional dereference 
[bugprone-optional-value-conversion]
+  std::make_optional(opt.value());
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: conversion from 
'std::optional' into 'int' and back into 'std::optional', remove 
potentially error-prone optional dereference 
[bugprone-optional-value-conversion]
 }
 
 void valid() {
   st

[llvm-branch-commits] [llvm] [YAML] fix output incorrect format for block scalar string (PR #131694)

2025-03-23 Thread Congcong Cai via llvm-branch-commits

HerrCai0907 wrote:

ping @thurstond 

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