checker.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2020 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ABSL_STRINGS_INTERNAL_STR_FORMAT_CHECKER_H_
  15. #define ABSL_STRINGS_INTERNAL_STR_FORMAT_CHECKER_H_
  16. #include <algorithm>
  17. #include "absl/base/attributes.h"
  18. #include "absl/strings/internal/str_format/arg.h"
  19. #include "absl/strings/internal/str_format/constexpr_parser.h"
  20. #include "absl/strings/internal/str_format/extension.h"
  21. // Compile time check support for entry points.
  22. #ifndef ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
  23. // We disable format checker under vscode intellisense compilation.
  24. // See https://github.com/microsoft/vscode-cpptools/issues/3683 for
  25. // more details.
  26. #if ABSL_HAVE_ATTRIBUTE(enable_if) && !defined(__native_client__) && \
  27. !defined(__INTELLISENSE__)
  28. #define ABSL_INTERNAL_ENABLE_FORMAT_CHECKER 1
  29. #endif // ABSL_HAVE_ATTRIBUTE(enable_if) && !defined(__native_client__) &&
  30. // !defined(__INTELLISENSE__)
  31. #endif // ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
  32. namespace absl {
  33. ABSL_NAMESPACE_BEGIN
  34. namespace str_format_internal {
  35. #ifdef ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
  36. template <FormatConversionCharSet... C>
  37. constexpr bool ValidFormatImpl(string_view format) {
  38. int next_arg = 0;
  39. const char* p = format.data();
  40. const char* const end = p + format.size();
  41. constexpr FormatConversionCharSet
  42. kAllowedConvs[(std::max)(sizeof...(C), size_t{1})] = {C...};
  43. bool used[(std::max)(sizeof...(C), size_t{1})]{};
  44. constexpr int kNumArgs = sizeof...(C);
  45. while (p != end) {
  46. while (p != end && *p != '%') ++p;
  47. if (p == end) {
  48. break;
  49. }
  50. if (p + 1 >= end) return false;
  51. if (p[1] == '%') {
  52. // %%
  53. p += 2;
  54. continue;
  55. }
  56. UnboundConversion conv(absl::kConstInit);
  57. p = ConsumeUnboundConversion(p + 1, end, &conv, &next_arg);
  58. if (p == nullptr) return false;
  59. if (conv.arg_position <= 0 || conv.arg_position > kNumArgs) {
  60. return false;
  61. }
  62. if (!Contains(kAllowedConvs[conv.arg_position - 1], conv.conv)) {
  63. return false;
  64. }
  65. used[conv.arg_position - 1] = true;
  66. for (auto extra : {conv.width, conv.precision}) {
  67. if (extra.is_from_arg()) {
  68. int pos = extra.get_from_arg();
  69. if (pos <= 0 || pos > kNumArgs) return false;
  70. used[pos - 1] = true;
  71. if (!Contains(kAllowedConvs[pos - 1], '*')) {
  72. return false;
  73. }
  74. }
  75. }
  76. }
  77. if (sizeof...(C) != 0) {
  78. for (bool b : used) {
  79. if (!b) return false;
  80. }
  81. }
  82. return true;
  83. }
  84. #endif // ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
  85. } // namespace str_format_internal
  86. ABSL_NAMESPACE_END
  87. } // namespace absl
  88. #endif // ABSL_STRINGS_INTERNAL_STR_FORMAT_CHECKER_H_