From: Rafael Espindola Date: Thu, 3 Jan 2013 04:29:20 +0000 (+0000) Subject: Warn on unused auto variables. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6769ccb6853871c085c8366ab5e993841ad56d00;p=clang Warn on unused auto variables. To do so we have to wait until we know that the type of a variable has been deduced. Sema::FinalizeDeclaration is the first callback that is used for decl with or without initializers. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@171458 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 624f40582b..61791b2aa5 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -4537,8 +4537,6 @@ Sema::ActOnVariableDeclarator(Scope *S, Declarator &D, DeclContext *DC, // member, set the visibility of this variable. if (NewVD->getLinkage() == ExternalLinkage && !DC->isRecord()) AddPushedVisibilityAttribute(NewVD); - - MarkUnusedFileScopedDecl(NewVD); return NewVD; } @@ -7348,6 +7346,9 @@ Sema::FinalizeDeclaration(Decl *ThisDecl) { if (!VD) return; + if (VD->isFileVarDecl()) + MarkUnusedFileScopedDecl(VD); + // Now we have parsed the initializer and can update the table of magic // tag values. if (!VD->hasAttr() || diff --git a/test/SemaCXX/warn-unused-filescoped.cpp b/test/SemaCXX/warn-unused-filescoped.cpp index 4cb38d997b..e12668bf2a 100644 --- a/test/SemaCXX/warn-unused-filescoped.cpp +++ b/test/SemaCXX/warn-unused-filescoped.cpp @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -fsyntax-only -verify -Wunused -Wunused-member-function -std=c++98 %s +// RUN: %clang_cc1 -fsyntax-only -verify -Wunused -Wunused-member-function -Wno-c++11-extensions -std=c++98 %s // RUN: %clang_cc1 -fsyntax-only -verify -Wunused -Wunused-member-function -std=c++11 %s static void f1(); // expected-warning{{unused}} @@ -132,3 +132,11 @@ namespace test6 { void bar(); }; } + +namespace pr14776 { + namespace { + struct X {}; + } + X a = X(); // expected-warning {{unused variable 'a'}} + auto b = X(); // expected-warning {{unused variable 'b'}} +}