Selaa lähdekoodia

refactor: clang-tidy for format.h

Sam Jaffe 5 päivää sitten
vanhempi
commit
e10fb6e947
2 muutettua tiedostoa jossa 89 lisäystä ja 54 poistoa
  1. 5 0
      .clang-tidy
  2. 84 54
      include/jvalidate/format.h

+ 5 - 0
.clang-tidy

@@ -4,6 +4,7 @@ Checks: >
   -bugprone-std-namespace-modification,
   -bugprone-exception-escape,
   -bugprone-macro-parentheses,
+  -bugprone-narrowing-conversions,
   -bugprone-crtp-constructor-accessibility,
   clang-diagnostic-*,
   -clang-diagnostic-pragma-once-outside-header,
@@ -13,7 +14,9 @@ Checks: >
   -cppcoreguidelines-avoid-const-or-ref-data-members,
   -cppcoreguidelines-use-enum-class,
   -cppcoreguidelines-pro-type-member-init,
+  -cppcoreguidelines-pro-type-vararg,
   -cppcoreguidelines-pro-bounds-avoid-unchecked-container-access,
+  -cppcoreguidelines-pro-bounds-array-to-pointer-decay,
   google-*,
   -google-readability-namespace-comments,
   llvm-*,
@@ -92,5 +95,7 @@ CheckOptions:
     value: true
   - key: readability-function-cognitive-complexity.IgnoreMacros
     value: true
+  - key: readability-simplify-boolean-expr.IgnoreMacros
+    value: true
 ...
 

+ 84 - 54
include/jvalidate/format.h

@@ -1,10 +1,22 @@
 #pragma once
+#include <jvalidate/_config.h>
+#include <jvalidate/_macro.h>
+
+/*
+ NOLINTBEGIN(readability-identifier-length,
+             bugprone-inc-dec-in-conditions,
+             cppcoreguidelines-avoid-magic-numbers,
+             bugprone-suspicious-stringview-data-usage,
+             readability-implicit-bool-conversion,
+             cppcoreguidelines-narrowing-conversions,
+             readability-identifier-length)
+ */
+#include <cstdint>
 #include <cstdio>
 #include <functional>
-#include <jvalidate/_macro.h>
 
 #include <cctype>
-#include <chrono>
+#include <chrono> // IWYU pragma: keep
 #include <cstddef>
 #include <cstring>
 #include <ctime>
@@ -62,7 +74,7 @@ inline bool is_hex(std::string_view s) {
   return s.find_first_not_of(g_hex_digits) == std::string::npos;
 }
 
-struct result {
+struct Result {
   ptrdiff_t consumed;
   bool valid;
 };
@@ -70,16 +82,16 @@ struct result {
 inline bool is_leapyear(int y) { return (y % 400) == 0 || ((y % 4) == 0 && (y % 100) != 0); }
 
 inline bool illegal_date(int y, int m, int d) {
-  static constexpr int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
+  static constexpr std::array<int, 12> days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
   if (is_leapyear(y) && m == 1) {
     --d;
   }
-  return d > days[m];
+  return m >= 0 && m < days.size() && d > days.at(m);
 }
 
-inline result date(std::string_view dt) {
+inline Result date(std::string_view dt) {
   struct tm tm;
-  if (auto end = strptime(dt.data(), "%Y-%m-%d", &tm); end) {
+  if (char * end = strptime(dt.data(), "%Y-%m-%d", &tm); end) {
     if ((end - dt.data()) != 10 || illegal_date(tm.tm_year + 1900, tm.tm_mon, tm.tm_mday)) {
       return {.consumed = 0, .valid = false};
     }
@@ -90,7 +102,7 @@ inline result date(std::string_view dt) {
 
 inline bool is_leapsecond(std::tm tm) {
   if (tm.tm_sec != 60) {
-    return true;
+    return true; // NOLINT(readability-simplify-boolean-expr) See below...
   }
 
 #if __cpp_lib_chrono >= 201907L
@@ -129,7 +141,7 @@ inline bool is_pchar(std::basic_string_view<CharT> part, size_t & pos,
     return pos + 2 < part.size() && std::strchr(g_hex_digits, part[++pos]) &&
            std::strchr(g_hex_digits, part[++pos]);
   }
-  return extra_valid_chars.find(part[pos]) != part.npos;
+  return extra_valid_chars.find(part[pos]) != std::basic_string_view<CharT>::npos;
 };
 
 inline bool is_uri_template_literal(std::u32string_view part, size_t & pos) {
@@ -163,7 +175,7 @@ inline bool is_uri_template_expression(std::u32string_view part) {
        part.remove_prefix(std::min(part.size(), pos)), pos = part.find(',')) {
     std::u32string_view varspec = part.substr(0, pos);
     std::u32string_view expand;
-    if (size_t const mod = varspec.find_first_of(U":*"); mod != varspec.npos) {
+    if (size_t const mod = varspec.find_first_of(U":*"); mod != std::u32string_view::npos) {
       expand = varspec.substr(mod + 1);
       varspec.remove_suffix(expand.size() + 1);
     }
@@ -175,7 +187,8 @@ inline bool is_uri_template_expression(std::u32string_view part) {
       return false;
     }
     for (size_t i = 0; i < varspec.size(); ++i) {
-      RETURN_UNLESS(is_uri_template_varchar(varspec, i) || (i > 0 && varspec[i] == '.'), false);
+      JVALIDATE_RETURN_UNLESS(is_uri_template_varchar(varspec, i) || (i > 0 && varspec[i] == '.'),
+                              false);
     }
   }
 
@@ -229,7 +242,7 @@ template <typename CharT> bool test_uri_part(std::basic_string_view<CharT> & uri
   auto part = uri.substr(pos + 1);
   uri = uri.substr(0, pos);
   for (size_t pos = 0; pos < part.size(); ++pos) {
-    RETURN_UNLESS(detail::is_pchar(part, pos, ":@/?"), false);
+    JVALIDATE_RETURN_UNLESS(detail::is_pchar(part, pos, ":@/?"), false);
   }
   return true;
 };
@@ -248,12 +261,12 @@ inline bool time(std::string_view dt) {
 }
 
 inline bool utc_millisec(std::string_view utc) {
-  int64_t itime;
+  int64_t itime = 0;
   if (auto [end, ec] = std::from_chars(utc.begin(), utc.end(), itime);
       ec == std::errc{} && end == utc.end()) {
     return true;
   }
-  double dtime;
+  double dtime = 0.0;
   auto [end, ec] = std::from_chars(utc.begin(), utc.end(), dtime);
   return ec == std::errc{} && end == utc.end();
 }
@@ -279,11 +292,11 @@ inline bool e_123_phone(std::string_view phone) {
   }
   if (phone[0] != '+') {
     constexpr size_t g_usa_phone_tokens = 3;
-    char area[4], head[4], tail[5];
+    char area[4], head[4], tail[5]; // NOLINT
     return sscanf(phone.data(), "(%3s) %3s %4s", area, head, tail) == g_usa_phone_tokens &&
            detail::is_dec(area, 3) && detail::is_dec(head, 3) && detail::is_dec(tail, 4);
   }
-  char tok0[4], tok1[4], tok2[4], tok3[5];
+  char tok0[4], tok1[4], tok2[4], tok3[5]; // NOLINT
   constexpr size_t g_i18n_phone_tokens = 4;
   return sscanf(phone.data(), "+%3s %3s %3s %4s", tok0, tok1, tok2, tok3) == g_i18n_phone_tokens &&
          detail::is_dec(tok0, 1, 3) && detail::is_dec(tok1, 2, 3) && detail::is_dec(tok2, 2, 3) &&
@@ -318,7 +331,8 @@ inline bool time(std::string_view dt) {
     return dt.size() == 1 && detail::is_leapsecond(tm);
   }
   if (std::strchr("+-", dt[0])) {
-    return strptime(dt.data() + 1, "%R", &tm) == dt.end() && detail::is_leapsecond(tm);
+    dt.remove_prefix(1);
+    return strptime(dt.data(), "%R", &tm) == dt.end() && detail::is_leapsecond(tm);
   }
   return false;
 }
@@ -337,28 +351,28 @@ template <typename CharT> inline bool uri(std::basic_string_view<CharT> uri) {
 
   // https://www.rfc-editor.org/rfc/rfc3986.html#appendix-A
   if (size_t const pos = uri.find(':'); pos != uri.npos) {
-    RETURN_UNLESS(std::isalpha(uri[0]), false);
+    JVALIDATE_RETURN_UNLESS(std::isalpha(uri[0]), false);
     for (size_t i = 1; i < pos; ++i) {
-      RETURN_UNLESS(std::isalnum(uri[i]) || std::strchr("+-.", uri[i]), false);
+      JVALIDATE_RETURN_UNLESS(std::isalnum(uri[i]) || std::strchr("+-.", uri[i]), false);
     }
     uri.remove_prefix(pos + 1);
   } else {
     return false;
   }
 
-  RETURN_UNLESS(detail::test_uri_part(uri, '#'), false);
-  RETURN_UNLESS(detail::test_uri_part(uri, '?'), false);
+  JVALIDATE_RETURN_UNLESS(detail::test_uri_part(uri, '#'), false);
+  JVALIDATE_RETURN_UNLESS(detail::test_uri_part(uri, '?'), false);
 
   auto path = uri;
   if (uri.starts_with(delim::double_slash)) {
     uri.remove_prefix(2);
     path = uri.substr(std::min(uri.size(), uri.find('/')));
     uri.remove_suffix(path.size());
-    RETURN_UNLESS(detail::is_uri_authority(uri), false);
+    JVALIDATE_RETURN_UNLESS(detail::is_uri_authority(uri), false);
   }
 
   for (size_t i = 0; i < path.size(); ++i) {
-    RETURN_UNLESS(detail::is_pchar(path, i, "/:@"), false);
+    JVALIDATE_RETURN_UNLESS(detail::is_pchar(path, i, "/:@"), false);
   }
 
   return true;
@@ -370,26 +384,26 @@ template <typename CharT> inline bool uri_reference(std::basic_string_view<CharT
     return true;
   }
 
-  RETURN_UNLESS(detail::test_uri_part(uri, '#'), false);
-  RETURN_UNLESS(detail::test_uri_part(uri, '?'), false);
+  JVALIDATE_RETURN_UNLESS(detail::test_uri_part(uri, '#'), false);
+  JVALIDATE_RETURN_UNLESS(detail::test_uri_part(uri, '?'), false);
 
   auto path = uri;
   if (uri.starts_with(delim::double_slash)) {
     uri.remove_prefix(2);
     path = uri.substr(std::min(uri.size(), uri.find('/')));
     uri.remove_suffix(path.size());
-    RETURN_UNLESS(detail::is_uri_authority(uri), false);
+    JVALIDATE_RETURN_UNLESS(detail::is_uri_authority(uri), false);
   }
 
   if (size_t const pos = path.find('/'); pos != path.npos) {
     for (size_t i = 0; i < pos; ++i) {
-      RETURN_UNLESS(detail::is_pchar(path, i, "@"), false);
+      JVALIDATE_RETURN_UNLESS(detail::is_pchar(path, i, "@"), false);
     }
     path.remove_prefix(pos);
   }
 
   for (size_t i = 0; i < path.size(); ++i) {
-    RETURN_UNLESS(detail::is_pchar(path, i, "/:@"), false);
+    JVALIDATE_RETURN_UNLESS(detail::is_pchar(path, i, "/:@"), false);
   }
 
   return true;
@@ -398,14 +412,14 @@ template <typename CharT> inline bool uri_reference(std::basic_string_view<CharT
 inline bool uri_template(std::u32string_view uri) {
   for (size_t i = 0; i < uri.size(); ++i) {
     if (uri[i] != '{') {
-      RETURN_UNLESS(detail::is_uri_template_literal(uri, i), false);
+      JVALIDATE_RETURN_UNLESS(detail::is_uri_template_literal(uri, i), false);
       continue;
     }
 
     std::u32string_view expr = uri.substr(i + 1);
     size_t const pos = expr.find('}');
-    RETURN_UNLESS(pos != uri.npos, false);
-    RETURN_UNLESS(detail::is_uri_template_expression(expr.substr(0, pos)), false);
+    JVALIDATE_RETURN_IF(pos == std::u32string_view::npos, false);
+    JVALIDATE_RETURN_UNLESS(detail::is_uri_template_expression(expr.substr(0, pos)), false);
     i += pos + 1;
   }
   return true;
@@ -414,9 +428,10 @@ inline bool uri_template(std::u32string_view uri) {
 inline bool uuid(std::string_view id) {
   constexpr size_t g_uuid_len = 36;
   constexpr size_t g_uuid_tokens = 5;
-  char tok0[9], tok1[5], tok2[5], tok3[5], tok4[13];
+  char tok0[9], tok1[5], tok2[5], tok3[5], tok4[13]; // NOLINT
 
   return id.size() == g_uuid_len &&
+         // NOLINTNEXTLINE(bugprone-suspicious-stringview-data-usage)
          sscanf(id.data(), "%8s-%4s-%4s-%4s-%12s", tok0, tok1, tok2, tok3, tok4) == g_uuid_tokens &&
          detail::is_hex(tok0) && detail::is_hex(tok1) && detail::is_hex(tok2) &&
          detail::is_hex(tok3) && detail::is_hex(tok4);
@@ -424,8 +439,9 @@ inline bool uuid(std::string_view id) {
 
 inline bool duration(std::string_view dur) {
   auto eat = [&dur](std::string_view text) {
-    char type;
-    unsigned int rep;
+    char type = '\0';
+    unsigned int rep = 0;
+    // NOLINTNEXTLINE(bugprone-suspicious-stringview-data-usage)
     if (sscanf(dur.data(), "%u%c", &rep, &type) != 2 || text.find(type) == std::string::npos) {
       return std::string::npos;
     }
@@ -454,8 +470,8 @@ inline bool duration(std::string_view dur) {
     std::string_view ymd{"YMD"};
     // Read YMD duration offsets in that order, allowing us to skip past them.
     while (not ymd.empty() && not dur.empty()) {
-      if (size_t n = eat(ymd); n != std::string::npos) {
-        ymd.remove_prefix(n + 1);
+      if (size_t const pos = eat(ymd); pos != std::string::npos) {
+        ymd.remove_prefix(pos + 1);
       } else {
         return false;
       }
@@ -475,8 +491,8 @@ inline bool duration(std::string_view dur) {
   std::string_view hms{"HMS"};
   // Read HMS duration offsets in that order, allowing us to skip past them.
   while (not hms.empty() && not dur.empty()) {
-    if (size_t n = eat(hms); n != std::string::npos) {
-      hms.remove_prefix(n + 1);
+    if (size_t const pos = eat(hms); pos != std::string::npos) {
+      hms.remove_prefix(pos + 1);
     } else {
       return false;
     }
@@ -561,10 +577,10 @@ template <typename CharT> inline bool hostname(std::basic_string_view<CharT> nam
 }
 
 inline bool ipv4(std::string_view ip) {
-  unsigned int ip0, ip1, ip2, ip3;
-  char eof;
+  unsigned int ip0, ip1, ip2, ip3; // NOLINT
+  char eof = '\0';
   // IPv4 address MAY only contain DIGITS and '.'
-  if (ip.find_first_not_of("0123456789.") != ip.npos) {
+  if (ip.find_first_not_of("0123456789.") != std::string_view::npos) {
     return false;
   }
 
@@ -572,7 +588,8 @@ inline bool ipv4(std::string_view ip) {
   if (ip[0] == '0' && std::isdigit(ip[1])) {
     return false;
   }
-  if (size_t n = ip.find(".0"); n != ip.npos && std::isdigit(ip[n + 2])) {
+  if (size_t const pos = ip.find(".0");
+      pos != std::string_view::npos && std::isdigit(ip[pos + 2])) {
     return false;
   }
 
@@ -621,17 +638,19 @@ inline bool ipv6(std::string_view ip) {
   }
 
   while (!ip.empty() && ++groups) {
-    int data;
+    int data = 0;
     if (sscanf(ip.data(), "%4x", &data) != 1) {
       // Not a 4-byte HEXDIGIT. Not sure that it's ever possible due to the
       // char filter above.
       return false;
     }
 
-    if (size_t const n = ip.find(':'); std::min(n, ip.size()) > 4) {
-      return false; // Segment too wide
-    } else if (n != std::string::npos) {
-      ip.remove_prefix(n + 1);
+    size_t const div_pos = ip.find(':');
+    if (std::min(div_pos, ip.size()) > 4) {
+      return false; // Segments must be between 1 and 4 characters long
+    }
+    if (div_pos != std::string::npos) {
+      ip.remove_prefix(div_pos + 1);
     } else {
       break; // End of String
     }
@@ -666,7 +685,7 @@ template <typename CharT> inline bool email(std::basic_string_view<CharT> em) {
   auto const who = em.substr(0, n);
   if (who.starts_with('"') && who.ends_with('"')) {
     // No validation
-  } else if (who.starts_with('.') || who.ends_with('.')) {
+  } else if (who.starts_with('.') || who.ends_with('.')) { // NOLINT(bugprone-branch-clone)
     return false;
   } else if (em.substr(0, n).find(delim::dotdot) != em.npos) {
     return false;
@@ -687,11 +706,11 @@ template <typename CharT> inline bool email(std::basic_string_view<CharT> em) {
 
   // When the DOMAIN is an IPv6, it must start with "IPv6:" for some
   // weird compatibility reason.
-  if (auto ip = detail::to_u8(domain); ip.starts_with("IPv6:")) {
+  auto ip = detail::to_u8(domain);
+  if (ip.starts_with("IPv6:")) {
     return ipv6(ip.substr(5));
-  } else {
-    return ipv4(ip);
   }
+  return ipv4(ip);
 }
 
 template <typename T> inline bool ctor_as_valid(std::string_view str) {
@@ -714,7 +733,7 @@ public:
   using StatelessPredicate = bool (*)(std::string_view);
   using Predicate = std::function<bool(std::string_view)>;
   using UserDefinedFormats = std::unordered_map<std::string, Predicate>;
-  enum class Status { Unknown, Unimplemented, Valid, Invalid };
+  enum class Status : int8_t { Unknown, Unimplemented, Valid, Invalid };
 
 private:
   // This isn't actually a user format, but we don't generate any special
@@ -766,8 +785,10 @@ private:
 
 public:
   FormatValidator() = default;
-  FormatValidator(Predicate is_regex) { formats_.insert_or_assign("regex", is_regex); }
-  FormatValidator(UserDefinedFormats const & formats, Predicate is_regex) : formats_(formats) {
+  explicit(false) FormatValidator(Predicate is_regex) {
+    formats_.insert_or_assign("regex", is_regex);
+  }
+  FormatValidator(UserDefinedFormats formats, Predicate is_regex) : formats_(std::move(formats)) {
     formats_.insert_or_assign("regex", is_regex);
   }
 
@@ -797,3 +818,12 @@ private:
 
 #undef CONSTRUCTS
 #undef UTF32
+/*
+ NOLINTEND(readability-identifier-length,
+           bugprone-inc-dec-in-conditions,
+           cppcoreguidelines-avoid-magic-numbers,
+           bugprone-suspicious-stringview-data-usage,
+           readability-implicit-bool-conversion,
+           cppcoreguidelines-narrowing-conversions,
+           readability-identifier-length)
+ */