[clang] Reland Print library module manifest path again (PR #84881)

2024-03-19 Thread Katherine Whitlock via cfe-commits

stellar-aria wrote:

Can we also have a fallback check for stdlibs that don't have a `.so` version 
(such as embedded)? Something as simple as:
```c++
std::string lib = GetFilePath("libc++.so", TC);
if (lib.empty())
  lib = GetFilePath("libc++.a", TC);
```

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


[clang-tools-extra] [clang-tidy] Add new check `readability-use-numeric-limits` (PR #127430)

2025-02-16 Thread Katherine Whitlock via cfe-commits

https://github.com/stellar-aria created 
https://github.com/llvm/llvm-project/pull/127430

The adds a check that replaces specific numeric literals like `32767` with the 
equivalent call to `std::numeric_limits` (such as 
`std::numeric_limits::max())`.

Partially addresses #34434, but notably does not handle cases listed in the 
title post such as `~0` and `-1`.

>From ba0eef9808b42b01e356cd5c87745f7477e07c68 Mon Sep 17 00:00:00 2001
From: Katherine Whitlock 
Date: Sun, 16 Feb 2025 22:45:06 -0500
Subject: [PATCH] [clang-tidy] Add new check `readability-use-numeric-limits`

---
 .../clang-tidy/readability/CMakeLists.txt |   1 +
 .../readability/ReadabilityTidyModule.cpp |   3 +
 .../readability/UseNumericLimitsCheck.cpp | 135 ++
 .../readability/UseNumericLimitsCheck.h   |  41 ++
 clang-tools-extra/docs/ReleaseNotes.rst   |   5 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../checks/readability/use-numeric-limits.rst |  22 +++
 .../readability/use-numeric-limits.cpp|  85 +++
 8 files changed, 293 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/use-numeric-limits.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/use-numeric-limits.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index 8f303c51e1b0d..e06f68bdda73f 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -57,6 +57,7 @@ add_clang_library(clangTidyReadabilityModule STATIC
   UniqueptrDeleteReleaseCheck.cpp
   UppercaseLiteralSuffixCheck.cpp
   UseAnyOfAllOfCheck.cpp
+  UseNumericLimitsCheck.cpp
   UseStdMinMaxCheck.cpp
 
   LINK_LIBS
diff --git a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp 
b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
index d61c0ba39658e..054083d25952a 100644
--- a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
@@ -60,6 +60,7 @@
 #include "UniqueptrDeleteReleaseCheck.h"
 #include "UppercaseLiteralSuffixCheck.h"
 #include "UseAnyOfAllOfCheck.h"
+#include "UseNumericLimitsCheck.h"
 #include "UseStdMinMaxCheck.h"
 
 namespace clang::tidy {
@@ -170,6 +171,8 @@ class ReadabilityModule : public ClangTidyModule {
 "readability-uppercase-literal-suffix");
 CheckFactories.registerCheck(
 "readability-use-anyofallof");
+CheckFactories.registerCheck(
+"readability-use-numeric-limits");
 CheckFactories.registerCheck(
 "readability-use-std-min-max");
   }
diff --git a/clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.cpp
new file mode 100644
index 0..b26ae008cbdd5
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.cpp
@@ -0,0 +1,135 @@
+//===--- UseNumericLimitsCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UseNumericLimitsCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Preprocessor.h"
+#include 
+#include 
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+UseNumericLimitsCheck::UseNumericLimitsCheck(StringRef Name,
+ ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  SignedConstants{
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+
+  },
+  UnsignedConstants{
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::max(),
+   "std::numeric

[clang-tools-extra] [clang-tidy] Add new check `readability-use-numeric-limits` (PR #127430)

2025-02-21 Thread Katherine Whitlock via cfe-commits


@@ -0,0 +1,164 @@
+//===--- UseNumericLimitsCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UseNumericLimitsCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Preprocessor.h"
+#include 
+#include 
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+UseNumericLimitsCheck::UseNumericLimitsCheck(StringRef Name,
+ ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  SignedConstants{
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+

stellar-aria wrote:

clang-format did not affect this, so I removed the line manually

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


[clang-tools-extra] [clang-tidy] Add new check `readability-use-numeric-limits` (PR #127430)

2025-02-21 Thread Katherine Whitlock via cfe-commits

https://github.com/stellar-aria updated 
https://github.com/llvm/llvm-project/pull/127430

>From ba0eef9808b42b01e356cd5c87745f7477e07c68 Mon Sep 17 00:00:00 2001
From: Katherine Whitlock 
Date: Sun, 16 Feb 2025 22:45:06 -0500
Subject: [PATCH 1/7] [clang-tidy] Add new check
 `readability-use-numeric-limits`

---
 .../clang-tidy/readability/CMakeLists.txt |   1 +
 .../readability/ReadabilityTidyModule.cpp |   3 +
 .../readability/UseNumericLimitsCheck.cpp | 135 ++
 .../readability/UseNumericLimitsCheck.h   |  41 ++
 clang-tools-extra/docs/ReleaseNotes.rst   |   5 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../checks/readability/use-numeric-limits.rst |  22 +++
 .../readability/use-numeric-limits.cpp|  85 +++
 8 files changed, 293 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/use-numeric-limits.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/use-numeric-limits.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index 8f303c51e1b0d..e06f68bdda73f 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -57,6 +57,7 @@ add_clang_library(clangTidyReadabilityModule STATIC
   UniqueptrDeleteReleaseCheck.cpp
   UppercaseLiteralSuffixCheck.cpp
   UseAnyOfAllOfCheck.cpp
+  UseNumericLimitsCheck.cpp
   UseStdMinMaxCheck.cpp
 
   LINK_LIBS
diff --git a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp 
b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
index d61c0ba39658e..054083d25952a 100644
--- a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
@@ -60,6 +60,7 @@
 #include "UniqueptrDeleteReleaseCheck.h"
 #include "UppercaseLiteralSuffixCheck.h"
 #include "UseAnyOfAllOfCheck.h"
+#include "UseNumericLimitsCheck.h"
 #include "UseStdMinMaxCheck.h"
 
 namespace clang::tidy {
@@ -170,6 +171,8 @@ class ReadabilityModule : public ClangTidyModule {
 "readability-uppercase-literal-suffix");
 CheckFactories.registerCheck(
 "readability-use-anyofallof");
+CheckFactories.registerCheck(
+"readability-use-numeric-limits");
 CheckFactories.registerCheck(
 "readability-use-std-min-max");
   }
diff --git a/clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.cpp
new file mode 100644
index 0..b26ae008cbdd5
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.cpp
@@ -0,0 +1,135 @@
+//===--- UseNumericLimitsCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UseNumericLimitsCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Preprocessor.h"
+#include 
+#include 
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+UseNumericLimitsCheck::UseNumericLimitsCheck(StringRef Name,
+ ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  SignedConstants{
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+
+  },
+  UnsignedConstants{
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  },
+  Inserter(Options.getLocalOrGlobal("IncludeStyle",
+utils::IncludeSorter::IS_LLVM),
+   areDiagsSelfC

[clang-tools-extra] [clang-tidy] Add new check `readability-use-numeric-limits` (PR #127430)

2025-02-21 Thread Katherine Whitlock via cfe-commits


@@ -0,0 +1,84 @@
+// RUN: %check_clang_tidy %s readability-use-numeric-limits %t
+#include 
+
+void constants() {
+  // CHECK-MESSAGES: :[[@LINE+2]]:14: warning: The constant -128 is being 
utilized. Consider using std::numeric_limits::min() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: int8_t a = std::numeric_limits::min();
+  int8_t a = -128;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:14: warning: The constant 127 is being 
utilized. Consider using std::numeric_limits::max() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: int8_t b = std::numeric_limits::max();
+  int8_t b = +127;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:14: warning: The constant 127 is being 
utilized. Consider using std::numeric_limits::max() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: int8_t c = std::numeric_limits::max();
+  int8_t c = 127;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant -32768 is being 
utilized. Consider using std::numeric_limits::min() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: int16_t d = std::numeric_limits::min();
+  int16_t d = -32768;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 32767 is being 
utilized. Consider using std::numeric_limits::max() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: int16_t e = std::numeric_limits::max();
+  int16_t e = +32767;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 32767 is being 
utilized. Consider using std::numeric_limits::max() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: int16_t f = std::numeric_limits::max();
+  int16_t f = 32767;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant -2147483648 is 
being utilized. Consider using std::numeric_limits::min() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: int32_t g = std::numeric_limits::min();
+  int32_t g = -2147483648;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 2147483647 is 
being utilized. Consider using std::numeric_limits::max() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: int32_t h = std::numeric_limits::max();
+  int32_t h = +2147483647;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 2147483647 is 
being utilized. Consider using std::numeric_limits::max() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: int32_t i = std::numeric_limits::max();
+  int32_t i = 2147483647;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 
-9223372036854775808 is being utilized. Consider using 
std::numeric_limits::min() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: int64_t j = std::numeric_limits::min();
+  int64_t j = -9223372036854775808;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 
9223372036854775807 is being utilized. Consider using 
std::numeric_limits::max() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: int64_t k = std::numeric_limits::max();
+  int64_t k = +9223372036854775807;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 
9223372036854775807 is being utilized. Consider using 
std::numeric_limits::max() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: int64_t l = std::numeric_limits::max();
+  int64_t l = 9223372036854775807;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 255 is being 
utilized. Consider using std::numeric_limits::max() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: uint8_t m = std::numeric_limits::max();
+  uint8_t m = 255;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 255 is being 
utilized. Consider using std::numeric_limits::max() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: uint8_t n = std::numeric_limits::max();
+  uint8_t n = +255;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:16: warning: The constant 65535 is being 
utilized. Consider using std::numeric_limits::max() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: uint16_t o = std::numeric_limits::max();
+  uint16_t o = 65535;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:16: warning: The constant 65535 is being 
utilized. Consider using std::numeric_limits::max() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: uint16_t p = std::numeric_limits::max();
+  uint16_t p = +65535;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:16: warning: The constant 4294967295 is 
being utilized. Consider using std::numeric_limits::max() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: uint32_t q = std::numeric_limits::max();
+  uint32_t q = 4294967295;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:16: warning: The constant 4294967295 is 
being utilized. Consider using std::numeric_limits::max() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: uint32_t r = std::numeric_limits::max();
+  uint32_t r = +4294967295;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:16: warning: The constant 
18446744073709551615 is being utilized. Consider using 
std::numeric_limits::max() instead [readabilit

[clang-tools-extra] [clang-tidy] Add new check `readability-use-numeric-limits` (PR #127430)

2025-02-22 Thread Katherine Whitlock via cfe-commits


@@ -0,0 +1,84 @@
+// RUN: %check_clang_tidy %s readability-use-numeric-limits %t
+#include 
+
+void constants() {
+  // CHECK-MESSAGES: :[[@LINE+2]]:14: warning: The constant -128 is being 
utilized. Consider using std::numeric_limits::min() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: int8_t a = std::numeric_limits::min();
+  int8_t a = -128;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:14: warning: The constant 127 is being 
utilized. Consider using std::numeric_limits::max() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: int8_t b = std::numeric_limits::max();
+  int8_t b = +127;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:14: warning: The constant 127 is being 
utilized. Consider using std::numeric_limits::max() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: int8_t c = std::numeric_limits::max();
+  int8_t c = 127;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant -32768 is being 
utilized. Consider using std::numeric_limits::min() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: int16_t d = std::numeric_limits::min();
+  int16_t d = -32768;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 32767 is being 
utilized. Consider using std::numeric_limits::max() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: int16_t e = std::numeric_limits::max();
+  int16_t e = +32767;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 32767 is being 
utilized. Consider using std::numeric_limits::max() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: int16_t f = std::numeric_limits::max();
+  int16_t f = 32767;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant -2147483648 is 
being utilized. Consider using std::numeric_limits::min() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: int32_t g = std::numeric_limits::min();
+  int32_t g = -2147483648;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 2147483647 is 
being utilized. Consider using std::numeric_limits::max() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: int32_t h = std::numeric_limits::max();
+  int32_t h = +2147483647;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 2147483647 is 
being utilized. Consider using std::numeric_limits::max() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: int32_t i = std::numeric_limits::max();
+  int32_t i = 2147483647;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 
-9223372036854775808 is being utilized. Consider using 
std::numeric_limits::min() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: int64_t j = std::numeric_limits::min();
+  int64_t j = -9223372036854775808;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 
9223372036854775807 is being utilized. Consider using 
std::numeric_limits::max() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: int64_t k = std::numeric_limits::max();
+  int64_t k = +9223372036854775807;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 
9223372036854775807 is being utilized. Consider using 
std::numeric_limits::max() instead [readability-use-numeric-limits]
+  // CHECK-FIXES: int64_t l = std::numeric_limits::max();
+  int64_t l = 9223372036854775807;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 255 is being 
utilized. Consider using std::numeric_limits::max() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: uint8_t m = std::numeric_limits::max();
+  uint8_t m = 255;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:15: warning: The constant 255 is being 
utilized. Consider using std::numeric_limits::max() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: uint8_t n = std::numeric_limits::max();
+  uint8_t n = +255;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:16: warning: The constant 65535 is being 
utilized. Consider using std::numeric_limits::max() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: uint16_t o = std::numeric_limits::max();
+  uint16_t o = 65535;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:16: warning: The constant 65535 is being 
utilized. Consider using std::numeric_limits::max() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: uint16_t p = std::numeric_limits::max();
+  uint16_t p = +65535;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:16: warning: The constant 4294967295 is 
being utilized. Consider using std::numeric_limits::max() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: uint32_t q = std::numeric_limits::max();
+  uint32_t q = 4294967295;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:16: warning: The constant 4294967295 is 
being utilized. Consider using std::numeric_limits::max() instead 
[readability-use-numeric-limits]
+  // CHECK-FIXES: uint32_t r = std::numeric_limits::max();
+  uint32_t r = +4294967295;
+
+  // CHECK-MESSAGES: :[[@LINE+2]]:16: warning: The constant 
18446744073709551615 is being utilized. Consider using 
std::numeric_limits::max() instead [readabilit

[clang-tools-extra] [clang-tidy] Add new check `readability-use-numeric-limits` (PR #127430)

2025-02-17 Thread Katherine Whitlock via cfe-commits

https://github.com/stellar-aria updated 
https://github.com/llvm/llvm-project/pull/127430

>From ba0eef9808b42b01e356cd5c87745f7477e07c68 Mon Sep 17 00:00:00 2001
From: Katherine Whitlock 
Date: Sun, 16 Feb 2025 22:45:06 -0500
Subject: [PATCH 1/2] [clang-tidy] Add new check
 `readability-use-numeric-limits`

---
 .../clang-tidy/readability/CMakeLists.txt |   1 +
 .../readability/ReadabilityTidyModule.cpp |   3 +
 .../readability/UseNumericLimitsCheck.cpp | 135 ++
 .../readability/UseNumericLimitsCheck.h   |  41 ++
 clang-tools-extra/docs/ReleaseNotes.rst   |   5 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../checks/readability/use-numeric-limits.rst |  22 +++
 .../readability/use-numeric-limits.cpp|  85 +++
 8 files changed, 293 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/use-numeric-limits.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/use-numeric-limits.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index 8f303c51e1b0d..e06f68bdda73f 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -57,6 +57,7 @@ add_clang_library(clangTidyReadabilityModule STATIC
   UniqueptrDeleteReleaseCheck.cpp
   UppercaseLiteralSuffixCheck.cpp
   UseAnyOfAllOfCheck.cpp
+  UseNumericLimitsCheck.cpp
   UseStdMinMaxCheck.cpp
 
   LINK_LIBS
diff --git a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp 
b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
index d61c0ba39658e..054083d25952a 100644
--- a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
@@ -60,6 +60,7 @@
 #include "UniqueptrDeleteReleaseCheck.h"
 #include "UppercaseLiteralSuffixCheck.h"
 #include "UseAnyOfAllOfCheck.h"
+#include "UseNumericLimitsCheck.h"
 #include "UseStdMinMaxCheck.h"
 
 namespace clang::tidy {
@@ -170,6 +171,8 @@ class ReadabilityModule : public ClangTidyModule {
 "readability-uppercase-literal-suffix");
 CheckFactories.registerCheck(
 "readability-use-anyofallof");
+CheckFactories.registerCheck(
+"readability-use-numeric-limits");
 CheckFactories.registerCheck(
 "readability-use-std-min-max");
   }
diff --git a/clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.cpp
new file mode 100644
index 0..b26ae008cbdd5
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.cpp
@@ -0,0 +1,135 @@
+//===--- UseNumericLimitsCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UseNumericLimitsCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Preprocessor.h"
+#include 
+#include 
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+UseNumericLimitsCheck::UseNumericLimitsCheck(StringRef Name,
+ ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  SignedConstants{
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+
+  },
+  UnsignedConstants{
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  },
+  Inserter(Options.getLocalOrGlobal("IncludeStyle",
+utils::IncludeSorter::IS_LLVM),
+   areDiagsSelfC

[clang-tools-extra] [clang-tidy] Add new check `readability-use-numeric-limits` (PR #127430)

2025-02-17 Thread Katherine Whitlock via cfe-commits


@@ -91,6 +91,11 @@ Improvements to clang-tidy
 New checks
 ^^
 
+- New :doc:`readability-use-numeric-limits
+  ` check to replace certain

stellar-aria wrote:

Reworded and reformatted to match 


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


[clang-tools-extra] [clang-tidy] Add new check `readability-use-numeric-limits` (PR #127430)

2025-02-17 Thread Katherine Whitlock via cfe-commits


@@ -91,6 +91,13 @@ Improvements to clang-tidy
 New checks
 ^^
 
+- New :doc:`readability-use-numeric-limits
+  ` check.
+
+  Replaces certain integer literals with equivalent calls to

stellar-aria wrote:

Fixed by matching the doc to this line

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


[clang-tools-extra] [clang-tidy] Add new check `readability-use-numeric-limits` (PR #127430)

2025-02-17 Thread Katherine Whitlock via cfe-commits

https://github.com/stellar-aria updated 
https://github.com/llvm/llvm-project/pull/127430

>From ba0eef9808b42b01e356cd5c87745f7477e07c68 Mon Sep 17 00:00:00 2001
From: Katherine Whitlock 
Date: Sun, 16 Feb 2025 22:45:06 -0500
Subject: [PATCH 1/3] [clang-tidy] Add new check
 `readability-use-numeric-limits`

---
 .../clang-tidy/readability/CMakeLists.txt |   1 +
 .../readability/ReadabilityTidyModule.cpp |   3 +
 .../readability/UseNumericLimitsCheck.cpp | 135 ++
 .../readability/UseNumericLimitsCheck.h   |  41 ++
 clang-tools-extra/docs/ReleaseNotes.rst   |   5 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../checks/readability/use-numeric-limits.rst |  22 +++
 .../readability/use-numeric-limits.cpp|  85 +++
 8 files changed, 293 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/use-numeric-limits.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/use-numeric-limits.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index 8f303c51e1b0d..e06f68bdda73f 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -57,6 +57,7 @@ add_clang_library(clangTidyReadabilityModule STATIC
   UniqueptrDeleteReleaseCheck.cpp
   UppercaseLiteralSuffixCheck.cpp
   UseAnyOfAllOfCheck.cpp
+  UseNumericLimitsCheck.cpp
   UseStdMinMaxCheck.cpp
 
   LINK_LIBS
diff --git a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp 
b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
index d61c0ba39658e..054083d25952a 100644
--- a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
@@ -60,6 +60,7 @@
 #include "UniqueptrDeleteReleaseCheck.h"
 #include "UppercaseLiteralSuffixCheck.h"
 #include "UseAnyOfAllOfCheck.h"
+#include "UseNumericLimitsCheck.h"
 #include "UseStdMinMaxCheck.h"
 
 namespace clang::tidy {
@@ -170,6 +171,8 @@ class ReadabilityModule : public ClangTidyModule {
 "readability-uppercase-literal-suffix");
 CheckFactories.registerCheck(
 "readability-use-anyofallof");
+CheckFactories.registerCheck(
+"readability-use-numeric-limits");
 CheckFactories.registerCheck(
 "readability-use-std-min-max");
   }
diff --git a/clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.cpp
new file mode 100644
index 0..b26ae008cbdd5
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.cpp
@@ -0,0 +1,135 @@
+//===--- UseNumericLimitsCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UseNumericLimitsCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Preprocessor.h"
+#include 
+#include 
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+UseNumericLimitsCheck::UseNumericLimitsCheck(StringRef Name,
+ ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  SignedConstants{
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+
+  },
+  UnsignedConstants{
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  },
+  Inserter(Options.getLocalOrGlobal("IncludeStyle",
+utils::IncludeSorter::IS_LLVM),
+   areDiagsSelfC

[clang-tools-extra] [clang-tidy] Add new check `readability-use-numeric-limits` (PR #127430)

2025-02-18 Thread Katherine Whitlock via cfe-commits

https://github.com/stellar-aria updated 
https://github.com/llvm/llvm-project/pull/127430

>From ba0eef9808b42b01e356cd5c87745f7477e07c68 Mon Sep 17 00:00:00 2001
From: Katherine Whitlock 
Date: Sun, 16 Feb 2025 22:45:06 -0500
Subject: [PATCH 1/6] [clang-tidy] Add new check
 `readability-use-numeric-limits`

---
 .../clang-tidy/readability/CMakeLists.txt |   1 +
 .../readability/ReadabilityTidyModule.cpp |   3 +
 .../readability/UseNumericLimitsCheck.cpp | 135 ++
 .../readability/UseNumericLimitsCheck.h   |  41 ++
 clang-tools-extra/docs/ReleaseNotes.rst   |   5 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../checks/readability/use-numeric-limits.rst |  22 +++
 .../readability/use-numeric-limits.cpp|  85 +++
 8 files changed, 293 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/use-numeric-limits.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/use-numeric-limits.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index 8f303c51e1b0d..e06f68bdda73f 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -57,6 +57,7 @@ add_clang_library(clangTidyReadabilityModule STATIC
   UniqueptrDeleteReleaseCheck.cpp
   UppercaseLiteralSuffixCheck.cpp
   UseAnyOfAllOfCheck.cpp
+  UseNumericLimitsCheck.cpp
   UseStdMinMaxCheck.cpp
 
   LINK_LIBS
diff --git a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp 
b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
index d61c0ba39658e..054083d25952a 100644
--- a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
@@ -60,6 +60,7 @@
 #include "UniqueptrDeleteReleaseCheck.h"
 #include "UppercaseLiteralSuffixCheck.h"
 #include "UseAnyOfAllOfCheck.h"
+#include "UseNumericLimitsCheck.h"
 #include "UseStdMinMaxCheck.h"
 
 namespace clang::tidy {
@@ -170,6 +171,8 @@ class ReadabilityModule : public ClangTidyModule {
 "readability-uppercase-literal-suffix");
 CheckFactories.registerCheck(
 "readability-use-anyofallof");
+CheckFactories.registerCheck(
+"readability-use-numeric-limits");
 CheckFactories.registerCheck(
 "readability-use-std-min-max");
   }
diff --git a/clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.cpp
new file mode 100644
index 0..b26ae008cbdd5
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.cpp
@@ -0,0 +1,135 @@
+//===--- UseNumericLimitsCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UseNumericLimitsCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Preprocessor.h"
+#include 
+#include 
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+UseNumericLimitsCheck::UseNumericLimitsCheck(StringRef Name,
+ ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  SignedConstants{
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+
+  },
+  UnsignedConstants{
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  },
+  Inserter(Options.getLocalOrGlobal("IncludeStyle",
+utils::IncludeSorter::IS_LLVM),
+   areDiagsSelfC

[clang-tools-extra] [clang-tidy] Add new check `readability-use-numeric-limits` (PR #127430)

2025-02-18 Thread Katherine Whitlock via cfe-commits


@@ -0,0 +1,41 @@
+//===--- UseNumericLimitsCheck.h - clang-tidy ---*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_USENUMERICLIMITSCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_USENUMERICLIMITSCHECK_H
+
+#include "../ClangTidyCheck.h"
+#include "../utils/IncludeInserter.h"
+#include 
+
+namespace clang::tidy::readability {
+
+/// Replaces certain integer literals with equivalent calls to
+/// ``std::numeric_limits``.
+/// For the user-facing documentation see:
+/// 
http://clang.llvm.org/extra/clang-tidy/checks/readability/use-numeric-limits.html
+class UseNumericLimitsCheck : public ClangTidyCheck {
+public:
+  UseNumericLimitsCheck(StringRef Name, ClangTidyContext *Context);
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
+   Preprocessor *ModuleExpanderPP) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
+return LangOpts.CPlusPlus11;

stellar-aria wrote:

Removed this line, which I assume drops it down to 98 based on what I could 
find in the docs?

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


[clang-tools-extra] [clang-tidy] Add new check `readability-use-numeric-limits` (PR #127430)

2025-02-18 Thread Katherine Whitlock via cfe-commits


@@ -0,0 +1,23 @@
+.. title:: clang-tidy - readability-use-numeric-limits
+
+readability-use-numeric-limits
+==
+
+ Replaces certain integer literals with equivalent calls to
+ ``std::numeric_limits::min()`` or ``std::numeric_limits::max()``.
+
+Before:
+
+.. code-block:: c++
+
+  void foo() {
+int32_t a = 2147483647;
+  }
+
+After:
+
+.. code-block:: c++
+
+  void foo() {
+int32_t a = std::numeric_limits::max();
+  }

stellar-aria wrote:

Added below, matching `modernize/replace-auto-ptr.rst`, and added the relevant 
`storeOptions` override to the check.

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


[clang-tools-extra] [clang-tidy] Add new check `readability-use-numeric-limits` (PR #127430)

2025-03-02 Thread Katherine Whitlock via cfe-commits


@@ -0,0 +1,163 @@
+//===--- UseNumericLimitsCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UseNumericLimitsCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Preprocessor.h"
+#include 
+#include 
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+UseNumericLimitsCheck::UseNumericLimitsCheck(StringRef Name,
+ ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  SignedConstants{
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  },
+  UnsignedConstants{
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  },
+  Inserter(Options.getLocalOrGlobal("IncludeStyle",
+utils::IncludeSorter::IS_LLVM),
+   areDiagsSelfContained()) {}
+
+void UseNumericLimitsCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+  Options.store(Opts, "IncludeStyle", Inserter.getStyle());
+}
+
+void UseNumericLimitsCheck::registerMatchers(MatchFinder *Finder) {
+  auto PositiveIntegerMatcher = [](auto Value) {
+return unaryOperator(
+hasOperatorName("+"),
+hasUnaryOperand(
+integerLiteral(equals(Value)).bind("positive-integer-literal")));
+  };
+
+  auto NegativeIntegerMatcher = [](auto Value) {
+return unaryOperator(
+hasOperatorName("-"),
+hasUnaryOperand(
+integerLiteral(equals(-Value)).bind("negative-integer-literal")));
+  };
+
+  auto BareIntegerMatcher = [](auto Value) {
+return integerLiteral(allOf(unless(hasParent(unaryOperator(
+hasAnyOperatorName("-", "+",
+equals(Value)))
+.bind("bare-integer-literal");
+  };
+
+  for (const auto &[Value, _] : SignedConstants) {
+if (Value < 0) {
+  Finder->addMatcher(
+  expr(NegativeIntegerMatcher(Value)).bind("unary-op-exp"), this);
+} else {
+  Finder->addMatcher(
+  expr(anyOf(PositiveIntegerMatcher(Value), BareIntegerMatcher(Value)))
+  .bind("unary-op-exp"),
+  this);
+}
+  }
+
+  for (const auto &[Value, _] : UnsignedConstants) {
+Finder->addMatcher(
+expr(anyOf(PositiveIntegerMatcher(Value), BareIntegerMatcher(Value)))
+.bind("unary-op-exp"),
+this);
+  }
+}
+
+void UseNumericLimitsCheck::registerPPCallbacks(
+const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) 
{
+  Inserter.registerPreprocessor(PP);
+}
+
+void UseNumericLimitsCheck::check(const MatchFinder::MatchResult &Result) {
+  const IntegerLiteral *MatchedDecl = nullptr;
+
+  const IntegerLiteral *NegativeMatchedDecl =
+  Result.Nodes.getNodeAs("negative-integer-literal");
+  const IntegerLiteral *PositiveMatchedDecl =
+  Result.Nodes.getNodeAs("positive-integer-literal");
+  const IntegerLiteral *BareMatchedDecl =
+  Result.Nodes.getNodeAs("bare-integer-literal");
+
+  if (NegativeMatchedDecl != nullptr) {
+MatchedDecl = NegativeMatchedDecl;
+  } else if (PositiveMatchedDecl != nullptr) {
+MatchedDecl = PositiveMatchedDecl;
+  } else if (BareMatchedDecl != nullptr) {
+MatchedDecl = BareMatchedDecl;
+  }
+
+  const llvm::APInt MatchedIntegerConstant = MatchedDecl->getValue();
+
+  auto Fixer = [&](auto SourceValue, auto Value,
+   const std::string &Replacement) {

stellar-aria wrote:

if I use a template parameter list as in
```c++
auto Fixer = [&](ValueType SourceValue, ValueType Value, ...
```
I get a warning that explicit template parameter lists for lambdas are a C++20 
extension?

Using a static_assert like
```c++
static_assert(std::is_same_v);
```
and/or an enable_if a

[clang-tools-extra] [clang-tidy] Add new check `readability-use-numeric-limits` (PR #127430)

2025-02-28 Thread Katherine Whitlock via cfe-commits

https://github.com/stellar-aria updated 
https://github.com/llvm/llvm-project/pull/127430

>From ba0eef9808b42b01e356cd5c87745f7477e07c68 Mon Sep 17 00:00:00 2001
From: Katherine Whitlock 
Date: Sun, 16 Feb 2025 22:45:06 -0500
Subject: [PATCH 01/10] [clang-tidy] Add new check
 `readability-use-numeric-limits`

---
 .../clang-tidy/readability/CMakeLists.txt |   1 +
 .../readability/ReadabilityTidyModule.cpp |   3 +
 .../readability/UseNumericLimitsCheck.cpp | 135 ++
 .../readability/UseNumericLimitsCheck.h   |  41 ++
 clang-tools-extra/docs/ReleaseNotes.rst   |   5 +
 .../docs/clang-tidy/checks/list.rst   |   1 +
 .../checks/readability/use-numeric-limits.rst |  22 +++
 .../readability/use-numeric-limits.cpp|  85 +++
 8 files changed, 293 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/readability/use-numeric-limits.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/use-numeric-limits.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt 
b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index 8f303c51e1b0d..e06f68bdda73f 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -57,6 +57,7 @@ add_clang_library(clangTidyReadabilityModule STATIC
   UniqueptrDeleteReleaseCheck.cpp
   UppercaseLiteralSuffixCheck.cpp
   UseAnyOfAllOfCheck.cpp
+  UseNumericLimitsCheck.cpp
   UseStdMinMaxCheck.cpp
 
   LINK_LIBS
diff --git a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp 
b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
index d61c0ba39658e..054083d25952a 100644
--- a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
@@ -60,6 +60,7 @@
 #include "UniqueptrDeleteReleaseCheck.h"
 #include "UppercaseLiteralSuffixCheck.h"
 #include "UseAnyOfAllOfCheck.h"
+#include "UseNumericLimitsCheck.h"
 #include "UseStdMinMaxCheck.h"
 
 namespace clang::tidy {
@@ -170,6 +171,8 @@ class ReadabilityModule : public ClangTidyModule {
 "readability-uppercase-literal-suffix");
 CheckFactories.registerCheck(
 "readability-use-anyofallof");
+CheckFactories.registerCheck(
+"readability-use-numeric-limits");
 CheckFactories.registerCheck(
 "readability-use-std-min-max");
   }
diff --git a/clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.cpp
new file mode 100644
index 0..b26ae008cbdd5
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/UseNumericLimitsCheck.cpp
@@ -0,0 +1,135 @@
+//===--- UseNumericLimitsCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UseNumericLimitsCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Preprocessor.h"
+#include 
+#include 
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+UseNumericLimitsCheck::UseNumericLimitsCheck(StringRef Name,
+ ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  SignedConstants{
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+
+  },
+  UnsignedConstants{
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  },
+  Inserter(Options.getLocalOrGlobal("IncludeStyle",
+utils::IncludeSorter::IS_LLVM),
+   areDiagsSel

[clang-tools-extra] [clang-tidy] Add new check `readability-use-numeric-limits` (PR #127430)

2025-02-28 Thread Katherine Whitlock via cfe-commits


@@ -0,0 +1,163 @@
+//===--- UseNumericLimitsCheck.cpp - clang-tidy 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "UseNumericLimitsCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Preprocessor.h"
+#include 
+#include 
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+UseNumericLimitsCheck::UseNumericLimitsCheck(StringRef Name,
+ ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  SignedConstants{
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::min(),
+   "std::numeric_limits::min()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  },
+  UnsignedConstants{
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  {std::numeric_limits::max(),
+   "std::numeric_limits::max()"},
+  },
+  Inserter(Options.getLocalOrGlobal("IncludeStyle",
+utils::IncludeSorter::IS_LLVM),
+   areDiagsSelfContained()) {}
+
+void UseNumericLimitsCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+  Options.store(Opts, "IncludeStyle", Inserter.getStyle());
+}
+
+void UseNumericLimitsCheck::registerMatchers(MatchFinder *Finder) {
+  auto PositiveIntegerMatcher = [](auto Value) {
+return unaryOperator(
+hasOperatorName("+"),
+hasUnaryOperand(
+integerLiteral(equals(Value)).bind("positive-integer-literal")));
+  };
+
+  auto NegativeIntegerMatcher = [](auto Value) {
+return unaryOperator(
+hasOperatorName("-"),
+hasUnaryOperand(
+integerLiteral(equals(-Value)).bind("negative-integer-literal")));
+  };
+
+  auto BareIntegerMatcher = [](auto Value) {
+return integerLiteral(allOf(unless(hasParent(unaryOperator(
+hasAnyOperatorName("-", "+",
+equals(Value)))
+.bind("bare-integer-literal");
+  };
+
+  for (const auto &[Value, _] : SignedConstants) {
+if (Value < 0) {
+  Finder->addMatcher(
+  expr(NegativeIntegerMatcher(Value)).bind("unary-op-exp"), this);
+} else {
+  Finder->addMatcher(
+  expr(anyOf(PositiveIntegerMatcher(Value), BareIntegerMatcher(Value)))
+  .bind("unary-op-exp"),
+  this);
+}
+  }
+
+  for (const auto &[Value, _] : UnsignedConstants) {
+Finder->addMatcher(
+expr(anyOf(PositiveIntegerMatcher(Value), BareIntegerMatcher(Value)))
+.bind("unary-op-exp"),
+this);
+  }
+}
+
+void UseNumericLimitsCheck::registerPPCallbacks(
+const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) 
{
+  Inserter.registerPreprocessor(PP);
+}
+
+void UseNumericLimitsCheck::check(const MatchFinder::MatchResult &Result) {
+  const IntegerLiteral *MatchedDecl = nullptr;
+
+  const IntegerLiteral *NegativeMatchedDecl =
+  Result.Nodes.getNodeAs("negative-integer-literal");
+  const IntegerLiteral *PositiveMatchedDecl =
+  Result.Nodes.getNodeAs("positive-integer-literal");
+  const IntegerLiteral *BareMatchedDecl =
+  Result.Nodes.getNodeAs("bare-integer-literal");
+
+  if (NegativeMatchedDecl != nullptr) {
+MatchedDecl = NegativeMatchedDecl;
+  } else if (PositiveMatchedDecl != nullptr) {
+MatchedDecl = PositiveMatchedDecl;
+  } else if (BareMatchedDecl != nullptr) {
+MatchedDecl = BareMatchedDecl;
+  }
+
+  const llvm::APInt MatchedIntegerConstant = MatchedDecl->getValue();
+
+  auto Fixer = [&](auto SourceValue, auto Value,
+   const std::string &Replacement) {
+SourceLocation Location = MatchedDecl->getExprLoc();
+SourceRange Range{MatchedDecl->getBeginLoc(), MatchedDecl->getEndLoc()};
+
+// Only valid if unary operator is present
+const UnaryOperator *UnaryOpExpr =
+Result.Nodes.getNodeAs("unary-op-exp");

stellar-aria wrote:

Fixed by moving "unary-o

[clang-tools-extra] [clang-tidy] Add new check `readability-use-numeric-limits` (PR #127430)

2025-02-28 Thread Katherine Whitlock via cfe-commits

stellar-aria wrote:

> What is the expected behavior of `+128` `-127`?

I feel like they shouldn't be matched, since with opposite signs they're not 
really within the set of limits? It just feels unintuitive to me for `-127` to 
be replaced by something like `-std::numeric_limits::max()`. I've added 
test cases for this, but I'm open to other possibilities as well. 

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