From: Alex Lorenz Date: Thu, 27 Oct 2016 13:30:51 +0000 (+0000) Subject: [Sema] -Wunused-variable warning for array variables should behave X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=39bf9749a5f06fd161878212c9718b6bf7190165;p=clang [Sema] -Wunused-variable warning for array variables should behave similarly to scalar variables. This commit makes the -Wunused-variable warning behaviour more consistent: Now clang won't warn for array variables where it doesn't warn for scalar variables. rdar://24158862 Differential Revision: https://reviews.llvm.org/D25937 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@285289 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index c8973ae100..a94ed53294 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -1522,7 +1522,7 @@ static bool ShouldDiagnoseUnusedDecl(const NamedDecl *D) { if (const VarDecl *VD = dyn_cast(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()) { @@ -1535,6 +1535,10 @@ static bool ShouldDiagnoseUnusedDecl(const NamedDecl *D) { 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()) { const TagDecl *Tag = TT->getDecl(); if (Tag->hasAttr()) diff --git a/test/SemaCXX/warn-everthing.cpp b/test/SemaCXX/warn-everthing.cpp index ad3dd8a24d..ff66c78cdf 100644 --- a/test/SemaCXX/warn-everthing.cpp +++ b/test/SemaCXX/warn-everthing.cpp @@ -9,5 +9,5 @@ public: }; void testPR12271() { // expected-warning {{no previous prototype for function 'testPR12271'}} - PR12271 a[1][1]; // expected-warning {{unused variable 'a'}} + PR12271 a[1][1]; } diff --git a/test/SemaCXX/warn-unused-variables.cpp b/test/SemaCXX/warn-unused-variables.cpp index 8dcbe7271d..e40cf1a2c9 100644 --- a/test/SemaCXX/warn-unused-variables.cpp +++ b/test/SemaCXX/warn-unused-variables.cpp @@ -150,3 +150,54 @@ namespace ctor_with_cleanups { } #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 + S1 nestedArray[1][2][3]; // no warning + + Adder scalerInFuncScope = 134; // no warning + Adder arrayInFuncScope[] = { 135, 136 }; // no warning + Adder nestedArrayInFuncScope[2][2] = { {1,2}, {3,4} }; // no warning + + Foo fooScalar = 1; // expected-warning {{unused variable 'fooScalar'}} + Foo fooArray[] = {1,2}; // expected-warning {{unused variable 'fooArray'}} + Foo fooNested[2][2] = { {1,2}, {3,4} }; // expected-warning {{unused variable 'fooNested'}} +} + +template +void bar() { + Adder scaler = 123; // no warning + Adder array[N] = {1,2}; // no warning +} + +void test() { + foo(10); + bar<2>(); +} + +}