1 // RUN: %clang_cc1 -fsyntax-only -verify -fms-extensions %s -triple x86_64-pc-win32
2 
3 struct __declspec(code_seg("my_one")) FooOne {
4   int barC();
5 };
6 
7 struct FooTwo {
8   int __declspec(code_seg("my_three")) barD();
9   int barE();
10 };
barC()11 int __declspec(code_seg("my_four")) FooOne::barC() { return 10; }
12 // expected-warning@-1 {{codeseg does not match previous declaration}}
13 // expected-note@3{{previous attribute is here}}
barD()14 int __declspec(code_seg("my_five")) FooTwo::barD() { return 20; }
15 // expected-warning@-1 {{codeseg does not match previous declaration}}
16 // expected-note@8 {{previous attribute is here}}
barE()17 int __declspec(code_seg("my_six")) FooTwo::barE() { return 30; }
18 // expected-warning@-1 {{codeseg does not match previous declaration}}
19 // expected-note@9 {{previous declaration is here}}
20 
21 // Microsoft docs say:
22 // If a base-class has a code_seg attribute, derived classes must have the
23 // same attribute.
24 struct __declspec(code_seg("my_base")) Base1 {};
25 struct Base2 {};
26 
27 struct D1 : Base1 {};
28 //expected-error@-1 {{derived class must specify the same code segment as its base classes}}
29 // expected-note@24 {{base class 'Base1' specified here}}
30 struct __declspec(code_seg("my_derived")) D2 : Base1 {};
31 // expected-error@-1 {{derived class must specify the same code segment as its base classes}}
32 // expected-note@24 {{base class 'Base1' specified here}}
33 struct __declspec(code_seg("my_derived")) D3 : Base2 {};
34 // expected-error@-1 {{derived class must specify the same code segment as its base classes}}
35 // expected-note@25 {{base class 'Base2' specified here}}
36 
37 template <typename T> struct __declspec(code_seg("my_base")) MB : T { };
38 template <typename T> struct __declspec(code_seg("my_derived")) MD : T { };
39 MB<Base1> mb1; // ok
40 MB<Base2> mb2;
41 // expected-error@37 {{derived class must specify the same code segment as its base classes}}
42 // expected-note@-2 {{in instantiation of template class}}
43 // expected-note@25  {{base class 'Base2' specified here}}
44 MD<Base1> md1;
45 // expected-error@38 {{derived class must specify the same code segment as its base classes}}
46 // expected-note@-2 {{in instantiation of template class}}
47 // expected-note@24 {{base class 'Base1' specified here}}
48 MD<Base2> md2;
49 // expected-error@38 {{derived class must specify the same code segment as its base classes}}
50 // expected-note@-2 {{in instantiation of template class}}
51 // expected-note@25 {{base class 'Base2' specified here}}
52 
53 // Virtual overrides must have the same code_seg.
54 struct __declspec(code_seg("my_one")) Base3 {
barABase355   virtual int barA() { return 1; }
barBBase356   virtual int __declspec(code_seg("my_two")) barB() { return 2; }
57 };
58 struct __declspec(code_seg("my_one")) Derived3 : Base3 {
barADerived359   int barA() { return 4; } // ok
barBDerived360   int barB() { return 6; }
61   // expected-error@-1 {{overriding virtual function must specify the same code segment as its overridden function}}
62   // expected-note@56 {{previous declaration is here}}
63 };
64 
65 struct Base4 {
barABase466   virtual int __declspec(code_seg("my_one")) barA() {return 1;}
barBBase467   virtual int barB() { return 2;}
68 };
69 struct Derived4 : Base4 {
barADerived470   virtual int barA() {return 1;}
71   // expected-error@-1 {{overriding virtual function must specify the same code segment as its overridden function}}
72   // expected-note@66 {{previous declaration is here}}
barBDerived473   virtual int __declspec(code_seg("my_two")) barB() {return 1;}
74   // expected-error@-1 {{overriding virtual function must specify the same code segment as its overridden function}}
75   // expected-note@67 {{previous declaration is here}}
76 };
77 
78 // MS gives an error when different code segments are used but a warning when a duplicate is used
79 
80 // Function
bar1()81 int __declspec(code_seg("foo")) __declspec(code_seg("foo")) bar1() { return 1; }
82 // expected-warning@-1 {{duplicate code segment specifiers}}
bar2()83 int __declspec(code_seg("foo")) __declspec(code_seg("bar")) bar2() { return 1; }
84 // expected-error@-1 {{conflicting code segment specifiers}}
85 
86 // Class
87 struct __declspec(code_seg("foo")) __declspec(code_seg("foo")) Foo {
88   // expected-warning@-1 {{duplicate code segment specifiers}}
bar3Foo89   int bar3() {return 0;}
90 };
91 struct __declspec(code_seg("foo")) __declspec(code_seg("bar")) FooSix {
92   // expected-error@-1 {{conflicting code segment specifiers}}
bar3FooSix93   int bar3() {return 0;}
94 };
95 
96 //Class Members
97 struct FooThree {
bar1FooThree98   int __declspec(code_seg("foo")) __declspec(code_seg("foo")) bar1() { return 1; }
99   // expected-warning@-1 {{duplicate code segment specifiers}}
bar2FooThree100   int __declspec(code_seg("foo")) __declspec(code_seg("bar")) bar2() { return 1; }
101   // expected-error@-1 {{conflicting code segment specifiers}}
102   int bar8();
bar9FooThree103   int bar9() { return 9; }
104 };
105 
106