[libcxx-commits] [PATCH] D107912: [libc++] Avoid conflating stderr and stdout in the DSL

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed Aug 11 09:28:53 PDT 2021


ldionne created this revision.
Herald added a subscriber: arichardson.
ldionne requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

This is a workaround until https://reviews.llvm.org/D81892 is merged
and the internal Lit shell stops conflating error output with normal
output. Without this, any program that writes to stderr will trip up
the programOutput function, because it will pick up the '# command stderr:'
string and think it's part of the command's stdout.

rdar://81056048


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D107912

Files:
  libcxx/test/libcxx/selftest/dsl/dsl.sh.py
  libcxx/utils/libcxx/test/dsl.py


Index: libcxx/utils/libcxx/test/dsl.py
===================================================================
--- libcxx/utils/libcxx/test/dsl.py
+++ libcxx/utils/libcxx/test/dsl.py
@@ -64,9 +64,20 @@
     if not os.path.exists(d):
       os.makedirs(d)
   res = lit.TestRunner.executeScriptInternal(test, litConfig, tmpBase, parsedCommands, execDir)
-  if isinstance(res, lit.Test.Result):
-    res = ('', '', 127, None)
-  return res
+  if isinstance(res, lit.Test.Result): # Handle failure to parse the Lit test
+    res = ('', res.output, 127, None)
+  (out, err, exitCode, timeoutInfo) = res
+
+  # TODO: As a temporary workaround until https://reviews.llvm.org/D81892 lands, manually
+  #       split any stderr output that is included in stdout. It shouldn't be there, but
+  #       the Lit internal shell conflates stderr and stdout.
+  conflatedErrorOutput = re.search("(# command stderr:.+$)", out, flags=re.DOTALL)
+  if conflatedErrorOutput:
+    conflatedErrorOutput = conflatedErrorOutput.group(0)
+    out = out[:-len(conflatedErrorOutput)]
+    err += conflatedErrorOutput
+
+  return (out, err, exitCode, timeoutInfo)
 
 def _makeConfigTest(config, testPrefix=''):
   sourceRoot = os.path.join(config.test_exec_root, '__config_src__')
@@ -122,7 +133,7 @@
       if exitCode != 0:
         return None
 
-      actualOut = re.search("command output:\n(.+)\n$", out, flags=re.DOTALL)
+      actualOut = re.search("# command output:\n(.+)\n$", out, flags=re.DOTALL)
       actualOut = actualOut.group(1) if actualOut else ""
       return actualOut
 
Index: libcxx/test/libcxx/selftest/dsl/dsl.sh.py
===================================================================
--- libcxx/test/libcxx/selftest/dsl/dsl.sh.py
+++ libcxx/test/libcxx/selftest/dsl/dsl.sh.py
@@ -196,6 +196,7 @@
         #include <cstdio>
         int main(int, char**) {
             std::printf("MACRO=%u\\n", MACRO);
+            return 0;
         }
         """
         compileFlagsIndex = findIndex(self.config.substitutions, lambda x: x[0] == '%{compile_flags}')
@@ -209,6 +210,19 @@
         output2 = dsl.programOutput(self.config, source)
         self.assertEqual(output2, "MACRO=2\n")
 
+    def test_program_stderr_is_not_conflated_with_stdout(self):
+        # Run a program that produces stdout output and stderr output too, making
+        # sure the stderr output does not pollute the stdout output.
+        source = """
+        #include <cstdio>
+        int main(int, char**) {
+            std::fprintf(stdout, "STDOUT-OUTPUT");
+            std::fprintf(stderr, "STDERR-OUTPUT");
+            return 0;
+        }
+        """
+        self.assertEqual(dsl.programOutput(self.config, source), "STDOUT-OUTPUT")
+
 
 class TestHasLocale(SetupConfigs):
     """


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D107912.365777.patch
Type: text/x-patch
Size: 2763 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20210811/efc86a30/attachment-0001.bin>


More information about the libcxx-commits mailing list