lex_helpers.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/lexer/lex_helpers.h"
  5. #include "llvm/Support/FormatVariadic.h"
  6. namespace Carbon {
  7. auto CanLexInteger(DiagnosticEmitter<const char*>& emitter,
  8. llvm::StringRef text) -> bool {
  9. // llvm::getAsInteger is used for parsing, but it's quadratic and visibly slow
  10. // on large integer values. This limit exists to avoid hitting those limits.
  11. // Per https://github.com/carbon-language/carbon-lang/issues/980, it may be
  12. // feasible to optimize integer parsing in order to address performance if
  13. // this limit becomes an issue.
  14. //
  15. // 2^128 would be 39 decimal digits or 128 binary. In either case, this limit
  16. // is far above the threshold for normal integers.
  17. constexpr size_t DigitLimit = 1000;
  18. if (text.size() > DigitLimit) {
  19. CARBON_DIAGNOSTIC(
  20. TooManyDigits, Error,
  21. "Found a sequence of {0} digits, which is greater than the "
  22. "limit of {1}.",
  23. size_t, size_t);
  24. emitter.Emit(text.begin(), TooManyDigits, text.size(), DigitLimit);
  25. return false;
  26. }
  27. return true;
  28. }
  29. } // namespace Carbon