blob: eced058f8ddc1aab6da616b381a71d108d61213f [file] [log] [blame]
David Majnemer475f9ea2015-10-08 08:28:09 +00001// RUN: %clang_cc1 -triple %itanium_abi_triple -fsyntax-only -verify %s
Eli Friedman574c7452009-04-07 19:37:57 +00002
Jordan Roseedff0202012-06-18 17:49:58 +00003#if defined(INCLUDE)
4// -------
5// This section acts like a header file.
6// -------
Jordan Rose2684c682012-06-15 18:19:48 +00007
8// Check the use of static variables in non-static inline functions.
Jordan Roseedff0202012-06-18 17:49:58 +00009static int staticVar; // expected-note + {{'staticVar' declared here}}
10static int staticFunction(); // expected-note + {{'staticFunction' declared here}}
Jordan Rosedc753b62012-06-20 21:09:10 +000011static struct { int x; } staticStruct; // expected-note + {{'staticStruct' declared here}}
Jordan Rose2684c682012-06-15 18:19:48 +000012
Jordan Rosedc753b62012-06-20 21:09:10 +000013inline 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}}
Jordan Rose2684c682012-06-15 18:19:48 +000017}
18
19extern inline int useStaticFromExtern () { // no suggestions
Jordan Rosedc753b62012-06-20 21:09:10 +000020 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}}
Jordan Rose2684c682012-06-15 18:19:48 +000022}
23
24static inline int useStaticFromStatic () {
25 staticFunction(); // no-warning
26 return staticVar; // no-warning
27}
Jordan Roseedff0202012-06-18 17:49:58 +000028
Jordan Rose815fe262012-06-21 05:54:50 +000029extern 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
37static int constFunction() __attribute__((const));
38
39inline int useConst () {
40 return constFunction(); // no-warning
41}
42
Jordan Roseedff0202012-06-18 17:49:58 +000043#else
44// -------
45// This is the main source file.
46// -------
47
48#define INCLUDE
49#include "inline.c"
50
51// Check that we don't allow illegal uses of inline
52inline int a; // expected-error{{'inline' can only appear on functions}}
53typedef inline int b; // expected-error{{'inline' can only appear on functions}}
54int d(inline int a); // expected-error{{'inline' can only appear on functions}}
55
56// Check that the warnings from the "header file" aren't on by default in
57// the main source file.
58
Jordan Rose28cd12f2012-06-18 22:09:19 +000059inline int useStaticMainFile () {
Jordan Roseedff0202012-06-18 17:49:58 +000060 staticFunction(); // no-warning
61 return staticVar; // no-warning
62}
63
64// Check that the warnings show up when explicitly requested.
65
66#pragma clang diagnostic push
Jordan Rosedc753b62012-06-20 21:09:10 +000067#pragma clang diagnostic warning "-Wstatic-in-inline"
Jordan Roseedff0202012-06-18 17:49:58 +000068
69inline int useStaticAgain () { // expected-note 2 {{use 'static' to give inline function 'useStaticAgain' internal linkage}}
Jordan Rosedc753b62012-06-20 21:09:10 +000070 staticFunction(); // expected-warning{{static function 'staticFunction' is used in an inline function with external linkage}}
71 return staticVar; // expected-warning{{static variable 'staticVar' is used in an inline function with external linkage}}
Jordan Roseedff0202012-06-18 17:49:58 +000072}
73
74#pragma clang diagnostic pop
75
John McCallc87d9722013-04-02 02:48:58 +000076inline void defineStaticVar() { // expected-note {{use 'static' to give inline function 'defineStaticVar' internal linkage}}
77 static const int x = 0; // ok
78 static int y = 0; // expected-warning {{non-constant static local variable in inline function may be different in different files}}
79}
80
81extern inline void defineStaticVarInExtern() {
82 static const int x = 0; // ok
83 static int y = 0; // ok
84}
85
Eli Friedman5ba37d52013-08-22 00:27:10 +000086// Check behavior of line markers.
87# 1 "XXX.h" 1
88inline int useStaticMainFileInLineMarker() { // expected-note 2 {{use 'static' to give inline function 'useStaticMainFileInLineMarker' internal linkage}}
89 staticFunction(); // expected-warning{{static function 'staticFunction' is used in an inline function with external linkage}}
90 return staticVar; // expected-warning{{static variable 'staticVar' is used in an inline function with external linkage}}
91}
92# 100 "inline.c" 2
93
94inline int useStaticMainFileAfterLineMarker() {
95 staticFunction(); // no-warning
96 return staticVar; // no-warning
97}
98
Jordan Roseedff0202012-06-18 17:49:58 +000099#endif
100
101