summaryrefslogtreecommitdiffstats
path: root/test/SemaCXX/code-seg.cpp
blob: c4ce6613ba9d3caf7cf92a277dee1f6c9312a6d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// RUN: %clang_cc1 -fsyntax-only -verify -fms-extensions %s -triple x86_64-pc-win32

struct __declspec(code_seg("my_one")) FooOne {
  int barC();
};

struct FooTwo {
  int __declspec(code_seg("my_three")) barD();
  int barE();
};
int __declspec(code_seg("my_four")) FooOne::barC() { return 10; }
// expected-warning@-1 {{codeseg does not match previous declaration}}
// expected-note@3{{previous attribute is here}}
int __declspec(code_seg("my_five")) FooTwo::barD() { return 20; }
// expected-warning@-1 {{codeseg does not match previous declaration}}
// expected-note@8 {{previous attribute is here}}
int __declspec(code_seg("my_six")) FooTwo::barE() { return 30; }
// expected-warning@-1 {{codeseg does not match previous declaration}}
// expected-note@9 {{previous declaration is here}}

// Microsoft docs say:
// If a base-class has a code_seg attribute, derived classes must have the
// same attribute.
struct __declspec(code_seg("my_base")) Base1 {};
struct Base2 {};

struct D1 : Base1 {};
//expected-error@-1 {{derived class must specify the same code segment as its base classes}}
// expected-note@24 {{base class 'Base1' specified here}}
struct __declspec(code_seg("my_derived")) D2 : Base1 {};
// expected-error@-1 {{derived class must specify the same code segment as its base classes}}
// expected-note@24 {{base class 'Base1' specified here}}
struct __declspec(code_seg("my_derived")) D3 : Base2 {};
// expected-error@-1 {{derived class must specify the same code segment as its base classes}}
// expected-note@25 {{base class 'Base2' specified here}}

template <typename T> struct __declspec(code_seg("my_base")) MB : T { };
template <typename T> struct __declspec(code_seg("my_derived")) MD : T { };
MB<Base1> mb1; // ok
MB<Base2> mb2;
// expected-error@37 {{derived class must specify the same code segment as its base classes}}
// expected-note@-2 {{in instantiation of template class}}
// expected-note@25  {{base class 'Base2' specified here}}
MD<Base1> md1;
// expected-error@38 {{derived class must specify the same code segment as its base classes}}
// expected-note@-2 {{in instantiation of template class}}
// expected-note@24 {{base class 'Base1' specified here}}
MD<Base2> md2;
// expected-error@38 {{derived class must specify the same code segment as its base classes}}
// expected-note@-2 {{in instantiation of template class}}
// expected-note@25 {{base class 'Base2' specified here}}

// Virtual overrides must have the same code_seg.
struct __declspec(code_seg("my_one")) Base3 {
  virtual int barA() { return 1; }
  virtual int __declspec(code_seg("my_two")) barB() { return 2; }
};
struct __declspec(code_seg("my_one")) Derived3 : Base3 {
  int barA() { return 4; } // ok
  int barB() { return 6; }
  // expected-error@-1 {{overriding virtual function must specify the same code segment as its overridden function}}
  // expected-note@56 {{previous declaration is here}}
};

struct Base4 {
  virtual int __declspec(code_seg("my_one")) barA() {return 1;}
  virtual int barB() { return 2;}
};
struct Derived4 : Base4 {
  virtual int barA() {return 1;}
  // expected-error@-1 {{overriding virtual function must specify the same code segment as its overridden function}}
  // expected-note@66 {{previous declaration is here}}
  virtual int __declspec(code_seg("my_two")) barB() {return 1;}
  // expected-error@-1 {{overriding virtual function must specify the same code segment as its overridden function}}
  // expected-note@67 {{previous declaration is here}}
};

// MS gives an error when different code segments are used but a warning when a duplicate is used

// Function
int __declspec(code_seg("foo")) __declspec(code_seg("foo")) bar1() { return 1; }
// expected-warning@-1 {{duplicate code segment specifiers}}
int __declspec(code_seg("foo")) __declspec(code_seg("bar")) bar2() { return 1; }
// expected-error@-1 {{conflicting code segment specifiers}}

// Class
struct __declspec(code_seg("foo")) __declspec(code_seg("foo")) Foo {
  // expected-warning@-1 {{duplicate code segment specifiers}}
  int bar3() {return 0;}
};
struct __declspec(code_seg("foo")) __declspec(code_seg("bar")) FooSix {
  // expected-error@-1 {{conflicting code segment specifiers}}
  int bar3() {return 0;}
};

//Class Members
struct FooThree {
  int __declspec(code_seg("foo")) __declspec(code_seg("foo")) bar1() { return 1; }
  // expected-warning@-1 {{duplicate code segment specifiers}}
  int __declspec(code_seg("foo")) __declspec(code_seg("bar")) bar2() { return 1; }
  // expected-error@-1 {{conflicting code segment specifiers}}
  int bar8();
  int bar9() { return 9; }
};