2015-10-08 08:28:09 +00:00
// RUN: %clang_cc1 -triple %itanium_abi_triple -fsyntax-only -verify %s
2009-04-07 19:37:57 +00:00
2012-06-18 17:49:58 +00:00
# if defined(INCLUDE)
// -------
// This section acts like a header file.
// -------
2012-06-15 18:19:48 +00:00
// Check the use of static variables in non-static inline functions.
2012-06-18 17:49:58 +00:00
static int staticVar ; // expected-note + {{'staticVar' declared here}}
2022-02-04 15:19:56 -05:00
static int staticFunction ( void ) ; // expected-note + {{'staticFunction' declared here}}
2012-06-20 21:09:10 +00:00
static struct { int x ; } staticStruct ; // expected-note + {{'staticStruct' declared here}}
2012-06-15 18:19:48 +00:00
2022-02-04 15:19:56 -05:00
inline int useStatic ( void ) { // expected-note 3 {{use 'static' to give inline function 'useStatic' internal linkage}}
2012-06-20 21:09:10 +00:00
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-15 18:19:48 +00:00
}
2022-02-04 15:19:56 -05:00
extern inline int useStaticFromExtern ( void ) { // no suggestions
2012-06-20 21:09:10 +00: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-15 18:19:48 +00:00
}
2022-02-04 15:19:56 -05:00
static inline int useStaticFromStatic ( void ) {
2012-06-15 18:19:48 +00:00
staticFunction ( ) ; // no-warning
return staticVar ; // no-warning
}
2012-06-18 17:49:58 +00:00
2022-02-04 15:19:56 -05:00
extern inline int useStaticInlineFromExtern ( void ) {
2012-06-21 05:54:50 +00:00
// 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
}
2022-02-04 15:19:56 -05:00
static int constFunction ( void ) __attribute__ ( ( const ) ) ;
2012-06-21 05:54:50 +00:00
2022-02-04 15:19:56 -05:00
inline int useConst ( void ) {
2012-06-21 05:54:50 +00:00
return constFunction ( ) ; // no-warning
}
2012-06-18 17:49:58 +00:00
# else
// -------
// This is the main source file.
// -------
# define INCLUDE
# include "inline.c"
// Check that we don't allow illegal uses of inline
2016-07-14 22:22:58 +00:00
inline int a ; // expected-error{{'inline' can only appear on functions}}
2012-06-18 17:49:58 +00:00
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.
2022-02-04 15:19:56 -05:00
inline int useStaticMainFile ( void ) {
2012-06-18 17:49:58 +00:00
staticFunction ( ) ; // no-warning
return staticVar ; // no-warning
}
// Check that the warnings show up when explicitly requested.
# pragma clang diagnostic push
2012-06-20 21:09:10 +00:00
# pragma clang diagnostic warning "-Wstatic-in-inline"
2012-06-18 17:49:58 +00:00
2022-02-04 15:19:56 -05:00
inline int useStaticAgain ( void ) { // expected-note 2 {{use 'static' to give inline function 'useStaticAgain' internal linkage}}
2012-06-20 21:09:10 +00: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-18 17:49:58 +00:00
}
# pragma clang diagnostic pop
2022-02-04 15:19:56 -05:00
inline void defineStaticVar ( void ) { // expected-note {{use 'static' to give inline function 'defineStaticVar' internal linkage}}
2013-04-02 02:48:58 +00:00
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}}
}
2022-02-04 15:19:56 -05:00
extern inline void defineStaticVarInExtern ( void ) {
2013-04-02 02:48:58 +00:00
static const int x = 0 ; // ok
static int y = 0 ; // ok
}
2013-08-22 00:27:10 +00:00
// Check behavior of line markers.
# 1 "XXX.h" 1
2022-02-04 15:19:56 -05:00
inline int useStaticMainFileInLineMarker ( void ) { // expected-note 2 {{use 'static' to give inline function 'useStaticMainFileInLineMarker' internal linkage}}
2013-08-22 00:27:10 +00: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}}
}
# 100 "inline.c" 2
2022-02-04 15:19:56 -05:00
inline int useStaticMainFileAfterLineMarker ( void ) {
2013-08-22 00:27:10 +00:00
staticFunction ( ) ; // no-warning
return staticVar ; // no-warning
}
2012-06-18 17:49:58 +00:00
# endif