]> granicus.if.org Git - clang/commitdiff
Mark all subsequent decls used.
authorRafael Espindola <rafael.espindola@gmail.com>
Tue, 8 Jan 2013 19:43:34 +0000 (19:43 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Tue, 8 Jan 2013 19:43:34 +0000 (19:43 +0000)
In the source

  static void f();
  static void f();
  template<typename T>
  static void g() {
    f();
  }
  static void f() {
  }
  void h() {
    g<int>();
  }

the call to f refers to the second decl, but it is only marked used at the end
of the translation unit during instantiation, after the third f decl has been
linked in.

With this patch we mark all subsequent decls used, so that it is easy to check
if a symbol is used or not.

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

lib/Sema/Sema.cpp
lib/Sema/SemaExpr.cpp
test/SemaCXX/warn-func-not-needed.cpp

index 66861ae663ad6fbabf4c2e2854c07230cfee2cc0..8f9e1a9a0f9541241bfa7293229c67e504d8732c 100644 (file)
@@ -328,11 +328,7 @@ CastKind Sema::ScalarTypeToBooleanCastKind(QualType ScalarTy) {
 
 /// \brief Used to prune the decls of Sema's UnusedFileScopedDecls vector.
 static bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) {
-  // Template instantiation can happen at the end of the translation unit
-  // and it sets the canonical (first) decl to used. Normal uses set the last
-  // decl at the time to used and subsequent decl inherit the flag. The net
-  // result is that we need to check both ends of the decl chain.
-  if (D->isUsed() || D->getMostRecentDecl()->isUsed())
+  if (D->getMostRecentDecl()->isUsed())
     return true;
 
   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
index 3702aa037c1d56b08d3672638e4929cad392f600..cc692cda6327036d142b992a066a96035069f4b2 100644 (file)
@@ -10495,7 +10495,18 @@ void Sema::MarkFunctionReferenced(SourceLocation Loc, FunctionDecl *Func) {
     if (old.isInvalid()) old = Loc;
   }
 
-  Func->setUsed(true);
+  // Normally the must current decl is marked used while processing the use and
+  // any subsequent decls are marked used by decl merging. This fails with
+  // template instantiation since marking can happen at the end of the file
+  // and, because of the two phase lookup, this function is called with at
+  // decl in the middle of a decl chain. We loop to maintain the invariant
+  // that once a decl is used, all decls after it are also used.
+  for (FunctionDecl *F = Func->getMostRecentDecl();;) {
+    F->setUsed(true);
+    if (F == Func)
+      break;
+    F = F->getPreviousDecl();
+  }
 }
 
 static void
index 0cc639fdd201b026222a2742b72f2ceda3b52dc5..d51c17356632e134ebde0a43611f192073daa341 100644 (file)
@@ -28,3 +28,17 @@ namespace test3 {
     g<int>();
   }
 }
+
+namespace test4 {
+  static void f();
+  static void f();
+  template<typename T>
+  static void g() {
+    f();
+  }
+  static void f() {
+  }
+  void h() {
+    g<int>();
+  }
+}