helpers.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132
  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/lex/helpers.h"
  5. namespace Carbon::Lex {
  6. auto CanLexInteger(DiagnosticEmitter<const char*>& emitter,
  7. llvm::StringRef text) -> bool {
  8. // llvm::getAsInteger is used for parsing, but it's quadratic and visibly slow
  9. // on large integer values. This limit exists to avoid hitting those limits.
  10. // Per https://github.com/carbon-language/carbon-lang/issues/980, it may be
  11. // feasible to optimize integer parsing in order to address performance if
  12. // this limit becomes an issue.
  13. //
  14. // 2^128 would be 39 decimal digits or 128 binary. In either case, this limit
  15. // is far above the threshold for normal integers.
  16. constexpr size_t DigitLimit = 1000;
  17. if (text.size() > DigitLimit) {
  18. CARBON_DIAGNOSTIC(
  19. TooManyDigits, Error,
  20. "Found a sequence of {0} digits, which is greater than the "
  21. "limit of {1}.",
  22. size_t, size_t);
  23. emitter.Emit(text.begin(), TooManyDigits, text.size(), DigitLimit);
  24. return false;
  25. }
  26. return true;
  27. }
  28. } // namespace Carbon::Lex