Use constant's location for reporting errors in parsing of hex constant

Before this the line following the error would be reported in some cases.

PiperOrigin-RevId: 270778722
This commit is contained in:
Jacques Pienaar 2019-09-23 15:51:11 -07:00 committed by A. Unique TensorFlower
parent 98d1d3fc43
commit 4a862fbd63
2 changed files with 20 additions and 13 deletions

View File

@ -1191,6 +1191,7 @@ Attribute Parser::parseDecOrHexAttr(Type type, bool isNegative) {
// Remember if the literal is hexadecimal. // Remember if the literal is hexadecimal.
StringRef spelling = getToken().getSpelling(); StringRef spelling = getToken().getSpelling();
auto loc = state.curToken.getLoc();
bool isHex = spelling.size() > 1 && spelling[1] == 'x'; bool isHex = spelling.size() > 1 && spelling[1] == 'x';
consumeToken(Token::integer); consumeToken(Token::integer);
@ -1202,16 +1203,19 @@ Attribute Parser::parseDecOrHexAttr(Type type, bool isNegative) {
return nullptr; return nullptr;
} }
// Hexadecimal representation of float literals is not supported for bfloat16. if (auto floatType = type.dyn_cast<FloatType>()) {
// When supported, the literal should be unsigned. // TODO(zinenko): Update once hex format for bfloat16 is supported.
auto floatType = type.dyn_cast<FloatType>(); if (type.isBF16())
if (floatType && !type.isBF16()) { return emitError(loc,
if (isNegative) { "hexadecimal float literal not supported for bfloat16"),
emitError("hexadecimal float literal should not have a leading minus"); nullptr;
return nullptr; if (isNegative)
} return emitError(
loc,
"hexadecimal float literal should not have a leading minus"),
nullptr;
if (!isHex) { if (!isHex) {
emitError("unexpected decimal integer literal for a float attribute") emitError(loc, "unexpected decimal integer literal for a float attribute")
.attachNote() .attachNote()
<< "add a trailing dot to make the literal a float"; << "add a trailing dot to make the literal a float";
return nullptr; return nullptr;
@ -1222,17 +1226,20 @@ Attribute Parser::parseDecOrHexAttr(Type type, bool isNegative) {
} }
if (!type.isIntOrIndex()) if (!type.isIntOrIndex())
return (emitError("integer literal not valid for specified type"), nullptr); return emitError(loc, "integer literal not valid for specified type"),
nullptr;
// Parse the integer literal. // Parse the integer literal.
int width = type.isIndex() ? 64 : type.getIntOrFloatBitWidth(); int width = type.isIndex() ? 64 : type.getIntOrFloatBitWidth();
APInt apInt(width, *val, isNegative); APInt apInt(width, *val, isNegative);
if (apInt != *val) if (apInt != *val)
return (emitError("integer constant out of range for attribute"), nullptr); return emitError(loc, "integer constant out of range for attribute"),
nullptr;
// Otherwise construct an integer attribute. // Otherwise construct an integer attribute.
if (isNegative ? (int64_t)-val.getValue() >= 0 : (int64_t)val.getValue() < 0) if (isNegative ? (int64_t)-val.getValue() >= 0 : (int64_t)val.getValue() < 0)
return (emitError("integer constant out of range for attribute"), nullptr); return emitError(loc, "integer constant out of range for attribute"),
nullptr;
return builder.getIntegerAttr(type, isNegative ? -apInt : apInt); return builder.getIntegerAttr(type, isNegative ? -apInt : apInt);
} }

View File

@ -1077,7 +1077,7 @@ func @invalid_region_dominance() {
// ----- // -----
func @hexadecimal_bf16() { func @hexadecimal_bf16() {
// expected-error @+1 {{integer literal not valid for specified type}} // expected-error @+1 {{hexadecimal float literal not supported for bfloat16}}
"foo"() {value = 0xffff : bf16} : () -> () "foo"() {value = 0xffff : bf16} : () -> ()
} }