From 09cb57ad66a4e57ff8e871815a97762ef441b53d Mon Sep 17 00:00:00 2001
From: Zubin Duggal <zubin.duggal@gmail.com>
Date: Mon, 8 Jan 2024 15:18:31 +0530
Subject: [PATCH] driver: Set -DPROFILING when compiling C++ sources with
 profiling

Earlier, we used to pass all preprocessor flags to the c++ compiler.
This meant that -DPROFILING was passed to the c++ compiler because
it was a part of C++ flags
However, this was incorrect and the behaviour was changed in
8ff3134ed4aa323b0199ad683f72165e51a59ab6. See #21291.

But that commit exposed this bug where -DPROFILING was no longer being passed
when compiling c++ sources.

The fix is to explicitly include -DPROFILING in `opt_cxx` when profiling is
enabled to ensure we pass the correct options for the way to both C and C++
compilers

Fixes #24286
---
 compiler/GHC/Driver/Session.hs    | 3 ++-
 compiler/GHC/Platform/Ways.hs     | 4 ++++
 testsuite/tests/driver/Makefile   | 2 --
 testsuite/tests/driver/T24286.cpp | 7 +++++++
 testsuite/tests/driver/all.T      | 1 +
 5 files changed, 14 insertions(+), 3 deletions(-)
 create mode 100644 testsuite/tests/driver/T24286.cpp

diff --git a/compiler/GHC/Driver/Session.hs b/compiler/GHC/Driver/Session.hs
index f700610037ed..e6fa37a5f3b7 100644
--- a/compiler/GHC/Driver/Session.hs
+++ b/compiler/GHC/Driver/Session.hs
@@ -438,7 +438,8 @@ opt_c                 :: DynFlags -> [String]
 opt_c dflags = concatMap (wayOptc (targetPlatform dflags)) (ways dflags)
             ++ toolSettings_opt_c (toolSettings dflags)
 opt_cxx               :: DynFlags -> [String]
-opt_cxx dflags= toolSettings_opt_cxx $ toolSettings dflags
+opt_cxx dflags = concatMap (wayOptcxx (targetPlatform dflags)) (ways dflags)
+           ++ toolSettings_opt_cxx (toolSettings dflags)
 opt_a                 :: DynFlags -> [String]
 opt_a dflags= toolSettings_opt_a $ toolSettings dflags
 opt_l                 :: DynFlags -> [String]
diff --git a/compiler/GHC/Platform/Ways.hs b/compiler/GHC/Platform/Ways.hs
index e66b8a496daa..903537242af1 100644
--- a/compiler/GHC/Platform/Ways.hs
+++ b/compiler/GHC/Platform/Ways.hs
@@ -31,6 +31,7 @@ module GHC.Platform.Ways
    , wayGeneralFlags
    , wayUnsetGeneralFlags
    , wayOptc
+   , wayOptcxx
    , wayOptl
    , wayOptP
    , wayDesc
@@ -177,6 +178,9 @@ wayOptc _ WayDebug      = []
 wayOptc _ WayDyn        = []
 wayOptc _ WayProf       = ["-DPROFILING"]
 
+wayOptcxx :: Platform -> Way -> [String]
+wayOptcxx = wayOptc -- Use the same flags as C
+
 -- | Pass these options to linker when enabling this way
 wayOptl :: Platform -> Way -> [String]
 wayOptl _ (WayCustom {}) = []
diff --git a/testsuite/tests/driver/Makefile b/testsuite/tests/driver/Makefile
index dc45e8486a5d..156fc0b58c02 100644
--- a/testsuite/tests/driver/Makefile
+++ b/testsuite/tests/driver/Makefile
@@ -808,5 +808,3 @@ T23339B:
 	"$(TEST_HC)" -tmpdir "$(PWD)/tmp" $(TEST_HC_OPTS) -v0 T23339B.hs -finfo-table-map
 	# Check that the file is kept and is the right one
 	find . -name "*.c" -exec cat {} \; | grep "init__ip_init"
-
-
diff --git a/testsuite/tests/driver/T24286.cpp b/testsuite/tests/driver/T24286.cpp
new file mode 100644
index 000000000000..905eb6c79a89
--- /dev/null
+++ b/testsuite/tests/driver/T24286.cpp
@@ -0,0 +1,7 @@
+#if !defined(PROFILING)
+#error PROFILING flag not set for C++ files, see #24286
+#endif
+
+int main() {
+  return 0;
+}
diff --git a/testsuite/tests/driver/all.T b/testsuite/tests/driver/all.T
index a873e607cf62..c8e091a0a7e7 100644
--- a/testsuite/tests/driver/all.T
+++ b/testsuite/tests/driver/all.T
@@ -326,3 +326,4 @@ test('T23339', req_c, makefile_test, [])
 test('T23339B', [extra_files(['T23339.hs']), req_c], makefile_test, [])
 test('T23613', normal, compile_and_run, ['-this-unit-id=foo'])
 test('T23944', [unless(have_dynamic(), skip), extra_files(['T23944A.hs'])], multimod_compile, ['T23944 T23944A', '-fprefer-byte-code -fbyte-code -fno-code -dynamic-too -fwrite-interface'])
+test('T24286', [cxx_src, unless(have_profiling(), skip), extra_files(['T24286.cpp'])], compile, ['-prof -no-hs-main'])
-- 
GitLab