Fix TextGenerator::UniqueIdentifier() failing with empty string arg

Real problem was that we relied on SymbolTable::New() to be called with
the default arg of "tint_symbol", which isn't ergonomic when forwarding
to this function from others, like TextGenerator::UniqueIdentifier().
Instead, make New() take emptry string by default, and make it use
"tint_symbol" if input arg is empty string.

Also made it so that SymbolTable::Register() must not take an empty
string, so we now assert, rather than return an invalid symbol.

Change-Id: I386ece318c86d1d399f1dd1557a95fecac01f7ec
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/56461
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
Antonio Maiorano 2021-06-29 22:07:05 +00:00 committed by Tint LUCI CQ
parent c035366cd5
commit 53069b283d
5 changed files with 17 additions and 11 deletions

View File

@ -54,7 +54,7 @@ TEST_F(IdentifierExpressionTest, Assert_DifferentProgramID_Symbol) {
{
ProgramBuilder b1;
ProgramBuilder b2;
b1.Expr(b2.Sym(""));
b1.Expr(b2.Sym("b2"));
},
"internal compiler error");
}

View File

@ -32,8 +32,7 @@ SymbolTable& SymbolTable::operator=(const SymbolTable& other) = default;
SymbolTable& SymbolTable::operator=(SymbolTable&&) = default;
Symbol SymbolTable::Register(const std::string& name) {
if (name == "")
return Symbol();
TINT_ASSERT(Symbol, !name.empty());
auto it = name_to_symbol_.find(name);
if (it != name_to_symbol_.end())
@ -63,7 +62,10 @@ std::string SymbolTable::NameFor(const Symbol symbol) const {
return it->second;
}
Symbol SymbolTable::New(std::string prefix /* = "tint_symbol" */) {
Symbol SymbolTable::New(std::string prefix /* = "" */) {
if (prefix.empty()) {
prefix = "tint_symbol";
}
auto it = name_to_symbol_.find(prefix);
if (it == name_to_symbol_.end()) {
return Register(prefix);

View File

@ -66,7 +66,7 @@ class SymbolTable {
/// @returns a new, unnamed symbol with the given name. If the name is already
/// taken then this will be suffixed with an underscore and a unique numerical
/// value
Symbol New(std::string name = "tint_symbol");
Symbol New(std::string name = "");
/// Foreach calls the callback function `F` for each symbol in the table.
/// @param callback must be a function or function-like object with the

View File

@ -14,7 +14,7 @@
#include "src/symbol_table.h"
#include "gtest/gtest.h"
#include "gtest/gtest-spi.h"
namespace tint {
namespace {
@ -49,10 +49,14 @@ TEST_F(SymbolTableTest, ReturnsBlankForMissingSymbol) {
EXPECT_EQ("$2", s.NameFor(Symbol(2, program_id)));
}
TEST_F(SymbolTableTest, ReturnsInvalidForBlankString) {
TEST_F(SymbolTableTest, AssertsForBlankString) {
EXPECT_FATAL_FAILURE(
{
auto program_id = ProgramID::New();
SymbolTable s{program_id};
EXPECT_FALSE(s.Register("").IsValid());
s.Register("");
},
"internal compiler error");
}
} // namespace

View File

@ -62,7 +62,7 @@ class TextGenerator {
/// @return a new, unique identifier with the given prefix.
/// @param prefix optional prefix to apply to the generated identifier. If
/// empty "tint" will be used.
/// empty "tint_symbol" will be used.
std::string UniqueIdentifier(const std::string& prefix = "");
protected: