]> granicus.if.org Git - clang/commitdiff
Consider hidden decls for isUsed checks.
authorRafael Espindola <rafael.espindola@gmail.com>
Tue, 22 Oct 2013 21:56:29 +0000 (21:56 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Tue, 22 Oct 2013 21:56:29 +0000 (21:56 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@193202 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDecl.cpp
test/Sema/warn-variable-not-needed.c [new file with mode: 0644]
test/SemaCXX/warn-func-not-needed.cpp

index d19d2c42a9d894eb4091093e43d9270425f7bf37..81ade1f3ce0911147c79ef96de073a37d225d036 100644 (file)
@@ -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/test/Sema/warn-variable-not-needed.c b/test/Sema/warn-variable-not-needed.c
new file mode 100644 (file)
index 0000000..472ac82
--- /dev/null
@@ -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;
index d51c17356632e134ebde0a43611f192073daa341..65721f44f57073467732cef5546e62cfb9493962 100644 (file)
@@ -42,3 +42,12 @@ namespace test4 {
     g<int>();
   }
 }
+
+namespace test4 {
+  static void func();
+  void bar() {
+    void func();
+    func();
+  }
+  static void func() {}
+}