1+ // Comprehensive test for -fms-cbstring flag functionality
2+ // RUN: %clang_cc1 -emit-llvm -fms-cbstring -fms-extensions %s -o - | FileCheck --check-prefixes=CBSTRING,CHECK %s
3+ // RUN: %clang_cc1 -emit-llvm -fms-extensions %s -o - | FileCheck --check-prefixes=NORMAL,CHECK %s
4+
5+ // Function declarations to avoid needing system headers
6+ int printf (const char * format , ...);
7+
8+ // Helper function declaration
9+ int uses_string (const char * );
10+
11+ // Global scope strings
12+ const char * global_str = "global_test" ;
13+ static const char * static_global_str = "static_global_test" ;
14+
15+ // String arrays
16+ const char * array [] = {"array_element1" , "array_element2" };
17+
18+ // Function in default text section
19+ void test_function () {
20+ const char * local_str = "local_test" ;
21+ static const char * static_local_str = "static_local_test" ;
22+
23+ // Concatenated strings
24+ const char * concat = "concat_" "string_test" ;
25+
26+ // Use strings to prevent optimization
27+ printf ("%s %s %s\n" , local_str , static_local_str , concat );
28+ }
29+
30+ // Function with custom code segment
31+ void __declspec(code_seg ("other_seg" )) in_other_seg (void )
32+ {
33+ uses_string ("test_in_other_seg" );
34+ }
35+
36+ int main () {
37+ const char * main_str = "main_function_test" ;
38+ static const char * static_main_str = "static_main_test" ;
39+
40+ // Use all strings to ensure they appear in IR
41+ printf ("%s %s %s %s %s\n" , global_str , static_global_str , array [0 ], array [1 ], main_str );
42+ printf ("%s\n" , static_main_str );
43+ test_function ();
44+ in_other_seg ();
45+
46+ return 0 ;
47+ }
48+
49+ // Check string constants in order they appear in IR:
50+ // Note: With -fms-cbstring, function strings get section attributes but remain comdats for proper deduplication
51+ // Note: Global strings keep default section behavior (like MSVC's CONST segment)
52+
53+ // Global strings - should keep default section behavior (not go to .text$s)
54+ // NORMAL: constant {{.*}} c"global_test\00"{{.*}}comdat
55+ // CBSTRING: constant {{.*}} c"global_test\00"
56+ // CBSTRING-NOT: section
57+ // CBSTRING-SAME: comdat
58+
59+ // Array elements (global scope) - should keep default section behavior
60+ // NORMAL: constant {{.*}} c"array_element1\00"{{.*}}comdat
61+ // CBSTRING: constant {{.*}} c"array_element1\00"
62+ // CBSTRING-NOT: section
63+ // CBSTRING-SAME: comdat
64+ // NORMAL: constant {{.*}} c"array_element2\00"{{.*}}comdat
65+ // CBSTRING: constant {{.*}} c"array_element2\00"
66+ // CBSTRING-NOT: section
67+ // CBSTRING-SAME: comdat
68+
69+ // Function local strings go to .text$s
70+ // CHECK: constant {{.*}} c"local_test\00"
71+ // CBSTRING-SAME: section ".text$s"
72+ // NORMAL-NOT: section
73+ // CHECK-SAME: comdat
74+
75+ // Static local strings keep default section behavior (like MSVC)
76+ // CHECK: constant {{.*}} c"static_local_test\00"
77+ // CBSTRING-NOT: section
78+ // NORMAL-NOT: section
79+ // CHECK-SAME: comdat
80+
81+ // Concatenated strings in functions go to .text$s
82+ // CHECK: constant {{.*}} c"concat_string_test\00"
83+ // CBSTRING-SAME: section ".text$s"
84+ // NORMAL-NOT: section
85+ // CHECK-SAME: comdat
86+
87+ // Printf format strings also go to .text$s when used in functions
88+ // CBSTRING: section ".text$s"{{.*}}comdat
89+
90+ // Strings in function with custom code segment go to other_seg$s
91+ // CHECK: constant {{.*}} c"test_in_other_seg\00"
92+ // CBSTRING-SAME: section "other_seg$s"
93+ // NORMAL-NOT: section
94+ // CHECK-SAME: comdat
95+
96+ // Main function strings go to .text$s
97+ // CHECK: constant {{.*}} c"main_function_test\00"
98+ // CBSTRING-SAME: section ".text$s"
99+ // NORMAL-NOT: section
100+ // CHECK-SAME: comdat
101+
102+ // Static main function strings keep default section behavior
103+ // NORMAL: constant {{.*}} c"static_main_test\00"{{.*}}comdat
104+ // CBSTRING: constant {{.*}} c"static_main_test\00"
105+ // CBSTRING-NOT: section
106+ // CBSTRING-SAME: comdat
107+
108+ // Static global strings keep default section behavior
109+ // NORMAL: constant {{.*}} c"static_global_test\00"{{.*}}comdat
110+ // CBSTRING: constant {{.*}} c"static_global_test\00"
111+ // CBSTRING-NOT: section
112+ // CBSTRING-SAME: comdat
113+
114+ // Check that functions are defined
115+ // CHECK: define dso_local void @test_function()
116+ // CHECK: define dso_local void @in_other_seg()
117+ // CHECK: define dso_local i32 @main()
0 commit comments