[clang] 9c0069d - [clang-format] Improve clang-format-diff.py error message

David Spickett via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 5 05:28:57 PST 2021


Author: David Spickett
Date: 2021-03-05T13:28:51Z
New Revision: 9c0069d836b326f3ae0b92a8f095b0707a944ed0

URL: https://github.com/llvm/llvm-project/commit/9c0069d836b326f3ae0b92a8f095b0707a944ed0
DIFF: https://github.com/llvm/llvm-project/commit/9c0069d836b326f3ae0b92a8f095b0707a944ed0.diff

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

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/tools/clang-format/clang-format-
diff .py b/clang/tools/clang-format/clang-format-
diff .py
index 6e653a134289..efed8168cc50 100755
--- a/clang/tools/clang-format/clang-format-
diff .py
+++ b/clang/tools/clang-format/clang-format-
diff .py
@@ -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)


        


More information about the cfe-commits mailing list