[cfe-commits] r158666 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExpr.cpp test/Sema/inline.c

Jordan Rose jordan_rose at apple.com
Mon Jun 18 10:49:58 PDT 2012


Author: jrose
Date: Mon Jun 18 12:49:58 2012
New Revision: 158666

URL: http://llvm.org/viewvc/llvm-project?rev=158666&view=rev
Log:
Allow internal decls in inline functions if the function is in the main file.

This handles the very common case of people writing inline functions in their
main source files and not tagging them as inline. These cases should still
behave as the user intended. (The diagnostic is still emitted as an extension.)

I'm reworking this code anyway to account for C++'s equivalent restriction in
[basic.def.odr]p6, but this should get some bots back to green.

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=158666&r1=158665&r2=158666&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Jun 18 12:49:58 2012
@@ -3000,6 +3000,10 @@
   "%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 ext_internal_in_extern_inline : Extension<
+  "%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<

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=158666&r1=158665&r2=158666&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Mon Jun 18 12:49:58 2012
@@ -194,7 +194,13 @@
     if (FunctionDecl *Current = getCurFunctionDecl()) {
       if (Current->isInlined() && Current->getLinkage() > InternalLinkage) {
         if (D->getLinkage() == InternalLinkage) {
-          Diag(Loc, diag::warn_internal_in_extern_inline)
+          // We won't warn by default if the inline function is in the main
+          // source file; in these cases it is almost certain that the inlining
+          // will only occur in this file, even if there is an external
+          // declaration as well.
+          bool IsFromMainFile = getSourceManager().isFromMainFile(Loc);
+          Diag(Loc, IsFromMainFile ? diag::ext_internal_in_extern_inline
+                                   : diag::warn_internal_in_extern_inline)
             << !isa<FunctionDecl>(D) << D << isa<CXXMethodDecl>(Current);
 
           // If the user didn't explicitly specify a storage class,

Modified: cfe/trunk/test/Sema/inline.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/inline.c?rev=158666&r1=158665&r2=158666&view=diff
==============================================================================
--- cfe/trunk/test/Sema/inline.c (original)
+++ cfe/trunk/test/Sema/inline.c Mon Jun 18 12:49:58 2012
@@ -1,14 +1,13 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s
 
-// 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}}
-
+#if defined(INCLUDE)
+// -------
+// This section acts like a header file.
+// -------
 
 // 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}}
+static int staticVar; // expected-note + {{'staticVar' declared here}}
+static int staticFunction(); // expected-note + {{'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}}
@@ -24,3 +23,40 @@
   staticFunction(); // no-warning
   return staticVar; // no-warning
 }
+
+#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.
+
+inline int useStaticMain () {
+  staticFunction(); // no-warning
+  return staticVar; // no-warning
+}
+
+// Check that the warnings show up when explicitly requested.
+
+#pragma clang diagnostic push
+#pragma clang diagnostic warning "-Winternal-linkage-in-inline"
+
+inline int useStaticAgain () { // expected-note 2 {{use 'static' to give inline function 'useStaticAgain' 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}}
+}
+
+#pragma clang diagnostic pop
+
+#endif
+
+





More information about the cfe-commits mailing list