[PATCH] D98032: [clang-format] Improve clang-format-diff.py error message

David Spickett via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 5 03:42:18 PST 2021


DavidSpickett created this revision.
DavidSpickett requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Previously if we couldn't run the clang-format command
for some reason, you'd get an unhelpful error message:

  OSError: [Errno 2] No such file or directory

Which doesn't tell you what was happening to cause this.

Catch the error and add the command we were attempting to run:

  RuntimeError: Failed to run "<...>/clang-food <...>" - No such file or directory"
  RuntimeError: Failed to run "<...>/clang-format <...>" - Permission denied"


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D98032

Files:
  clang/tools/clang-format/clang-format-diff.py


Index: clang/tools/clang-format/clang-format-diff.py
===================================================================
--- clang/tools/clang-format/clang-format-diff.py
+++ clang/tools/clang-format/clang-format-diff.py
@@ -103,11 +103,19 @@
     command.extend(lines)
     if args.style:
       command.extend(['-style', args.style])
-    p = subprocess.Popen(command,
-                         stdout=subprocess.PIPE,
-                         stderr=None,
-                         stdin=subprocess.PIPE,
-                         universal_newlines=True)
+
+    try:
+      p = subprocess.Popen(command,
+                           stdout=subprocess.PIPE,
+                           stderr=None,
+                           stdin=subprocess.PIPE,
+                           universal_newlines=True)
+    except OSError as e:
+      # Give the user more context when clang-format isn't
+      # found/isn't executable, etc.
+      raise RuntimeError(
+        'Failed to run "%s" - %s"' % (" ".join(command), e.strerror))
+
     stdout, stderr = p.communicate()
     if p.returncode != 0:
       sys.exit(p.returncode)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D98032.328469.patch
Type: text/x-patch
Size: 1125 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210305/f7ca46b9/attachment.bin>


More information about the cfe-commits mailing list