[clang-tools-extra] [clang-tidy] add modernize-use-std-numbers (PR #66583)
5chmidti wrote: Thanks https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
PiotrZSL wrote: I will compile this locally and try to fix it & recommit. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
5chmidti wrote: @PiotrZSL check out https://lab.llvm.org/buildbot/#/builders/230/builds/6, the bot was red on previous runs, but those are unrelated to how it fails now. The bot fails for this patch with the following build failure: ``` /home/buildbots/ppc64le-clang-test-suite/clang-ppc64le-test-suite/llvm-project/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp: In member function ‘virtual void clang::tidy::modernize::UseStdNumbersCheck::registerMatchers(clang::ast_matchers::MatchFinder*)’: /home/buildbots/ppc64le-clang-test-suite/clang-ppc64le-test-suite/llvm-project/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp:319:27: error: invalid initialization of reference of type ‘const llvm::ArrayRef >&’ from expression of type ‘const std::initializer_list >’ anyOfExhaustive(ConstantMatchers), ^~~~ In file included from /home/buildbots/ppc64le-clang-test-suite/clang-ppc64le-test-suite/llvm-project/clang/include/clang/ASTMatchers/ASTMatchers.h:73, from /home/buildbots/ppc64le-clang-test-suite/clang-ppc64le-test-suite/llvm-project/clang/include/clang/ASTMatchers/ASTMatchFinder.h:43, from /home/buildbots/ppc64le-clang-test-suite/clang-ppc64le-test-suite/llvm-project/clang-tools-extra/clang-tidy/modernize/../ClangTidyCheck.h:14, from /home/buildbots/ppc64le-clang-test-suite/clang-ppc64le-test-suite/llvm-project/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.h:12, from /home/buildbots/ppc64le-clang-test-suite/clang-ppc64le-test-suite/llvm-project/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp:9: /home/buildbots/ppc64le-clang-test-suite/clang-ppc64le-test-suite/llvm-project/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp:64:28: note: in passing argument 1 of ‘clang::ast_matchers::internal::Matcher {anonymous}::anyOfExhaustive(const llvm::ArrayRef >&)’ AST_MATCHER_P(clang::Expr, anyOfExhaustive, ^~~ /home/buildbots/ppc64le-clang-test-suite/clang-ppc64le-test-suite/llvm-project/clang/include/clang/ASTMatchers/ASTMatchersMacros.h:149:57: note: in definition of macro ‘AST_MATCHER_P_OVERLOAD’ inline ::clang::ast_matchers::internal::Matcher DefineMatcher( \ ^ /home/buildbots/ppc64le-clang-test-suite/clang-ppc64le-test-suite/llvm-project/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp:64:1: note: in expansion of macro ‘AST_MATCHER_P’ AST_MATCHER_P(clang::Expr, anyOfExhaustive, ^ ``` The mismatch happens for the template arguments of `ArrayRef` and `initializer_list` in `anyOfExhaustive`: `const llvm::ArrayRef >&` `const std::initializer_list >` I think the easiest fix is to add `const` to the `ArrayRef` in `anyOfExhaustive` like so: ```c++ AST_MATCHER_P(clang::Expr, anyOfExhaustive, llvm::ArrayRef>, Exprs) { ``` https://github.com/llvm/llvm-project/blob/65cb5d58eb5783c17fd2121db79ebac950b767b2/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp#L64-L75 FYI I don't have write access, so I cannot push a fix myself. Or just revert? What do you think? https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
5chmidti wrote: Thank you for the reviews https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
https://github.com/PiotrZSL closed https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -0,0 +1,73 @@ +.. title:: clang-tidy - modernize-use-std-numbers + +modernize-use-std-numbers += + +Finds constants and function calls to math functions that can be replaced +with c++20's mathematical constants from the ``numbers`` header and offers +fix-it hints. +Does not match the use of variables with that value, and instead, +offers a replacement at the definition of those variables. +Function calls that match the pattern of how the constant is calculated are +matched and replaced with the ``std::numbers`` constant. +The use of macros gets replaced with the corresponding ``std::numbers`` +constant, instead of changing the macro definition. + +The following list of constants from the ``numbers`` header are supported: + +* e +* log2e +* log10e +* pi +* inv_pi +* inv_sqrtpi +* ln2 +* ln10 +* sqrt2 +* sqrt3 +* inv_sqrt3 +* egamma +* phi + +The list currently includes all constants as of C++20. + +The replacements use the type of the matched constant and can remove explicit casts, +i.e., switching between ``std::numbers::e``, ``std::numbers::e_v`` and ``std::numbers::e_v`` 5chmidti wrote: done https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -0,0 +1,73 @@ +.. title:: clang-tidy - modernize-use-std-numbers + +modernize-use-std-numbers += + +Finds constants and function calls to math functions that can be replaced +with c++20's mathematical constants from the ``numbers`` header and offers +fix-it hints. +Does not match the use of variables with that value, and instead, +offers a replacement at the definition of those variables. +Function calls that match the pattern of how the constant is calculated are +matched and replaced with the ``std::numbers`` constant. +The use of macros gets replaced with the corresponding ``std::numbers`` +constant, instead of changing the macro definition. + +The following list of constants from the ``numbers`` header are supported: + +* e 5chmidti wrote: done https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -0,0 +1,73 @@ +.. title:: clang-tidy - modernize-use-std-numbers + +modernize-use-std-numbers += + +Finds constants and function calls to math functions that can be replaced +with c++20's mathematical constants from the ``numbers`` header and offers 5chmidti wrote: done https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -193,6 +193,13 @@ New checks replacing with ``starts_with`` when the method exists in the class. Notably, this will work with ``std::string`` and ``std::string_view``. +- New :doc:`modernize-use-std-numbers + ` check. + + Finds constants and function calls to math functions that can be replaced + with c++20's mathematical constants from the ``numbers`` header and 5chmidti wrote: Thx, done https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -5,7 +5,7 @@ modernize-use-std-numbers Finds constants and function calls to math functions that can be replaced with c++20's mathematical constants from the ``numbers`` header and offers fix-it hints. -Does not match the use of variables or macros with that value and instead, offers a replacement +Does not match the use of variables or macros with that value, and instead, offers a replacement 5chmidti wrote: This looks like an older review comment from your previous review was probably saved by GitHub. The comma exists in the current version and the column limit is not exceeded. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -193,6 +193,13 @@ New checks replacing with ``starts_with`` when the method exists in the class. Notably, this will work with ``std::string`` and ``std::string_view``. +- New :doc:`modernize-use-std-numbers + ` check. + + Finds constants and function calls to math functions that can be replaced + with c++20's mathematical constants from the ``numbers`` header and EugeneZelenko wrote: Uppercase `C`. See https://en.cppreference.com. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -193,6 +193,13 @@ New checks replacing with ``starts_with`` when the method exists in the class. Notably, this will work with ``std::string`` and ``std::string_view``. +- New :doc:`modernize-use-std-numbers + ` check. + + Finds constants and function calls to math functions that can be replaced + with c++20's mathematical constants from the ``numbers`` header and 5chmidti wrote: Do you just want the `C` to be big, or do you want the version inside of single or double `? https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -0,0 +1,73 @@ +.. title:: clang-tidy - modernize-use-std-numbers + +modernize-use-std-numbers += + +Finds constants and function calls to math functions that can be replaced +with c++20's mathematical constants from the ``numbers`` header and offers +fix-it hints. +Does not match the use of variables with that value, and instead, +offers a replacement at the definition of those variables. +Function calls that match the pattern of how the constant is calculated are +matched and replaced with the ``std::numbers`` constant. +The use of macros gets replaced with the corresponding ``std::numbers`` +constant, instead of changing the macro definition. + +The following list of constants from the ``numbers`` header are supported: + +* e +* log2e +* log10e +* pi +* inv_pi +* inv_sqrtpi +* ln2 +* ln10 +* sqrt2 +* sqrt3 +* inv_sqrt3 +* egamma +* phi + +The list currently includes all constants as of C++20. + +The replacements use the type of the matched constant and can remove explicit casts, +i.e., switching between ``std::numbers::e``, ``std::numbers::e_v`` and ``std::numbers::e_v`` EugeneZelenko wrote: Please follow 8- characters limit. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -0,0 +1,73 @@ +.. title:: clang-tidy - modernize-use-std-numbers + +modernize-use-std-numbers += + +Finds constants and function calls to math functions that can be replaced +with c++20's mathematical constants from the ``numbers`` header and offers EugeneZelenko wrote: Ditto. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -0,0 +1,73 @@ +.. title:: clang-tidy - modernize-use-std-numbers + +modernize-use-std-numbers += + +Finds constants and function calls to math functions that can be replaced +with c++20's mathematical constants from the ``numbers`` header and offers +fix-it hints. +Does not match the use of variables with that value, and instead, +offers a replacement at the definition of those variables. +Function calls that match the pattern of how the constant is calculated are +matched and replaced with the ``std::numbers`` constant. +The use of macros gets replaced with the corresponding ``std::numbers`` +constant, instead of changing the macro definition. + +The following list of constants from the ``numbers`` header are supported: + +* e EugeneZelenko wrote: Please enclose each constant in double back-ticks. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -5,7 +5,7 @@ modernize-use-std-numbers Finds constants and function calls to math functions that can be replaced with c++20's mathematical constants from the ``numbers`` header and offers fix-it hints. -Does not match the use of variables or macros with that value and instead, offers a replacement +Does not match the use of variables or macros with that value, and instead, offers a replacement EugeneZelenko wrote: Please follow 80 characters limit. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
https://github.com/EugeneZelenko edited https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -193,6 +193,13 @@ New checks replacing with ``starts_with`` when the method exists in the class. Notably, this will work with ``std::string`` and ``std::string_view``. +- New :doc:`modernize-use-std-numbers + ` check. + + Finds constants and function calls to math functions that can be replaced + with c++20's mathematical constants from the ``numbers`` header and EugeneZelenko wrote: `C++20`. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
https://github.com/EugeneZelenko requested changes to this pull request. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -0,0 +1,74 @@ +.. title:: clang-tidy - modernize-use-std-numbers + +modernize-use-std-numbers += + +Finds constants and function calls to math functions that can be replaced +with c++20's mathematical constants from the ``numbers`` header and offers +fix-it hints. +Does not match the use of variables with that value, and instead, +offers a replacement at the definition of those variables. +Function calls that match the pattern of how the constant is calculated are +matched and replaced with the ``std::numbers`` constant. +The use of macros gets replaced with the corresponding ``std::numbers`` +constant, instead of changing the macro definition. + +The following list of constants from the ``numbers`` header are supported: + +* e +* log2e +* log10e +* pi +* inv_pi +* inv_sqrtpi +* ln2 +* ln10 +* sqrt2 +* sqrt3 +* inv_sqrt3 +* egamma +* phi + +The list currently includes all constants as of C++20. + +The replacements use the type of the matched constant and can remove explicit casts, +i.e., switching between ``std::numbers::e``, ``std::numbers::e_v`` and ``std::numbers::e_v`` +where appropriate. + +.. code-block:: c++ + +double sqrt(double); +double log2(double); +void sink(auto&&) {} +void floatSink(float); + +#define MY_PI 3.1415926 + +void foo() { +const double Pi = 3.141592653589; // const double Pi = std::numbers::pi +const auto Use = Pi / 2;// no match for Pi +static constexpr double Euler = 2.7182818; // static constexpr double Euler = std::numbers::e; + +log2(exp(1)); // std::numbers::log2e; +log2(Euler);// std::numbers::log2e; +1 / sqrt(MY_PI);// std::numbers::inv_sqrtpi; +sink(MY_PI);// sink(std::numbers::pi); +floatSink(MY_PI); // floatSink(std::numbers::pi); +floatSink(static_cast(MY_PI)); // floatSink(std::numbers::pi_v); +} + +Options +--- + +.. option:: DiffThreshold + +A floating point value that sets the detection threshold for when literals +match a constant. +A literal matches a constant if +``abs(literal - constant) < DiffThreshold`` evaluates to true. 5chmidti wrote: meant to write done, not click resolve https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -0,0 +1,74 @@ +.. title:: clang-tidy - modernize-use-std-numbers + +modernize-use-std-numbers += + +Finds constants and function calls to math functions that can be replaced +with c++20's mathematical constants from the ``numbers`` header and offers +fix-it hints. +Does not match the use of variables with that value, and instead, +offers a replacement at the definition of those variables. +Function calls that match the pattern of how the constant is calculated are +matched and replaced with the ``std::numbers`` constant. +The use of macros gets replaced with the corresponding ``std::numbers`` +constant, instead of changing the macro definition. + +The following list of constants from the ``numbers`` header are supported: + +* e +* log2e +* log10e +* pi +* inv_pi +* inv_sqrtpi +* ln2 +* ln10 +* sqrt2 +* sqrt3 +* inv_sqrt3 +* egamma +* phi + +The list currently includes all constants as of C++20. + +The replacements use the type of the matched constant and can remove explicit casts, +i.e., switching between ``std::numbers::e``, ``std::numbers::e_v`` and ``std::numbers::e_v`` +where appropriate. + +.. code-block:: c++ + +double sqrt(double); +double log2(double); +void sink(auto&&) {} +void floatSink(float); + +#define MY_PI 3.1415926 + +void foo() { +const double Pi = 3.141592653589; // const double Pi = std::numbers::pi +const auto Use = Pi / 2;// no match for Pi +static constexpr double Euler = 2.7182818; // static constexpr double Euler = std::numbers::e; + +log2(exp(1)); // std::numbers::log2e; +log2(Euler);// std::numbers::log2e; +1 / sqrt(MY_PI);// std::numbers::inv_sqrtpi; +sink(MY_PI);// sink(std::numbers::pi); +floatSink(MY_PI); // floatSink(std::numbers::pi); +floatSink(static_cast(MY_PI)); // floatSink(std::numbers::pi_v); +} + +Options +--- + +.. option:: DiffThreshold + +A floating point value that sets the detection threshold for when literals +match a constant. +A literal matches a constant if 5chmidti wrote: meant to write done, not click resolve https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
5chmidti wrote: That email is fine with me https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -0,0 +1,74 @@ +.. title:: clang-tidy - modernize-use-std-numbers + +modernize-use-std-numbers += + +Finds constants and function calls to math functions that can be replaced +with c++20's mathematical constants from the ``numbers`` header and offers +fix-it hints. +Does not match the use of variables with that value, and instead, +offers a replacement at the definition of those variables. +Function calls that match the pattern of how the constant is calculated are +matched and replaced with the ``std::numbers`` constant. +The use of macros gets replaced with the corresponding ``std::numbers`` +constant, instead of changing the macro definition. + +The following list of constants from the ``numbers`` header are supported: + +* e +* log2e +* log10e +* pi +* inv_pi +* inv_sqrtpi +* ln2 +* ln10 +* sqrt2 +* sqrt3 +* inv_sqrt3 +* egamma +* phi + +The list currently includes all constants as of C++20. + +The replacements use the type of the matched constant and can remove explicit casts, +i.e., switching between ``std::numbers::e``, ``std::numbers::e_v`` and ``std::numbers::e_v`` +where appropriate. + +.. code-block:: c++ + +double sqrt(double); +double log2(double); +void sink(auto&&) {} +void floatSink(float); + +#define MY_PI 3.1415926 + +void foo() { +const double Pi = 3.141592653589; // const double Pi = std::numbers::pi +const auto Use = Pi / 2;// no match for Pi +static constexpr double Euler = 2.7182818; // static constexpr double Euler = std::numbers::e; + +log2(exp(1)); // std::numbers::log2e; +log2(Euler);// std::numbers::log2e; +1 / sqrt(MY_PI);// std::numbers::inv_sqrtpi; +sink(MY_PI);// sink(std::numbers::pi); +floatSink(MY_PI); // floatSink(std::numbers::pi); +floatSink(static_cast(MY_PI)); // floatSink(std::numbers::pi_v); +} + +Options +--- + +.. option:: DiffThreshold + +A floating point value that sets the detection threshold for when literals +match a constant. +A literal matches a constant if +``abs(literal - constant) < DiffThreshold`` evaluates to true. PiotrZSL wrote: true -> `true` https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
https://github.com/PiotrZSL approved this pull request. Unless you want this to be merged as 44101708+5chmi...@users.noreply.github.com, correct your github settings, squash all commits into one and make sure that author of commit is correct. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -0,0 +1,74 @@ +.. title:: clang-tidy - modernize-use-std-numbers + +modernize-use-std-numbers += + +Finds constants and function calls to math functions that can be replaced +with c++20's mathematical constants from the ``numbers`` header and offers +fix-it hints. +Does not match the use of variables with that value, and instead, +offers a replacement at the definition of those variables. +Function calls that match the pattern of how the constant is calculated are +matched and replaced with the ``std::numbers`` constant. +The use of macros gets replaced with the corresponding ``std::numbers`` +constant, instead of changing the macro definition. + +The following list of constants from the ``numbers`` header are supported: + +* e +* log2e +* log10e +* pi +* inv_pi +* inv_sqrtpi +* ln2 +* ln10 +* sqrt2 +* sqrt3 +* inv_sqrt3 +* egamma +* phi + +The list currently includes all constants as of C++20. + +The replacements use the type of the matched constant and can remove explicit casts, +i.e., switching between ``std::numbers::e``, ``std::numbers::e_v`` and ``std::numbers::e_v`` +where appropriate. + +.. code-block:: c++ + +double sqrt(double); +double log2(double); +void sink(auto&&) {} +void floatSink(float); + +#define MY_PI 3.1415926 + +void foo() { +const double Pi = 3.141592653589; // const double Pi = std::numbers::pi +const auto Use = Pi / 2;// no match for Pi +static constexpr double Euler = 2.7182818; // static constexpr double Euler = std::numbers::e; + +log2(exp(1)); // std::numbers::log2e; +log2(Euler);// std::numbers::log2e; +1 / sqrt(MY_PI);// std::numbers::inv_sqrtpi; +sink(MY_PI);// sink(std::numbers::pi); +floatSink(MY_PI); // floatSink(std::numbers::pi); +floatSink(static_cast(MY_PI)); // floatSink(std::numbers::pi_v); +} + +Options +--- + +.. option:: DiffThreshold + +A floating point value that sets the detection threshold for when literals +match a constant. +A literal matches a constant if PiotrZSL wrote: Continue in previous line. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
https://github.com/PiotrZSL edited https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
5chmidti wrote: I fetched from the wrong remote. Conflicts are resolved. The options docs were added in 284fa1b94ef72c29f13a2ec50ca76b429c842b0c https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
5chmidti wrote: Force push to resolve conflicts https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -0,0 +1,58 @@ +.. title:: clang-tidy - modernize-use-std-numbers + +modernize-use-std-numbers += + +Finds constants and function calls to math functions that can be replaced +with c++20's mathematical constants from the ``numbers`` header and offers +fix-it hints. +Does not match the use of variables with that value, and instead, +offers a replacement at the definition of those variables. +Function calls that match the pattern of how the constant is calculated are +matched and replaced with the ``std::numbers`` constant. +The use of macros gets replaced with the corresponding ``std::numbers`` +constant, instead of changing the macro definition. + +The following list of constants from the ``numbers`` header are supported: + +* e +* log2e +* log10e +* pi +* inv_pi +* inv_sqrtpi +* ln2 +* ln10 +* sqrt2 +* sqrt3 +* inv_sqrt3 +* egamma +* phi + +The list currently includes all constants as of C++20. + +The replacements use the type of the matched constant and can remove explicit casts, +i.e., switching between ``std::numbers::e``, ``std::numbers::e_v`` and ``std::numbers::e_v`` +where appropriate. + +.. code-block:: c++ + +double sqrt(double); +double log2(double); +void sink(auto&&) {} +void floatSink(float); + +#define MY_PI 3.1415926 + +void foo() { +const double Pi = 3.141592653589; // const double Pi = std::numbers::pi +const auto Use = Pi / 2;// no match for Pi +static constexpr double Euler = 2.7182818; // static constexpr double Euler = std::numbers::e; + +log2(exp(1)); // std::numbers::log2e; +log2(Euler);// std::numbers::log2e; +1 / sqrt(MY_PI);// std::numbers::inv_sqrtpi; +sink(MY_PI);// sink(std::numbers::pi); +floatSink(MY_PI); // floatSink(std::numbers::pi); +floatSink(static_cast(MY_PI)); // floatSink(std::numbers::pi_v); +} 5chmidti wrote: Done https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -0,0 +1,58 @@ +.. title:: clang-tidy - modernize-use-std-numbers + +modernize-use-std-numbers += + +Finds constants and function calls to math functions that can be replaced +with c++20's mathematical constants from the ``numbers`` header and offers +fix-it hints. +Does not match the use of variables with that value, and instead, +offers a replacement at the definition of those variables. +Function calls that match the pattern of how the constant is calculated are +matched and replaced with the ``std::numbers`` constant. +The use of macros gets replaced with the corresponding ``std::numbers`` +constant, instead of changing the macro definition. + +The following list of constants from the ``numbers`` header are supported: + +* e +* log2e +* log10e +* pi +* inv_pi +* inv_sqrtpi +* ln2 +* ln10 +* sqrt2 +* sqrt3 +* inv_sqrt3 +* egamma +* phi + +The list currently includes all constants as of C++20. + +The replacements use the type of the matched constant and can remove explicit casts, +i.e., switching between ``std::numbers::e``, ``std::numbers::e_v`` and ``std::numbers::e_v`` +where appropriate. + +.. code-block:: c++ + +double sqrt(double); +double log2(double); +void sink(auto&&) {} +void floatSink(float); + +#define MY_PI 3.1415926 + +void foo() { +const double Pi = 3.141592653589; // const double Pi = std::numbers::pi +const auto Use = Pi / 2;// no match for Pi +static constexpr double Euler = 2.7182818; // static constexpr double Euler = std::numbers::e; + +log2(exp(1)); // std::numbers::log2e; +log2(Euler);// std::numbers::log2e; +1 / sqrt(MY_PI);// std::numbers::inv_sqrtpi; +sink(MY_PI);// sink(std::numbers::pi); +floatSink(MY_PI); // floatSink(std::numbers::pi); +floatSink(static_cast(MY_PI)); // floatSink(std::numbers::pi_v); +} PiotrZSL wrote: Add documentation for check configuration: IncludeStyle and DiffThreshold https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
https://github.com/PiotrZSL approved this pull request. LGTM, this should be merged so other llvm users could test it. As check is complicated I expect that there can be some false-positives or issues reported when llvm 18 would release. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
https://github.com/PiotrZSL edited https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -128,169 +128,169 @@ void foo(){ const auto Actually2 = 2; bar::sqrt(Actually2); -// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: prefer std::numbers math constant [modernize-use-std-numbers] +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2' math constant [modernize-use-std-numbers] // CHECK-FIXES: std::numbers::sqrt2; exp(1); -// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: prefer std::numbers math constant [modernize-use-std-numbers] +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: prefer 'std::numbers::e' math constant [modernize-use-std-numbers] // CHECK-FIXES: std::numbers::e; exp(One); -// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: prefer std::numbers math constant [modernize-use-std-numbers] +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: prefer 'std::numbers::e' math constant [modernize-use-std-numbers] // CHECK-FIXES: std::numbers::e; exp(1.01); -// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: prefer std::numbers math constant [modernize-use-std-numbers] +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: prefer 'std::numbers::e' math constant [modernize-use-std-numbers] // CHECK-FIXES: std::numbers::e; log2(exp(1)); -// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: prefer std::numbers math constant [modernize-use-std-numbers] -// CHECK-MESSAGES: :[[@LINE-2]]:10: warning: prefer std::numbers math constant [modernize-use-std-numbers] +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: prefer 'std::numbers::log2e' math constant [modernize-use-std-numbers] +// CHECK-MESSAGES: :[[@LINE-2]]:10: warning: prefer 'std::numbers::e' math constant [modernize-use-std-numbers] // CHECK-FIXES: std::numbers::log2e; log2(Euler); -// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: prefer std::numbers math constant [modernize-use-std-numbers] +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: prefer 'std::numbers::log2e' math constant [modernize-use-std-numbers] // CHECK-FIXES: std::numbers::log2e; log2(bar::e); -// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: prefer std::numbers math constant [modernize-use-std-numbers] +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: prefer 'std::numbers::log2e' math constant [modernize-use-std-numbers] // CHECK-FIXES: std::numbers::log2e; log2(Euler5); -// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: prefer std::numbers math constant [modernize-use-std-numbers] +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: prefer 'std::numbers::log2e' math constant [modernize-use-std-numbers] // CHECK-FIXES: std::numbers::log2e; log2(Euler6); -// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: prefer std::numbers math constant [modernize-use-std-numbers] +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: prefer 'std::numbers::log2e_v' math constant [modernize-use-std-numbers] // CHECK-FIXES: std::numbers::log2e_v; log2(NotEuler7); auto log2e = 1.4426950; -// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: prefer std::numbers math constant [modernize-use-std-numbers] +// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: prefer 'std::numbers::log2e' math constant [modernize-use-std-numbers] // CHECK-FIXES: auto log2e = std::numbers::log2e; floatSink(log2(Euler)); -// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: prefer std::numbers math constant [modernize-use-std-numbers] +// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: prefer 'std::numbers::log2e_v' math constant [modernize-use-std-numbers] // CHECK-FIXES: floatSink(std::numbers::log2e_v); floatSink(static_cast(log2(Euler))); -// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: prefer std::numbers math constant [modernize-use-std-numbers] -// CHECK-MESSAGES: :[[@LINE-2]]:34: warning: prefer std::numbers math constant [modernize-use-std-numbers] +// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: prefer 'std::numbers::log2e_v' math constant [modernize-use-std-numbers] // CHECK-FIXES: floatSink(std::numbers::log2e_v); floatSink(1.4426950); -// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: prefer std::numbers math constant [modernize-use-std-numbers] +// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: prefer 'std::numbers::log2e_v' math constant [modernize-use-std-numbers] // CHECK-FIXES: floatSink(std::numbers::log2e_v); floatSink(static_cast(1.4426950)); -// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: prefer std::numbers math constant [modernize-use-std-numbers] -// CHECK-MESSAGES: :[[@LINE-2]]:34: warning: prefer std::numbers math constant [modernize-use-std-numbers] +// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: prefer 'std::numbers::log2e_v' math constant [modernize-use-std-numbers] // CHECK-FIXES: floatSink(std::numbers::log2e_v); floatSink(log2(static_cast(Euler))); -// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: prefer std::numbers math constant
[clang-tools-extra] [clang-tidy] add modernize-use-std-numbers (PR #66583)
5chmidti wrote: It looks like GitHub decided to not list all new commits in the conversation view... there are a few more in the commits tab. The newly pushed changes start at commit [refactor from using transformer to a normal check](https://github.com/llvm/llvm-project/pull/66583/commits/b1adc10d5a0b1456725979a1f480a517b37b6b2f). The reason I found the issue I have created is what I had to change/remove in this commit: [fix literal in template problem in exchange for not resolving implicit casts](https://github.com/llvm/llvm-project/pull/66583/commits/01d6b2e4e01e06e894710756ee83c668995bafbb) I wanted the replacements to resolve implicit casts, like they do for explicit casts currently. The test shows the (IMO) degraded quality of the replacements. After my findings/comment in that issue, I decided to push my changes this way to maybe move forward with this check and leave the implicit cast matching for another pr in the future. What do you think? > Looks like having this check implemented as an multiple matchers isn't a good > idea, simply because we pickup first one that match instead a nearest one. > This leads to bugs when dealing with proper values. There is a single matcher that matches literals and formulas. Formulas are applied as soon as a matched pattern is found. All matched literals are collected and sorted, if no formula was matched and the literal that differs the least from its constant is selected. > In ideal conditions something like x* 3.14 should be even detected as PI. and > also this precision 0.001 should be put into configuration option. This is now configurable with the config value `DiffThreshold`. The default value is `0.001`, like it was before. Should this default be lower to detect your `3.14` example? > Also warning message should already say what from std::numbers should be used > and how far are current and proposed values from them self. The messages now follow the following format: - `prefer 'std::numbers::pi' to this literal, differs by '2.34e-2'` (using scientific notation to be more compact than printing `0.123456789`) - `prefer 'std::numbers::pi_v' to this formula` (the message uses the replacement code from the fix-it hint) - `prefer 'std::numbers::pi' to this macro, differs by '2.34e-2'` (if we are replacing a macro that is just a literal) - `prefer 'std::numbers::inv_sqrt3' to this macro` (for things like `#define INV_SQRT3 1 / bar::sqrt(3)`) So the messages include what kind of symbol/code is being replaced, the inserted code, and the difference to the inserted value if the replaced code is a literal or a macro that is a literal. The difference that is being printed is the absolute difference. Does the sign matter? https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
PiotrZSL wrote: > @PiotrZSL I have implemented the changes you have requested, but because of > #72536, I am not going to push those changes yet, unless you prefer it if I > do. I might take a look into fixing this issue. I came across this when I > checked what would happen when the big templated function was instantiated, > just to be sure the test would pass. TK_IgnoreUnlessSpelledInSource should do a trick. I will re-check this review in some near free time. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
5chmidti wrote: @PiotrZSL I have implemented the changes you have requested, but because of #72536, I am not going to push those changes yet, unless you prefer it if I do. I might take a look into fixing this issue. I came across this when I checked what would happen when the big templated function was instantiated, just to be sure the test would pass. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
https://github.com/PiotrZSL edited https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
https://github.com/PiotrZSL requested changes to this pull request. Example: ``` llvm/include/llvm/Support/MathExtras.h:59:31: warning: prefer std::numbers math constant [modernize-use-std-numbers] 59 | inv_sqrt3f = .577350269F, // (0x1.279a74P-1) | ^~~ | std::numbers::egamma_v ``` ``` egammaf = .577215665F ``` Looks like having this check implemented as an multiple matchers isn't a good idea, simply because we pickup first one that match instead a nearest one. This leads to bugs when dealing with proper values. In ideal conditions something like x* 3.14 should be even detected as PI. Also warning message should already say what from std::numbers should be used and how far are current and proposed values from them self. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -0,0 +1,28 @@ +.. title:: clang-tidy - modernize-use-std-numbers + +modernize-use-std-numbers += + +Finds constants and function calls to math functions that can be replaced +with c++20's mathematical constants from the ``numbers`` header and offers fix-it hints. +Does not match the use of variables with that value, and instead, +offers a replacement at the definition of those variables. + 5chmidti wrote: I’ve also extended the documentation slightly to explain the replacements a bit further. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
https://github.com/5chmidti edited https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -0,0 +1,284 @@ +//===--- UseStdNumbersCheck.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 "UseStdNumbersCheck.h" +#include "../ClangTidyDiagnosticConsumer.h" +#include "../utils/TransformerClangTidyCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/Expr.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/Type.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/ASTMatchersInternal.h" +#include "clang/ASTMatchers/ASTMatchersMacros.h" +#include "clang/Basic/LLVM.h" +#include "clang/Tooling/Transformer/RewriteRule.h" +#include "clang/Tooling/Transformer/Stencil.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/MathExtras.h" +#include +#include +#include +#include + +namespace { +using namespace clang::ast_matchers; +using clang::ast_matchers::internal::Matcher; +using clang::transformer::addInclude; +using clang::transformer::applyFirst; +using clang::transformer::ASTEdit; +using clang::transformer::cat; +using clang::transformer::changeTo; +using clang::transformer::edit; +using clang::transformer::EditGenerator; +using clang::transformer::flattenVector; +using clang::transformer::RewriteRuleWith; +using llvm::StringRef; + +constexpr auto DiffThreshold = 0.001; + +AST_MATCHER_P(clang::FloatingLiteral, near, double, Value) { + return std::abs(Node.getValueAsApproximateDouble() - Value) < DiffThreshold; +} + +AST_MATCHER_P(clang::QualType, hasCanonicalTypeUnqualified, + Matcher, InnerMatcher) { + return InnerMatcher.matches(Node->getCanonicalTypeUnqualified(), Finder, + Builder); +} + +AST_MATCHER(clang::QualType, isArithmetic) { return Node->isArithmeticType(); } +AST_MATCHER(clang::QualType, isFloating) { return Node->isFloatingType(); } + +auto ignoreImplicitAndArithmeticCasting(const Matcher Matcher) { + return expr( + ignoringImplicit(expr(hasType(qualType(isArithmetic())), +ignoringParenCasts(ignoringImplicit(Matcher); +} + +auto ignoreImplicitAndFloatingCasting(const Matcher Matcher) { + return expr( + ignoringImplicit(expr(hasType(qualType(isFloating())), +ignoringParenCasts(ignoringImplicit(Matcher); +} + +auto matchMathCall(const StringRef FunctionName, + const Matcher ArgumentMatcher) { + return callExpr( + callee(functionDecl(hasName(FunctionName), + hasParameter(0, hasType(isArithmetic(), + hasArgument(0, ArgumentMatcher)); +} + +auto matchSqrt(const Matcher ArgumentMatcher) { + return matchMathCall("sqrt", ArgumentMatcher); +} + +// Used for top-level matchers (i.e. the match that replaces Val with its +// constant). +// +// E.g. The matcher of `std::numbers::pi` uses this matcher to look to +// floatLiterals that have the value of pi. +// +// We only care about the literal if the match is for a top-level match +auto matchFloatLiteralNear(const double Val) { + return expr(ignoreImplicitAndFloatingCasting(floatLiteral(near(Val; +} + +// Used for non-top-level matchers (i.e. matchers that are used as inner +// matchers for top-level matchers). +// +// E.g.: The matcher of `std::numbers::log2e` uses this matcher to check if `e` +// of `log2(e)` is declared constant and initialized with the value for eulers +// number. +// +// Here, we do care about literals and about DeclRefExprs to variable +// declarations that are constant and initialized with `Val`. This allows +// top-level matchers to see through declared constants for their inner matches +// like the `std::numbers::log2e` matcher. +auto matchFloatValueNear(const double Val) { + const auto Float = floatLiteral(near(Val)); + + const auto Dref = declRefExpr(to(varDecl( + anyOf(isConstexpr(), +varDecl(hasType(qualType(isConstQualified(), isArithmetic(), 5chmidti wrote: This is not needed. I just checked https://github.com/llvm/llvm-project/blob/af4abc4fa7778f83ba7fd759b818352dc2d19469/clang/lib/AST/Type.cpp#L2186 The canonical type is used to check if the type is a floating type, instead of the `BaseType` pointer in `ExtQualsTypeCommonBase`. Tests that confirm this behavior are - `bar::sqrt(Actually2MyFloat);` - `log2(Euler5);` (`Euler5` has the type `my_double`) - `log2(Euler6);` (`Euler6` has the type `my_float`) https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -0,0 +1,28 @@ +.. title:: clang-tidy - modernize-use-std-numbers + +modernize-use-std-numbers += + +Finds constants and function calls to math functions that can be replaced +with c++20's mathematical constants from the ``numbers`` header and offers fix-it hints. +Does not match the use of variables with that value, and instead, +offers a replacement at the definition of those variables. + 5chmidti wrote: done https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -0,0 +1,284 @@ +//===--- UseStdNumbersCheck.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 "UseStdNumbersCheck.h" +#include "../ClangTidyDiagnosticConsumer.h" +#include "../utils/TransformerClangTidyCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/Expr.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/Type.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/ASTMatchersInternal.h" +#include "clang/ASTMatchers/ASTMatchersMacros.h" +#include "clang/Basic/LLVM.h" +#include "clang/Tooling/Transformer/RewriteRule.h" +#include "clang/Tooling/Transformer/Stencil.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/MathExtras.h" +#include +#include +#include +#include + +namespace { +using namespace clang::ast_matchers; +using clang::ast_matchers::internal::Matcher; +using clang::transformer::addInclude; +using clang::transformer::applyFirst; +using clang::transformer::ASTEdit; +using clang::transformer::cat; +using clang::transformer::changeTo; +using clang::transformer::edit; +using clang::transformer::EditGenerator; +using clang::transformer::flattenVector; +using clang::transformer::RewriteRuleWith; +using llvm::StringRef; + +constexpr auto DiffThreshold = 0.001; + +AST_MATCHER_P(clang::FloatingLiteral, near, double, Value) { + return std::abs(Node.getValueAsApproximateDouble() - Value) < DiffThreshold; +} + +AST_MATCHER_P(clang::QualType, hasCanonicalTypeUnqualified, + Matcher, InnerMatcher) { + return InnerMatcher.matches(Node->getCanonicalTypeUnqualified(), Finder, 5chmidti wrote: done https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -0,0 +1,284 @@ +//===--- UseStdNumbersCheck.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 "UseStdNumbersCheck.h" +#include "../ClangTidyDiagnosticConsumer.h" +#include "../utils/TransformerClangTidyCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/Expr.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/Type.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/ASTMatchersInternal.h" +#include "clang/ASTMatchers/ASTMatchersMacros.h" +#include "clang/Basic/LLVM.h" +#include "clang/Tooling/Transformer/RewriteRule.h" +#include "clang/Tooling/Transformer/Stencil.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/MathExtras.h" +#include +#include +#include +#include + +namespace { +using namespace clang::ast_matchers; +using clang::ast_matchers::internal::Matcher; +using clang::transformer::addInclude; +using clang::transformer::applyFirst; +using clang::transformer::ASTEdit; +using clang::transformer::cat; +using clang::transformer::changeTo; +using clang::transformer::edit; +using clang::transformer::EditGenerator; +using clang::transformer::flattenVector; +using clang::transformer::RewriteRuleWith; +using llvm::StringRef; + +constexpr auto DiffThreshold = 0.001; + +AST_MATCHER_P(clang::FloatingLiteral, near, double, Value) { + return std::abs(Node.getValueAsApproximateDouble() - Value) < DiffThreshold; +} + +AST_MATCHER_P(clang::QualType, hasCanonicalTypeUnqualified, + Matcher, InnerMatcher) { + return InnerMatcher.matches(Node->getCanonicalTypeUnqualified(), Finder, + Builder); +} + +AST_MATCHER(clang::QualType, isArithmetic) { return Node->isArithmeticType(); } 5chmidti wrote: done https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -0,0 +1,28 @@ +.. title:: clang-tidy - modernize-use-std-numbers + +modernize-use-std-numbers += + +Finds constants and function calls to math functions that can be replaced +with c++20's mathematical constants from the ``numbers`` header and offers fix-it hints. +Does not match the use of variables with that value, and instead, +offers a replacement at the definition of those variables. + PiotrZSL wrote: List here all possible replacements. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -0,0 +1,284 @@ +//===--- UseStdNumbersCheck.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 "UseStdNumbersCheck.h" +#include "../ClangTidyDiagnosticConsumer.h" +#include "../utils/TransformerClangTidyCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/Expr.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/Type.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/ASTMatchersInternal.h" +#include "clang/ASTMatchers/ASTMatchersMacros.h" +#include "clang/Basic/LLVM.h" +#include "clang/Tooling/Transformer/RewriteRule.h" +#include "clang/Tooling/Transformer/Stencil.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/MathExtras.h" +#include +#include +#include +#include + +namespace { +using namespace clang::ast_matchers; +using clang::ast_matchers::internal::Matcher; +using clang::transformer::addInclude; +using clang::transformer::applyFirst; +using clang::transformer::ASTEdit; +using clang::transformer::cat; +using clang::transformer::changeTo; +using clang::transformer::edit; +using clang::transformer::EditGenerator; +using clang::transformer::flattenVector; +using clang::transformer::RewriteRuleWith; +using llvm::StringRef; + +constexpr auto DiffThreshold = 0.001; + +AST_MATCHER_P(clang::FloatingLiteral, near, double, Value) { + return std::abs(Node.getValueAsApproximateDouble() - Value) < DiffThreshold; +} + +AST_MATCHER_P(clang::QualType, hasCanonicalTypeUnqualified, + Matcher, InnerMatcher) { + return InnerMatcher.matches(Node->getCanonicalTypeUnqualified(), Finder, + Builder); +} + +AST_MATCHER(clang::QualType, isArithmetic) { return Node->isArithmeticType(); } +AST_MATCHER(clang::QualType, isFloating) { return Node->isFloatingType(); } + +auto ignoreImplicitAndArithmeticCasting(const Matcher Matcher) { + return expr( + ignoringImplicit(expr(hasType(qualType(isArithmetic())), +ignoringParenCasts(ignoringImplicit(Matcher); +} + +auto ignoreImplicitAndFloatingCasting(const Matcher Matcher) { + return expr( + ignoringImplicit(expr(hasType(qualType(isFloating())), +ignoringParenCasts(ignoringImplicit(Matcher); +} + +auto matchMathCall(const StringRef FunctionName, + const Matcher ArgumentMatcher) { + return callExpr( + callee(functionDecl(hasName(FunctionName), + hasParameter(0, hasType(isArithmetic(), + hasArgument(0, ArgumentMatcher)); +} + +auto matchSqrt(const Matcher ArgumentMatcher) { + return matchMathCall("sqrt", ArgumentMatcher); +} + +// Used for top-level matchers (i.e. the match that replaces Val with its +// constant). +// +// E.g. The matcher of `std::numbers::pi` uses this matcher to look to +// floatLiterals that have the value of pi. +// +// We only care about the literal if the match is for a top-level match +auto matchFloatLiteralNear(const double Val) { + return expr(ignoreImplicitAndFloatingCasting(floatLiteral(near(Val; +} + +// Used for non-top-level matchers (i.e. matchers that are used as inner +// matchers for top-level matchers). +// +// E.g.: The matcher of `std::numbers::log2e` uses this matcher to check if `e` +// of `log2(e)` is declared constant and initialized with the value for eulers +// number. +// +// Here, we do care about literals and about DeclRefExprs to variable +// declarations that are constant and initialized with `Val`. This allows +// top-level matchers to see through declared constants for their inner matches +// like the `std::numbers::log2e` matcher. +auto matchFloatValueNear(const double Val) { + const auto Float = floatLiteral(near(Val)); + + const auto Dref = declRefExpr(to(varDecl( + anyOf(isConstexpr(), +varDecl(hasType(qualType(isConstQualified(), isArithmetic(), PiotrZSL wrote: Consier checking here CanonicalType to ignore all typedefs https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -0,0 +1,284 @@ +//===--- UseStdNumbersCheck.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 "UseStdNumbersCheck.h" +#include "../ClangTidyDiagnosticConsumer.h" +#include "../utils/TransformerClangTidyCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/Expr.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/Type.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/ASTMatchersInternal.h" +#include "clang/ASTMatchers/ASTMatchersMacros.h" +#include "clang/Basic/LLVM.h" +#include "clang/Tooling/Transformer/RewriteRule.h" +#include "clang/Tooling/Transformer/Stencil.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/MathExtras.h" +#include +#include +#include +#include + +namespace { +using namespace clang::ast_matchers; +using clang::ast_matchers::internal::Matcher; +using clang::transformer::addInclude; +using clang::transformer::applyFirst; +using clang::transformer::ASTEdit; +using clang::transformer::cat; +using clang::transformer::changeTo; +using clang::transformer::edit; +using clang::transformer::EditGenerator; +using clang::transformer::flattenVector; +using clang::transformer::RewriteRuleWith; +using llvm::StringRef; + +constexpr auto DiffThreshold = 0.001; + +AST_MATCHER_P(clang::FloatingLiteral, near, double, Value) { + return std::abs(Node.getValueAsApproximateDouble() - Value) < DiffThreshold; +} + +AST_MATCHER_P(clang::QualType, hasCanonicalTypeUnqualified, + Matcher, InnerMatcher) { + return InnerMatcher.matches(Node->getCanonicalTypeUnqualified(), Finder, + Builder); +} + +AST_MATCHER(clang::QualType, isArithmetic) { return Node->isArithmeticType(); } PiotrZSL wrote: Same in those both, verify that Type isn't null. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -0,0 +1,284 @@ +//===--- UseStdNumbersCheck.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 "UseStdNumbersCheck.h" +#include "../ClangTidyDiagnosticConsumer.h" +#include "../utils/TransformerClangTidyCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/Expr.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/Type.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/ASTMatchersInternal.h" +#include "clang/ASTMatchers/ASTMatchersMacros.h" +#include "clang/Basic/LLVM.h" +#include "clang/Tooling/Transformer/RewriteRule.h" +#include "clang/Tooling/Transformer/Stencil.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/MathExtras.h" +#include +#include +#include +#include + +namespace { +using namespace clang::ast_matchers; +using clang::ast_matchers::internal::Matcher; +using clang::transformer::addInclude; +using clang::transformer::applyFirst; +using clang::transformer::ASTEdit; +using clang::transformer::cat; +using clang::transformer::changeTo; +using clang::transformer::edit; +using clang::transformer::EditGenerator; +using clang::transformer::flattenVector; +using clang::transformer::RewriteRuleWith; +using llvm::StringRef; + +constexpr auto DiffThreshold = 0.001; + +AST_MATCHER_P(clang::FloatingLiteral, near, double, Value) { + return std::abs(Node.getValueAsApproximateDouble() - Value) < DiffThreshold; +} + +AST_MATCHER_P(clang::QualType, hasCanonicalTypeUnqualified, + Matcher, InnerMatcher) { + return InnerMatcher.matches(Node->getCanonicalTypeUnqualified(), Finder, PiotrZSL wrote: Verify here that Node is Null, so `return !Node.isNull() && InnerMatcher...` https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
https://github.com/5chmidti updated https://github.com/llvm/llvm-project/pull/66583 >From 8b8acedc0e6c432545744a113128cfcb4f2da7e5 Mon Sep 17 00:00:00 2001 From: Julian Schmidt <44101708+5chmi...@users.noreply.github.com> Date: Sat, 16 Sep 2023 16:24:13 +0200 Subject: [PATCH 01/20] [clang-tidy] add modernize-use-std-numbers check This check finds constants and function calls to math functions that can be replaced with c++20's mathematical constants ('numbers' header) and offers fixit-hints. Does not match the use of variables or macros with that value and instead, offers a replacement at the definition of said variables and macros. --- .../clang-tidy/modernize/CMakeLists.txt | 1 + .../modernize/ModernizeTidyModule.cpp | 3 + .../modernize/UseStdNumbersCheck.cpp | 377 ++ .../clang-tidy/modernize/UseStdNumbersCheck.h | 37 ++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../docs/clang-tidy/checks/list.rst | 1 + .../checks/modernize/use-std-numbers.rst | 25 ++ .../checkers/modernize/use-std-numbers.cpp| 205 ++ 8 files changed, 655 insertions(+) create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-numbers.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index 717c400c4790330..d82353d74fbd0d4 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -16,6 +16,7 @@ add_clang_library(clangTidyModernizeModule MakeSharedCheck.cpp MakeSmartPtrCheck.cpp MakeUniqueCheck.cpp + UseStdNumbersCheck.cpp ModernizeTidyModule.cpp PassByValueCheck.cpp RawStringLiteralCheck.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index 73751cf2705068d..73584e20166f66a 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -18,6 +18,7 @@ #include "MacroToEnumCheck.h" #include "MakeSharedCheck.h" #include "MakeUniqueCheck.h" +#include "UseStdNumbersCheck.h" #include "PassByValueCheck.h" #include "RawStringLiteralCheck.h" #include "RedundantVoidArgCheck.h" @@ -65,6 +66,8 @@ class ModernizeModule : public ClangTidyModule { CheckFactories.registerCheck("modernize-macro-to-enum"); CheckFactories.registerCheck("modernize-make-shared"); CheckFactories.registerCheck("modernize-make-unique"); +CheckFactories.registerCheck( +"modernize-use-std-numbers"); CheckFactories.registerCheck("modernize-pass-by-value"); CheckFactories.registerCheck("modernize-use-std-print"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp new file mode 100644 index 000..c23dc6671013bc3 --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp @@ -0,0 +1,377 @@ +//===--- UseStdNumbersCheck.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 "UseStdNumbersCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Expr.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/Type.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/ASTMatchersInternal.h" +#include "clang/ASTMatchers/ASTMatchersMacros.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/TokenKinds.h" +#include "clang/Lex/PPCallbacks.h" +#include "clang/Lex/Preprocessor.h" +#include "clang/Lex/Token.h" +#include "clang/Tooling/Transformer/RewriteRule.h" +#include "clang/Tooling/Transformer/Stencil.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/MathExtras.h" +#include +#include + +namespace { +using namespace clang::ast_matchers; +using clang::ast_matchers::internal::Matcher; +using clang::transformer::addInclude; +using clang::transformer::applyFirst; +using clang::transformer::ASTEdit; +using clang::transformer::cat; +using clang::transformer::changeTo; +using clang::transformer::edit; +using clang::transformer::EditGenerator; +using clang::transformer::flattenVector; +using clang::transformer::RewriteRuleWith; +using llvm::StringRef; + +constexpr double Pi =
[clang-tools-extra] [clang-tidy] add modernize-use-std-numbers (PR #66583)
5chmidti wrote: Nice, the rebase fixed the Windows failure https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
5chmidti wrote: I rebased and force-pushed my branch to fix the formatting CI (fetch depth for comparison with head was too big). I pushed no actual changes in that force-push (one documentation ordering change in `fix lexicographical ordering in some places after rename` became redundant). @EugeneZelenko's comments were already addressed and, even without the force-push, had already lost the context. I'm not sure what the issue on Windows is. It looks like nothing within the function template `baz` is matching. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
https://github.com/5chmidti updated https://github.com/llvm/llvm-project/pull/66583 >From 8b8acedc0e6c432545744a113128cfcb4f2da7e5 Mon Sep 17 00:00:00 2001 From: Julian Schmidt <44101708+5chmi...@users.noreply.github.com> Date: Sat, 16 Sep 2023 16:24:13 +0200 Subject: [PATCH 01/19] [clang-tidy] add modernize-use-std-numbers check This check finds constants and function calls to math functions that can be replaced with c++20's mathematical constants ('numbers' header) and offers fixit-hints. Does not match the use of variables or macros with that value and instead, offers a replacement at the definition of said variables and macros. --- .../clang-tidy/modernize/CMakeLists.txt | 1 + .../modernize/ModernizeTidyModule.cpp | 3 + .../modernize/UseStdNumbersCheck.cpp | 377 ++ .../clang-tidy/modernize/UseStdNumbersCheck.h | 37 ++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../docs/clang-tidy/checks/list.rst | 1 + .../checks/modernize/use-std-numbers.rst | 25 ++ .../checkers/modernize/use-std-numbers.cpp| 205 ++ 8 files changed, 655 insertions(+) create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-numbers.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index 717c400c4790330..d82353d74fbd0d4 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -16,6 +16,7 @@ add_clang_library(clangTidyModernizeModule MakeSharedCheck.cpp MakeSmartPtrCheck.cpp MakeUniqueCheck.cpp + UseStdNumbersCheck.cpp ModernizeTidyModule.cpp PassByValueCheck.cpp RawStringLiteralCheck.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index 73751cf2705068d..73584e20166f66a 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -18,6 +18,7 @@ #include "MacroToEnumCheck.h" #include "MakeSharedCheck.h" #include "MakeUniqueCheck.h" +#include "UseStdNumbersCheck.h" #include "PassByValueCheck.h" #include "RawStringLiteralCheck.h" #include "RedundantVoidArgCheck.h" @@ -65,6 +66,8 @@ class ModernizeModule : public ClangTidyModule { CheckFactories.registerCheck("modernize-macro-to-enum"); CheckFactories.registerCheck("modernize-make-shared"); CheckFactories.registerCheck("modernize-make-unique"); +CheckFactories.registerCheck( +"modernize-use-std-numbers"); CheckFactories.registerCheck("modernize-pass-by-value"); CheckFactories.registerCheck("modernize-use-std-print"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp new file mode 100644 index 000..c23dc6671013bc3 --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp @@ -0,0 +1,377 @@ +//===--- UseStdNumbersCheck.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 "UseStdNumbersCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Expr.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/Type.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/ASTMatchersInternal.h" +#include "clang/ASTMatchers/ASTMatchersMacros.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/TokenKinds.h" +#include "clang/Lex/PPCallbacks.h" +#include "clang/Lex/Preprocessor.h" +#include "clang/Lex/Token.h" +#include "clang/Tooling/Transformer/RewriteRule.h" +#include "clang/Tooling/Transformer/Stencil.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/MathExtras.h" +#include +#include + +namespace { +using namespace clang::ast_matchers; +using clang::ast_matchers::internal::Matcher; +using clang::transformer::addInclude; +using clang::transformer::applyFirst; +using clang::transformer::ASTEdit; +using clang::transformer::cat; +using clang::transformer::changeTo; +using clang::transformer::edit; +using clang::transformer::EditGenerator; +using clang::transformer::flattenVector; +using clang::transformer::RewriteRuleWith; +using llvm::StringRef; + +constexpr double Pi =
[clang-tools-extra] [clang-tidy] add modernize-use-std-numbers (PR #66583)
https://github.com/5chmidti updated https://github.com/llvm/llvm-project/pull/66583 >From 8f5e9e6024b0db8f251625669adbc5d607da83cb Mon Sep 17 00:00:00 2001 From: Julian Schmidt <44101708+5chmi...@users.noreply.github.com> Date: Sat, 16 Sep 2023 16:24:13 +0200 Subject: [PATCH 01/19] [clang-tidy] add modernize-use-std-numbers check This check finds constants and function calls to math functions that can be replaced with c++20's mathematical constants ('numbers' header) and offers fixit-hints. Does not match the use of variables or macros with that value and instead, offers a replacement at the definition of said variables and macros. --- .../clang-tidy/modernize/CMakeLists.txt | 1 + .../modernize/ModernizeTidyModule.cpp | 3 + .../modernize/UseStdNumbersCheck.cpp | 377 ++ .../clang-tidy/modernize/UseStdNumbersCheck.h | 37 ++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../docs/clang-tidy/checks/list.rst | 3 +- .../checks/modernize/use-std-numbers.rst | 25 ++ .../checkers/modernize/use-std-numbers.cpp| 205 ++ 8 files changed, 656 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-numbers.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index 717c400c4790330..d82353d74fbd0d4 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -16,6 +16,7 @@ add_clang_library(clangTidyModernizeModule MakeSharedCheck.cpp MakeSmartPtrCheck.cpp MakeUniqueCheck.cpp + UseStdNumbersCheck.cpp ModernizeTidyModule.cpp PassByValueCheck.cpp RawStringLiteralCheck.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index 73751cf2705068d..73584e20166f66a 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -18,6 +18,7 @@ #include "MacroToEnumCheck.h" #include "MakeSharedCheck.h" #include "MakeUniqueCheck.h" +#include "UseStdNumbersCheck.h" #include "PassByValueCheck.h" #include "RawStringLiteralCheck.h" #include "RedundantVoidArgCheck.h" @@ -65,6 +66,8 @@ class ModernizeModule : public ClangTidyModule { CheckFactories.registerCheck("modernize-macro-to-enum"); CheckFactories.registerCheck("modernize-make-shared"); CheckFactories.registerCheck("modernize-make-unique"); +CheckFactories.registerCheck( +"modernize-use-std-numbers"); CheckFactories.registerCheck("modernize-pass-by-value"); CheckFactories.registerCheck("modernize-use-std-print"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp new file mode 100644 index 000..c23dc6671013bc3 --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp @@ -0,0 +1,377 @@ +//===--- UseStdNumbersCheck.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 "UseStdNumbersCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Expr.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/Type.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/ASTMatchersInternal.h" +#include "clang/ASTMatchers/ASTMatchersMacros.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/TokenKinds.h" +#include "clang/Lex/PPCallbacks.h" +#include "clang/Lex/Preprocessor.h" +#include "clang/Lex/Token.h" +#include "clang/Tooling/Transformer/RewriteRule.h" +#include "clang/Tooling/Transformer/Stencil.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/MathExtras.h" +#include +#include + +namespace { +using namespace clang::ast_matchers; +using clang::ast_matchers::internal::Matcher; +using clang::transformer::addInclude; +using clang::transformer::applyFirst; +using clang::transformer::ASTEdit; +using clang::transformer::cat; +using clang::transformer::changeTo; +using clang::transformer::edit; +using clang::transformer::EditGenerator; +using clang::transformer::flattenVector; +using clang::transformer::RewriteRuleWith; +using llvm::StringRef; + +constexpr double Pi =
[clang-tools-extra] [clang-tidy] add modernize-use-std-numbers (PR #66583)
https://github.com/5chmidti edited https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
https://github.com/5chmidti updated https://github.com/llvm/llvm-project/pull/66583 >From 8f5e9e6024b0db8f251625669adbc5d607da83cb Mon Sep 17 00:00:00 2001 From: Julian Schmidt <44101708+5chmi...@users.noreply.github.com> Date: Sat, 16 Sep 2023 16:24:13 +0200 Subject: [PATCH 01/17] [clang-tidy] add modernize-use-std-numbers check This check finds constants and function calls to math functions that can be replaced with c++20's mathematical constants ('numbers' header) and offers fixit-hints. Does not match the use of variables or macros with that value and instead, offers a replacement at the definition of said variables and macros. --- .../clang-tidy/modernize/CMakeLists.txt | 1 + .../modernize/ModernizeTidyModule.cpp | 3 + .../modernize/UseStdNumbersCheck.cpp | 377 ++ .../clang-tidy/modernize/UseStdNumbersCheck.h | 37 ++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../docs/clang-tidy/checks/list.rst | 3 +- .../checks/modernize/use-std-numbers.rst | 25 ++ .../checkers/modernize/use-std-numbers.cpp| 205 ++ 8 files changed, 656 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-numbers.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index 717c400c4790330..d82353d74fbd0d4 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -16,6 +16,7 @@ add_clang_library(clangTidyModernizeModule MakeSharedCheck.cpp MakeSmartPtrCheck.cpp MakeUniqueCheck.cpp + UseStdNumbersCheck.cpp ModernizeTidyModule.cpp PassByValueCheck.cpp RawStringLiteralCheck.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index 73751cf2705068d..73584e20166f66a 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -18,6 +18,7 @@ #include "MacroToEnumCheck.h" #include "MakeSharedCheck.h" #include "MakeUniqueCheck.h" +#include "UseStdNumbersCheck.h" #include "PassByValueCheck.h" #include "RawStringLiteralCheck.h" #include "RedundantVoidArgCheck.h" @@ -65,6 +66,8 @@ class ModernizeModule : public ClangTidyModule { CheckFactories.registerCheck("modernize-macro-to-enum"); CheckFactories.registerCheck("modernize-make-shared"); CheckFactories.registerCheck("modernize-make-unique"); +CheckFactories.registerCheck( +"modernize-use-std-numbers"); CheckFactories.registerCheck("modernize-pass-by-value"); CheckFactories.registerCheck("modernize-use-std-print"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp new file mode 100644 index 000..c23dc6671013bc3 --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp @@ -0,0 +1,377 @@ +//===--- UseStdNumbersCheck.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 "UseStdNumbersCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Expr.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/Type.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/ASTMatchersInternal.h" +#include "clang/ASTMatchers/ASTMatchersMacros.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/TokenKinds.h" +#include "clang/Lex/PPCallbacks.h" +#include "clang/Lex/Preprocessor.h" +#include "clang/Lex/Token.h" +#include "clang/Tooling/Transformer/RewriteRule.h" +#include "clang/Tooling/Transformer/Stencil.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/MathExtras.h" +#include +#include + +namespace { +using namespace clang::ast_matchers; +using clang::ast_matchers::internal::Matcher; +using clang::transformer::addInclude; +using clang::transformer::applyFirst; +using clang::transformer::ASTEdit; +using clang::transformer::cat; +using clang::transformer::changeTo; +using clang::transformer::edit; +using clang::transformer::EditGenerator; +using clang::transformer::flattenVector; +using clang::transformer::RewriteRuleWith; +using llvm::StringRef; + +constexpr double Pi =
[clang-tools-extra] [clang-tidy] add modernize-use-std-numbers (PR #66583)
https://github.com/5chmidti updated https://github.com/llvm/llvm-project/pull/66583 >From 8f5e9e6024b0db8f251625669adbc5d607da83cb Mon Sep 17 00:00:00 2001 From: Julian Schmidt <44101708+5chmi...@users.noreply.github.com> Date: Sat, 16 Sep 2023 16:24:13 +0200 Subject: [PATCH 01/16] [clang-tidy] add modernize-use-std-numbers check This check finds constants and function calls to math functions that can be replaced with c++20's mathematical constants ('numbers' header) and offers fixit-hints. Does not match the use of variables or macros with that value and instead, offers a replacement at the definition of said variables and macros. --- .../clang-tidy/modernize/CMakeLists.txt | 1 + .../modernize/ModernizeTidyModule.cpp | 3 + .../modernize/UseStdNumbersCheck.cpp | 377 ++ .../clang-tidy/modernize/UseStdNumbersCheck.h | 37 ++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../docs/clang-tidy/checks/list.rst | 3 +- .../checks/modernize/use-std-numbers.rst | 25 ++ .../checkers/modernize/use-std-numbers.cpp| 205 ++ 8 files changed, 656 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-numbers.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index 717c400c4790330..d82353d74fbd0d4 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -16,6 +16,7 @@ add_clang_library(clangTidyModernizeModule MakeSharedCheck.cpp MakeSmartPtrCheck.cpp MakeUniqueCheck.cpp + UseStdNumbersCheck.cpp ModernizeTidyModule.cpp PassByValueCheck.cpp RawStringLiteralCheck.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index 73751cf2705068d..73584e20166f66a 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -18,6 +18,7 @@ #include "MacroToEnumCheck.h" #include "MakeSharedCheck.h" #include "MakeUniqueCheck.h" +#include "UseStdNumbersCheck.h" #include "PassByValueCheck.h" #include "RawStringLiteralCheck.h" #include "RedundantVoidArgCheck.h" @@ -65,6 +66,8 @@ class ModernizeModule : public ClangTidyModule { CheckFactories.registerCheck("modernize-macro-to-enum"); CheckFactories.registerCheck("modernize-make-shared"); CheckFactories.registerCheck("modernize-make-unique"); +CheckFactories.registerCheck( +"modernize-use-std-numbers"); CheckFactories.registerCheck("modernize-pass-by-value"); CheckFactories.registerCheck("modernize-use-std-print"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp new file mode 100644 index 000..c23dc6671013bc3 --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp @@ -0,0 +1,377 @@ +//===--- UseStdNumbersCheck.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 "UseStdNumbersCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Expr.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/Type.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/ASTMatchersInternal.h" +#include "clang/ASTMatchers/ASTMatchersMacros.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/TokenKinds.h" +#include "clang/Lex/PPCallbacks.h" +#include "clang/Lex/Preprocessor.h" +#include "clang/Lex/Token.h" +#include "clang/Tooling/Transformer/RewriteRule.h" +#include "clang/Tooling/Transformer/Stencil.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/MathExtras.h" +#include +#include + +namespace { +using namespace clang::ast_matchers; +using clang::ast_matchers::internal::Matcher; +using clang::transformer::addInclude; +using clang::transformer::applyFirst; +using clang::transformer::ASTEdit; +using clang::transformer::cat; +using clang::transformer::changeTo; +using clang::transformer::edit; +using clang::transformer::EditGenerator; +using clang::transformer::flattenVector; +using clang::transformer::RewriteRuleWith; +using llvm::StringRef; + +constexpr double Pi =
[clang-tools-extra] [clang-tidy] add modernize-use-std-numbers (PR #66583)
https://github.com/5chmidti updated https://github.com/llvm/llvm-project/pull/66583 >From 8f5e9e6024b0db8f251625669adbc5d607da83cb Mon Sep 17 00:00:00 2001 From: Julian Schmidt <44101708+5chmi...@users.noreply.github.com> Date: Sat, 16 Sep 2023 16:24:13 +0200 Subject: [PATCH 01/15] [clang-tidy] add modernize-use-std-numbers check This check finds constants and function calls to math functions that can be replaced with c++20's mathematical constants ('numbers' header) and offers fixit-hints. Does not match the use of variables or macros with that value and instead, offers a replacement at the definition of said variables and macros. --- .../clang-tidy/modernize/CMakeLists.txt | 1 + .../modernize/ModernizeTidyModule.cpp | 3 + .../modernize/UseStdNumbersCheck.cpp | 377 ++ .../clang-tidy/modernize/UseStdNumbersCheck.h | 37 ++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../docs/clang-tidy/checks/list.rst | 3 +- .../checks/modernize/use-std-numbers.rst | 25 ++ .../checkers/modernize/use-std-numbers.cpp| 205 ++ 8 files changed, 656 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-numbers.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index 717c400c4790330..d82353d74fbd0d4 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -16,6 +16,7 @@ add_clang_library(clangTidyModernizeModule MakeSharedCheck.cpp MakeSmartPtrCheck.cpp MakeUniqueCheck.cpp + UseStdNumbersCheck.cpp ModernizeTidyModule.cpp PassByValueCheck.cpp RawStringLiteralCheck.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index 73751cf2705068d..73584e20166f66a 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -18,6 +18,7 @@ #include "MacroToEnumCheck.h" #include "MakeSharedCheck.h" #include "MakeUniqueCheck.h" +#include "UseStdNumbersCheck.h" #include "PassByValueCheck.h" #include "RawStringLiteralCheck.h" #include "RedundantVoidArgCheck.h" @@ -65,6 +66,8 @@ class ModernizeModule : public ClangTidyModule { CheckFactories.registerCheck("modernize-macro-to-enum"); CheckFactories.registerCheck("modernize-make-shared"); CheckFactories.registerCheck("modernize-make-unique"); +CheckFactories.registerCheck( +"modernize-use-std-numbers"); CheckFactories.registerCheck("modernize-pass-by-value"); CheckFactories.registerCheck("modernize-use-std-print"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp new file mode 100644 index 000..c23dc6671013bc3 --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp @@ -0,0 +1,377 @@ +//===--- UseStdNumbersCheck.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 "UseStdNumbersCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Expr.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/Type.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/ASTMatchersInternal.h" +#include "clang/ASTMatchers/ASTMatchersMacros.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/TokenKinds.h" +#include "clang/Lex/PPCallbacks.h" +#include "clang/Lex/Preprocessor.h" +#include "clang/Lex/Token.h" +#include "clang/Tooling/Transformer/RewriteRule.h" +#include "clang/Tooling/Transformer/Stencil.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/MathExtras.h" +#include +#include + +namespace { +using namespace clang::ast_matchers; +using clang::ast_matchers::internal::Matcher; +using clang::transformer::addInclude; +using clang::transformer::applyFirst; +using clang::transformer::ASTEdit; +using clang::transformer::cat; +using clang::transformer::changeTo; +using clang::transformer::edit; +using clang::transformer::EditGenerator; +using clang::transformer::flattenVector; +using clang::transformer::RewriteRuleWith; +using llvm::StringRef; + +constexpr double Pi =
[clang-tools-extra] [clang-tidy] add modernize-use-std-numbers (PR #66583)
PiotrZSL wrote: No need to change to `sink(std::numbers::phi)`, simply just replace numbers, leave usage like it is. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
5chmidti wrote: > I think you can't, instead of handling macro definitions, you just should > handle places where macro is used. It's fine if someone define a macro MY_PI, > check should flag usage of such macro, not necessarily a macro. > > Instead of using MacroDefined and try to find those #define MY_PI, you should > just use MacroExpands, and find usage of those system macros like M_PI, and > suggest replacement usage of them without checking an value. > > As for #define MY_PI 3.14, this should be handled like it is now, simply > isMacro shouldn't be used, and warning should be produced inside macro. Sounds good to do it like this for macros. My original thought about this was to be the as least intrusive as possible in terms of the number of matches and fixes this check does, but I'm all for replacing these macros. Should variables be treated the same way? I.e. should the fix for ```c++ static constexpr double Phi = 1.6180339; sink(Phi); ``` be changed from ```c++ static constexpr double Phi = std::numbers::phi; sink(Phi) ``` to ```c++ static constexpr double Phi = std::numbers::phi; sink(std::numbers::phi) ``` ? I could add an option `MatchVariableUses` for this: - Never (the current way) - (LocalVariables (matches only uses of variables that are (function-)local declared))? - Always (like the above proposed last change of the fixits) defaulting to... https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
PiotrZSL wrote: > * How do I correctly parse the values in a macro? While the test locally > works, it doesn't in CI. I also don't differentiate between what kind of > numeric constant the token is, which might be another problem. Any pointers > on how to do this correctly are greatly appreciated. > * Macros currently don't distinguish between `float`, `double` and `long > double` like the AST-based check does. The values matched in the PPCallback > should be able to provide the `..._v<>` versions of replacements as well. The > question about how to detect the difference is probably related to the point > above. I think you can't, instead of handling macro definitions, you just should handle places where macro is used. It's fine if someone define a macro MY_PI, check should flag usage of such macro, not necessarily a macro. Instead of using MacroDefined and try to find those #define MY_PI, you should just use MacroExpands, and find usage of those system macros like M_PI, and suggest replacement usage of them without checking an value. As for #define MY_PI 3.14, this should be handled like it is now, simply isMacro shouldn't be used, and warning should be produced inside macro. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
https://github.com/5chmidti updated https://github.com/llvm/llvm-project/pull/66583 >From 8f5e9e6024b0db8f251625669adbc5d607da83cb Mon Sep 17 00:00:00 2001 From: Julian Schmidt <44101708+5chmi...@users.noreply.github.com> Date: Sat, 16 Sep 2023 16:24:13 +0200 Subject: [PATCH 01/13] [clang-tidy] add modernize-use-std-numbers check This check finds constants and function calls to math functions that can be replaced with c++20's mathematical constants ('numbers' header) and offers fixit-hints. Does not match the use of variables or macros with that value and instead, offers a replacement at the definition of said variables and macros. --- .../clang-tidy/modernize/CMakeLists.txt | 1 + .../modernize/ModernizeTidyModule.cpp | 3 + .../modernize/UseStdNumbersCheck.cpp | 377 ++ .../clang-tidy/modernize/UseStdNumbersCheck.h | 37 ++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../docs/clang-tidy/checks/list.rst | 3 +- .../checks/modernize/use-std-numbers.rst | 25 ++ .../checkers/modernize/use-std-numbers.cpp| 205 ++ 8 files changed, 656 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-numbers.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index 717c400c4790330..d82353d74fbd0d4 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -16,6 +16,7 @@ add_clang_library(clangTidyModernizeModule MakeSharedCheck.cpp MakeSmartPtrCheck.cpp MakeUniqueCheck.cpp + UseStdNumbersCheck.cpp ModernizeTidyModule.cpp PassByValueCheck.cpp RawStringLiteralCheck.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index 73751cf2705068d..73584e20166f66a 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -18,6 +18,7 @@ #include "MacroToEnumCheck.h" #include "MakeSharedCheck.h" #include "MakeUniqueCheck.h" +#include "UseStdNumbersCheck.h" #include "PassByValueCheck.h" #include "RawStringLiteralCheck.h" #include "RedundantVoidArgCheck.h" @@ -65,6 +66,8 @@ class ModernizeModule : public ClangTidyModule { CheckFactories.registerCheck("modernize-macro-to-enum"); CheckFactories.registerCheck("modernize-make-shared"); CheckFactories.registerCheck("modernize-make-unique"); +CheckFactories.registerCheck( +"modernize-use-std-numbers"); CheckFactories.registerCheck("modernize-pass-by-value"); CheckFactories.registerCheck("modernize-use-std-print"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp new file mode 100644 index 000..c23dc6671013bc3 --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp @@ -0,0 +1,377 @@ +//===--- UseStdNumbersCheck.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 "UseStdNumbersCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Expr.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/Type.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/ASTMatchersInternal.h" +#include "clang/ASTMatchers/ASTMatchersMacros.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/TokenKinds.h" +#include "clang/Lex/PPCallbacks.h" +#include "clang/Lex/Preprocessor.h" +#include "clang/Lex/Token.h" +#include "clang/Tooling/Transformer/RewriteRule.h" +#include "clang/Tooling/Transformer/Stencil.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/MathExtras.h" +#include +#include + +namespace { +using namespace clang::ast_matchers; +using clang::ast_matchers::internal::Matcher; +using clang::transformer::addInclude; +using clang::transformer::applyFirst; +using clang::transformer::ASTEdit; +using clang::transformer::cat; +using clang::transformer::changeTo; +using clang::transformer::edit; +using clang::transformer::EditGenerator; +using clang::transformer::flattenVector; +using clang::transformer::RewriteRuleWith; +using llvm::StringRef; + +constexpr double Pi =
[clang-tools-extra] [clang-tidy] add modernize-use-std-numbers (PR #66583)
https://github.com/5chmidti updated https://github.com/llvm/llvm-project/pull/66583 >From 8f5e9e6024b0db8f251625669adbc5d607da83cb Mon Sep 17 00:00:00 2001 From: Julian Schmidt <44101708+5chmi...@users.noreply.github.com> Date: Sat, 16 Sep 2023 16:24:13 +0200 Subject: [PATCH 01/12] [clang-tidy] add modernize-use-std-numbers check This check finds constants and function calls to math functions that can be replaced with c++20's mathematical constants ('numbers' header) and offers fixit-hints. Does not match the use of variables or macros with that value and instead, offers a replacement at the definition of said variables and macros. --- .../clang-tidy/modernize/CMakeLists.txt | 1 + .../modernize/ModernizeTidyModule.cpp | 3 + .../modernize/UseStdNumbersCheck.cpp | 377 ++ .../clang-tidy/modernize/UseStdNumbersCheck.h | 37 ++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../docs/clang-tidy/checks/list.rst | 3 +- .../checks/modernize/use-std-numbers.rst | 25 ++ .../checkers/modernize/use-std-numbers.cpp| 205 ++ 8 files changed, 656 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-numbers.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index 717c400c4790330..d82353d74fbd0d4 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -16,6 +16,7 @@ add_clang_library(clangTidyModernizeModule MakeSharedCheck.cpp MakeSmartPtrCheck.cpp MakeUniqueCheck.cpp + UseStdNumbersCheck.cpp ModernizeTidyModule.cpp PassByValueCheck.cpp RawStringLiteralCheck.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index 73751cf2705068d..73584e20166f66a 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -18,6 +18,7 @@ #include "MacroToEnumCheck.h" #include "MakeSharedCheck.h" #include "MakeUniqueCheck.h" +#include "UseStdNumbersCheck.h" #include "PassByValueCheck.h" #include "RawStringLiteralCheck.h" #include "RedundantVoidArgCheck.h" @@ -65,6 +66,8 @@ class ModernizeModule : public ClangTidyModule { CheckFactories.registerCheck("modernize-macro-to-enum"); CheckFactories.registerCheck("modernize-make-shared"); CheckFactories.registerCheck("modernize-make-unique"); +CheckFactories.registerCheck( +"modernize-use-std-numbers"); CheckFactories.registerCheck("modernize-pass-by-value"); CheckFactories.registerCheck("modernize-use-std-print"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp new file mode 100644 index 000..c23dc6671013bc3 --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp @@ -0,0 +1,377 @@ +//===--- UseStdNumbersCheck.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 "UseStdNumbersCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Expr.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/Type.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/ASTMatchersInternal.h" +#include "clang/ASTMatchers/ASTMatchersMacros.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/TokenKinds.h" +#include "clang/Lex/PPCallbacks.h" +#include "clang/Lex/Preprocessor.h" +#include "clang/Lex/Token.h" +#include "clang/Tooling/Transformer/RewriteRule.h" +#include "clang/Tooling/Transformer/Stencil.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/MathExtras.h" +#include +#include + +namespace { +using namespace clang::ast_matchers; +using clang::ast_matchers::internal::Matcher; +using clang::transformer::addInclude; +using clang::transformer::applyFirst; +using clang::transformer::ASTEdit; +using clang::transformer::cat; +using clang::transformer::changeTo; +using clang::transformer::edit; +using clang::transformer::EditGenerator; +using clang::transformer::flattenVector; +using clang::transformer::RewriteRuleWith; +using llvm::StringRef; + +constexpr double Pi =
[clang-tools-extra] [clang-tidy] add modernize-use-std-numbers (PR #66583)
5chmidti wrote: > * How do I correctly parse the values in a macro? While the test locally > works, it doesn't in CI. I also don't differentiate between what kind of > numeric constant the token is, which might be another problem. Any pointers > on how to do this correctly are greatly appreciated. > * Macros currently don't distinguish between `float`, `double` and `long > double` like the AST-based check does. The values matched in the PPCallback > should be able to provide the `..._v<>` versions of replacements as well. The > question about how to detect the difference is probably related to the point > above. Ping. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
https://github.com/5chmidti updated https://github.com/llvm/llvm-project/pull/66583 >From 8f5e9e6024b0db8f251625669adbc5d607da83cb Mon Sep 17 00:00:00 2001 From: Julian Schmidt <44101708+5chmi...@users.noreply.github.com> Date: Sat, 16 Sep 2023 16:24:13 +0200 Subject: [PATCH 1/9] [clang-tidy] add modernize-use-std-numbers check This check finds constants and function calls to math functions that can be replaced with c++20's mathematical constants ('numbers' header) and offers fixit-hints. Does not match the use of variables or macros with that value and instead, offers a replacement at the definition of said variables and macros. --- .../clang-tidy/modernize/CMakeLists.txt | 1 + .../modernize/ModernizeTidyModule.cpp | 3 + .../modernize/UseStdNumbersCheck.cpp | 377 ++ .../clang-tidy/modernize/UseStdNumbersCheck.h | 37 ++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../docs/clang-tidy/checks/list.rst | 3 +- .../checks/modernize/use-std-numbers.rst | 25 ++ .../checkers/modernize/use-std-numbers.cpp| 205 ++ 8 files changed, 656 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-numbers.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index 717c400c4790330..d82353d74fbd0d4 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -16,6 +16,7 @@ add_clang_library(clangTidyModernizeModule MakeSharedCheck.cpp MakeSmartPtrCheck.cpp MakeUniqueCheck.cpp + UseStdNumbersCheck.cpp ModernizeTidyModule.cpp PassByValueCheck.cpp RawStringLiteralCheck.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index 73751cf2705068d..73584e20166f66a 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -18,6 +18,7 @@ #include "MacroToEnumCheck.h" #include "MakeSharedCheck.h" #include "MakeUniqueCheck.h" +#include "UseStdNumbersCheck.h" #include "PassByValueCheck.h" #include "RawStringLiteralCheck.h" #include "RedundantVoidArgCheck.h" @@ -65,6 +66,8 @@ class ModernizeModule : public ClangTidyModule { CheckFactories.registerCheck("modernize-macro-to-enum"); CheckFactories.registerCheck("modernize-make-shared"); CheckFactories.registerCheck("modernize-make-unique"); +CheckFactories.registerCheck( +"modernize-use-std-numbers"); CheckFactories.registerCheck("modernize-pass-by-value"); CheckFactories.registerCheck("modernize-use-std-print"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp new file mode 100644 index 000..c23dc6671013bc3 --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp @@ -0,0 +1,377 @@ +//===--- UseStdNumbersCheck.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 "UseStdNumbersCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Expr.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/Type.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/ASTMatchersInternal.h" +#include "clang/ASTMatchers/ASTMatchersMacros.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/TokenKinds.h" +#include "clang/Lex/PPCallbacks.h" +#include "clang/Lex/Preprocessor.h" +#include "clang/Lex/Token.h" +#include "clang/Tooling/Transformer/RewriteRule.h" +#include "clang/Tooling/Transformer/Stencil.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/MathExtras.h" +#include +#include + +namespace { +using namespace clang::ast_matchers; +using clang::ast_matchers::internal::Matcher; +using clang::transformer::addInclude; +using clang::transformer::applyFirst; +using clang::transformer::ASTEdit; +using clang::transformer::cat; +using clang::transformer::changeTo; +using clang::transformer::edit; +using clang::transformer::EditGenerator; +using clang::transformer::flattenVector; +using clang::transformer::RewriteRuleWith; +using llvm::StringRef; + +constexpr double Pi =
[clang-tools-extra] [clang-tidy] add modernize-use-std-numbers (PR #66583)
https://github.com/5chmidti updated https://github.com/llvm/llvm-project/pull/66583 >From 8f5e9e6024b0db8f251625669adbc5d607da83cb Mon Sep 17 00:00:00 2001 From: Julian Schmidt <44101708+5chmi...@users.noreply.github.com> Date: Sat, 16 Sep 2023 16:24:13 +0200 Subject: [PATCH 1/6] [clang-tidy] add modernize-use-std-numbers check This check finds constants and function calls to math functions that can be replaced with c++20's mathematical constants ('numbers' header) and offers fixit-hints. Does not match the use of variables or macros with that value and instead, offers a replacement at the definition of said variables and macros. --- .../clang-tidy/modernize/CMakeLists.txt | 1 + .../modernize/ModernizeTidyModule.cpp | 3 + .../modernize/UseStdNumbersCheck.cpp | 377 ++ .../clang-tidy/modernize/UseStdNumbersCheck.h | 37 ++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../docs/clang-tidy/checks/list.rst | 3 +- .../checks/modernize/use-std-numbers.rst | 25 ++ .../checkers/modernize/use-std-numbers.cpp| 205 ++ 8 files changed, 656 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-numbers.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index 717c400c4790330..d82353d74fbd0d4 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -16,6 +16,7 @@ add_clang_library(clangTidyModernizeModule MakeSharedCheck.cpp MakeSmartPtrCheck.cpp MakeUniqueCheck.cpp + UseStdNumbersCheck.cpp ModernizeTidyModule.cpp PassByValueCheck.cpp RawStringLiteralCheck.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index 73751cf2705068d..73584e20166f66a 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -18,6 +18,7 @@ #include "MacroToEnumCheck.h" #include "MakeSharedCheck.h" #include "MakeUniqueCheck.h" +#include "UseStdNumbersCheck.h" #include "PassByValueCheck.h" #include "RawStringLiteralCheck.h" #include "RedundantVoidArgCheck.h" @@ -65,6 +66,8 @@ class ModernizeModule : public ClangTidyModule { CheckFactories.registerCheck("modernize-macro-to-enum"); CheckFactories.registerCheck("modernize-make-shared"); CheckFactories.registerCheck("modernize-make-unique"); +CheckFactories.registerCheck( +"modernize-use-std-numbers"); CheckFactories.registerCheck("modernize-pass-by-value"); CheckFactories.registerCheck("modernize-use-std-print"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp new file mode 100644 index 000..c23dc6671013bc3 --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp @@ -0,0 +1,377 @@ +//===--- UseStdNumbersCheck.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 "UseStdNumbersCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Expr.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/Type.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/ASTMatchersInternal.h" +#include "clang/ASTMatchers/ASTMatchersMacros.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/TokenKinds.h" +#include "clang/Lex/PPCallbacks.h" +#include "clang/Lex/Preprocessor.h" +#include "clang/Lex/Token.h" +#include "clang/Tooling/Transformer/RewriteRule.h" +#include "clang/Tooling/Transformer/Stencil.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/MathExtras.h" +#include +#include + +namespace { +using namespace clang::ast_matchers; +using clang::ast_matchers::internal::Matcher; +using clang::transformer::addInclude; +using clang::transformer::applyFirst; +using clang::transformer::ASTEdit; +using clang::transformer::cat; +using clang::transformer::changeTo; +using clang::transformer::edit; +using clang::transformer::EditGenerator; +using clang::transformer::flattenVector; +using clang::transformer::RewriteRuleWith; +using llvm::StringRef; + +constexpr double Pi =
[clang-tools-extra] [clang-tidy] add modernize-use-std-numbers (PR #66583)
https://github.com/5chmidti updated https://github.com/llvm/llvm-project/pull/66583 >From 8f5e9e6024b0db8f251625669adbc5d607da83cb Mon Sep 17 00:00:00 2001 From: Julian Schmidt <44101708+5chmi...@users.noreply.github.com> Date: Sat, 16 Sep 2023 16:24:13 +0200 Subject: [PATCH 1/5] [clang-tidy] add modernize-use-std-numbers check This check finds constants and function calls to math functions that can be replaced with c++20's mathematical constants ('numbers' header) and offers fixit-hints. Does not match the use of variables or macros with that value and instead, offers a replacement at the definition of said variables and macros. --- .../clang-tidy/modernize/CMakeLists.txt | 1 + .../modernize/ModernizeTidyModule.cpp | 3 + .../modernize/UseStdNumbersCheck.cpp | 377 ++ .../clang-tidy/modernize/UseStdNumbersCheck.h | 37 ++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../docs/clang-tidy/checks/list.rst | 3 +- .../checks/modernize/use-std-numbers.rst | 25 ++ .../checkers/modernize/use-std-numbers.cpp| 205 ++ 8 files changed, 656 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-numbers.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index 717c400c4790330..d82353d74fbd0d4 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -16,6 +16,7 @@ add_clang_library(clangTidyModernizeModule MakeSharedCheck.cpp MakeSmartPtrCheck.cpp MakeUniqueCheck.cpp + UseStdNumbersCheck.cpp ModernizeTidyModule.cpp PassByValueCheck.cpp RawStringLiteralCheck.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index 73751cf2705068d..73584e20166f66a 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -18,6 +18,7 @@ #include "MacroToEnumCheck.h" #include "MakeSharedCheck.h" #include "MakeUniqueCheck.h" +#include "UseStdNumbersCheck.h" #include "PassByValueCheck.h" #include "RawStringLiteralCheck.h" #include "RedundantVoidArgCheck.h" @@ -65,6 +66,8 @@ class ModernizeModule : public ClangTidyModule { CheckFactories.registerCheck("modernize-macro-to-enum"); CheckFactories.registerCheck("modernize-make-shared"); CheckFactories.registerCheck("modernize-make-unique"); +CheckFactories.registerCheck( +"modernize-use-std-numbers"); CheckFactories.registerCheck("modernize-pass-by-value"); CheckFactories.registerCheck("modernize-use-std-print"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp new file mode 100644 index 000..c23dc6671013bc3 --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp @@ -0,0 +1,377 @@ +//===--- UseStdNumbersCheck.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 "UseStdNumbersCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Expr.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/Type.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/ASTMatchersInternal.h" +#include "clang/ASTMatchers/ASTMatchersMacros.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/TokenKinds.h" +#include "clang/Lex/PPCallbacks.h" +#include "clang/Lex/Preprocessor.h" +#include "clang/Lex/Token.h" +#include "clang/Tooling/Transformer/RewriteRule.h" +#include "clang/Tooling/Transformer/Stencil.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/MathExtras.h" +#include +#include + +namespace { +using namespace clang::ast_matchers; +using clang::ast_matchers::internal::Matcher; +using clang::transformer::addInclude; +using clang::transformer::applyFirst; +using clang::transformer::ASTEdit; +using clang::transformer::cat; +using clang::transformer::changeTo; +using clang::transformer::edit; +using clang::transformer::EditGenerator; +using clang::transformer::flattenVector; +using clang::transformer::RewriteRuleWith; +using llvm::StringRef; + +constexpr double Pi =
[clang-tools-extra] [clang-tidy] add modernize-use-std-numbers (PR #66583)
https://github.com/5chmidti edited https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
https://github.com/5chmidti updated https://github.com/llvm/llvm-project/pull/66583 >From 8f5e9e6024b0db8f251625669adbc5d607da83cb Mon Sep 17 00:00:00 2001 From: Julian Schmidt <44101708+5chmi...@users.noreply.github.com> Date: Sat, 16 Sep 2023 16:24:13 +0200 Subject: [PATCH 1/4] [clang-tidy] add modernize-use-std-numbers check This check finds constants and function calls to math functions that can be replaced with c++20's mathematical constants ('numbers' header) and offers fixit-hints. Does not match the use of variables or macros with that value and instead, offers a replacement at the definition of said variables and macros. --- .../clang-tidy/modernize/CMakeLists.txt | 1 + .../modernize/ModernizeTidyModule.cpp | 3 + .../modernize/UseStdNumbersCheck.cpp | 377 ++ .../clang-tidy/modernize/UseStdNumbersCheck.h | 37 ++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../docs/clang-tidy/checks/list.rst | 3 +- .../checks/modernize/use-std-numbers.rst | 25 ++ .../checkers/modernize/use-std-numbers.cpp| 205 ++ 8 files changed, 656 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-numbers.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index 717c400c4790330..d82353d74fbd0d4 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -16,6 +16,7 @@ add_clang_library(clangTidyModernizeModule MakeSharedCheck.cpp MakeSmartPtrCheck.cpp MakeUniqueCheck.cpp + UseStdNumbersCheck.cpp ModernizeTidyModule.cpp PassByValueCheck.cpp RawStringLiteralCheck.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index 73751cf2705068d..73584e20166f66a 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -18,6 +18,7 @@ #include "MacroToEnumCheck.h" #include "MakeSharedCheck.h" #include "MakeUniqueCheck.h" +#include "UseStdNumbersCheck.h" #include "PassByValueCheck.h" #include "RawStringLiteralCheck.h" #include "RedundantVoidArgCheck.h" @@ -65,6 +66,8 @@ class ModernizeModule : public ClangTidyModule { CheckFactories.registerCheck("modernize-macro-to-enum"); CheckFactories.registerCheck("modernize-make-shared"); CheckFactories.registerCheck("modernize-make-unique"); +CheckFactories.registerCheck( +"modernize-use-std-numbers"); CheckFactories.registerCheck("modernize-pass-by-value"); CheckFactories.registerCheck("modernize-use-std-print"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp new file mode 100644 index 000..c23dc6671013bc3 --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp @@ -0,0 +1,377 @@ +//===--- UseStdNumbersCheck.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 "UseStdNumbersCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Expr.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/Type.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/ASTMatchersInternal.h" +#include "clang/ASTMatchers/ASTMatchersMacros.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/TokenKinds.h" +#include "clang/Lex/PPCallbacks.h" +#include "clang/Lex/Preprocessor.h" +#include "clang/Lex/Token.h" +#include "clang/Tooling/Transformer/RewriteRule.h" +#include "clang/Tooling/Transformer/Stencil.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/MathExtras.h" +#include +#include + +namespace { +using namespace clang::ast_matchers; +using clang::ast_matchers::internal::Matcher; +using clang::transformer::addInclude; +using clang::transformer::applyFirst; +using clang::transformer::ASTEdit; +using clang::transformer::cat; +using clang::transformer::changeTo; +using clang::transformer::edit; +using clang::transformer::EditGenerator; +using clang::transformer::flattenVector; +using clang::transformer::RewriteRuleWith; +using llvm::StringRef; + +constexpr double Pi =
[clang-tools-extra] [clang-tidy] add modernize-use-std-numbers (PR #66583)
@@ -0,0 +1,25 @@ +.. title:: clang-tidy - modernize-use-std-numbers + +modernize-use-std-numbers += + +This check finds constants and function calls to math functions that can be replaced EugeneZelenko wrote: Please synchronize first statement with Release Notes. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -168,6 +168,12 @@ New checks Replace ``enable_if`` with C++20 requires clauses. +- New :doc:`modernize-use-std-numbers + ` check. + + Finds constants and function calls to math functions that can be replaced + with c++20's mathematical constants ('numbers' header). EugeneZelenko wrote: Please use double back-ticks for `numbers`. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -0,0 +1,377 @@ +//===--- UseStdNumbersCheck.cpp - clang_tidy -===// EugeneZelenko wrote: Please make it same length as similar line at bottom of comment. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
@@ -0,0 +1,25 @@ +.. title:: clang-tidy - modernize-use-std-numbers + +modernize-use-std-numbers += + +This check finds constants and function calls to math functions that can be replaced +with c++20's mathematical constants ('numbers' header) and offers fixit-hints. EugeneZelenko wrote: `fix-it`. https://github.com/llvm/llvm-project/pull/66583 ___ 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 modernize-use-std-numbers (PR #66583)
https://github.com/5chmidti updated https://github.com/llvm/llvm-project/pull/66583 >From 8f5e9e6024b0db8f251625669adbc5d607da83cb Mon Sep 17 00:00:00 2001 From: Julian Schmidt <44101708+5chmi...@users.noreply.github.com> Date: Sat, 16 Sep 2023 16:24:13 +0200 Subject: [PATCH 1/3] [clang-tidy] add modernize-use-std-numbers check This check finds constants and function calls to math functions that can be replaced with c++20's mathematical constants ('numbers' header) and offers fixit-hints. Does not match the use of variables or macros with that value and instead, offers a replacement at the definition of said variables and macros. --- .../clang-tidy/modernize/CMakeLists.txt | 1 + .../modernize/ModernizeTidyModule.cpp | 3 + .../modernize/UseStdNumbersCheck.cpp | 377 ++ .../clang-tidy/modernize/UseStdNumbersCheck.h | 37 ++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../docs/clang-tidy/checks/list.rst | 3 +- .../checks/modernize/use-std-numbers.rst | 25 ++ .../checkers/modernize/use-std-numbers.cpp| 205 ++ 8 files changed, 656 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-numbers.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index 717c400c4790330..d82353d74fbd0d4 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -16,6 +16,7 @@ add_clang_library(clangTidyModernizeModule MakeSharedCheck.cpp MakeSmartPtrCheck.cpp MakeUniqueCheck.cpp + UseStdNumbersCheck.cpp ModernizeTidyModule.cpp PassByValueCheck.cpp RawStringLiteralCheck.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index 73751cf2705068d..73584e20166f66a 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -18,6 +18,7 @@ #include "MacroToEnumCheck.h" #include "MakeSharedCheck.h" #include "MakeUniqueCheck.h" +#include "UseStdNumbersCheck.h" #include "PassByValueCheck.h" #include "RawStringLiteralCheck.h" #include "RedundantVoidArgCheck.h" @@ -65,6 +66,8 @@ class ModernizeModule : public ClangTidyModule { CheckFactories.registerCheck("modernize-macro-to-enum"); CheckFactories.registerCheck("modernize-make-shared"); CheckFactories.registerCheck("modernize-make-unique"); +CheckFactories.registerCheck( +"modernize-use-std-numbers"); CheckFactories.registerCheck("modernize-pass-by-value"); CheckFactories.registerCheck("modernize-use-std-print"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp new file mode 100644 index 000..c23dc6671013bc3 --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp @@ -0,0 +1,377 @@ +//===--- UseStdNumbersCheck.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 "UseStdNumbersCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Expr.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/Type.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/ASTMatchersInternal.h" +#include "clang/ASTMatchers/ASTMatchersMacros.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/TokenKinds.h" +#include "clang/Lex/PPCallbacks.h" +#include "clang/Lex/Preprocessor.h" +#include "clang/Lex/Token.h" +#include "clang/Tooling/Transformer/RewriteRule.h" +#include "clang/Tooling/Transformer/Stencil.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/MathExtras.h" +#include +#include + +namespace { +using namespace clang::ast_matchers; +using clang::ast_matchers::internal::Matcher; +using clang::transformer::addInclude; +using clang::transformer::applyFirst; +using clang::transformer::ASTEdit; +using clang::transformer::cat; +using clang::transformer::changeTo; +using clang::transformer::edit; +using clang::transformer::EditGenerator; +using clang::transformer::flattenVector; +using clang::transformer::RewriteRuleWith; +using llvm::StringRef; + +constexpr double Pi =
[clang-tools-extra] [clang-tidy] add modernize-use-std-numbers (PR #66583)
https://github.com/5chmidti updated https://github.com/llvm/llvm-project/pull/66583 >From 8f5e9e6024b0db8f251625669adbc5d607da83cb Mon Sep 17 00:00:00 2001 From: Julian Schmidt <44101708+5chmi...@users.noreply.github.com> Date: Sat, 16 Sep 2023 16:24:13 +0200 Subject: [PATCH] [clang-tidy] add modernize-use-std-numbers check This check finds constants and function calls to math functions that can be replaced with c++20's mathematical constants ('numbers' header) and offers fixit-hints. Does not match the use of variables or macros with that value and instead, offers a replacement at the definition of said variables and macros. --- .../clang-tidy/modernize/CMakeLists.txt | 1 + .../modernize/ModernizeTidyModule.cpp | 3 + .../modernize/UseStdNumbersCheck.cpp | 377 ++ .../clang-tidy/modernize/UseStdNumbersCheck.h | 37 ++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../docs/clang-tidy/checks/list.rst | 3 +- .../checks/modernize/use-std-numbers.rst | 25 ++ .../checkers/modernize/use-std-numbers.cpp| 205 ++ 8 files changed, 656 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-numbers.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index 717c400c4790330..d82353d74fbd0d4 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -16,6 +16,7 @@ add_clang_library(clangTidyModernizeModule MakeSharedCheck.cpp MakeSmartPtrCheck.cpp MakeUniqueCheck.cpp + UseStdNumbersCheck.cpp ModernizeTidyModule.cpp PassByValueCheck.cpp RawStringLiteralCheck.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index 73751cf2705068d..73584e20166f66a 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -18,6 +18,7 @@ #include "MacroToEnumCheck.h" #include "MakeSharedCheck.h" #include "MakeUniqueCheck.h" +#include "UseStdNumbersCheck.h" #include "PassByValueCheck.h" #include "RawStringLiteralCheck.h" #include "RedundantVoidArgCheck.h" @@ -65,6 +66,8 @@ class ModernizeModule : public ClangTidyModule { CheckFactories.registerCheck("modernize-macro-to-enum"); CheckFactories.registerCheck("modernize-make-shared"); CheckFactories.registerCheck("modernize-make-unique"); +CheckFactories.registerCheck( +"modernize-use-std-numbers"); CheckFactories.registerCheck("modernize-pass-by-value"); CheckFactories.registerCheck("modernize-use-std-print"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp new file mode 100644 index 000..c23dc6671013bc3 --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp @@ -0,0 +1,377 @@ +//===--- UseStdNumbersCheck.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 "UseStdNumbersCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Expr.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/Type.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/ASTMatchersInternal.h" +#include "clang/ASTMatchers/ASTMatchersMacros.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/TokenKinds.h" +#include "clang/Lex/PPCallbacks.h" +#include "clang/Lex/Preprocessor.h" +#include "clang/Lex/Token.h" +#include "clang/Tooling/Transformer/RewriteRule.h" +#include "clang/Tooling/Transformer/Stencil.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/MathExtras.h" +#include +#include + +namespace { +using namespace clang::ast_matchers; +using clang::ast_matchers::internal::Matcher; +using clang::transformer::addInclude; +using clang::transformer::applyFirst; +using clang::transformer::ASTEdit; +using clang::transformer::cat; +using clang::transformer::changeTo; +using clang::transformer::edit; +using clang::transformer::EditGenerator; +using clang::transformer::flattenVector; +using clang::transformer::RewriteRuleWith; +using llvm::StringRef; + +constexpr double Pi =
[clang-tools-extra] [clang-tidy] add modernize-use-std-numbers (PR #66583)
https://github.com/5chmidti updated https://github.com/llvm/llvm-project/pull/66583 >From 26fb41a2903110b6fc8bd986284d41b1fe6814d5 Mon Sep 17 00:00:00 2001 From: Julian Schmidt <44101708+5chmi...@users.noreply.github.com> Date: Sat, 16 Sep 2023 16:24:13 +0200 Subject: [PATCH] [clang-tidy] add modernize-use-std-numbers check This check finds constants and function calls to math functions that can be replaced with c++20's mathematical constants ('numbers' header) and offers fixit-hints. Does not match the use of variables or macros with that value and instead, offers a replacement at the definition of said variables and macros. --- .../clang-tidy/modernize/CMakeLists.txt | 1 + .../modernize/ModernizeTidyModule.cpp | 3 + .../modernize/UseStdNumbersCheck.cpp | 377 ++ .../clang-tidy/modernize/UseStdNumbersCheck.h | 37 ++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../docs/clang-tidy/checks/list.rst | 3 +- .../checks/modernize/use-std-numbers.rst | 25 ++ .../checkers/modernize/use-std-numbers.cpp| 205 ++ 8 files changed, 656 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/modernize/use-std-numbers.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index 717c400c4790330..d82353d74fbd0d4 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -16,6 +16,7 @@ add_clang_library(clangTidyModernizeModule MakeSharedCheck.cpp MakeSmartPtrCheck.cpp MakeUniqueCheck.cpp + UseStdNumbersCheck.cpp ModernizeTidyModule.cpp PassByValueCheck.cpp RawStringLiteralCheck.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index 73751cf2705068d..73584e20166f66a 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -18,6 +18,7 @@ #include "MacroToEnumCheck.h" #include "MakeSharedCheck.h" #include "MakeUniqueCheck.h" +#include "UseStdNumbersCheck.h" #include "PassByValueCheck.h" #include "RawStringLiteralCheck.h" #include "RedundantVoidArgCheck.h" @@ -65,6 +66,8 @@ class ModernizeModule : public ClangTidyModule { CheckFactories.registerCheck("modernize-macro-to-enum"); CheckFactories.registerCheck("modernize-make-shared"); CheckFactories.registerCheck("modernize-make-unique"); +CheckFactories.registerCheck( +"modernize-use-std-numbers"); CheckFactories.registerCheck("modernize-pass-by-value"); CheckFactories.registerCheck("modernize-use-std-print"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp new file mode 100644 index 000..272b17919ba86f9 --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/UseStdNumbersCheck.cpp @@ -0,0 +1,377 @@ +//===--- UseStdNumbersCheck.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 "UseStdNumbersCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Expr.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/Type.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/ASTMatchersInternal.h" +#include "clang/ASTMatchers/ASTMatchersMacros.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/TokenKinds.h" +#include "clang/Lex/PPCallbacks.h" +#include "clang/Lex/Preprocessor.h" +#include "clang/Lex/Token.h" +#include "clang/Tooling/Transformer/RewriteRule.h" +#include "clang/Tooling/Transformer/Stencil.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/MathExtras.h" +#include +#include + +namespace { +using namespace clang::ast_matchers; +using clang::ast_matchers::internal::Matcher; +using clang::transformer::addInclude; +using clang::transformer::applyFirst; +using clang::transformer::ASTEdit; +using clang::transformer::cat; +using clang::transformer::changeTo; +using clang::transformer::edit; +using clang::transformer::EditGenerator; +using clang::transformer::flattenVector; +using clang::transformer::RewriteRuleWith; +using llvm::StringRef; + +constexpr double Pi =
[clang-tools-extra] [clang-tidy] add modernize-use-std-numbers (PR #66583)
https://github.com/5chmidti edited https://github.com/llvm/llvm-project/pull/66583 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits