lint.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. const auto kDefaultEnvironment = SPV_ENV_UNIVERSAL_1_6;
  20. namespace {
  21. // Status and actions to perform after parsing command-line arguments.
  22. enum LintActions { LINT_CONTINUE, LINT_STOP };
  23. struct LintStatus {
  24. LintActions action;
  25. int code;
  26. };
  27. // Parses command-line flags. |argc| contains the number of command-line flags.
  28. // |argv| points to an array of strings holding the flags.
  29. //
  30. // On return, this function stores the name of the input program in |in_file|.
  31. // The return value indicates whether optimization should continue and a status
  32. // code indicating an error or success.
  33. LintStatus ParseFlags(int argc, const char** argv, const char** in_file) {
  34. // TODO (dongja): actually parse flags, etc.
  35. if (argc != 2) {
  36. spvtools::Error(spvtools::utils::CLIMessageConsumer, nullptr, {},
  37. "expected exactly one argument: in_file");
  38. return {LINT_STOP, 1};
  39. }
  40. *in_file = argv[1];
  41. return {LINT_CONTINUE, 0};
  42. }
  43. } // namespace
  44. int main(int argc, const char** argv) {
  45. const char* in_file = nullptr;
  46. spv_target_env target_env = kDefaultEnvironment;
  47. spvtools::Linter linter(target_env);
  48. linter.SetMessageConsumer(spvtools::utils::CLIMessageConsumer);
  49. LintStatus status = ParseFlags(argc, argv, &in_file);
  50. if (status.action == LINT_STOP) {
  51. return status.code;
  52. }
  53. std::vector<uint32_t> binary;
  54. if (!ReadBinaryFile(in_file, &binary)) {
  55. return 1;
  56. }
  57. bool ok = linter.Run(binary.data(), binary.size());
  58. return ok ? 0 : 1;
  59. }