Skip to content

Commit 9693056

Browse files
authored
no static initializers (KhronosGroup#6128)
Chromium bans static initializers of class objects. Wrap the std::array static variables with a function that returns a reference to them. This is option 2 to remove static initializers, at https://chromium.googlesource.com/chromium/src.git/+/HEAD/docs/static_initializers.md#Removing-Static-Initializers Also, lookup methods update a pointer-to-const pointer.
1 parent f1524d1 commit 9693056

25 files changed

+113
-97
lines changed

source/assembly_grammar.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ spv_result_t spvTextParseMaskOperand(const spv_operand_type_t type,
6060
do {
6161
end = std::find(begin, text_end, separator);
6262

63-
spvtools::OperandDesc* entry = nullptr;
63+
const spvtools::OperandDesc* entry = nullptr;
6464
if (auto error =
6565
spvtools::LookupOperand(type, begin, end - begin, &entry)) {
6666
return error;
@@ -175,7 +175,7 @@ CapabilitySet AssemblyGrammar::filterCapsAgainstTargetEnv(
175175
CapabilitySet cap_set;
176176
const auto version = spvVersionForTargetEnv(target_env_);
177177
for (uint32_t i = 0; i < count; ++i) {
178-
spvtools::OperandDesc* entry = nullptr;
178+
const spvtools::OperandDesc* entry = nullptr;
179179
if (SPV_SUCCESS ==
180180
spvtools::LookupOperand(SPV_OPERAND_TYPE_CAPABILITY,
181181
static_cast<uint32_t>(cap_array[i]), &entry)) {
@@ -193,7 +193,7 @@ CapabilitySet AssemblyGrammar::filterCapsAgainstTargetEnv(
193193

194194
const char* AssemblyGrammar::lookupOperandName(spv_operand_type_t type,
195195
uint32_t operand) const {
196-
spvtools::OperandDesc* desc = nullptr;
196+
const spvtools::OperandDesc* desc = nullptr;
197197
if (spvtools::LookupOperand(type, operand, &desc) != SPV_SUCCESS || !desc) {
198198
return "Unknown";
199199
}

source/binary.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ spv_result_t Parser::parseInstruction() {
318318
return diagnostic() << "Invalid instruction word count: "
319319
<< inst_word_count;
320320
}
321-
spvtools::InstructionDesc* opcode_desc = nullptr;
321+
const spvtools::InstructionDesc* opcode_desc = nullptr;
322322
if (spvtools::LookupOpcode(static_cast<spv::Op>(inst.opcode), &opcode_desc))
323323
return diagnostic() << "Invalid opcode: " << inst.opcode;
324324

@@ -522,7 +522,7 @@ spv_result_t Parser::parseOperand(size_t inst_offset,
522522
return diagnostic()
523523
<< "Invalid " << spvOperandTypeStr(type) << ": " << word;
524524
}
525-
spvtools::InstructionDesc* opcode_entry = nullptr;
525+
const spvtools::InstructionDesc* opcode_entry = nullptr;
526526
if (spvtools::LookupOpcode(spv::Op(word), &opcode_entry)) {
527527
return diagnostic(SPV_ERROR_INTERNAL)
528528
<< "OpSpecConstant opcode table out of sync";
@@ -688,7 +688,7 @@ spv_result_t Parser::parseOperand(size_t inst_offset,
688688
if (type == SPV_OPERAND_TYPE_OPTIONAL_FPENCODING)
689689
parsed_operand.type = SPV_OPERAND_TYPE_FPENCODING;
690690

691-
spvtools::OperandDesc* entry = nullptr;
691+
const spvtools::OperandDesc* entry = nullptr;
692692
if (spvtools::LookupOperand(type, word, &entry)) {
693693
return diagnostic()
694694
<< "Invalid " << spvOperandTypeStr(parsed_operand.type)
@@ -699,7 +699,7 @@ spv_result_t Parser::parseOperand(size_t inst_offset,
699699
} break;
700700

701701
case SPV_OPERAND_TYPE_SOURCE_LANGUAGE: {
702-
spvtools::OperandDesc* entry = nullptr;
702+
const spvtools::OperandDesc* entry = nullptr;
703703
if (spvtools::LookupOperand(type, word, &entry)) {
704704
return diagnostic()
705705
<< "Invalid " << spvOperandTypeStr(parsed_operand.type)
@@ -754,7 +754,7 @@ spv_result_t Parser::parseOperand(size_t inst_offset,
754754
uint32_t remaining_word = word;
755755
for (uint32_t mask = (1u << 31); remaining_word; mask >>= 1) {
756756
if (remaining_word & mask) {
757-
spvtools::OperandDesc* entry = nullptr;
757+
const spvtools::OperandDesc* entry = nullptr;
758758
if (spvtools::LookupOperand(type, mask, &entry)) {
759759
return diagnostic()
760760
<< "Invalid " << spvOperandTypeStr(parsed_operand.type)
@@ -767,7 +767,7 @@ spv_result_t Parser::parseOperand(size_t inst_offset,
767767
}
768768
if (word == 0) {
769769
// An all-zeroes mask *might* also be valid.
770-
spvtools::OperandDesc* entry = nullptr;
770+
const spvtools::OperandDesc* entry = nullptr;
771771
if (SPV_SUCCESS == spvtools::LookupOperand(type, 0, &entry)) {
772772
// Prepare for its operands, if any.
773773
spvPushOperandTypes(entry->operands(), expected_operands);

source/disassemble.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ void InstructionDisassembler::EmitOperand(std::ostream& stream,
886886
}
887887
} break;
888888
case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER: {
889-
spvtools::InstructionDesc* opcodeEntry = nullptr;
889+
const spvtools::InstructionDesc* opcodeEntry = nullptr;
890890
if (LookupOpcode(spv::Op(word), &opcodeEntry))
891891
assert(false && "should have caught this earlier");
892892
SetRed(stream);
@@ -949,7 +949,7 @@ void InstructionDisassembler::EmitOperand(std::ostream& stream,
949949
case SPV_OPERAND_TYPE_QUANTIZATION_MODES:
950950
case SPV_OPERAND_TYPE_FPENCODING:
951951
case SPV_OPERAND_TYPE_OVERFLOW_MODES: {
952-
spvtools::OperandDesc* entry = nullptr;
952+
const spvtools::OperandDesc* entry = nullptr;
953953
if (spvtools::LookupOperand(operand.type, word, &entry))
954954
assert(false && "should have caught this earlier");
955955
stream << entry->name().data();
@@ -969,7 +969,7 @@ void InstructionDisassembler::EmitOperand(std::ostream& stream,
969969
if (spvOperandIsConcreteMask(operand.type)) {
970970
EmitMaskOperand(stream, operand.type, word);
971971
} else if (spvOperandIsConcrete(operand.type)) {
972-
spvtools::OperandDesc* entry = nullptr;
972+
const spvtools::OperandDesc* entry = nullptr;
973973
if (spvtools::LookupOperand(operand.type, word, &entry))
974974
assert(false && "should have caught this earlier");
975975
stream << entry->name().data();
@@ -992,7 +992,7 @@ void InstructionDisassembler::EmitMaskOperand(std::ostream& stream,
992992
for (mask = 1; remaining_word; mask <<= 1) {
993993
if (remaining_word & mask) {
994994
remaining_word ^= mask;
995-
spvtools::OperandDesc* entry = nullptr;
995+
const spvtools::OperandDesc* entry = nullptr;
996996
if (spvtools::LookupOperand(type, mask, &entry))
997997
assert(false && "should have caught this earlier");
998998
if (num_emitted) stream << "|";
@@ -1003,7 +1003,7 @@ void InstructionDisassembler::EmitMaskOperand(std::ostream& stream,
10031003
if (!num_emitted) {
10041004
// An operand value of 0 was provided, so represent it by the name
10051005
// of the 0 value. In many cases, that's "None".
1006-
spvtools::OperandDesc* entry = nullptr;
1006+
const spvtools::OperandDesc* entry = nullptr;
10071007
if (SPV_SUCCESS == spvtools::LookupOperand(type, 0, &entry))
10081008
stream << entry->name().data();
10091009
}

source/link/linker.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ spv_result_t MergeModules(const MessageConsumer& consumer,
292292
const uint32_t module_addressing_model =
293293
memory_model_inst->GetSingleWordOperand(0u);
294294
if (module_addressing_model != linked_addressing_model) {
295-
spvtools::OperandDesc* linked_desc = nullptr;
296-
spvtools::OperandDesc* module_desc = nullptr;
295+
const spvtools::OperandDesc* linked_desc = nullptr;
296+
const spvtools::OperandDesc* module_desc = nullptr;
297297
spvtools::LookupOperand(SPV_OPERAND_TYPE_ADDRESSING_MODEL,
298298
linked_addressing_model, &linked_desc);
299299
spvtools::LookupOperand(SPV_OPERAND_TYPE_ADDRESSING_MODEL,
@@ -308,8 +308,8 @@ spv_result_t MergeModules(const MessageConsumer& consumer,
308308
const uint32_t module_memory_model =
309309
memory_model_inst->GetSingleWordOperand(1u);
310310
if (module_memory_model != linked_memory_model) {
311-
spvtools::OperandDesc* linked_desc = nullptr;
312-
spvtools::OperandDesc* module_desc = nullptr;
311+
const spvtools::OperandDesc* linked_desc = nullptr;
312+
const spvtools::OperandDesc* module_desc = nullptr;
313313
spvtools::LookupOperand(SPV_OPERAND_TYPE_MEMORY_MODEL,
314314
linked_memory_model, &linked_desc);
315315
spvtools::LookupOperand(SPV_OPERAND_TYPE_MEMORY_MODEL,
@@ -335,7 +335,7 @@ spv_result_t MergeModules(const MessageConsumer& consumer,
335335
return v.first == model && v.second == name;
336336
});
337337
if (i != entry_points.end()) {
338-
spvtools::OperandDesc* desc = nullptr;
338+
const spvtools::OperandDesc* desc = nullptr;
339339
spvtools::LookupOperand(SPV_OPERAND_TYPE_EXECUTION_MODEL, model, &desc);
340340
return DiagnosticStream(position, consumer, "", SPV_ERROR_INTERNAL)
341341
<< "The entry point \"" << name << "\", with execution model "

source/name_mapper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ spv_result_t FriendlyNameMapper::ParseInstruction(
328328

329329
std::string FriendlyNameMapper::NameForEnumOperand(spv_operand_type_t type,
330330
uint32_t word) {
331-
spvtools::OperandDesc* desc = nullptr;
331+
const spvtools::OperandDesc* desc = nullptr;
332332
if (SPV_SUCCESS == spvtools::LookupOperand(type, word, &desc)) {
333333
return desc->name().data();
334334
} else {

source/opcode.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void spvInstructionCopy(const uint32_t* words, const spv::Op opcode,
8888
}
8989

9090
const char* spvOpcodeString(const uint32_t opcode) {
91-
spvtools::InstructionDesc* desc = nullptr;
91+
const spvtools::InstructionDesc* desc = nullptr;
9292
if (SPV_SUCCESS !=
9393
spvtools::LookupOpcode(static_cast<spv::Op>(opcode), &desc)) {
9494
assert(0 && "Unreachable!");

source/operand.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ void spvPushOperandTypesForMask(const spv_operand_type_t type,
247247
for (uint32_t candidate_bit = (1u << 31u); candidate_bit;
248248
candidate_bit >>= 1) {
249249
if (candidate_bit & mask) {
250-
spvtools::OperandDesc* entry = nullptr;
250+
const spvtools::OperandDesc* entry = nullptr;
251251
if (SPV_SUCCESS == spvtools::LookupOperand(type, candidate_bit, &entry)) {
252252
spvPushOperandTypes(entry->operands(), pattern);
253253
}

source/opt/feature_manager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void FeatureManager::AddCapability(spv::Capability cap) {
5454

5555
capabilities_.insert(cap);
5656

57-
spvtools::OperandDesc* desc = nullptr;
57+
const spvtools::OperandDesc* desc = nullptr;
5858
if (SPV_SUCCESS == spvtools::LookupOperand(SPV_OPERAND_TYPE_CAPABILITY,
5959
uint32_t(cap), &desc)) {
6060
for (auto capability :

source/opt/replace_invalid_opc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ uint32_t ReplaceInvalidOpcodePass::GetSpecialConstant(uint32_t type_id) {
209209
}
210210

211211
std::string ReplaceInvalidOpcodePass::BuildWarningMessage(spv::Op opcode) {
212-
spvtools::InstructionDesc* opcode_desc = nullptr;
212+
const spvtools::InstructionDesc* opcode_desc = nullptr;
213213
spvtools::LookupOpcode(opcode, &opcode_desc);
214214
std::string message = "Removing ";
215215
message += opcode_desc->name().data();

source/opt/trim_capabilities_pass.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ constexpr std::array<std::pair<spv::Op, OpcodeHandler>, 14> kOpcodeHandlers{{
449449
namespace {
450450
ExtensionSet getExtensionsRelatedTo(const CapabilitySet& capabilities) {
451451
ExtensionSet output;
452-
spvtools::OperandDesc* desc = nullptr;
452+
const spvtools::OperandDesc* desc = nullptr;
453453
for (auto capability : capabilities) {
454454
if (SPV_SUCCESS !=
455455
spvtools::LookupOperand(SPV_OPERAND_TYPE_CAPABILITY,
@@ -512,7 +512,7 @@ void TrimCapabilitiesPass::addInstructionRequirementsForOpcode(
512512
return;
513513
}
514514

515-
spvtools::InstructionDesc* desc;
515+
const spvtools::InstructionDesc* desc;
516516
auto result = spvtools::LookupOpcode(opcode, &desc);
517517
if (result != SPV_SUCCESS) {
518518
return;
@@ -550,7 +550,7 @@ void TrimCapabilitiesPass::addInstructionRequirementsForOperand(
550550

551551
// case 1: Operand is a single value, can directly lookup.
552552
if (!spvOperandIsConcreteMask(operand.type)) {
553-
spvtools::OperandDesc* desc = nullptr;
553+
const spvtools::OperandDesc* desc = nullptr;
554554
auto result =
555555
spvtools::LookupOperand(operand.type, operand.words[0], &desc);
556556
if (result != SPV_SUCCESS) {
@@ -568,7 +568,7 @@ void TrimCapabilitiesPass::addInstructionRequirementsForOperand(
568568
continue;
569569
}
570570

571-
spvtools::OperandDesc* desc = nullptr;
571+
const spvtools::OperandDesc* desc = nullptr;
572572
auto result = spvtools::LookupOperand(operand.type, mask, &desc);
573573
if (result != SPV_SUCCESS) {
574574
continue;
@@ -647,7 +647,7 @@ void TrimCapabilitiesPass::addInstructionRequirements(
647647
void TrimCapabilitiesPass::AddExtensionsForOperand(
648648
const spv_operand_type_t type, const uint32_t value,
649649
ExtensionSet* extensions) const {
650-
spvtools::OperandDesc* desc = nullptr;
650+
const spvtools::OperandDesc* desc = nullptr;
651651
spv_result_t result = spvtools::LookupOperand(type, value, &desc);
652652
if (result != SPV_SUCCESS) {
653653
return;

0 commit comments

Comments
 (0)