From d73f79f1924bfb703f62e4a413428d7948414274 Mon Sep 17 00:00:00 2001
From: Antonio Maiorano <amaiorano@gmail.com>
Date: Sat, 3 Dec 2016 00:40:44 -0500
Subject: [PATCH] clang-format-vsix: fail when clang-format outputs to stderr

When clang-format outputs to stderr but returns 0, the extension will format the code anyway. This happens, for instance, when there's a syntax error or unknown value in a .clang-format file; the result is that the extension silently formats using the fallback style without informing the user of the problem. This change treats stderr output as an error, making sure it gets displayed to the user, and not formatting the code.
---
 tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs b/tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
index 6af2fd1..48a6efb 100644
--- a/tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
+++ b/tools/clang-format-vs/ClangFormat/ClangFormatPackage.cs
@@ -295,12 +295,18 @@ namespace LLVM.ClangFormat
             string output = process.StandardOutput.ReadToEnd();
             // 5. clang-format is done, wait until it is fully shut down.
             process.WaitForExit();
-            if (process.ExitCode != 0)
+
+            // FIXME: If clang-format writes enough to the standard error stream to block,
+            // we will never reach this point; instead, read the standard error asynchronously.
+            string stdErr = process.StandardError.ReadToEnd();
+
+            if (process.ExitCode != 0 || stdErr.Length > 0)
             {
-                // FIXME: If clang-format writes enough to the standard error stream to block,
-                // we will never reach this point; instead, read the standard error asynchronously.
-                throw new Exception(process.StandardError.ReadToEnd());
+                throw new Exception(
+                    (stdErr.Length > 0 ? stdErr + "\n\n" : "") +
+                    "(Exit Code: " + process.ExitCode + ")");
             }
+
             return output;
         }
 
-- 
2.10.1.windows.1

