From 77c60085afbd50c03ed49129b213de64e209e6d6 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Tue, 22 Oct 2013 21:56:29 +0000 Subject: [PATCH] Consider hidden decls for isUsed checks. This fixes pr17624. A FIXME from Richard Smith: It seems to me that the root cause is that a per-Decl 'used' flag doesn't really make much sense in the way we use it now. I think we should either track whether that particular declaration is used (with isUsed scanning the entire redecl chain), or we should only have one flag for the entire redeclaration chain (perhaps by always looking at the flag on either the most recent decl or the canonical decl). Modeling it as "is this declaration or any previous declaration used" is weird, and requires contortions like the loop at the end of Sema::MarkFunctionReferenced. llvm-svn: 193202 --- clang/lib/Sema/SemaDecl.cpp | 4 ++-- clang/test/Sema/warn-variable-not-needed.c | 9 +++++++++ clang/test/SemaCXX/warn-func-not-needed.cpp | 9 +++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 clang/test/Sema/warn-variable-not-needed.c diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index d19d2c42a9d8..81ade1f3ce09 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -2788,7 +2788,7 @@ bool Sema::MergeCompatibleFunctionDecls(FunctionDecl *New, FunctionDecl *Old, New->setPure(); // Merge "used" flag. - New->setIsUsed(Old->isUsed(false)); + New->setIsUsed(Old->getMostRecentDecl()->isUsed(false)); // Merge attributes from the parameters. These can mismatch with K&R // declarations. @@ -3101,7 +3101,7 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous) { } // Merge "used" flag. - New->setIsUsed(Old->isUsed(false)); + New->setIsUsed(Old->getMostRecentDecl()->isUsed(false)); // Keep a chain of previous declarations. New->setPreviousDecl(Old); diff --git a/clang/test/Sema/warn-variable-not-needed.c b/clang/test/Sema/warn-variable-not-needed.c new file mode 100644 index 000000000000..472ac8298999 --- /dev/null +++ b/clang/test/Sema/warn-variable-not-needed.c @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -Wall %s +// expected-no-diagnostics + +static int a; +int bar() { + extern int a; + return a; +} +static int a; diff --git a/clang/test/SemaCXX/warn-func-not-needed.cpp b/clang/test/SemaCXX/warn-func-not-needed.cpp index d51c17356632..65721f44f570 100644 --- a/clang/test/SemaCXX/warn-func-not-needed.cpp +++ b/clang/test/SemaCXX/warn-func-not-needed.cpp @@ -42,3 +42,12 @@ namespace test4 { g(); } } + +namespace test4 { + static void func(); + void bar() { + void func(); + func(); + } + static void func() {} +}