2011-06-14 07:56:42 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -verify %s
|
2009-04-08 03:37:57 +08:00
|
|
|
|
2012-06-19 01:49:58 +08:00
|
|
|
#if defined(INCLUDE)
|
|
|
|
// -------
|
|
|
|
// This section acts like a header file.
|
|
|
|
// -------
|
2012-06-16 02:19:48 +08:00
|
|
|
|
|
|
|
// Check the use of static variables in non-static inline functions.
|
2012-06-19 01:49:58 +08:00
|
|
|
static int staticVar; // expected-note + {{'staticVar' declared here}}
|
|
|
|
static int staticFunction(); // expected-note + {{'staticFunction' declared here}}
|
2012-06-21 05:09:10 +08:00
|
|
|
static struct { int x; } staticStruct; // expected-note + {{'staticStruct' declared here}}
|
2012-06-16 02:19:48 +08:00
|
|
|
|
2012-06-21 05:09:10 +08:00
|
|
|
inline int useStatic () { // expected-note 3 {{use 'static' to give inline function 'useStatic' internal linkage}}
|
|
|
|
staticFunction(); // expected-warning{{static function 'staticFunction' is used in an inline function with external linkage}}
|
|
|
|
(void)staticStruct.x; // expected-warning{{static variable 'staticStruct' is used in an inline function with external linkage}}
|
|
|
|
return staticVar; // expected-warning{{static variable 'staticVar' is used in an inline function with external linkage}}
|
2012-06-16 02:19:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern inline int useStaticFromExtern () { // no suggestions
|
2012-06-21 05:09:10 +08:00
|
|
|
staticFunction(); // expected-warning{{static function 'staticFunction' is used in an inline function with external linkage}}
|
|
|
|
return staticVar; // expected-warning{{static variable 'staticVar' is used in an inline function with external linkage}}
|
2012-06-16 02:19:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int useStaticFromStatic () {
|
|
|
|
staticFunction(); // no-warning
|
|
|
|
return staticVar; // no-warning
|
|
|
|
}
|
2012-06-19 01:49:58 +08:00
|
|
|
|
2012-06-21 13:54:50 +08:00
|
|
|
extern inline int useStaticInlineFromExtern () {
|
|
|
|
// Heuristic: if the function we're using is also inline, don't warn.
|
|
|
|
// This can still be wrong (in this case, we end up inlining calls to
|
|
|
|
// staticFunction and staticVar) but this got very noisy even using
|
|
|
|
// standard headers.
|
|
|
|
return useStaticFromStatic(); // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
static int constFunction() __attribute__((const));
|
|
|
|
|
|
|
|
inline int useConst () {
|
|
|
|
return constFunction(); // no-warning
|
|
|
|
}
|
|
|
|
|
2012-06-19 01:49:58 +08:00
|
|
|
#else
|
|
|
|
// -------
|
|
|
|
// This is the main source file.
|
|
|
|
// -------
|
|
|
|
|
|
|
|
#define INCLUDE
|
|
|
|
#include "inline.c"
|
|
|
|
|
|
|
|
// Check that we don't allow illegal uses of inline
|
|
|
|
inline int a; // expected-error{{'inline' can only appear on functions}}
|
|
|
|
typedef inline int b; // expected-error{{'inline' can only appear on functions}}
|
|
|
|
int d(inline int a); // expected-error{{'inline' can only appear on functions}}
|
|
|
|
|
|
|
|
// Check that the warnings from the "header file" aren't on by default in
|
|
|
|
// the main source file.
|
|
|
|
|
2012-06-19 06:09:19 +08:00
|
|
|
inline int useStaticMainFile () {
|
2012-06-19 01:49:58 +08:00
|
|
|
staticFunction(); // no-warning
|
|
|
|
return staticVar; // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the warnings show up when explicitly requested.
|
|
|
|
|
|
|
|
#pragma clang diagnostic push
|
2012-06-21 05:09:10 +08:00
|
|
|
#pragma clang diagnostic warning "-Wstatic-in-inline"
|
2012-06-19 01:49:58 +08:00
|
|
|
|
|
|
|
inline int useStaticAgain () { // expected-note 2 {{use 'static' to give inline function 'useStaticAgain' internal linkage}}
|
2012-06-21 05:09:10 +08:00
|
|
|
staticFunction(); // expected-warning{{static function 'staticFunction' is used in an inline function with external linkage}}
|
|
|
|
return staticVar; // expected-warning{{static variable 'staticVar' is used in an inline function with external linkage}}
|
2012-06-19 01:49:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
|
2013-04-02 10:48:58 +08:00
|
|
|
inline void defineStaticVar() { // expected-note {{use 'static' to give inline function 'defineStaticVar' internal linkage}}
|
|
|
|
static const int x = 0; // ok
|
|
|
|
static int y = 0; // expected-warning {{non-constant static local variable in inline function may be different in different files}}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern inline void defineStaticVarInExtern() {
|
|
|
|
static const int x = 0; // ok
|
|
|
|
static int y = 0; // ok
|
|
|
|
}
|
|
|
|
|
2012-06-19 01:49:58 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|