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

Serberoth via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon May 17 20:12:43 PDT 2021


serberoth created this revision.
serberoth added reviewers: erik.pilkington, rsmith, TH3CHARLie, fixing bugs in llvm.
serberoth requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Bug 49633 - Added a warning for global and namespace declared variables that are declared as both static and inline when using C++17 and above.


Repository:
  rG LLVM Github Monorepo

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,16 @@
+// 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}}
+
+};
+
+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
@@ -7124,6 +7124,11 @@
       Diag(D.getDeclSpec().getInlineSpecLoc(),
            diag::err_inline_declaration_block_scope) << Name
         << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
+    } else if (SC == SC_Static && DC->isFileContext() &&
+               getLangOpts().CPlusPlus17) {
+      // static inline declarations in the global or namespace scope should get
+      // a warning indicating they are contradictory in nature.
+      Diag(D.getIdentifierLoc(), diag::warn_cxx17_static_inline) << Name;
     } else {
       Diag(D.getDeclSpec().getInlineSpecLoc(),
            getLangOpts().CPlusPlus17 ? diag::warn_cxx14_compat_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_cxx17_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.346035.patch
Type: text/x-patch
Size: 3864 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210518/7b2bc322/attachment.bin>


More information about the cfe-commits mailing list