OSDN Git Service

[LCG] Hoist the definitions of the stream operator friends to be inline
authorChandler Carruth <chandlerc@gmail.com>
Thu, 7 Jul 2016 07:52:07 +0000 (07:52 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Thu, 7 Jul 2016 07:52:07 +0000 (07:52 +0000)
friend definitions.

Based on the experiments Sean Silva and Reid did, this seems the safest
course of action and also will work around a questionable warning
provided by GCC6 on the old form of the code. Thanks for Davide pointing
out the issue and other suggesting ways to fix.

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

include/llvm/Analysis/LazyCallGraph.h
lib/Analysis/LazyCallGraph.cpp

index 47cb310..9f62eaa 100644 (file)
@@ -224,7 +224,9 @@ public:
     void removeEdgeInternal(Function &ChildF);
 
     /// Print the name of this node's function.
-    friend raw_ostream &operator<<(raw_ostream &OS, const Node &N);
+    friend raw_ostream &operator<<(raw_ostream &OS, const Node &N) {
+      return OS << N.F.getName();
+    }
 
     /// Dump the name of this node's function to stderr.
     void dump() const;
@@ -364,7 +366,26 @@ public:
     ///
     /// We print the function names in the SCC wrapped in '()'s and skipping
     /// the middle functions if there are a large number.
-    friend raw_ostream &operator<<(raw_ostream &OS, const SCC &C);
+    //
+    // Note: this is defined inline to dodge issues with GCC's interpretation
+    // of enclosing namespaces for friend function declarations.
+    friend raw_ostream &operator<<(raw_ostream &OS, const SCC &C) {
+      OS << '(';
+      int i = 0;
+      for (LazyCallGraph::Node &N : C) {
+        if (i > 0)
+          OS << ", ";
+        // Elide the inner elements if there are too many.
+        if (i > 8) {
+          OS << "..., " << *C.Nodes.back();
+          break;
+        }
+        OS << N;
+        ++i;
+      }
+      OS << ')';
+      return OS;
+    }
 
     /// Dump a short description of this SCC to stderr.
     void dump() const;
@@ -436,7 +457,26 @@ public:
     ///
     /// We print the SCCs wrapped in '[]'s and skipping the middle SCCs if
     /// there are a large number.
-    friend raw_ostream &operator<<(raw_ostream &OS, const RefSCC &RC);
+    //
+    // Note: this is defined inline to dodge issues with GCC's interpretation
+    // of enclosing namespaces for friend function declarations.
+    friend raw_ostream &operator<<(raw_ostream &OS, const RefSCC &RC) {
+      OS << '[';
+      int i = 0;
+      for (LazyCallGraph::SCC &C : RC) {
+        if (i > 0)
+          OS << ", ";
+        // Elide the inner elements if there are too many.
+        if (i > 4) {
+          OS << "..., " << *RC.SCCs.back();
+          break;
+        }
+        OS << C;
+        ++i;
+      }
+      OS << ']';
+      return OS;
+    }
 
     /// Dump a short description of this RefSCC to stderr.
     void dump() const;
index 2d34a24..acff852 100644 (file)
@@ -15,7 +15,6 @@
 #include "llvm/IR/PassManager.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/GraphWriter.h"
-#include "llvm/Support/raw_ostream.h"
 
 using namespace llvm;
 
@@ -120,10 +119,6 @@ void LazyCallGraph::Node::removeEdgeInternal(Function &Target) {
   EdgeIndexMap.erase(IndexMapI);
 }
 
-raw_ostream &llvm::operator<<(raw_ostream &OS, const LazyCallGraph::Node &N) {
-  return OS << N.F.getName();
-}
-
 void LazyCallGraph::Node::dump() const {
   dbgs() << *this << '\n';
 }
@@ -181,24 +176,6 @@ LazyCallGraph &LazyCallGraph::operator=(LazyCallGraph &&G) {
   return *this;
 }
 
-raw_ostream &llvm::operator<<(raw_ostream &OS, const LazyCallGraph::SCC &C) {
-  OS << '(';
-  int i = 0;
-  for (LazyCallGraph::Node &N : C) {
-    if (i > 0)
-      OS << ", ";
-    // Elide the inner elements if there are too many.
-    if (i > 8) {
-      OS << "..., " << *C.Nodes.back();
-      break;
-    }
-    OS << N;
-    ++i;
-  }
-  OS << ')';
-  return OS;
-}
-
 void LazyCallGraph::SCC::dump() const {
   dbgs() << *this << '\n';
 }
@@ -224,25 +201,6 @@ void LazyCallGraph::SCC::verify() {
 
 LazyCallGraph::RefSCC::RefSCC(LazyCallGraph &G) : G(&G) {}
 
-raw_ostream &llvm::operator<<(raw_ostream &OS,
-                              const LazyCallGraph::RefSCC &RC) {
-  OS << '[';
-  int i = 0;
-  for (LazyCallGraph::SCC &C : RC) {
-    if (i > 0)
-      OS << ", ";
-    // Elide the inner elements if there are too many.
-    if (i > 4) {
-      OS << "..., " << *RC.SCCs.back();
-      break;
-    }
-    OS << C;
-    ++i;
-  }
-  OS << ']';
-  return OS;
-}
-
 void LazyCallGraph::RefSCC::dump() const {
   dbgs() << *this << '\n';
 }