mirror of
https://github.com/KhronosGroup/SPIRV-Tools
synced 2024-12-25 01:01:04 +00:00
String literals must be quoted.
This commit is contained in:
parent
affa696027
commit
98290a243e
@ -251,6 +251,8 @@ spv_result_t spvTextToLiteral(const char *textValue, spv_literal_t *pLiteral) {
|
|||||||
bool isString = false;
|
bool isString = false;
|
||||||
|
|
||||||
const size_t len = strlen(textValue);
|
const size_t len = strlen(textValue);
|
||||||
|
if (len == 0) return SPV_FAILED_MATCH;
|
||||||
|
|
||||||
for (uint64_t index = 0; index < len; ++index) {
|
for (uint64_t index = 0; index < len; ++index) {
|
||||||
switch (textValue[index]) {
|
switch (textValue[index]) {
|
||||||
case '0':
|
case '0':
|
||||||
@ -284,12 +286,14 @@ spv_result_t spvTextToLiteral(const char *textValue, spv_literal_t *pLiteral) {
|
|||||||
pLiteral->type = spv_literal_type_t(99);
|
pLiteral->type = spv_literal_type_t(99);
|
||||||
|
|
||||||
if (isString || numPeriods > 1 || (isSigned && len==1)) {
|
if (isString || numPeriods > 1 || (isSigned && len==1)) {
|
||||||
// TODO(dneto): Quotes should be required, and stripped.
|
|
||||||
// TODO(dneto): Allow escaping.
|
// TODO(dneto): Allow escaping.
|
||||||
|
if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"')
|
||||||
|
return SPV_FAILED_MATCH;
|
||||||
pLiteral->type = SPV_LITERAL_TYPE_STRING;
|
pLiteral->type = SPV_LITERAL_TYPE_STRING;
|
||||||
// Need room for the null-terminator.
|
// Need room for the null-terminator.
|
||||||
if (len + 1 > sizeof(pLiteral->value.str)) return SPV_ERROR_OUT_OF_MEMORY;
|
if (len >= sizeof(pLiteral->value.str)) return SPV_ERROR_OUT_OF_MEMORY;
|
||||||
strncpy(pLiteral->value.str, textValue, len+1);
|
strncpy(pLiteral->value.str, textValue+1, len-2);
|
||||||
|
pLiteral->value.str[len-2] = 0;
|
||||||
} else if (numPeriods == 1) {
|
} else if (numPeriods == 1) {
|
||||||
double d = std::strtod(textValue, nullptr);
|
double d = std::strtod(textValue, nullptr);
|
||||||
float f = (float)d;
|
float f = (float)d;
|
||||||
|
@ -117,7 +117,10 @@ spv_result_t spvTextStringGet(const spv_text text,
|
|||||||
/// @return result code
|
/// @return result code
|
||||||
spv_result_t spvTextToUInt32(const char *textValue, uint32_t *pValue);
|
spv_result_t spvTextToUInt32(const char *textValue, uint32_t *pValue);
|
||||||
|
|
||||||
/// @brief Convert the input text to one of the number types
|
/// @brief Convert the input text to one of the number types.
|
||||||
|
///
|
||||||
|
/// String literals must be surrounded by double-quotes ("), which are
|
||||||
|
/// then stripped.
|
||||||
///
|
///
|
||||||
/// @param[in] textValue input text to parse
|
/// @param[in] textValue input text to parse
|
||||||
/// @param[out] pLiteral the returned literal number
|
/// @param[out] pLiteral the returned literal number
|
||||||
|
@ -84,40 +84,59 @@ TEST(TextLiteral, GoodFloat) {
|
|||||||
EXPECT_EQ(-.25, l.value.f);
|
EXPECT_EQ(-.25, l.value.f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(TextLiteral, BadString) {
|
||||||
|
spv_literal_t l;
|
||||||
|
|
||||||
|
EXPECT_EQ(SPV_FAILED_MATCH, spvTextToLiteral("", &l));
|
||||||
|
EXPECT_EQ(SPV_FAILED_MATCH, spvTextToLiteral("-", &l));
|
||||||
|
EXPECT_EQ(SPV_FAILED_MATCH, spvTextToLiteral("--", &l));
|
||||||
|
EXPECT_EQ(SPV_FAILED_MATCH, spvTextToLiteral("1-2", &l));
|
||||||
|
EXPECT_EQ(SPV_FAILED_MATCH, spvTextToLiteral("123a", &l));
|
||||||
|
EXPECT_EQ(SPV_FAILED_MATCH, spvTextToLiteral("12.2.3", &l));
|
||||||
|
EXPECT_EQ(SPV_FAILED_MATCH, spvTextToLiteral("\"", &l));
|
||||||
|
EXPECT_EQ(SPV_FAILED_MATCH, spvTextToLiteral("\"z", &l));
|
||||||
|
EXPECT_EQ(SPV_FAILED_MATCH, spvTextToLiteral("a\"", &l));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(TextLiteral, GoodString) {
|
TEST(TextLiteral, GoodString) {
|
||||||
spv_literal_t l;
|
spv_literal_t l;
|
||||||
|
|
||||||
ASSERT_EQ(SPV_SUCCESS, spvTextToLiteral("-", &l));
|
ASSERT_EQ(SPV_SUCCESS, spvTextToLiteral("\"-\"", &l));
|
||||||
EXPECT_EQ(SPV_LITERAL_TYPE_STRING, l.type);
|
EXPECT_EQ(SPV_LITERAL_TYPE_STRING, l.type);
|
||||||
EXPECT_STREQ("-", l.value.str);
|
EXPECT_STREQ("-", l.value.str);
|
||||||
|
|
||||||
ASSERT_EQ(SPV_SUCCESS, spvTextToLiteral("--", &l));
|
ASSERT_EQ(SPV_SUCCESS, spvTextToLiteral("\"--\"", &l));
|
||||||
EXPECT_EQ(SPV_LITERAL_TYPE_STRING, l.type);
|
EXPECT_EQ(SPV_LITERAL_TYPE_STRING, l.type);
|
||||||
EXPECT_STREQ("--", l.value.str);
|
EXPECT_STREQ("--", l.value.str);
|
||||||
|
|
||||||
ASSERT_EQ(SPV_SUCCESS, spvTextToLiteral("1-2", &l));
|
ASSERT_EQ(SPV_SUCCESS, spvTextToLiteral("\"1-2\"", &l));
|
||||||
EXPECT_EQ(SPV_LITERAL_TYPE_STRING, l.type);
|
EXPECT_EQ(SPV_LITERAL_TYPE_STRING, l.type);
|
||||||
EXPECT_STREQ("1-2", l.value.str);
|
EXPECT_STREQ("1-2", l.value.str);
|
||||||
|
|
||||||
ASSERT_EQ(SPV_SUCCESS, spvTextToLiteral("123a", &l));
|
ASSERT_EQ(SPV_SUCCESS, spvTextToLiteral("\"123a\"", &l));
|
||||||
EXPECT_EQ(SPV_LITERAL_TYPE_STRING, l.type);
|
EXPECT_EQ(SPV_LITERAL_TYPE_STRING, l.type);
|
||||||
EXPECT_STREQ("123a", l.value.str);
|
EXPECT_STREQ("123a", l.value.str);
|
||||||
|
|
||||||
ASSERT_EQ(SPV_SUCCESS, spvTextToLiteral("12.2.3", &l));
|
ASSERT_EQ(SPV_SUCCESS, spvTextToLiteral("\"12.2.3\"", &l));
|
||||||
EXPECT_EQ(SPV_LITERAL_TYPE_STRING, l.type);
|
EXPECT_EQ(SPV_LITERAL_TYPE_STRING, l.type);
|
||||||
EXPECT_STREQ("12.2.3", l.value.str);
|
EXPECT_STREQ("12.2.3", l.value.str);
|
||||||
|
|
||||||
|
// TODO(dneto): escaping in strings is not supported yet.
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(TextLiteral, StringTooLong) {
|
TEST(TextLiteral, StringTooLong) {
|
||||||
spv_literal_t l;
|
spv_literal_t l;
|
||||||
std::string too_long(SPV_LIMIT_LITERAL_STRING_MAX, 'a');
|
std::string too_long = std::string("\"") +
|
||||||
|
std::string(SPV_LIMIT_LITERAL_STRING_MAX - 2, 'a') +
|
||||||
|
"\"";
|
||||||
EXPECT_EQ(SPV_ERROR_OUT_OF_MEMORY, spvTextToLiteral(too_long.data(), &l));
|
EXPECT_EQ(SPV_ERROR_OUT_OF_MEMORY, spvTextToLiteral(too_long.data(), &l));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(TextLiteral, GoodLongString) {
|
TEST(TextLiteral, GoodLongString) {
|
||||||
spv_literal_t l;
|
spv_literal_t l;
|
||||||
std::string good_long(SPV_LIMIT_LITERAL_STRING_MAX-1, 'a');
|
std::string unquoted(SPV_LIMIT_LITERAL_STRING_MAX - 3, 'a');
|
||||||
|
std::string good_long = std::string("\"") + unquoted + "\"";
|
||||||
EXPECT_EQ(SPV_SUCCESS, spvTextToLiteral(good_long.data(), &l));
|
EXPECT_EQ(SPV_SUCCESS, spvTextToLiteral(good_long.data(), &l));
|
||||||
EXPECT_EQ(SPV_LITERAL_TYPE_STRING, l.type);
|
EXPECT_EQ(SPV_LITERAL_TYPE_STRING, l.type);
|
||||||
EXPECT_STREQ(good_long.data(), l.value.str);
|
EXPECT_STREQ(unquoted.data(), l.value.str);
|
||||||
}
|
}
|
||||||
|
@ -468,7 +468,7 @@ TEST_F(ValidateID, OpConstantCompositeMatrixGood) {
|
|||||||
%2 = OpTypeVector %1 4
|
%2 = OpTypeVector %1 4
|
||||||
%3 = OpTypeMatrix %2 4
|
%3 = OpTypeMatrix %2 4
|
||||||
%4 = OpConstant %1 1.0
|
%4 = OpConstant %1 1.0
|
||||||
%5 = OpConstant %1 %5 0.0
|
%5 = OpConstant %1 0.0
|
||||||
%6 = OpConstantComposite %2 %4 %5 %5 %5
|
%6 = OpConstantComposite %2 %4 %5 %5 %5
|
||||||
%7 = OpConstantComposite %2 %5 %4 %5 %5
|
%7 = OpConstantComposite %2 %5 %4 %5 %5
|
||||||
%8 = OpConstantComposite %2 %5 %5 %4 %5
|
%8 = OpConstantComposite %2 %5 %5 %4 %5
|
||||||
|
Loading…
Reference in New Issue
Block a user