Plugin support for llvmc2 (a-la opt).

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@56465 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/llvmc2/AutoGenerated.cpp b/tools/llvmc2/AutoGenerated.cpp
index 4cc85f9..9f34e58 100644
--- a/tools/llvmc2/AutoGenerated.cpp
+++ b/tools/llvmc2/AutoGenerated.cpp
@@ -11,20 +11,5 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "AutoGenerated.h"
-#include "CompilationGraph.h"
-#include "Tool.h"
-
-#include "llvm/ADT/StringExtras.h"
-#include "llvm/Support/CommandLine.h"
-
-#include <cstdlib>
-#include <stdexcept>
-
-using namespace llvm;
-using namespace llvmc;
-
-extern cl::opt<std::string> OutputFilename;
-
 // The auto-generated file
 #include "AutoGenerated.inc"
diff --git a/tools/llvmc2/AutoGenerated.h b/tools/llvmc2/AutoGenerated.h
deleted file mode 100644
index 0de7208..0000000
--- a/tools/llvmc2/AutoGenerated.h
+++ /dev/null
@@ -1,34 +0,0 @@
-//===--- AutoGenerated.h - The LLVM Compiler Driver -------------*- C++ -*-===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open
-// Source License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-//  Auto-generated tool descriptions - public interface.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_TOOLS_LLVMC2_AUTOGENERATED_H
-#define LLVM_TOOLS_LLVMC2_AUTOGENERATED_H
-
-#include "llvm/ADT/StringMap.h"
-
-#include <string>
-
-namespace llvmc {
-
-  class LanguageMap;
-  class CompilationGraph;
-
-  /// PopulateLanguageMap - The auto-generated function that fills in
-  /// the language map (map from file extensions to language names).
-  void PopulateLanguageMap(LanguageMap& langMap);
-  /// PopulateCompilationGraph - The auto-generated function that
-  /// populates the compilation graph with nodes and edges.
-  void PopulateCompilationGraph(CompilationGraph& tools);
-}
-
-#endif // LLVM_TOOLS_LLVMC2_AUTOGENERATED_H
diff --git a/tools/llvmc2/CompilationGraph.h b/tools/llvmc2/CompilationGraph.h
index 71ae227..57758bf 100644
--- a/tools/llvmc2/CompilationGraph.h
+++ b/tools/llvmc2/CompilationGraph.h
@@ -14,7 +14,6 @@
 #ifndef LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H
 #define LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H
 
-#include "AutoGenerated.h"
 #include "Tool.h"
 
 #include "llvm/ADT/GraphTraits.h"
@@ -30,6 +29,7 @@
 
 namespace llvmc {
 
+  class CompilationGraph;
   typedef llvm::StringSet<> InputLanguagesSet;
 
   /// LanguageMap - Maps from extensions to language names.
diff --git a/tools/llvmc2/Plugin.cpp b/tools/llvmc2/Plugin.cpp
new file mode 100644
index 0000000..70d9987
--- /dev/null
+++ b/tools/llvmc2/Plugin.cpp
@@ -0,0 +1,41 @@
+//===--- Plugin.cpp - The LLVM Compiler Driver ------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open
+// Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  Plugin support for llvmc2.
+//
+//===----------------------------------------------------------------------===//
+
+#include "Plugin.h"
+
+#include <vector>
+
+namespace {
+  typedef std::vector<llvmc::BasePlugin*> PluginRegistry;
+  static PluginRegistry GlobalPluginRegistry;
+}
+
+namespace llvmc {
+
+  RegisterPluginImpl::RegisterPluginImpl(BasePlugin* plugin) {
+    GlobalPluginRegistry.push_back(plugin);
+  }
+
+  void PopulateLanguageMap(LanguageMap& langMap) {
+    for (PluginRegistry::const_iterator B = GlobalPluginRegistry.begin(),
+           E = GlobalPluginRegistry.end(); B != E; ++B)
+      (*B)->PopulateLanguageMap(langMap);
+  }
+
+  void PopulateCompilationGraph(CompilationGraph& graph) {
+    for (PluginRegistry::const_iterator B = GlobalPluginRegistry.begin(),
+           E = GlobalPluginRegistry.end(); B != E; ++B)
+      (*B)->PopulateCompilationGraph(graph);
+  }
+
+}
diff --git a/tools/llvmc2/Plugin.h b/tools/llvmc2/Plugin.h
new file mode 100644
index 0000000..ba22450
--- /dev/null
+++ b/tools/llvmc2/Plugin.h
@@ -0,0 +1,56 @@
+//===--- Plugin.h - The LLVM Compiler Driver --------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open
+// Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  Plugin support for llvmc2.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_TOOLS_LLVMC2_PLUGIN_H
+#define LLVM_TOOLS_LLVMC2_PLUGIN_H
+
+namespace llvmc {
+
+  class LanguageMap;
+  class CompilationGraph;
+
+  /// BasePlugin - An abstract base class for all LLVMC plugins.
+  struct BasePlugin {
+
+    /// PopulateLanguageMap - The auto-generated function that fills in
+    /// the language map (map from file extensions to language names).
+    virtual void PopulateLanguageMap(LanguageMap&) const = 0;
+
+    /// PopulateCompilationGraph - The auto-generated function that
+    /// populates the compilation graph with nodes and edges.
+    virtual void PopulateCompilationGraph(CompilationGraph&) const = 0;
+  };
+
+  // Helper class for RegisterPlugin.
+  class RegisterPluginImpl {
+  protected:
+    RegisterPluginImpl(BasePlugin*);
+  };
+
+  /// RegisterPlugin<T> template - Used to register LLVMC plugins.
+  template <class T>
+  struct RegisterPlugin : RegisterPluginImpl {
+    RegisterPlugin() : RegisterPluginImpl (new T()) {}
+  };
+
+  /// PopulateLanguageMap - Fills in the language map by calling
+  /// PopulateLanguageMap methods of all plugins.
+  void PopulateLanguageMap(LanguageMap& langMap);
+
+  /// PopulateCompilationGraph - Populates the compilation graph by
+  /// calling PopulateCompilationGraph methods of all plugins.
+  void PopulateCompilationGraph(CompilationGraph& tools);
+
+}
+
+#endif // LLVM_TOOLS_LLVMC2_PLUGIN_H
diff --git a/tools/llvmc2/Tool.h b/tools/llvmc2/Tool.h
index f4fe0c4..6f7cb8d 100644
--- a/tools/llvmc2/Tool.h
+++ b/tools/llvmc2/Tool.h
@@ -25,6 +25,7 @@
 
 namespace llvmc {
 
+  class LanguageMap;
   typedef std::vector<llvm::sys::Path> PathVector;
   typedef llvm::StringSet<> InputLanguagesSet;
 
diff --git a/tools/llvmc2/llvmc.cpp b/tools/llvmc2/llvmc.cpp
index 08a5989..bd5ea2b 100644
--- a/tools/llvmc2/llvmc.cpp
+++ b/tools/llvmc2/llvmc.cpp
@@ -16,10 +16,11 @@
 
 #include "CompilationGraph.h"
 #include "Error.h"
-#include "Tool.h"
+#include "Plugin.h"
 
 #include "llvm/System/Path.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/PluginLoader.h"
 
 #include <iostream>
 #include <stdexcept>
diff --git a/tools/llvmc2/plugins/Hello/Hello.cpp b/tools/llvmc2/plugins/Hello/Hello.cpp
new file mode 100644
index 0000000..729d3c8
--- /dev/null
+++ b/tools/llvmc2/plugins/Hello/Hello.cpp
@@ -0,0 +1,35 @@
+//===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Test plugin for LLVMC.
+//
+//===----------------------------------------------------------------------===//
+
+// TODO: Since llvmc2 has now gained support for plugins, its header
+// files should be probably moved into LLVM include dir.
+
+#include "../../CompilationGraph.h"
+#include "../../Plugin.h"
+
+#include <iostream>
+
+namespace {
+struct MyPlugin : public llvmc::BasePlugin {
+  void PopulateLanguageMap(llvmc::LanguageMap&) const
+  { std::cout << "Hello!\n"; }
+
+  void PopulateCompilationGraph(llvmc::CompilationGraph&) const
+  {}
+};
+
+static llvmc::RegisterPlugin<MyPlugin> RP;
+
+}
+
+
diff --git a/tools/llvmc2/plugins/Hello/Makefile b/tools/llvmc2/plugins/Hello/Makefile
new file mode 100644
index 0000000..3aeef24
--- /dev/null
+++ b/tools/llvmc2/plugins/Hello/Makefile
@@ -0,0 +1,17 @@
+##===- lib/Transforms/Hello/Makefile -----------------------*- Makefile -*-===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+
+LEVEL = ../../../..
+LIBRARYNAME = LLVMCHello
+LOADABLE_MODULE = 1
+REQUIRES_EH = 1
+USEDLIBS =
+
+include $(LEVEL)/Makefile.common
+