validate_debug.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (c) 2018 Google LLC.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/spirv_target_env.h"
  15. #include "source/val/instruction.h"
  16. #include "source/val/validate.h"
  17. #include "source/val/validation_state.h"
  18. namespace spvtools {
  19. namespace val {
  20. namespace {
  21. spv_result_t ValidateMemberName(ValidationState_t& _, const Instruction* inst) {
  22. const auto type_id = inst->GetOperandAs<uint32_t>(0);
  23. const auto type = _.FindDef(type_id);
  24. if (!type || spv::Op::OpTypeStruct != type->opcode()) {
  25. return _.diag(SPV_ERROR_INVALID_ID, inst)
  26. << "OpMemberName Type <id> " << _.getIdName(type_id)
  27. << " is not a struct type.";
  28. }
  29. const auto member_id = inst->GetOperandAs<uint32_t>(1);
  30. const auto member_count = (uint32_t)(type->words().size() - 2);
  31. if (member_count <= member_id) {
  32. return _.diag(SPV_ERROR_INVALID_ID, inst)
  33. << "OpMemberName Member <id> " << _.getIdName(member_id)
  34. << " index is larger than Type <id> " << _.getIdName(type->id())
  35. << "s member count.";
  36. }
  37. return SPV_SUCCESS;
  38. }
  39. spv_result_t ValidateLine(ValidationState_t& _, const Instruction* inst) {
  40. const auto file_id = inst->GetOperandAs<uint32_t>(0);
  41. const auto file = _.FindDef(file_id);
  42. if (!file || spv::Op::OpString != file->opcode()) {
  43. return _.diag(SPV_ERROR_INVALID_ID, inst)
  44. << "OpLine Target <id> " << _.getIdName(file_id)
  45. << " is not an OpString.";
  46. }
  47. return SPV_SUCCESS;
  48. }
  49. } // namespace
  50. spv_result_t DebugPass(ValidationState_t& _, const Instruction* inst) {
  51. switch (inst->opcode()) {
  52. case spv::Op::OpMemberName:
  53. if (auto error = ValidateMemberName(_, inst)) return error;
  54. break;
  55. case spv::Op::OpLine:
  56. if (auto error = ValidateLine(_, inst)) return error;
  57. break;
  58. default:
  59. break;
  60. }
  61. return SPV_SUCCESS;
  62. }
  63. } // namespace val
  64. } // namespace spvtools