[COFF] Check errorCount before committing the output file

This avoids producing an output file if errors appeared late in the
linking process (e.g. while fixing relocations, or as in the test,
while checking for multiple resources). If an output file is produced,
build tools might not retry building it on rebuilds, even if a previous
build failed due to the error return code.

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

git-svn-id: https://llvm.org/svn/llvm-project/lld/trunk@369445 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/COFF/Writer.cpp b/COFF/Writer.cpp
index 032439e..0adf155 100644
--- a/COFF/Writer.cpp
+++ b/COFF/Writer.cpp
@@ -628,6 +628,9 @@
 
   writeMapFile(outputSections);
 
+  if (errorCount())
+    return;
+
   ScopedTimer t2(diskCommitTimer);
   if (auto e = buffer->commit())
     fatal("failed to write the output file: " + toString(std::move(e)));
diff --git a/test/COFF/multiple-resource-objs.test b/test/COFF/multiple-resource-objs.test
index 4263c64..be5f9a2 100644
--- a/test/COFF/multiple-resource-objs.test
+++ b/test/COFF/multiple-resource-objs.test
@@ -1,7 +1,9 @@
 # RUN: llvm-cvtres /out:%t_resource.obj %S/Inputs/resource.res
 # RUN: llvm-cvtres /out:%t_id.obj %S/Inputs/id.res
+# RUN: rm -f %t.exe
 # RUN: not lld-link /out:%t.exe /dll /noentry %t_id.obj %t_resource.obj 2>&1 | \
 # RUN:     FileCheck --check-prefix=TWOOBJ %s
+# RUN: not test -f %t.exe
 
 TWOOBJ: error: {{.*}}_resource.obj: more than one resource obj file not allowed, already got {{.*}}_id.obj