lex_helpers.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. namespace {
  8. struct TooManyDigits : DiagnosticBase<TooManyDigits> {
  9. static constexpr llvm::StringLiteral ShortName = "syntax-invalid-number";
  10. auto Format() -> std::string {
  11. return llvm::formatv(
  12. "Found a sequence of {0} digits, which is greater than the "
  13. "limit of {1}.",
  14. count, limit)
  15. .str();
  16. }
  17. size_t count;
  18. size_t limit;
  19. };
  20. } // namespace
  21. auto CanLexInteger(DiagnosticEmitter<const char*>& emitter,
  22. llvm::StringRef text) -> bool {
  23. // llvm::getAsInteger is used for parsing, but it's quadratic and visibly slow
  24. // on large integer values. This limit exists to avoid hitting those limits.
  25. // Per https://github.com/carbon-language/carbon-lang/issues/980, it may be
  26. // feasible to optimize integer parsing in order to address performance if
  27. // this limit becomes an issue.
  28. //
  29. // 2^128 would be 39 decimal digits or 128 binary. In either case, this limit
  30. // is far above the threshold for normal integers.
  31. constexpr size_t DigitLimit = 1000;
  32. if (text.size() > DigitLimit) {
  33. emitter.EmitError<TooManyDigits>(
  34. text.begin(), {.count = text.size(), .limit = DigitLimit});
  35. return false;
  36. }
  37. return true;
  38. }
  39. } // namespace Carbon