[PATCH] D102663: Bug 49633 - Added warning for static inline global and namespaced declarations

Serberoth via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed May 26 21:52:18 PDT 2021


serberoth updated this revision to Diff 348156.
serberoth retitled this revision from "Bug 49633 - Added warning for static inline global and namespaced declarations for c++17+" to "Bug 49633 - Added warning for static inline global and namespaced declarations".
serberoth edited the summary of this revision.
serberoth added a comment.

Updates to testing to ensure warning occurs for variables declared in anonymous namespace.
Updates per rsmith to ensure that ext compat warning still occurs fixing regressive behaviour.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102663/new/

https://reviews.llvm.org/D102663

Files:
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaDecl.cpp
  clang/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.type.class.deduct/p1.cpp
  clang/test/Parser/cxx1z-decomposition.cpp
  clang/test/Sema/c17-global-static-inline.cpp


Index: clang/test/Sema/c17-global-static-inline.cpp
===================================================================
--- /dev/null
+++ clang/test/Sema/c17-global-static-inline.cpp
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -std=c++17 %s -verify
+
+static inline int should_warn; // expected-warning{{variable 'should_warn' declared as both inline and with static storage declaration}}
+
+namespace TestNamespace {
+  
+  static inline int NamespacedShouldWarn; // expected-warning{{variable 'NamespacedShouldWarn' declared as both inline and with static storage declaration}}
+
+};
+
+namespace {
+
+  static inline int AnonNamespaceShouldWarn; // expected-warning{{variable 'AnonNamespaceShouldWarn' declared as both inline and with static storage declaration}}
+
+};
+
+static constexpr inline const volatile float f = 0.0f; // expected-warning{{variable 'f' declared as both inline and with static storage declaration}}
+
+static inline int TestFunction(int s, int e, int n) {
+  int m = (n - s) / (e - s);
+  return m * m * m * (m * (m * 6 - 15) + 10);
+}
Index: clang/test/Parser/cxx1z-decomposition.cpp
===================================================================
--- clang/test/Parser/cxx1z-decomposition.cpp
+++ clang/test/Parser/cxx1z-decomposition.cpp
@@ -84,7 +84,7 @@
     constexpr auto &[i] = n; // expected-error {{cannot be declared 'constexpr'}}
   }
 
-  static constexpr inline thread_local auto &[j1] = n; // expected-error {{cannot be declared with 'constexpr inline' specifiers}}
+  static constexpr inline thread_local auto &[j1] = n; // expected-error {{cannot be declared with 'constexpr inline' specifiers}} expected-warning{{variable 'j1' declared as both inline and with static storage declaration}}
   static thread_local auto &[j2] = n; // expected-warning {{declared with 'static thread_local' specifiers is a C++20 extension}}
 
   inline auto &[k] = n; // expected-error {{cannot be declared 'inline'}}
Index: clang/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.type.class.deduct/p1.cpp
===================================================================
--- clang/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.type.class.deduct/p1.cpp
+++ clang/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.type.class.deduct/p1.cpp
@@ -4,7 +4,7 @@
 A() -> A<int>;
 A(int) -> A<char>;
 
-static constexpr inline const volatile A a = {}; // ok, specifiers are permitted
+static constexpr inline const volatile A a = {}; // expected-warning{{variable 'a' declared as both inline and with static storage declaration}}
 A b;
 A c [[]] {};
 
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -7125,6 +7125,11 @@
            diag::err_inline_declaration_block_scope) << Name
         << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
     } else {
+      // static inline declarations in the global or namespace scope should get
+      // a warning indicating they are contradictory in nature.
+      if (SC == SC_Static && DC->isFileContext())
+        Diag(D.getIdentifierLoc(), diag::warn_static_inline) << Name;
+      
       Diag(D.getDeclSpec().getInlineSpecLoc(),
            getLangOpts().CPlusPlus17 ? diag::warn_cxx14_compat_inline_variable
                                      : diag::ext_inline_variable);
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1466,6 +1466,10 @@
 def warn_cxx14_compat_inline_variable : Warning<
   "inline variables are incompatible with C++ standards before C++17">,
   DefaultIgnore, InGroup<CXXPre17Compat>;
+def warn_static_inline : Warning<
+  "variable %0 declared as both inline and with static storage declaration">,
+  InGroup<CXX17>;
+
 
 def warn_inline_namespace_reopened_noninline : Warning<
   "inline namespace reopened as a non-inline namespace">,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102663.348156.patch
Type: text/x-patch
Size: 4021 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210527/edfc4984/attachment.bin>


More information about the cfe-commits mailing list