inline.c revision a7846852ba8b2b53664a674a41d116e0419d8768
1// RUN: %clang_cc1 -fsyntax-only -verify %s
2
3#if defined(INCLUDE)
4// -------
5// This section acts like a header file.
6// -------
7
8// Check the use of static variables in non-static inline functions.
9static int staticVar; // expected-note + {{'staticVar' declared here}}
10static int staticFunction(); // expected-note + {{'staticFunction' declared here}}
11static struct { int x; } staticStruct; // expected-note + {{'staticStruct' declared here}}
12
13inline int useStatic () { // expected-note 3 {{use 'static' to give inline function 'useStatic' internal linkage}}
14  staticFunction(); // expected-warning{{static function 'staticFunction' is used in an inline function with external linkage}}
15  (void)staticStruct.x; // expected-warning{{static variable 'staticStruct' is used in an inline function with external linkage}}
16  return staticVar; // expected-warning{{static variable 'staticVar' is used in an inline function with external linkage}}
17}
18
19extern inline int useStaticFromExtern () { // no suggestions
20  staticFunction(); // expected-warning{{static function 'staticFunction' is used in an inline function with external linkage}}
21  return staticVar; // expected-warning{{static variable 'staticVar' is used in an inline function with external linkage}}
22}
23
24static inline int useStaticFromStatic () {
25  staticFunction(); // no-warning
26  return staticVar; // no-warning
27}
28
29extern inline int useStaticInlineFromExtern () {
30  // Heuristic: if the function we're using is also inline, don't warn.
31  // This can still be wrong (in this case, we end up inlining calls to
32  // staticFunction and staticVar) but this got very noisy even using
33  // standard headers.
34  return useStaticFromStatic(); // no-warning
35}
36
37#if __has_attribute(const)
38static int constFunction() __attribute__((const));
39#endif
40
41inline int useConst () {
42  return constFunction(); // no-warning
43}
44
45#else
46// -------
47// This is the main source file.
48// -------
49
50#define INCLUDE
51#include "inline.c"
52
53// Check that we don't allow illegal uses of inline
54inline int a; // expected-error{{'inline' can only appear on functions}}
55typedef inline int b; // expected-error{{'inline' can only appear on functions}}
56int d(inline int a); // expected-error{{'inline' can only appear on functions}}
57
58// Check that the warnings from the "header file" aren't on by default in
59// the main source file.
60
61inline int useStaticMainFile () {
62  staticFunction(); // no-warning
63  return staticVar; // no-warning
64}
65
66// Check that the warnings show up when explicitly requested.
67
68#pragma clang diagnostic push
69#pragma clang diagnostic warning "-Wstatic-in-inline"
70
71inline int useStaticAgain () { // expected-note 2 {{use 'static' to give inline function 'useStaticAgain' internal linkage}}
72  staticFunction(); // expected-warning{{static function 'staticFunction' is used in an inline function with external linkage}}
73  return staticVar; // expected-warning{{static variable 'staticVar' is used in an inline function with external linkage}}
74}
75
76#pragma clang diagnostic pop
77
78#endif
79
80
81