[PATCH] D45403: Make [[maybe_unused]] work with static data members

Arthur O'Dwyer via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 9 14:54:54 PDT 2018


Quuxplusone added inline comments.


================
Comment at: lib/Sema/SemaDeclAttr.cpp:2054
-  }
-
   // If this is spelled as the standard C++17 attribute, but not in C++17, warn
----------------
@aaron.ballman writes:
> I don't think we're currently diagnosing static data members of classes as being unused in the first place; are there plans to implement that functionality so that someone might want to write the attribute there?

FWIW, "plans to implement that functionality" (in Clang) is not the only reason to accept the attribute. Someone might write the attribute in their codebase that currently compiles with some //other// compiler which implements the functionality (or at least does not reject it); it would be mildly unfortunate if that made their codebase non-portable-to-Clang. (Only "mildly" because the diagnostic being removed here is just a warning; the user could suppress it if they really needed to.)

Here's an example of code that compiles cleanly with GCC but gives an arguably "false positive" diagnostic when compiled with Clang.
https://wandbox.org/permlink/UG4kG5XTBn12xfuu
Now, admittedly, both GCC and Clang produce a "false negative" re the unused private static member `y`; but that false negative might get fixed in the future. The user writes their code //today// and it must compile //today//. :)



================
Comment at: test/CXX/dcl.dcl/dcl.attr/dcl.attr.unused/p2.cpp:5
   int I [[maybe_unused]];
-  static int SI [[maybe_unused]]; // expected-warning {{'maybe_unused' attribute only applies to variables, functions, methods, types, enumerations, enumerators, labels, and non-static data members}}
+  static int SI [[maybe_unused]];
 };
----------------
aaron.ballman wrote:
> cpplearner wrote:
> > lebedev.ri wrote:
> > > As the code comment noted, http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf, page 199:
> > > ```
> > > 2 The attribute may be applied to the declaration of a class, a typedef-name, a variable, a **non-static** data
> > > member, a function, an enumeration, or an enumerator.
> > > ```
> > That section says that [[maybe_unused]] can be applied to a non-static data member, which doesn't mean it can't be applied to a static data member.
> > 
> > And I'm arguing that since a static data member is a variable, [dcl.attr.unused]p2 actually allows [[maybe_unused]] to be applied to a static data member.
> Yes -- through twisty standardese, a static data member of a class might be a variable. This test case, however, is only a declaration and not a definition (which means it's not an object, which means it's not a variable). Should the attribute still appertain in this case?
The attribute does currently apply to declarations as well as definitions, although you have to be a real language lawyer to observe it.
https://wandbox.org/permlink/WBLWBdd42rv95UaS


Repository:
  rC Clang

https://reviews.llvm.org/D45403





More information about the cfe-commits mailing list