]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
d: Remove generated D header files on error (PR101657)
authorIain Buclaw <ibuclaw@gdcproject.org>
Mon, 26 Jul 2021 15:24:38 +0000 (17:24 +0200)
committerIain Buclaw <ibuclaw@gdcproject.org>
Thu, 29 Jul 2021 14:16:19 +0000 (16:16 +0200)
If an error occurs later during compilation, remember that we generated
the headers, so that they can be removed before exit.

PR d/101657

gcc/d/ChangeLog:

* d-lang.cc (d_parse_file): Remove generated D header files on error.

gcc/testsuite/ChangeLog:

* gdc.dg/pr101657.d: New test.

gcc/d/d-lang.cc
gcc/testsuite/gdc.dg/pr101657.d [new file with mode: 0644]

index 6ad3823d910dcece0a7883708ebbabb14c3b8745..ac0945b1f3444886735443c3ebdbaf48ae8b2e3e 100644 (file)
@@ -1000,6 +1000,10 @@ d_parse_file (void)
        }
     }
 
+  /* If an error occurs later during compilation, remember that we generated
+     the headers, so that they can be removed before exit.  */
+  bool dump_headers = false;
+
   if (global.errors)
     goto had_errors;
 
@@ -1019,6 +1023,8 @@ d_parse_file (void)
 
          genhdrfile (m);
        }
+
+      dump_headers = true;
     }
 
   if (global.errors)
@@ -1243,6 +1249,19 @@ d_parse_file (void)
      exit with an error status.  */
   errorcount += (global.errors + global.warnings);
 
+  /* Remove generated .di files on error.  */
+  if (errorcount && dump_headers)
+    {
+      for (size_t i = 0; i < modules.length; i++)
+       {
+         Module *m = modules[i];
+         if (d_option.fonly && m != Module::rootModule)
+           continue;
+
+         remove (m->hdrfile->toChars ());
+       }
+    }
+
   /* Write out globals.  */
   d_finish_compilation (vec_safe_address (global_declarations),
                        vec_safe_length (global_declarations));
diff --git a/gcc/testsuite/gdc.dg/pr101657.d b/gcc/testsuite/gdc.dg/pr101657.d
new file mode 100644 (file)
index 0000000..0d77c36
--- /dev/null
@@ -0,0 +1,14 @@
+// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101657
+// { dg-do compile }
+// { dg-additional-options "-H" }
+
+void fun101657()
+{
+    fail; // { dg-error "undefined identifier 'fail'" }
+}
+
+// { dg-final { if ![file exists pr101657.di] \{                } }
+// { dg-final {     pass "gdc.dg/pr101657.d   (file exists pr101657.di)" } }
+// { dg-final { \} else \{                                      } }
+// { dg-final {     fail "gdc.dg/pr101657.d   (file exists pr101657.di)" } }
+// { dg-final { \}                                              } }