Warn when a static variable is referenced in a non-static inline function.
This is explicitly forbidden in C99 6.7.4p3. This is /not/ forbidden in C++,
probably because by default file-scope const/constexpr variables have internal
linkage, while functions have external linkage. There's also the issue of
anonymous namespaces to consider. Nevertheless, there should probably be a
similar warning, since the semantics of inlining a function that references
a variable with internal linkage do not seem well-defined.
<rdar://problem/11577619>
llvm-svn: 158531
diff --git a/clang/test/Sema/inline.c b/clang/test/Sema/inline.c
index 3c99f24..37dba8c 100644
--- a/clang/test/Sema/inline.c
+++ b/clang/test/Sema/inline.c
@@ -4,3 +4,23 @@
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 the use of static variables in non-static inline functions.
+static int staticVar; // expected-note 2 {{'staticVar' declared here}}
+static int staticFunction(); // expected-note 2 {{'staticFunction' declared here}}
+
+inline int useStatic () { // expected-note 2 {{use 'static' to give inline function 'useStatic' internal linkage}}
+ staticFunction(); // expected-warning{{function 'staticFunction' has internal linkage but is used in an inline function with external linkage}}
+ return staticVar; // expected-warning{{variable 'staticVar' has internal linkage but is used in an inline function with external linkage}}
+}
+
+extern inline int useStaticFromExtern () { // no suggestions
+ staticFunction(); // expected-warning{{function 'staticFunction' has internal linkage but is used in an inline function with external linkage}}
+ return staticVar; // expected-warning{{variable 'staticVar' has internal linkage but is used in an inline function with external linkage}}
+}
+
+static inline int useStaticFromStatic () {
+ staticFunction(); // no-warning
+ return staticVar; // no-warning
+}