Commit 443c1a946d5b22d35995a1f525ec3d58e8b467a9

Authored by Philip Top
Committed by GitHub
1 parent 020a21af

fix: wrap min and max for MSVC (#741)

* wrap min and max in parenthesis for Visual studio 2017 and earlier compatibility

* try pre-commit to detect min/max issues

* actually fix the identified issues from the new check

* more min/max fixes
.pre-commit-config.yaml
... ... @@ -71,6 +71,14 @@ repos:
71 71 entry: PyBind|Numpy|Cmake|CCache|PyTest|Github
72 72 exclude: .pre-commit-config.yaml
73 73  
  74 + - repo: local
  75 + hooks:
  76 + - id: avoid-msvc-macro
  77 + name: Avoid MSVC <=2017 min/max macro (use extra parens)
  78 + language: pygrep
  79 + entry: \b(min|max)\(
  80 + exclude: .pre-commit-config.yaml
  81 +
74 82 - repo: https://github.com/codespell-project/codespell
75 83 rev: v2.1.0
76 84 hooks:
... ...
include/CLI/TypeTools.hpp
... ... @@ -1542,8 +1542,8 @@ inline std::string sum_string_vector(const std::vector&lt;std::string&gt; &amp;values) {
1542 1542 output.append(arg);
1543 1543 }
1544 1544 } else {
1545   - if(val <= static_cast<double>(std::numeric_limits<std::int64_t>::min()) ||
1546   - val >= static_cast<double>(std::numeric_limits<std::int64_t>::max()) ||
  1545 + if(val <= static_cast<double>((std::numeric_limits<std::int64_t>::min)()) ||
  1546 + val >= static_cast<double>((std::numeric_limits<std::int64_t>::max)()) ||
1547 1547 val == static_cast<std::int64_t>(val)) {
1548 1548 output = detail::value_string(static_cast<int64_t>(val));
1549 1549 } else {
... ...
include/CLI/Validators.hpp
... ... @@ -527,7 +527,7 @@ class Range : public Validator {
527 527 /// Check for a non negative number
528 528 const Range NonNegativeNumber((std::numeric_limits<double>::max)(), "NONNEGATIVE");
529 529  
530   -/// Check for a positive valued number (val>0.0), min() her is the smallest positive number
  530 +/// Check for a positive valued number (val>0.0), <double>::min here is the smallest positive number
531 531 const Range PositiveNumber((std::numeric_limits<double>::min)(), (std::numeric_limits<double>::max)(), "POSITIVE");
532 532  
533 533 /// Produce a bounded range (factory). Min and max are inclusive.
... ...
tests/HelpersTest.cpp
... ... @@ -541,68 +541,68 @@ TEST_CASE(&quot;CheckedMultiply: Int&quot;, &quot;[helpers]&quot;) {
541 541 REQUIRE(CLI::detail::checked_multiply(a, b));
542 542 REQUIRE(0 == a);
543 543  
544   - a = std::numeric_limits<int>::max();
  544 + a = (std::numeric_limits<int>::max)();
545 545 b = 1;
546 546 REQUIRE(CLI::detail::checked_multiply(a, b));
547   - REQUIRE(std::numeric_limits<int>::max() == a);
  547 + REQUIRE((std::numeric_limits<int>::max)() == a);
548 548  
549   - a = std::numeric_limits<int>::max();
  549 + a = (std::numeric_limits<int>::max)();
550 550 b = 2;
551 551 REQUIRE(!CLI::detail::checked_multiply(a, b));
552   - REQUIRE(std::numeric_limits<int>::max() == a);
  552 + REQUIRE((std::numeric_limits<int>::max)() == a);
553 553  
554   - a = std::numeric_limits<int>::max();
  554 + a = (std::numeric_limits<int>::max)();
555 555 b = -1;
556 556 REQUIRE(CLI::detail::checked_multiply(a, b));
557   - REQUIRE(-std::numeric_limits<int>::max() == a);
  557 + REQUIRE(-(std::numeric_limits<int>::max)() == a);
558 558  
559   - a = std::numeric_limits<int>::max();
560   - b = std::numeric_limits<int>::max();
  559 + a = (std::numeric_limits<int>::max)();
  560 + b = (std::numeric_limits<int>::max)();
561 561 REQUIRE(!CLI::detail::checked_multiply(a, b));
562   - REQUIRE(std::numeric_limits<int>::max() == a);
  562 + REQUIRE((std::numeric_limits<int>::max)() == a);
563 563  
564   - a = std::numeric_limits<int>::min();
565   - b = std::numeric_limits<int>::max();
  564 + a = (std::numeric_limits<int>::min)();
  565 + b = (std::numeric_limits<int>::max)();
566 566 REQUIRE(!CLI::detail::checked_multiply(a, b));
567   - REQUIRE(std::numeric_limits<int>::min() == a);
  567 + REQUIRE((std::numeric_limits<int>::min)() == a);
568 568  
569   - a = std::numeric_limits<int>::min();
  569 + a = (std::numeric_limits<int>::min)();
570 570 b = 1;
571 571 REQUIRE(CLI::detail::checked_multiply(a, b));
572   - REQUIRE(std::numeric_limits<int>::min() == a);
  572 + REQUIRE((std::numeric_limits<int>::min)() == a);
573 573  
574   - a = std::numeric_limits<int>::min();
  574 + a = (std::numeric_limits<int>::min)();
575 575 b = -1;
576 576 REQUIRE(!CLI::detail::checked_multiply(a, b));
577   - REQUIRE(std::numeric_limits<int>::min() == a);
  577 + REQUIRE((std::numeric_limits<int>::min)() == a);
578 578  
579   - b = std::numeric_limits<int>::min();
  579 + b = (std::numeric_limits<int>::min)();
580 580 a = -1;
581 581 REQUIRE(!CLI::detail::checked_multiply(a, b));
582 582 REQUIRE(-1 == a);
583 583  
584   - a = std::numeric_limits<int>::min() / 100;
  584 + a = (std::numeric_limits<int>::min)() / 100;
585 585 b = 99;
586 586 REQUIRE(CLI::detail::checked_multiply(a, b));
587   - REQUIRE(std::numeric_limits<int>::min() / 100 * 99 == a);
  587 + REQUIRE((std::numeric_limits<int>::min)() / 100 * 99 == a);
588 588  
589   - a = std::numeric_limits<int>::min() / 100;
  589 + a = (std::numeric_limits<int>::min)() / 100;
590 590 b = -101;
591 591 REQUIRE(!CLI::detail::checked_multiply(a, b));
592   - REQUIRE(std::numeric_limits<int>::min() / 100 == a);
  592 + REQUIRE((std::numeric_limits<int>::min)() / 100 == a);
593 593 a = 2;
594   - b = std::numeric_limits<int>::min() / 2;
  594 + b = (std::numeric_limits<int>::min)() / 2;
595 595 REQUIRE(CLI::detail::checked_multiply(a, b));
596   - a = std::numeric_limits<int>::min() / 2;
  596 + a = (std::numeric_limits<int>::min)() / 2;
597 597 b = 2;
598 598 REQUIRE(CLI::detail::checked_multiply(a, b));
599 599  
600 600 a = 4;
601   - b = std::numeric_limits<int>::min() / 4;
  601 + b = (std::numeric_limits<int>::min)() / 4;
602 602 REQUIRE(CLI::detail::checked_multiply(a, b));
603 603  
604 604 a = 48;
605   - b = std::numeric_limits<int>::min() / 48;
  605 + b = (std::numeric_limits<int>::min)() / 48;
606 606 REQUIRE(CLI::detail::checked_multiply(a, b));
607 607 }
608 608  
... ... @@ -622,25 +622,25 @@ TEST_CASE(&quot;CheckedMultiply: SizeT&quot;, &quot;[helpers]&quot;) {
622 622 REQUIRE(CLI::detail::checked_multiply(a, b));
623 623 REQUIRE(0u == a);
624 624  
625   - a = std::numeric_limits<std::size_t>::max();
  625 + a = (std::numeric_limits<std::size_t>::max)();
626 626 b = 1u;
627 627 REQUIRE(CLI::detail::checked_multiply(a, b));
628   - REQUIRE(std::numeric_limits<std::size_t>::max() == a);
  628 + REQUIRE((std::numeric_limits<std::size_t>::max)() == a);
629 629  
630   - a = std::numeric_limits<std::size_t>::max();
  630 + a = (std::numeric_limits<std::size_t>::max)();
631 631 b = 2u;
632 632 REQUIRE(!CLI::detail::checked_multiply(a, b));
633   - REQUIRE(std::numeric_limits<std::size_t>::max() == a);
  633 + REQUIRE((std::numeric_limits<std::size_t>::max)() == a);
634 634  
635   - a = std::numeric_limits<std::size_t>::max();
636   - b = std::numeric_limits<std::size_t>::max();
  635 + a = (std::numeric_limits<std::size_t>::max)();
  636 + b = (std::numeric_limits<std::size_t>::max)();
637 637 REQUIRE(!CLI::detail::checked_multiply(a, b));
638   - REQUIRE(std::numeric_limits<std::size_t>::max() == a);
  638 + REQUIRE((std::numeric_limits<std::size_t>::max)() == a);
639 639  
640   - a = std::numeric_limits<std::size_t>::max() / 100;
  640 + a = (std::numeric_limits<std::size_t>::max)() / 100;
641 641 b = 99u;
642 642 REQUIRE(CLI::detail::checked_multiply(a, b));
643   - REQUIRE(std::numeric_limits<std::size_t>::max() / 100u * 99u == a);
  643 + REQUIRE((std::numeric_limits<std::size_t>::max)() / 100u * 99u == a);
644 644 }
645 645  
646 646 TEST_CASE("CheckedMultiply: Float", "[helpers]") {
... ... @@ -664,30 +664,30 @@ TEST_CASE(&quot;CheckedMultiply: Float&quot;, &quot;[helpers]&quot;) {
664 664 REQUIRE(CLI::detail::checked_multiply(a, b));
665 665 REQUIRE(-INFINITY == Approx(a));
666 666  
667   - a = std::numeric_limits<float>::max() / 100.0F;
  667 + a = (std::numeric_limits<float>::max)() / 100.0F;
668 668 b = 1.0F;
669 669 REQUIRE(CLI::detail::checked_multiply(a, b));
670   - REQUIRE(std::numeric_limits<float>::max() / 100.0F == Approx(a));
  670 + REQUIRE((std::numeric_limits<float>::max)() / 100.0F == Approx(a));
671 671  
672   - a = std::numeric_limits<float>::max() / 100.0F;
  672 + a = (std::numeric_limits<float>::max)() / 100.0F;
673 673 b = 99.0F;
674 674 REQUIRE(CLI::detail::checked_multiply(a, b));
675   - REQUIRE(std::numeric_limits<float>::max() / 100.0F * 99.0F == Approx(a));
  675 + REQUIRE((std::numeric_limits<float>::max)() / 100.0F * 99.0F == Approx(a));
676 676  
677   - a = std::numeric_limits<float>::max() / 100.0F;
  677 + a = (std::numeric_limits<float>::max)() / 100.0F;
678 678 b = 101;
679 679 REQUIRE(!CLI::detail::checked_multiply(a, b));
680   - REQUIRE(std::numeric_limits<float>::max() / 100.0F == Approx(a));
  680 + REQUIRE((std::numeric_limits<float>::max)() / 100.0F == Approx(a));
681 681  
682   - a = std::numeric_limits<float>::max() / 100.0F;
  682 + a = (std::numeric_limits<float>::max)() / 100.0F;
683 683 b = -99;
684 684 REQUIRE(CLI::detail::checked_multiply(a, b));
685   - REQUIRE(std::numeric_limits<float>::max() / 100.0F * -99.0F == Approx(a));
  685 + REQUIRE((std::numeric_limits<float>::max)() / 100.0F * -99.0F == Approx(a));
686 686  
687   - a = std::numeric_limits<float>::max() / 100.0F;
  687 + a = (std::numeric_limits<float>::max)() / 100.0F;
688 688 b = -101;
689 689 REQUIRE(!CLI::detail::checked_multiply(a, b));
690   - REQUIRE(std::numeric_limits<float>::max() / 100.0F == Approx(a));
  690 + REQUIRE((std::numeric_limits<float>::max)() / 100.0F == Approx(a));
691 691 }
692 692  
693 693 TEST_CASE("CheckedMultiply: Double", "[helpers]") {
... ... @@ -711,30 +711,30 @@ TEST_CASE(&quot;CheckedMultiply: Double&quot;, &quot;[helpers]&quot;) {
711 711 REQUIRE(CLI::detail::checked_multiply(a, b));
712 712 REQUIRE(-INFINITY == Approx(a));
713 713  
714   - a = std::numeric_limits<double>::max() / 100;
  714 + a = (std::numeric_limits<double>::max)() / 100;
715 715 b = 1;
716 716 REQUIRE(CLI::detail::checked_multiply(a, b));
717   - REQUIRE(std::numeric_limits<double>::max() / 100 == Approx(a));
  717 + REQUIRE((std::numeric_limits<double>::max)() / 100 == Approx(a));
718 718  
719   - a = std::numeric_limits<double>::max() / 100;
  719 + a = (std::numeric_limits<double>::max)() / 100;
720 720 b = 99;
721 721 REQUIRE(CLI::detail::checked_multiply(a, b));
722   - REQUIRE(std::numeric_limits<double>::max() / 100 * 99 == Approx(a));
  722 + REQUIRE((std::numeric_limits<double>::max)() / 100 * 99 == Approx(a));
723 723  
724   - a = std::numeric_limits<double>::max() / 100;
  724 + a = (std::numeric_limits<double>::max)() / 100;
725 725 b = 101;
726 726 REQUIRE(!CLI::detail::checked_multiply(a, b));
727   - REQUIRE(std::numeric_limits<double>::max() / 100 == Approx(a));
  727 + REQUIRE((std::numeric_limits<double>::max)() / 100 == Approx(a));
728 728  
729   - a = std::numeric_limits<double>::max() / 100;
  729 + a = (std::numeric_limits<double>::max)() / 100;
730 730 b = -99;
731 731 REQUIRE(CLI::detail::checked_multiply(a, b));
732   - REQUIRE(std::numeric_limits<double>::max() / 100 * -99 == Approx(a));
  732 + REQUIRE((std::numeric_limits<double>::max)() / 100 * -99 == Approx(a));
733 733  
734   - a = std::numeric_limits<double>::max() / 100;
  734 + a = (std::numeric_limits<double>::max)() / 100;
735 735 b = -101;
736 736 REQUIRE(!CLI::detail::checked_multiply(a, b));
737   - REQUIRE(std::numeric_limits<double>::max() / 100 == Approx(a));
  737 + REQUIRE((std::numeric_limits<double>::max)() / 100 == Approx(a));
738 738 }
739 739  
740 740 // Yes, this is testing an app_helper :)
... ... @@ -1029,11 +1029,11 @@ TEST_CASE(&quot;Types: TypeName&quot;, &quot;[helpers]&quot;) {
1029 1029  
1030 1030 TEST_CASE("Types: OverflowSmall", "[helpers]") {
1031 1031 signed char x;
1032   - auto strmax = std::to_string(std::numeric_limits<signed char>::max() + 1);
  1032 + auto strmax = std::to_string((std::numeric_limits<signed char>::max)() + 1);
1033 1033 CHECK_FALSE(CLI::detail::lexical_cast(strmax, x));
1034 1034  
1035 1035 unsigned char y;
1036   - strmax = std::to_string(std::numeric_limits<unsigned char>::max() + 1);
  1036 + strmax = std::to_string((std::numeric_limits<unsigned char>::max)() + 1);
1037 1037 CHECK_FALSE(CLI::detail::lexical_cast(strmax, y));
1038 1038 }
1039 1039  
... ... @@ -1051,7 +1051,7 @@ TEST_CASE(&quot;Types: LexicalCastInt&quot;, &quot;[helpers]&quot;) {
1051 1051 CHECK_FALSE(CLI::detail::lexical_cast(signed_input, x_unsigned));
1052 1052  
1053 1053 unsigned char y;
1054   - std::string overflow_input = std::to_string(std::numeric_limits<uint64_t>::max()) + "0";
  1054 + std::string overflow_input = std::to_string((std::numeric_limits<uint64_t>::max)()) + "0";
1055 1055 CHECK_FALSE(CLI::detail::lexical_cast(overflow_input, y));
1056 1056  
1057 1057 char y_signed;
... ... @@ -1078,7 +1078,7 @@ TEST_CASE(&quot;Types: LexicalCastDouble&quot;, &quot;[helpers]&quot;) {
1078 1078 std::string bad_input = "hello";
1079 1079 CHECK_FALSE(CLI::detail::lexical_cast(bad_input, x));
1080 1080  
1081   - std::string overflow_input = "1" + std::to_string(std::numeric_limits<long double>::max());
  1081 + std::string overflow_input = "1" + std::to_string((std::numeric_limits<long double>::max)());
1082 1082 CHECK(CLI::detail::lexical_cast(overflow_input, x));
1083 1083 CHECK_FALSE(std::isfinite(x));
1084 1084  
... ...