[PATCH] D25937: [Sema] -Wunused-variable warning for variables with array types should behave similarly to variables with scalar types

Alex Lorenz via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 26 07:22:58 PDT 2016


arphaman updated this revision to Diff 75877.
arphaman added a comment.

The updated patch incorporates John's suggestions into code.


Repository:
  rL LLVM

https://reviews.llvm.org/D25937

Files:
  lib/Sema/SemaDecl.cpp
  test/SemaCXX/warn-everthing.cpp
  test/SemaCXX/warn-unused-variables.cpp


Index: test/SemaCXX/warn-unused-variables.cpp
===================================================================
--- test/SemaCXX/warn-unused-variables.cpp
+++ test/SemaCXX/warn-unused-variables.cpp
@@ -150,3 +150,51 @@
 }
 
 #include "Inputs/warn-unused-variables.h"
+
+namespace arrayRecords {
+
+int total = 0;
+
+class Adder {
+public:
+  Adder(int x); // out of line below
+  ~Adder() {}
+};
+
+Adder::Adder(int x) {
+  total += x;
+}
+
+struct Foo {
+  int x;
+  Foo(int x) : x(x) {}
+};
+
+struct S1 {
+  S1();
+};
+
+void foo(int size) {
+  S1 y; // no warning
+  S1 yarray[2]; // no warning
+  S1 dynArray[size]; // no warning
+
+  Adder scalerInFuncScope = 134; // no warning
+  Adder arrayInFuncScope[] = { 135, 136 };  // no warning
+
+  Foo fooScalar = 1; // expected-warning {{unused variable 'fooScalar'}}
+  Foo fooArray[] = {1,2}; // expected-warning {{unused variable 'fooArray'}}
+}
+
+template<int N>
+void bar() {
+  Adder scaler = 123; // no warning
+  Adder array[N] = {1,2}; // no warning
+}
+
+void test() {
+  foo(10);
+  bar<2>();
+}
+
+}
Index: test/SemaCXX/warn-everthing.cpp
===================================================================
--- test/SemaCXX/warn-everthing.cpp
+++ test/SemaCXX/warn-everthing.cpp
@@ -9,5 +9,5 @@
 };
 
 void testPR12271() { // expected-warning {{no previous prototype for function 'testPR12271'}}
-  PR12271 a[1][1]; // expected-warning {{unused variable 'a'}}
+  PR12271 a[1][1];
 }
Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp
+++ lib/Sema/SemaDecl.cpp
@@ -1522,7 +1522,7 @@
   if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
 
     // White-list anything with an __attribute__((unused)) type.
-    QualType Ty = VD->getType();
+    const auto *Ty = VD->getType().getTypePtr();
 
     // Only look at the outermost level of typedef.
     if (const TypedefType *TT = Ty->getAs<TypedefType>()) {
@@ -1535,6 +1535,10 @@
     if (Ty->isIncompleteType() || Ty->isDependentType())
       return false;
 
+    // Look at the element type to ensure that the warning behaviour is
+    // consistent for both scalars and arrays.
+    Ty = Ty->getBaseElementTypeUnsafe();
+
     if (const TagType *TT = Ty->getAs<TagType>()) {
       const TagDecl *Tag = TT->getDecl();
       if (Tag->hasAttr<UnusedAttr>())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25937.75877.patch
Type: text/x-patch
Size: 2344 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20161026/7524d142/attachment.bin>


More information about the cfe-commits mailing list