Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More conversion of old printf style to std::format style #1504

Merged
merged 2 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/liboslcomp/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,7 @@ ASTunary_expression::ASTunary_expression (OSLCompilerImpl *comp, int op,
: ASTNode (unary_expression_node, comp, op, expr)
{
// Check for a user-overloaded function for this operator
Symbol *sym = comp->symtab().find (ustring::sprintf ("__operator__%s__", opword()));
Symbol *sym = comp->symtab().find(ustring::fmtformat("__operator__{}__", opword()));
if (sym && sym->symtype() == SymTypeFunction)
m_function_overload = (FunctionSymbol *)sym;
}
Expand Down Expand Up @@ -1171,7 +1171,7 @@ ASTbinary_expression::ASTbinary_expression (OSLCompilerImpl *comp, Operator op,
// Check for a user-overloaded function for this operator.
// Disallow a few ops from overloading.
if (op != And && op != Or) {
ustring funcname = ustring::sprintf ("__operator__%s__", opword());
ustring funcname = ustring::fmtformat("__operator__{}__", opword());
Symbol *sym = comp->symtab().find (funcname);
if (sym && sym->symtype() == SymTypeFunction)
m_function_overload = (FunctionSymbol *)sym;
Expand Down
81 changes: 40 additions & 41 deletions src/liboslcomp/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ OSLCompilerImpl::insert_code(int opnum, const char* opname, size_t nargs,
Symbol*
OSLCompilerImpl::make_temporary(const TypeSpec& type)
{
ustring name = ustring::sprintf("$tmp%d", ++m_next_temp);
ustring name = ustring::fmtformat("$tmp{}", ++m_next_temp);
Symbol* s = new Symbol(name, type, SymTypeTemp);
symtab().insert(s);

Expand Down Expand Up @@ -131,7 +131,7 @@ OSLCompilerImpl::add_struct_fields(StructSpec* structspec, ustring basename,
// arraylen is the length of the array of the surrounding data type
for (int i = 0; i < (int)structspec->numfields(); ++i) {
const StructSpec::FieldSpec& field(structspec->field(i));
ustring fieldname = ustring::sprintf("%s.%s", basename, field.name);
ustring fieldname = ustring::fmtformat("{}.{}", basename, field.name);
TypeSpec type = field.type;
int arr = type.arraylength();
if (arr && arraylen) {
Expand Down Expand Up @@ -175,7 +175,7 @@ OSLCompilerImpl::make_constant(ustring val)
return sym;
}
// It's not a constant we've added before
ustring name = ustring::sprintf("$const%d", ++m_next_const);
ustring name = ustring::fmtformat("$const{}", ++m_next_const);
ConstantSymbol* s = new ConstantSymbol(name, val);
symtab().insert(s);
m_const_syms.push_back(s);
Expand All @@ -194,7 +194,7 @@ OSLCompilerImpl::make_constant(TypeDesc type, const void* val)
return sym;
}
// It's not a constant we've added before
ustring name = ustring::sprintf("$const%d", ++m_next_const);
ustring name = ustring::fmtformat("$const{}", ++m_next_const);
ConstantSymbol* s = new ConstantSymbol(name, type);
memcpy(s->dataptr(), val, typesize);
symtab().insert(s);
Expand All @@ -212,7 +212,7 @@ OSLCompilerImpl::make_constant(int val)
return sym;
}
// It's not a constant we've added before
ustring name = ustring::sprintf("$const%d", ++m_next_const);
ustring name = ustring::fmtformat("$const{}", ++m_next_const);
ConstantSymbol* s = new ConstantSymbol(name, val);
symtab().insert(s);
m_const_syms.push_back(s);
Expand All @@ -229,7 +229,7 @@ OSLCompilerImpl::make_constant(float val)
return sym;
}
// It's not a constant we've added before
ustring name = ustring::sprintf("$const%d", ++m_next_const);
ustring name = ustring::fmtformat("$const{}", ++m_next_const);
ConstantSymbol* s = new ConstantSymbol(name, val);
symtab().insert(s);
m_const_syms.push_back(s);
Expand All @@ -247,7 +247,7 @@ OSLCompilerImpl::make_constant(TypeDesc type, float x, float y, float z)
return sym;
}
// It's not a constant we've added before
ustring name = ustring::sprintf("$const%d", ++m_next_const);
ustring name = ustring::fmtformat("$const{}", ++m_next_const);
ConstantSymbol* s = new ConstantSymbol(name, type, x, y, z);
symtab().insert(s);
m_const_syms.push_back(s);
Expand Down Expand Up @@ -550,8 +550,8 @@ ASTNode::codegen_assign_struct(StructSpec* structspec, ustring dstsym,
// struct within struct -- recurse
ustring fieldname(structspec->field(i).name);
codegen_assign_struct(fieldtype.structspec(),
ustring::sprintf("%s.%s", dstsym, fieldname),
ustring::sprintf("%s.%s", srcsym, fieldname),
ustring::fmtformat("{}.{}", dstsym, fieldname),
ustring::fmtformat("{}.{}", srcsym, fieldname),
arrayindex, copywholearrays, 0, paraminit);
continue;
}
Expand All @@ -560,8 +560,8 @@ ASTNode::codegen_assign_struct(StructSpec* structspec, ustring dstsym,
// struct array within struct -- loop over indices and recurse
OSL_ASSERT(!arrayindex && "two levels of arrays not allowed");
ustring fieldname(structspec->field(i).name);
ustring dstfield = ustring::sprintf("%s.%s", dstsym, fieldname);
ustring srcfield = ustring::sprintf("%s.%s", srcsym, fieldname);
ustring dstfield = ustring::fmtformat("{}.{}", dstsym, fieldname);
ustring srcfield = ustring::fmtformat("{}.{}", srcsym, fieldname);
for (int i = 0; i < fieldtype.arraylength(); ++i) {
codegen_assign_struct(fieldtype.structspec(), dstfield,
srcfield, m_compiler->make_constant(i),
Expand Down Expand Up @@ -632,27 +632,27 @@ ASTNode::one_default_literal(const Symbol* sym, ASTNode* init, std::string& out,
completed = false;
} else if (type.is_int()) {
if (islit && lit->typespec().is_int())
out += Strutil::sprintf("%d", lit->intval());
out += fmtformat("{}", lit->intval());
else {
out += "0"; // FIXME?
completed = false;
}
} else if (type.is_float()) {
if (islit && lit->typespec().is_int())
out += Strutil::sprintf("%d", lit->intval());
out += fmtformat("{}", lit->intval());
else if (islit && lit->typespec().is_float())
out += Strutil::sprintf("%.9g", lit->floatval());
out += fmtformat("{:.9g}", lit->floatval());
else {
out += "0"; // FIXME?
completed = false;
}
} else if (type.is_triple()) {
if (islit && lit->typespec().is_int()) {
float f = lit->intval();
out += Strutil::sprintf("%.9g%s%.9g%s%.9g", f, sep, f, sep, f);
out += fmtformat("{:.9g}{}{:.9g}{}{:.9g}", f, sep, f, sep, f);
} else if (islit && lit->typespec().is_float()) {
float f = lit->floatval();
out += Strutil::sprintf("%.9g%s%.9g%s%.9g", f, sep, f, sep, f);
out += fmtformat("{:.9g}{}{:.9g}{}{:.9g}", f, sep, f, sep, f);
} else if (init && init->typespec() == type
&& (init->nodetype() == ASTNode::type_constructor_node
|| (init->nodetype() == ASTNode::compound_initializer_node
Expand Down Expand Up @@ -682,26 +682,26 @@ ASTNode::one_default_literal(const Symbol* sym, ASTNode* init, std::string& out,
}
}
if (nargs == 1)
out += Strutil::sprintf("%.9g%s%.9g%s%.9g", f[0], sep, f[0],
sep, f[0]);
out += fmtformat("{:.9g}{}{:.9g}{}{:.9g}", f[0], sep, f[0], sep,
f[0]);
else
out += Strutil::sprintf("%.9g%s%.9g%s%.9g", f[0], sep, f[1],
sep, f[2]);
out += fmtformat("{:.9g}{}{:.9g}{}{:.9g}", f[0], sep, f[1], sep,
f[2]);
} else {
out += Strutil::sprintf("0%s0%s0", sep, sep);
out += fmtformat("0{}0{}0", sep, sep);
completed = false;
}
} else if (type.is_matrix()) {
if (islit && lit->typespec().is_int()) {
float f = lit->intval();
for (int c = 0; c < 16; ++c)
out += Strutil::sprintf("%.9g%s", (c / 4) == (c % 4) ? f : 0.0f,
c < 15 ? sep : "");
out += fmtformat("{:.9g}{}", (c / 4) == (c % 4) ? f : 0.0f,
c < 15 ? sep : "");
} else if (islit && lit->typespec().is_float()) {
float f = lit->floatval();
for (int c = 0; c < 16; ++c)
out += Strutil::sprintf("%.9g%s", (c / 4) == (c % 4) ? f : 0.0f,
c < 15 ? sep : "");
out += fmtformat("{:.9g}{}", (c / 4) == (c % 4) ? f : 0.0f,
c < 15 ? sep : "");
} else if (init && init->typespec() == type
&& init->nodetype() == ASTNode::type_constructor_node) {
ASTtype_constructor* ctr = (ASTtype_constructor*)init;
Expand Down Expand Up @@ -729,22 +729,21 @@ ASTNode::one_default_literal(const Symbol* sym, ASTNode* init, std::string& out,
}
if (nargs == 1) {
for (int c = 0; c < 16; ++c)
out += Strutil::sprintf("%.9g%s",
(c / 4) == (c % 4) ? f[0] : 0.0f,
c < 15 ? sep : "");
out += fmtformat("{:.9g}{}",
(c / 4) == (c % 4) ? f[0] : 0.0f,
c < 15 ? sep : "");
} else {
for (int c = 0; c < 16; ++c)
out += Strutil::sprintf("%.9g%s", f[c], c < 15 ? sep : "");
out += fmtformat("{:.9g}{}", f[c], c < 15 ? sep : "");
}
} else {
for (int c = 0; c < 16; ++c)
out += Strutil::sprintf("0%s", c < 15 ? sep : "");
out += fmtformat("0{}", c < 15 ? sep : "");
completed = false;
}
} else if (type.is_string()) {
if (islit && lit->typespec().is_string())
out += Strutil::sprintf("\"%s\"",
Strutil::escape_chars(lit->strval()));
out += fmtformat("\"{}\"", Strutil::escape_chars(lit->strval()));
else {
out += "\"\""; // FIXME?
completed = false;
Expand Down Expand Up @@ -903,8 +902,8 @@ ASTNode::codegen_initlist(ref init, TypeSpec type, Symbol* sym)
for (int i = 0; init && i < structspec->numfields();
init = init->next(), ++i) {
const StructSpec::FieldSpec& field(structspec->field(i));
ustring fieldname = ustring::sprintf("%s.%s", sym->mangled(),
field.name);
ustring fieldname = ustring::fmtformat("{}.{}", sym->mangled(),
field.name);
Symbol* fieldsym = m_compiler->symtab().find_exact(fieldname);
if (paraminit) {
OSL_DASSERT(nodetype() == variable_declaration_node);
Expand Down Expand Up @@ -1073,8 +1072,8 @@ ASTNode::codegen_struct_initializers(ref init, Symbol* sym, bool is_constructor,
init = init->next(), ++i) {
// Structure element -- assign to the i-th member field
const StructSpec::FieldSpec& field(structspec->field(i));
ustring fieldname = ustring::sprintf("%s.%s", sym->mangled(),
field.name);
ustring fieldname = ustring::fmtformat("{}.{}", sym->mangled(),
field.name);
Symbol* fieldsym = m_compiler->symtab().find_exact(fieldname);
if (fieldsym->typespec().is_structure_based()
&& (init->nodetype() == type_constructor_node
Expand Down Expand Up @@ -1240,8 +1239,8 @@ ASTindex::codegen_copy_struct_array_element(StructSpec* structspec,
// struct within struct -- recurse!
codegen_copy_struct_array_element(
type.structspec(),
ustring::sprintf("%s.%s", destname, field.name),
ustring::sprintf("%s.%s", srcname, field.name), index);
ustring::fmtformat("{}.{}", destname, field.name),
ustring::fmtformat("{}.{}", srcname, field.name), index);
} else {
OSL_ASSERT(!type.is_array());
Symbol *dfield, *sfield;
Expand Down Expand Up @@ -1464,7 +1463,7 @@ ASTunary_expression::codegen(Symbol* dest)
if (m_function_overload) {
// A little crazy, but we temporarily construct an ASTfunction_call
// in order to codegen this overloaded operator.
ustring funcname = ustring::sprintf("__operator__%s__", opword());
ustring funcname = ustring::fmtformat("__operator__{}__", opword());
ASTfunction_call fc(m_compiler, funcname, expr().get(),
m_function_overload);
fc.typecheck(typespec());
Expand Down Expand Up @@ -2023,8 +2022,8 @@ ASTfunction_call::struct_pair_all_fields(StructSpec* structspec, ustring formal,
// struct within struct -- recurse!
struct_pair_all_fields(
type.structspec(),
ustring::sprintf("%s.%s", formal, field.name),
ustring::sprintf("%s.%s", actual, field.name), arrayindex);
ustring::fmtformat("{}.{}", formal, field.name),
ustring::fmtformat("{}.{}", actual, field.name), arrayindex);
} else {
Symbol *fsym, *asym;
m_compiler->struct_field_pair(structspec, fi, formal, actual, fsym,
Expand Down
7 changes: 0 additions & 7 deletions src/liboslcomp/oslcomp_pvt.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,13 +390,6 @@ class OSLCompilerImpl {
void write_oso_metadata(const ASTNode* metanode) const;
void write_dependency_file(string_view filename);

template<typename... Args>
OSL_DEPRECATED("Use osofmt with std::format conventions")
inline void osof(const char* fmt, const Args&... args) const
{
(*m_osofile) << OIIO::Strutil::sprintf(fmt, args...);
}

// Output text to the osofile, using std::format formatting conventions.
template<typename... Args>
inline void osofmt(const char* fmt, const Args&... args) const
Expand Down
5 changes: 2 additions & 3 deletions src/liboslcomp/symtab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace pvt { // OSL::pvt
std::string
Symbol::mangled() const
{
std::string result = scope() ? Strutil::sprintf("___%d_%s", scope(), m_name)
std::string result = scope() ? fmtformat("___{}_{}", scope(), m_name)
: m_name.string();
return result; // Force NRVO (named value return optimization)
}
Expand Down Expand Up @@ -54,8 +54,7 @@ Symbol::symtype_shortname(SymType s)
std::string
StructSpec::mangled() const
{
return scope() ? Strutil::sprintf("___%d_%s", scope(), m_name)
: m_name.string();
return scope() ? fmtformat("___{}_{}", scope(), m_name) : m_name.string();
}


Expand Down
12 changes: 6 additions & 6 deletions src/liboslexec/automata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ NdfAutomata::State::tostr()const
for (IntSet::const_iterator j = dest.begin(); j != dest.end(); ++j) {
if (s[s.size()-1] != '{')
s += ", ";
s += Strutil::sprintf("%d", *j);
s += Strutil::to_string(*j);
}
s += "}";
}
Expand All @@ -108,12 +108,12 @@ NdfAutomata::State::tostr()const
}
s += "]:";
}
s += Strutil::sprintf("%d", m_wildcard_trans);
s += Strutil::to_string(m_wildcard_trans);
}
// and finally the rule if we have it
if (m_rule) {
s += " | ";
s += Strutil::sprintf("%p", m_rule);
s += Strutil::fmt::format("{:p}", m_rule);
}
return s;
}
Expand Down Expand Up @@ -336,7 +336,7 @@ DfAutomata::State::tostr()const
else
s += sym.c_str();
s += ":";
s += Strutil::sprintf("%d", dest);
s += Strutil::to_string(dest);
}
// wildcard
if (m_wildcard_trans >= 0) {
Expand All @@ -354,15 +354,15 @@ DfAutomata::State::tostr()const
}
s += "}:";
}
s += Strutil::sprintf("%d", m_wildcard_trans);
s += Strutil::to_string(m_wildcard_trans);
}
// and the rules
if (m_rules.size()) {
s += " | [";
for (RuleSet::const_iterator i = m_rules.begin(); i != m_rules.end(); ++i) {
if (s[s.size()-1] != '[')
s += ", ";
s += Strutil::sprintf("%p", *i);
s += Strutil::fmt::format("{:p}", *i);
}
s += "]";
}
Expand Down
4 changes: 2 additions & 2 deletions src/liboslexec/backendllvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ BackendLLVM::getLLVMSymbolBase (const Symbol &sym)
std::string mangled_name = dealiased->mangled();
AllocationMap::iterator map_iter = named_values().find (mangled_name);
if (map_iter == named_values().end()) {
shadingcontext()->errorf("Couldn't find symbol '%s' (unmangled = '%s'). Did you forget to allocate it?",
mangled_name, dealiased->unmangled());
shadingcontext()->errorfmt("Couldn't find symbol '{}' (unmangled = '{}'). Did you forget to allocate it?",
mangled_name, dealiased->unmangled());
return 0;
}
return (llvm::Value*) map_iter->second;
Expand Down
3 changes: 2 additions & 1 deletion src/liboslexec/backendllvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,8 @@ class BackendLLVM final : public OSOProcessorBase {
///
llvm::BasicBlock *llvm_exit_instance_block () {
if (! m_exit_instance_block) {
std::string name = Strutil::sprintf ("%s_%d_exit_", inst()->layername(), inst()->id());
std::string name = fmtformat("{}_{}_exit_", inst()->layername(),
inst()->id());
m_exit_instance_block = ll.new_basic_block (name);
}
return m_exit_instance_block;
Expand Down
6 changes: 3 additions & 3 deletions src/liboslexec/batched_backendllvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,9 +450,9 @@ BatchedBackendLLVM::getLLVMSymbolBase(const Symbol& sym)
std::string mangled_name = dealiased->mangled();
AllocationMap::iterator map_iter = named_values().find(mangled_name);
if (map_iter == named_values().end()) {
shadingcontext()->errorf(
"Couldn't find symbol '%s' (unmangled = '%s'). Did you forget to allocate it?",
mangled_name.c_str(), dealiased->unmangled());
shadingcontext()->errorfmt(
"Couldn't find symbol '{}' (unmangled = '{}'). Did you forget to allocate it?",
mangled_name, dealiased->unmangled());
return 0;
}
return (llvm::Value*)map_iter->second;
Expand Down
5 changes: 2 additions & 3 deletions src/liboslexec/batched_backendllvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -678,9 +678,8 @@ class BatchedBackendLLVM : public OSOProcessorBase {
llvm::BasicBlock* llvm_exit_instance_block()
{
if (!m_exit_instance_block) {
std::string name = Strutil::sprintf("%s_%d_exit_",
inst()->layername(),
inst()->id());
std::string name = fmtformat("{}_{}_exit_", inst()->layername(),
inst()->id());
m_exit_instance_block = ll.new_basic_block(name);
}
return m_exit_instance_block;
Expand Down
Loading