Clang Project

clang_source_code/test/Sema/pragma-attribute-strict-subjects.c
1// RUN: %clang_cc1 -fsyntax-only -Wno-pragmas -verify %s
2
3#pragma clang attribute push (__attribute__((annotate("test"))), apply_to = any(function, variable))
4
5#pragma clang attribute pop
6
7// Check for contradictions in rules for attribute without a strict subject set:
8
9#pragma clang attribute push (__attribute__((annotate("subRuleContradictions"))), apply_to = any(variable, variable(is_parameter), function(is_member), variable(is_global)))
10// expected-error@-1 {{redundant attribute subject matcher sub-rule 'variable(is_parameter)'; 'variable' already matches those declarations}}
11// expected-error@-2 {{redundant attribute subject matcher sub-rule 'variable(is_global)'; 'variable' already matches those declarations}}
12
13#pragma clang attribute pop
14
15#pragma clang attribute push (__attribute__((annotate("subRuleContradictions2"))), apply_to = any(function(is_member), function))
16// expected-error@-1 {{redundant attribute subject matcher sub-rule 'function(is_member)'; 'function' already matches those declarations}}
17
18#pragma clang attribute pop
19
20#pragma clang attribute push (__attribute__((annotate("subRuleContradictions3"))), apply_to = any(variable, variable(unless(is_parameter))))
21// expected-error@-1 {{redundant attribute subject matcher sub-rule 'variable(unless(is_parameter))'; 'variable' already matches those declarations}}
22
23#pragma clang attribute pop
24
25#pragma clang attribute push (__attribute__((annotate("negatedSubRuleContradictions1"))), apply_to = any(variable(is_parameter), variable(unless(is_parameter))))
26// expected-error@-1 {{negated attribute subject matcher sub-rule 'variable(unless(is_parameter))' contradicts sub-rule 'variable(is_parameter)'}}
27
28#pragma clang attribute pop
29
30#pragma clang attribute push (__attribute__((annotate("negatedSubRuleContradictions2"))), apply_to = any(variable(unless(is_parameter)), variable(is_thread_local), function, variable(is_global)))
31// expected-error@-1 {{negated attribute subject matcher sub-rule 'variable(unless(is_parameter))' contradicts sub-rule 'variable(is_global)'}}
32// We have just one error, don't error on 'variable(is_global)'
33
34#pragma clang attribute pop
35
36// Verify the strict subject set verification.
37
38#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = any(function))
39// No error
40#pragma clang attribute pop
41
42#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = any(record(unless(is_union)), function, variable))
43// No error
44#pragma clang attribute pop
45
46#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = any(function, variable, record(unless(is_union))))
47// No error
48#pragma clang attribute pop
49
50#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = any(variable, record(unless(is_union)), function))
51// No error
52#pragma clang attribute pop
53
54#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = any(function, record(unless(is_union)), variable, enum))
55// expected-error@-1 {{attribute 'abi_tag' can't be applied to 'enum'}}
56#pragma clang attribute pop
57
58#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = any(enum_constant, function, record(unless(is_union)), variable, variable(is_parameter)))
59// expected-error@-1 {{attribute 'abi_tag' can't be applied to 'variable(is_parameter)', and 'enum_constant'}}
60#pragma clang attribute pop
61
62#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = any(function, record(unless(is_union)), enum))
63// expected-error@-1 {{attribute 'abi_tag' can't be applied to 'enum'}}
64#pragma clang attribute pop
65
66// Verify the non-strict subject set verification.
67
68#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = any(function))
69
70#pragma clang attribute pop
71
72#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = variable)
73
74#pragma clang attribute pop
75
76#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = any(record(unless(is_union))))
77
78#pragma clang attribute pop
79
80#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = any(function, variable))
81
82#pragma clang attribute pop
83
84#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = any(variable, record(unless(is_union))))
85
86#pragma clang attribute pop
87
88#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = any(record(unless(is_union)), function))
89
90#pragma clang attribute pop
91
92#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = any(record(unless(is_union)), function, variable))
93
94#pragma clang attribute pop
95
96
97#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = any(record(unless(is_union)), function, variable, enum, enum_constant))
98// expected-error@-1 {{attribute 'abi_tag' can't be applied to 'enum_constant', and 'enum'}}
99
100#pragma clang attribute pop
101
102#pragma clang attribute push (__attribute__((abi_tag("a"))), apply_to = enum)
103// expected-error@-1 {{attribute 'abi_tag' can't be applied to 'enum'}}
104
105#pragma clang attribute pop
106
107// Handle attributes whose subjects are supported only in other language modes:
108
109#pragma clang attribute push(__attribute__((abi_tag("b"))), apply_to = any(namespace, record(unless(is_union)), variable, function))
110// 'namespace' is accepted!
111#pragma clang attribute pop
112
113#pragma clang attribute push(__attribute__((abi_tag("b"))), apply_to = any(namespace))
114// 'namespace' is accepted!
115#pragma clang attribute pop
116
117#pragma clang attribute push(__attribute__((objc_subclassing_restricted)), apply_to = objc_interface)
118// No error!
119#pragma clang attribute pop
120
121#pragma clang attribute push(__attribute__((objc_subclassing_restricted)), apply_to = objc_interface)
122// No error!
123#pragma clang attribute pop
124
125#pragma clang attribute push(__attribute__((objc_subclassing_restricted)), apply_to = any(objc_interface, objc_protocol))
126// expected-error@-1 {{attribute 'objc_subclassing_restricted' can't be applied to 'objc_protocol'}}
127#pragma clang attribute pop
128
129#pragma clang attribute push(__attribute__((objc_subclassing_restricted)), apply_to = any(objc_protocol))
130// expected-error@-1 {{attribute 'objc_subclassing_restricted' can't be applied to 'objc_protocol'}}
131// Don't report an error about missing 'objc_interface' as we aren't parsing
132// Objective-C.
133#pragma clang attribute pop
134
135#pragma clang attribute push(__attribute__((objc_subclassing_restricted)), apply_to = any(objc_interface, objc_protocol))
136// expected-error@-1 {{attribute 'objc_subclassing_restricted' can't be applied to 'objc_protocol'}}
137#pragma clang attribute pop
138
139#pragma clang attribute push(__attribute__((objc_subclassing_restricted)), apply_to = any(objc_protocol))
140// expected-error@-1 {{attribute 'objc_subclassing_restricted' can't be applied to 'objc_protocol'}}
141// Don't report an error about missing 'objc_interface' as we aren't parsing
142// Objective-C.
143#pragma clang attribute pop
144
145// Use of matchers from other language modes should not cause for attributes
146// without subject list:
147#pragma clang attribute push (__attribute__((annotate("test"))), apply_to = objc_method)
148
149#pragma clang attribute pop
150
151#pragma clang attribute push (__attribute__((annotate("test"))), apply_to = any(objc_interface, objc_protocol))
152
153#pragma clang attribute pop
154