linter.cpp 1.8 KB

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