PRESUBMIT.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright 2020 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. """
  5. Presubmit script for the printing backend.
  6. See https://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
  7. for more details about the presubmit API.
  8. """
  9. USE_PYTHON3 = True
  10. def _CheckForStringViewFromNullableIppApi(input_api, output_api):
  11. """
  12. Looks for all affected lines in CL where one constructs either
  13. base::StringPiece or std::string_view from any ipp*() CUPS API call.
  14. Assumes over-broadly that all ipp*() calls can return NULL.
  15. Returns affected lines as a list of presubmit errors.
  16. """
  17. # Attempts to detect source lines like:
  18. # * base::StringPiece foo = ippDoBar();
  19. # * base::StringPiece foo(ippDoBar());
  20. # and the same for std::string_view.
  21. string_view_re = input_api.re.compile(
  22. r"^.+(base::StringPiece|std::string_view)\s+\w+( = |\()ipp[A-Z].+$")
  23. violations = input_api.canned_checks._FindNewViolationsOfRule(
  24. lambda extension, line:
  25. not (extension in ("cc", "h") and string_view_re.search(line)),
  26. input_api, None)
  27. bulleted_violations = [" * {}".format(entry) for entry in violations]
  28. if bulleted_violations:
  29. return [output_api.PresubmitError(
  30. ("Possible construction of base::StringPiece or std::string_view "
  31. "from CUPS IPP API (that can probably return NULL):\n{}").format(
  32. "\n".join(bulleted_violations))),]
  33. return []
  34. def _CommonChecks(input_api, output_api):
  35. """Actual implementation of presubmits for the printing backend."""
  36. results = []
  37. results.extend(_CheckForStringViewFromNullableIppApi(input_api, output_api))
  38. return results
  39. def CheckChangeOnUpload(input_api, output_api):
  40. """Mandatory presubmit entry point."""
  41. return _CommonChecks(input_api, output_api)
  42. def CheckChangeOnCommit(input_api, output_api):
  43. """Mandatory presubmit entry point."""
  44. return _CommonChecks(input_api, output_api)