@@ -402,7 +402,7 @@ TEST(StartTracingActivation, ActivatesIfNotActive) {
402402 TraceInfo::TheTraceInfo = nullptr ;
403403
404404 // StartTracing should call InitTracing (covers line 151).
405- std::string Path = CppInterOp::Tracing::StartTracing ();
405+ std::string Path = CppInterOp::Tracing::StartTracing (/* WriteOnStdErr= */ false );
406406 ASSERT_NE (TraceInfo::TheTraceInfo, nullptr );
407407 ASSERT_FALSE (Path.empty ());
408408
@@ -711,7 +711,7 @@ static std::string ReadFileToString(const std::string& path) {
711711// This test reads source files from the build tree via CPPINTEROP_DIR.
712712TEST_F (TracingTest, StartStopTracingWritesToFile) {
713713 // StartTracing begins recording; StopTracing writes the file.
714- std::string Path = CppInterOp::Tracing::StartTracing ();
714+ std::string Path = CppInterOp::Tracing::StartTracing (/* WriteOnStdErr= */ false );
715715 ASSERT_FALSE (Path.empty ());
716716
717717 // Calls within the region are recorded.
@@ -734,7 +734,7 @@ TEST_F(TracingTest, OnlyRegionCallsAreRecorded) {
734734 // Calls before StartTracing should not appear.
735735 VoidFunc ();
736736
737- std::string Path = CppInterOp::Tracing::StartTracing ();
737+ std::string Path = CppInterOp::Tracing::StartTracing (/* WriteOnStdErr= */ false );
738738 ASSERT_FALSE (Path.empty ());
739739
740740 AnnotatedFunction (42 );
@@ -767,7 +767,7 @@ TEST_F(TracingTest, StartTracingWithEnvVarNarrowsToRegion) {
767767 // This call is before the region.
768768 VoidFunc ();
769769
770- std::string Path = CppInterOp::Tracing::StartTracing ();
770+ std::string Path = CppInterOp::Tracing::StartTracing (/* WriteOnStdErr= */ false );
771771 ASSERT_FALSE (Path.empty ());
772772
773773 AnnotatedFunction (7 );
@@ -786,11 +786,13 @@ TEST_F(TracingTest, StartTracingWithEnvVarNarrowsToRegion) {
786786
787787TEST_F (TracingTest, MultipleStartStopRegions) {
788788 // Multiple regions should each produce their own file.
789- std::string Path1 = CppInterOp::Tracing::StartTracing ();
789+ std::string Path1 =
790+ CppInterOp::Tracing::StartTracing (/* WriteOnStdErr=*/ false );
790791 AnnotatedFunction (1 );
791792 CppInterOp::Tracing::StopTracing ();
792793
793- std::string Path2 = CppInterOp::Tracing::StartTracing ();
794+ std::string Path2 =
795+ CppInterOp::Tracing::StartTracing (/* WriteOnStdErr=*/ false );
794796 AnnotatedFunction (2 );
795797 CppInterOp::Tracing::StopTracing ();
796798
@@ -810,6 +812,48 @@ TEST_F(TracingTest, MultipleStartStopRegions) {
810812 llvm::sys::fs::remove (Path2);
811813}
812814
815+ // ---------------------------------------------------------------------------
816+ // Tests: StartTracing WriteOnStdErr option
817+ // ---------------------------------------------------------------------------
818+
819+ TEST_F (TracingTest, WriteOnStdErrStreamsEntriesImmediately) {
820+ testing::internal::CaptureStderr ();
821+
822+ // Default is WriteOnStdErr=true — no file is created.
823+ std::string Path = CppInterOp::Tracing::StartTracing ();
824+ EXPECT_TRUE (Path.empty ());
825+
826+ AnnotatedFunction (99 );
827+
828+ // Capture stderr *before* StopTracing — the entry must already be there.
829+ std::string Mid = testing::internal::GetCapturedStderr ();
830+ EXPECT_THAT (Mid, HasSubstr (" Cpp::AnnotatedFunction(99)" ))
831+ << " Entry should appear on stderr immediately, not after StopTracing" ;
832+
833+ // Restart capture for the rest.
834+ testing::internal::CaptureStderr ();
835+ VoidFunc ();
836+ CppInterOp::Tracing::StopTracing (" test-version" );
837+ std::string Rest = testing::internal::GetCapturedStderr ();
838+ EXPECT_THAT (Rest, HasSubstr (" Cpp::VoidFunc()" ));
839+ }
840+
841+ TEST_F (TracingTest, WriteToFileWhenStdErrDisabled) {
842+ std::string Path = CppInterOp::Tracing::StartTracing (/* WriteOnStdErr=*/ false );
843+ ASSERT_FALSE (Path.empty ());
844+
845+ AnnotatedFunction (77 );
846+
847+ CppInterOp::Tracing::StopTracing ();
848+
849+ // The file should contain the full reproducer.
850+ std::string FileContent = ReadFileToString (Path);
851+ EXPECT_THAT (FileContent, HasSubstr (" #include <CppInterOp/CppInterOp.h>" ));
852+ EXPECT_THAT (FileContent, HasSubstr (" Cpp::AnnotatedFunction(77)" ));
853+
854+ llvm::sys::fs::remove (Path);
855+ }
856+
813857// ---------------------------------------------------------------------------
814858// Tests: all CPPINTEROP_API functions must have INTEROP_TRACE
815859// ---------------------------------------------------------------------------
0 commit comments