[cfe-commits] r158531 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExpr.cpp test/Sema/inline.c
Jordan Rose
jordan_rose at apple.com
Fri Jun 15 11:19:48 PDT 2012
Author: jrose
Date: Fri Jun 15 13:19:48 2012
New Revision: 158531
URL: http://llvm.org/viewvc/llvm-project?rev=158531&view=rev
Log:
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>
Modified:
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/lib/Sema/SemaExpr.cpp
cfe/trunk/test/Sema/inline.c
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=158531&r1=158530&r2=158531&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri Jun 15 13:19:48 2012
@@ -2998,6 +2998,15 @@
DiagGroup<"undefined-internal">;
def note_used_here : Note<"used here">;
+def warn_internal_in_extern_inline : ExtWarn<
+ "%select{function|variable}0 %1 has internal linkage but is used in an "
+ "inline %select{function|method}2 with external linkage">,
+ InGroup<DiagGroup<"internal-linkage-in-inline"> >;
+def note_internal_decl_declared_here : Note<
+ "%0 declared here">;
+def note_convert_inline_to_static : Note<
+ "use 'static' to give inline function %0 internal linkage">;
+
def warn_redefinition_of_typedef : ExtWarn<
"redefinition of typedef %0 is a C11 feature">,
InGroup<DiagGroup<"typedef-redefinition"> >;
Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=158531&r1=158530&r2=158531&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Fri Jun 15 13:19:48 2012
@@ -182,6 +182,38 @@
// Warn if this is used but marked unused.
if (D->hasAttr<UnusedAttr>())
Diag(Loc, diag::warn_used_but_marked_unused) << D->getDeclName();
+
+ // Warn if we're in an extern inline function referring to a decl
+ // with internal linkage. (C99 6.7.4p3)
+ // FIXME: This is not explicitly forbidden in C++, but it's not clear
+ // what the correct behavior is. We should probably still have a warning.
+ // (However, in C++ const variables have internal linkage by default, while
+ // functions still have external linkage by default, so this warning becomes
+ // very noisy.)
+ if (!getLangOpts().CPlusPlus) {
+ if (FunctionDecl *Current = getCurFunctionDecl()) {
+ if (Current->isInlined() && Current->getLinkage() > InternalLinkage) {
+ if (D->getLinkage() == InternalLinkage) {
+ Diag(Loc, diag::warn_internal_in_extern_inline)
+ << !isa<FunctionDecl>(D) << D << isa<CXXMethodDecl>(Current);
+
+ // If the user didn't explicitly specify a storage class,
+ // suggest adding "static" to fix the problem.
+ const FunctionDecl *FirstDecl = Current->getCanonicalDecl();
+ if (FirstDecl->getStorageClassAsWritten() == SC_None) {
+ SourceLocation DeclBegin = FirstDecl->getSourceRange().getBegin();
+ Diag(DeclBegin, diag::note_convert_inline_to_static)
+ << Current << FixItHint::CreateInsertion(DeclBegin, "static ");
+ }
+
+ Diag(D->getCanonicalDecl()->getLocation(),
+ diag::note_internal_decl_declared_here)
+ << D;
+ }
+ }
+ }
+ }
+
return false;
}
Modified: cfe/trunk/test/Sema/inline.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/inline.c?rev=158531&r1=158530&r2=158531&view=diff
==============================================================================
--- cfe/trunk/test/Sema/inline.c (original)
+++ cfe/trunk/test/Sema/inline.c Fri Jun 15 13:19:48 2012
@@ -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
+}
More information about the cfe-commits
mailing list