arphaman created this revision.
arphaman added reviewers: rjmccall, thakis.
arphaman added a subscriber: cfe-commits.
arphaman set the repository for this revision to rL LLVM.

This patch makes the `-Wunused-variable` warning behaviour more consistent: Now 
clang won't warn on variables with constant array types where it doesn't warn 
on variables with scalar types.


Repository:
  rL LLVM

https://reviews.llvm.org/D25937

Files:
  lib/Sema/SemaDecl.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,50 @@
 }
 
 #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() {
+  S1 y; // no warning
+  S1 yarray[2]; // 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();
+  bar<2>();
+}
+
+}
Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp
+++ lib/Sema/SemaDecl.cpp
@@ -1535,6 +1535,11 @@
     if (Ty->isIncompleteType() || Ty->isDependentType())
       return false;
 
+    // Look at the element type when the type is a constant array to ensure
+    // that the warning behaviour is consistent for both scalars and arrays.
+    if (Ty->isConstantArrayType())
+      Ty = Ty->getAsArrayTypeUnsafe()->getElementType();
+
     if (const TagType *TT = Ty->getAs<TagType>()) {
       const TagDecl *Tag = TT->getDecl();
       if (Tag->hasAttr<UnusedAttr>())


Index: test/SemaCXX/warn-unused-variables.cpp
===================================================================
--- test/SemaCXX/warn-unused-variables.cpp
+++ test/SemaCXX/warn-unused-variables.cpp
@@ -150,3 +150,50 @@
 }
 
 #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() {
+  S1 y; // no warning
+  S1 yarray[2]; // 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();
+  bar<2>();
+}
+
+}
Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp
+++ lib/Sema/SemaDecl.cpp
@@ -1535,6 +1535,11 @@
     if (Ty->isIncompleteType() || Ty->isDependentType())
       return false;
 
+    // Look at the element type when the type is a constant array to ensure
+    // that the warning behaviour is consistent for both scalars and arrays.
+    if (Ty->isConstantArrayType())
+      Ty = Ty->getAsArrayTypeUnsafe()->getElementType();
+
     if (const TagType *TT = Ty->getAs<TagType>()) {
       const TagDecl *Tag = TT->getDecl();
       if (Tag->hasAttr<UnusedAttr>())
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to