Avoid hardcoded 4096 constant
This commit creates a static constexpr limit for the IntegerType
bitwidth and uses it. The check had to be moved because Token is
not aware of IR/Type and it was a sign the abstraction leaked:
bitwidth limit is not a property of the Token but of the IntegerType.
Added a positive and a negative test at the limit.
PiperOrigin-RevId: 210388192
diff --git a/lib/Parser/Parser.cpp b/lib/Parser/Parser.cpp
index 285312f..756f25d 100644
--- a/lib/Parser/Parser.cpp
+++ b/lib/Parser/Parser.cpp
@@ -316,6 +316,10 @@
auto width = getToken().getIntTypeBitwidth();
if (!width.hasValue())
return (emitError("invalid integer width"), nullptr);
+ if (width > IntegerType::kMaxWidth)
+ return (emitError("integer bitwidth is limited to " +
+ Twine(IntegerType::kMaxWidth) + " bits"),
+ nullptr);
consumeToken(Token::inttype);
return builder.getIntegerType(width.getValue());
}
diff --git a/lib/Parser/Token.cpp b/lib/Parser/Token.cpp
index 4c3f9e4..4e01f5a 100644
--- a/lib/Parser/Token.cpp
+++ b/lib/Parser/Token.cpp
@@ -72,9 +72,7 @@
Optional<unsigned> Token::getIntTypeBitwidth() const {
unsigned result = 0;
if (spelling[1] == '0' ||
- spelling.drop_front().getAsInteger(10, result) ||
- // Arbitrary but large limit on bitwidth.
- result > 4096 || result == 0)
+ spelling.drop_front().getAsInteger(10, result) || result == 0)
return None;
return result;
}