[clang-format] Improve clang-format-diff.py error message
authorDavid Spickett <david.spickett@linaro.org>
Fri, 5 Mar 2021 11:13:58 +0000 (11:13 +0000)
committerDavid Spickett <david.spickett@linaro.org>
Fri, 5 Mar 2021 13:28:51 +0000 (13:28 +0000)
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"
```

Reviewed By: krasimir

Differential Revision: https://reviews.llvm.org/D98032

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

index 6e653a1..efed816 100755 (executable)
@@ -103,11 +103,19 @@ def main():
     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)