xref: /llvm-project/clang/test/CodeGenCXX/code-seg.cpp (revision 7963e8bebb90543d997bf99ae5f8cf9be579d9ea)
1 // RUN: %clang_cc1 -emit-llvm -triple i686-pc-win32 -fms-extensions -verify -o - %s | FileCheck %s
2 // expected-no-diagnostics
3 
4 // Simple case
5 
bar_one()6 int __declspec(code_seg("foo_one")) bar_one() { return 1; }
7 //CHECK: define {{.*}}bar_one{{.*}} section "foo_one"
8 
9 // Simple case - explicit attribute used over pragma
10 #pragma code_seg("foo_two")
bar2()11 int __declspec(code_seg("foo_three")) bar2() { return 2; }
12 //CHECK: define {{.*}}bar2{{.*}} section "foo_three"
13 
14 // Check that attribute on one function doesn't affect another
another1()15 int another1() { return 1001; }
16 //CHECK: define {{.*}}another1{{.*}} section "foo_two"
17 
18 // Member functions
19 
20 struct __declspec(code_seg("foo_four")) Foo {
bar3Foo21   int bar3() {return 0;}
22   int bar4();
bar6Foo23   int __declspec(code_seg("foo_six")) bar6() { return 6; }
bar7Foo24   int bar7() { return 7; }
25   struct Inner {
bar5Foo::Inner26     int bar5() { return 5; }
27   } z;
baz1Foo28   virtual int baz1() { return 1; }
29 };
30 
31 struct __declspec(code_seg("foo_four")) FooTwo : Foo {
baz1FooTwo32   int baz1() { return 20; }
33 };
34 
caller1()35 int caller1() {
36   Foo f; return f.bar3();
37 }
38 
39 //CHECK: define {{.*}}bar3@Foo{{.*}} section "foo_four"
bar4()40 int Foo::bar4() { return 4; }
41 //CHECK: define {{.*}}bar4@Foo{{.*}} section "foo_four"
42 
43 #pragma code_seg("someother")
44 
caller2()45 int caller2() {
46   Foo f;
47   Foo *fp = new FooTwo;
48   return f.z.bar5() + f.bar6() + f.bar7() + fp->baz1();
49 }
50 // MS Compiler and Docs do not match for nested routines
51 // Doc says:      define {{.*}}bar5@Inner@Foo{{.*}} section "foo_four"
52 // Compiler says: define {{.*}}bar5@Inner@Foo{{.*}} section "foo_two"
53 // A bug has been reported: see https://reviews.llvm.org/D22931, the
54 // Microsoft feedback page is no longer available.
55 //CHECK: define {{.*}}bar5@Inner@Foo{{.*}} section "foo_two"
56 //CHECK: define {{.*}}bar6@Foo{{.*}} section "foo_six"
57 //CHECK: define {{.*}}bar7@Foo{{.*}} section "foo_four"
58 // Check that code_seg active at class declaration is not used on member
59 // declared outside class when it is not active.
60 
61 #pragma code_seg(push,"AnotherSeg")
62 
63 struct FooThree {
64   int bar8();
bar9FooThree65   int bar9() { return 9; }
66 };
67 
68 #pragma code_seg(pop)
69 
70 
bar8()71 int FooThree::bar8() {return 0;}
72 
caller3()73 int caller3()
74 {
75   FooThree f;
76   return f.bar8() + f.bar9();
77 }
78 
79 //CHECK: define {{.*}}bar8@FooThree{{.*}} section "someother"
80 //CHECK: define {{.*}}bar9@FooThree{{.*}} section "AnotherSeg"
81 
82 struct NonTrivialCopy {
83   NonTrivialCopy();
84   NonTrivialCopy(const NonTrivialCopy&);
85   ~NonTrivialCopy();
86 };
87 
88 // check the section for compiler-generated function with declspec.
89 
90 struct __declspec(code_seg("foo_seven")) FooFour {
FooFourFooFour91   FooFour() {}
bar10FooFour92   int __declspec(code_seg("foo_eight")) bar10(int t) { return t; }
93   NonTrivialCopy f;
94 };
95 
96 //CHECK: define {{.*}}0FooFour@@QAE@ABU0@@Z{{.*}} section "foo_seven"
97 // check the section for compiler-generated function with no declspec.
98 
99 struct FooFive {
FooFiveFooFive100   FooFive() {}
bar11FooFive101   int __declspec(code_seg("foo_nine")) bar11(int t) { return t; }
102   NonTrivialCopy f;
103 };
104 
105 //CHECK: define {{.*}}0FooFive@@QAE@ABU0@@Z{{.*}} section "someother"
106 
107 #pragma code_seg("YetAnother")
caller4()108 int caller4()
109 {
110   FooFour z1;
111   FooFour z2 = z1;
112   FooFive y1;
113   FooFive y2 = y1;
114  return z2.bar10(0) + y2.bar11(1);
115 }
116 
117 //CHECK: define {{.*}}bar10@FooFour{{.*}} section "foo_eight"
118 //CHECK: define {{.*}}bar11@FooFive{{.*}} section "foo_nine"
119 
120 struct FooSix {
121   #pragma code_seg("foo_ten")
bar12FooSix122   int bar12() { return 12; }
123   #pragma code_seg("foo_eleven")
bar13FooSix124   int bar13() { return 13; }
125 };
126 
bar14()127 int bar14() { return 14; }
128 //CHECK: define {{.*}}bar14{{.*}} section "foo_eleven"
129 
caller5()130 int caller5()
131 {
132   FooSix fsix;
133   return fsix.bar12() + fsix.bar13();
134 }
135 
136 //CHECK: define {{.*}}bar12@FooSix{{.*}} section "foo_ten"
137 //CHECK: define {{.*}}bar13@FooSix{{.*}} section "foo_eleven"
138 //CHECK: define {{.*}}baz1@FooTwo{{.*}} section "foo_four"
139 
140