102 lines · c
1// RUN: %clang_cc1 -triple %itanium_abi_triple -fsyntax-only -verify %s2 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(void); // expected-note + {{'staticFunction' declared here}}11static struct { int x; } staticStruct; // expected-note + {{'staticStruct' declared here}}12 13inline int useStatic (void) { // expected-note 3 {{use 'static' to give inline function 'useStatic' internal linkage}}14 staticFunction(); // expected-warning{{using static function 'staticFunction' in an inline function with external linkage is a C2y extension}}15 (void)staticStruct.x; // expected-warning{{using static variable 'staticStruct' in an inline function with external linkage is a C2y extension}}16 return staticVar; // expected-warning{{using static variable 'staticVar' in an inline function with external linkage is a C2y extension}}17}18 19extern inline int useStaticFromExtern (void) { // no suggestions20 staticFunction(); // expected-warning{{using static function 'staticFunction' in an inline function with external linkage is a C2y extension}}21 return staticVar; // expected-warning{{using static variable 'staticVar' in an inline function with external linkage is a C2y extension}}22}23 24static inline int useStaticFromStatic (void) {25 staticFunction(); // no-warning26 return staticVar; // no-warning27}28 29extern inline int useStaticInlineFromExtern (void) {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 to32 // staticFunction and staticVar) but this got very noisy even using33 // standard headers.34 return useStaticFromStatic(); // no-warning35}36 37static int constFunction(void) __attribute__((const));38 39inline int useConst (void) {40 return constFunction(); // no-warning41}42 43#else44// -------45// This is the main source file.46// -------47 48#define INCLUDE49#include "inline.c"50 51// Check that we don't allow illegal uses of inline52inline 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 in57// the main source file.58 59inline int useStaticMainFile (void) {60 staticFunction(); // no-warning61 return staticVar; // no-warning62}63 64// Check that the warnings show up when explicitly requested.65 66#pragma clang diagnostic push67#pragma clang diagnostic warning "-Wstatic-in-inline"68 69inline int useStaticAgain (void) { // expected-note 2 {{use 'static' to give inline function 'useStaticAgain' internal linkage}}70 staticFunction(); // expected-warning{{using static function 'staticFunction' in an inline function with external linkage is a C2y extension}}71 return staticVar; // expected-warning{{using static variable 'staticVar' in an inline function with external linkage is a C2y extension}}72}73 74#pragma clang diagnostic pop75 76inline void defineStaticVar(void) { // expected-note {{use 'static' to give inline function 'defineStaticVar' internal linkage}}77 static const int x = 0; // ok78 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(void) {82 static const int x = 0; // ok83 static int y = 0; // ok84}85 86// Check behavior of line markers.87# 1 "XXX.h" 188inline int useStaticMainFileInLineMarker(void) { // expected-note 2 {{use 'static' to give inline function 'useStaticMainFileInLineMarker' internal linkage}}89 staticFunction(); // expected-warning{{using static function 'staticFunction' in an inline function with external linkage is a C2y extension}}90 return staticVar; // expected-warning{{using static variable 'staticVar' in an inline function with external linkage is a C2y extension}}91}92# 100 "inline.c" 293 94inline int useStaticMainFileAfterLineMarker(void) {95 staticFunction(); // no-warning96 return staticVar; // no-warning97}98 99#endif100 101 102