From 4d59abf295cd371448f22c1724b955dce4974302 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Arsen=20Arsenovi=C4=87?= <arsen@aarsen.me>
Date: Mon, 6 May 2024 14:23:58 +0200
Subject: [PATCH] Add the cmm_cpp_is_gcc predicate to the testsuite

A future C-- test called T24474-cmm-override-g0 relies on the
GCC-specific behaviour of -g3 implying -dD, which, in turn, leads to it
emitting #defines past the preprocessing stage.  Clang, at least, does
not do this, so the test would fail if ran on Clang.

As the behaviour here being tested is ``-optCmmP-g3'' undoing effects of
the workaround we apply as a fix for bug #24474, and the workaround was
for GCC-specific behaviour, the test needs to be marked as fragile on
other compilers.
---
 testsuite/config/ghc            | 23 ++++++++++++++++++-----
 testsuite/driver/testglobals.py |  3 +++
 testsuite/driver/testlib.py     |  4 ++++
 3 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/testsuite/config/ghc b/testsuite/config/ghc
index 01d1fcf14e9d..90b11bc57fc4 100644
--- a/testsuite/config/ghc
+++ b/testsuite/config/ghc
@@ -223,7 +223,7 @@ def get_compiler_info():
     # See Note [Replacing backward slashes in config.libdir].
     config.libdir = config.libdir.replace('\\', '/')
 
-    def test_compile(flags):
+    def test_compile(flags, test_filename='test.hs', test_src=None):
         """
         Check whether GHC can compile in the given way. This is used as a
         proxy to determine, e.g., whether profiled libraries were built, or
@@ -234,12 +234,15 @@ def get_compiler_info():
 
         res = False
 
-        with tempfile.TemporaryDirectory() as d:
-            src = Path(d) / 'test.hs'
-            src.write_text(textwrap.dedent('''
+        if test_src is None:
+            test_src = '''
                 module Main where
                 main = putStrLn "Hello World!"
-            '''))
+            '''
+
+        with tempfile.TemporaryDirectory() as d:
+            src = Path(d) / test_filename
+            src.write_text(textwrap.dedent(test_src))
             try:
                 p = subprocess.run(
                     '{} -v0 {} -o test '.format(config.compiler, src) + ' '.join(flags),
@@ -295,6 +298,16 @@ def get_compiler_info():
         config.plugin_way_flags = "-static"
         config.ghc_th_way       = "normal"
 
+    config.cmm_cpp_is_gcc = test_compile(
+        ['-c'],
+        test_filename='test.cmm',
+        test_src='''
+        #if defined(__clang__) || !defined(__GNUC__)
+        # error "not gcc"
+        #endif
+        '''
+    )
+
 # Note [Replacing backward slashes in config.libdir]
 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 #
diff --git a/testsuite/driver/testglobals.py b/testsuite/driver/testglobals.py
index 8617841a5573..d515a0e7e0e1 100644
--- a/testsuite/driver/testglobals.py
+++ b/testsuite/driver/testglobals.py
@@ -232,6 +232,9 @@ class TestConfig:
         # The path specifies the file in which to write the dependencies
         self.only_report_hadrian_deps = None # type: Optional[Path]
 
+        # Are we using GCC to preprocess C--?
+        self.cmm_cpp_is_gcc = False
+
     def validate(self) -> None:
         """ Check the TestConfig for self-consistency """
         def assert_implies(a: bool, b: bool):
diff --git a/testsuite/driver/testlib.py b/testsuite/driver/testlib.py
index db157ae0f277..7ba54da41589 100644
--- a/testsuite/driver/testlib.py
+++ b/testsuite/driver/testlib.py
@@ -996,6 +996,10 @@ def llvm_build ( ) -> bool:
 def have_thread_sanitizer( ) -> bool:
     return config.have_thread_sanitizer
 
+
+def gcc_as_cmmp() -> bool:
+    return config.cmm_cpp_is_gcc
+
 # ---
 
 # Note [Measuring residency]
-- 
GitLab