linter.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright (c) 2021 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 "spirv-tools/linter.hpp"
  15. #include "source/lint/lints.h"
  16. #include "source/opt/build_module.h"
  17. #include "source/opt/ir_context.h"
  18. #include "spirv-tools/libspirv.h"
  19. #include "spirv-tools/libspirv.hpp"
  20. #include "spirv/unified1/spirv.h"
  21. namespace spvtools {
  22. struct Linter::Impl {
  23. explicit Impl(spv_target_env env) : target_env(env) {
  24. message_consumer = [](spv_message_level_t /*level*/, const char* /*source*/,
  25. const spv_position_t& /*position*/,
  26. const char* /*message*/) {};
  27. }
  28. spv_target_env target_env; // Target environment.
  29. MessageConsumer message_consumer; // Message consumer.
  30. };
  31. Linter::Linter(spv_target_env env) : impl_(new Impl(env)) {}
  32. Linter::~Linter() {}
  33. void Linter::SetMessageConsumer(MessageConsumer consumer) {
  34. impl_->message_consumer = std::move(consumer);
  35. }
  36. const MessageConsumer& Linter::Consumer() const {
  37. return impl_->message_consumer;
  38. }
  39. bool Linter::Run(const uint32_t* binary, size_t binary_size) {
  40. std::unique_ptr<opt::IRContext> context =
  41. BuildModule(SPV_ENV_VULKAN_1_2, Consumer(), binary, binary_size);
  42. if (context == nullptr) return false;
  43. bool result = true;
  44. result &= lint::lints::CheckDivergentDerivatives(context.get());
  45. return result;
  46. }
  47. } // namespace spvtools