lint.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 <iostream>
  15. #include "source/opt/log.h"
  16. #include "spirv-tools/linter.hpp"
  17. #include "tools/io.h"
  18. #include "tools/util/cli_consumer.h"
  19. #include "tools/util/flags.h"
  20. namespace {
  21. constexpr auto kDefaultEnvironment = SPV_ENV_UNIVERSAL_1_6;
  22. constexpr auto kHelpTextFmt =
  23. R"(%s - Lint a SPIR-V binary module.
  24. Usage: %s [options] <filename>
  25. Options:
  26. -h, --help Print this help.
  27. --version Display assembler version information.
  28. )";
  29. } // namespace
  30. // clang-format off
  31. FLAG_SHORT_bool( h, /* default_value= */ false, /* required= */ false);
  32. FLAG_LONG_bool( help, /* default_value= */ false, /* required= */ false);
  33. FLAG_LONG_bool( version, /* default_value= */ false, /* required= */ false);
  34. // clang-format on
  35. int main(int, const char** argv) {
  36. if (!flags::Parse(argv)) {
  37. return 1;
  38. }
  39. if (flags::h.value() || flags::help.value()) {
  40. printf(kHelpTextFmt, argv[0], argv[0]);
  41. return 0;
  42. }
  43. if (flags::version.value()) {
  44. printf("%s\n", spvSoftwareVersionDetailsString());
  45. return 0;
  46. }
  47. if (flags::positional_arguments.size() != 1) {
  48. spvtools::Error(spvtools::utils::CLIMessageConsumer, nullptr, {},
  49. "expected exactly one input file.");
  50. return 1;
  51. }
  52. spvtools::Linter linter(kDefaultEnvironment);
  53. linter.SetMessageConsumer(spvtools::utils::CLIMessageConsumer);
  54. std::vector<uint32_t> binary;
  55. if (!ReadBinaryFile(flags::positional_arguments[0].c_str(), &binary)) {
  56. return 1;
  57. }
  58. return linter.Run(binary.data(), binary.size()) ? 0 : 1;
  59. }