Addressed Simplification concerns:
  * TransformListener completely gone.
  * pimpl idiom in TransformRegistry no longer used.
  * Some misc style issues.

  Also added transform-enabling argument handling as it's part of foundation as
  well.

Hi klimek,

http://llvm-reviews.chandlerc.com/D221

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D221?vs=542&id=548#toc

Files:
  cpp11-migrate/CMakeLists.txt
  cpp11-migrate/Cpp11Migrate.cpp
  cpp11-migrate/Transform.cpp
  cpp11-migrate/Transform.h
  cpp11-migrate/TransformNameParser.h
  cpp11-migrate/TransformRegistry.cpp
  cpp11-migrate/TransformRegistry.h
  cpp11-migrate/TransformSupport.h
Index: cpp11-migrate/CMakeLists.txt
===================================================================
--- cpp11-migrate/CMakeLists.txt
+++ cpp11-migrate/CMakeLists.txt
@@ -3,6 +3,8 @@
 
 add_clang_executable(cpp11-migrate
   Cpp11Migrate.cpp
+  TransformRegistry.cpp
+  Transform.cpp
   )
 
 target_link_libraries(cpp11-migrate
Index: cpp11-migrate/Cpp11Migrate.cpp
===================================================================
--- cpp11-migrate/Cpp11Migrate.cpp
+++ cpp11-migrate/Cpp11Migrate.cpp
@@ -27,21 +27,49 @@
 ///
 //===----------------------------------------------------------------------===//
 
-#include "clang/Basic/FileManager.h"
-#include "clang/Frontend/CompilerInstance.h"
+#include "TransformSupport.h"
+#include "TransformNameParser.h"
+#include "Transform.h"
 #include "clang/Frontend/FrontendActions.h"
-#include "clang/Tooling/Refactoring.h"
 #include "clang/Tooling/Tooling.h"
 #include "clang/Tooling/CommonOptionsParser.h"
 
 namespace cl = llvm::cl;
 using namespace clang::tooling;
 
+// Helper RAII class for created Transforms.
+class TransformVec {
+public:
+  TransformVec(size_t Reserve) {
+    Transforms.reserve(Reserve);
+  }
+  ~TransformVec() {
+    for (std::vector<Transform*>::iterator I = Transforms.begin();
+         I != Transforms.end(); ++I) {
+      delete *I;
+    }
+  }
+
+  void push_back(Transform *T) { Transforms.push_back(T); }
+
+private:
+  std::vector<Transform*> Transforms;
+};
 
 int main(int argc, const char **argv) {
+  cl::list<const TransformInfo *, bool, TransformNameParser>
+  TransformList(cl::desc("Transforms available"));
+
   CommonOptionsParser OptionsParser(argc, argv);
 
-  // TODO: Create transforms requested by command-line.
+  // Create transforms requested by command-line.
+  TransformVec Transforms(TransformList.size());
+  for (unsigned int i = 0; i < TransformList.size(); ++i) {
+    const TransformInfo *Info = TransformList[i];
+    Transforms.push_back(Info->getCreator()());
+  }
+
+  // Initial syntax check.
   ClangTool SyntaxTool(OptionsParser.getCompilations(),
                        OptionsParser.getSourcePathList());
 
Index: cpp11-migrate/Transform.cpp
===================================================================
--- /dev/null
+++ cpp11-migrate/Transform.cpp
@@ -0,0 +1,17 @@
+//===-- cpp11-migrate/Transform.cpp - Transform Impl ------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+/// \file
+/// \brief This file provides implementations for the Transform class and other
+/// support classes.
+///
+//===----------------------------------------------------------------------===//
+
+#include "Transform.h"
+#include "TransformSupport.h"
+
Index: cpp11-migrate/Transform.h
===================================================================
--- /dev/null
+++ cpp11-migrate/Transform.h
@@ -0,0 +1,20 @@
+//===-- cpp11-migrate/Transform.h - Transform Base Class Def'n --*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+/// \file
+/// \brief This file provides the definition for the base Transform class from
+/// which all transforms must subclass.
+///
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_TOOLS_CLANG_TOOLS_EXTRA_CPP11_MIGRATE_TRANSFORM_H
+#define LLVM_TOOLS_CLANG_TOOLS_EXTRA_CPP11_MIGRATE_TRANSFORM_H
+
+class Transform {
+};
+
+#endif // LLVM_TOOLS_CLANG_TOOLS_EXTRA_CPP11_MIGRATE_TRANSFORM_H
Index: cpp11-migrate/TransformNameParser.h
===================================================================
--- /dev/null
+++ cpp11-migrate/TransformNameParser.h
@@ -0,0 +1,58 @@
+//===-- cpp11-migrate/TransformNameParser.h ---------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+/// \file
+/// \brief This file provides the definition and implementation for
+/// TransformNameParser which is responsible for adding command-line args
+/// for each registered Transform.
+///
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_TOOLS_CLANG_TOOLS_EXTRA_CPP11_MIGRATE_TRANSFORM_NAME_PARSER_H
+#define LLVM_TOOLS_CLANG_TOOLS_EXTRA_CPP11_MIGRATE_TRANSFORM_NAME_PARSER_H
+
+#include "TransformSupport.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/raw_ostream.h"
+
+class TransformNameParser : public llvm::cl::parser<const TransformInfo *> {
+public:
+  void initialize(llvm::cl::Option &O) {
+    llvm::cl::parser<const TransformInfo *>::initialize(O);
+
+    TransformRegistry *Registry = TransformRegistry::getRegistry();
+    for (TransformRegistry::const_iterator I = Registry->begin();
+         I != Registry->end(); ++I) {
+      if (findOption(I->getValue()->getName()) != getNumOptions()) {
+        llvm::errs() << "Two transforms with the same name (-"
+          << I->getValue()->getName() << ") attempted to be registered!\n";
+        llvm_unreachable(0);
+      }
+      addLiteralOption(I->getValue()->getName(),
+                       I->getValue(),
+                       I->getValue()->getDesc());
+    }
+  }
+
+  virtual void
+  printOptionInfo(const llvm::cl::Option &O,
+                  size_t GlobalWidth) const LLVM_OVERRIDE {
+    TransformNameParser *PNP = const_cast<TransformNameParser *>(this);
+    llvm::array_pod_sort(PNP->Values.begin(), PNP->Values.end(), NameCompare);
+    llvm::cl::parser<const TransformInfo*>::printOptionInfo(O, GlobalWidth);
+  }
+
+private:
+  static int NameCompare(const void *VT1, const void *VT2) {
+    typedef TransformNameParser::OptionInfo ValType;
+    return std::strcmp(static_cast<const ValType *>(VT1)->Name,
+                       static_cast<const ValType *>(VT2)->Name);
+  }
+
+};
+#endif // LLVM_TOOLS_CLANG_TOOLS_EXTRA_CPP11_MIGRATE_TRANSFORM_NAME_PARSER_H
Index: cpp11-migrate/TransformRegistry.cpp
===================================================================
--- /dev/null
+++ cpp11-migrate/TransformRegistry.cpp
@@ -0,0 +1,32 @@
+//===- cpp11-migrate/TransformRegistry.cpp - Registration Impl --*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+/// \file
+/// \brief This file provides the implementation of the TransformRegistry.
+///
+//===----------------------------------------------------------------------===//
+
+#include "TransformRegistry.h"
+#include "TransformSupport.h"
+#include "llvm/Support/ManagedStatic.h"
+
+llvm::ManagedStatic<TransformRegistry> TheRegistry;
+
+TransformRegistry *TransformRegistry::getRegistry() {
+  return &*TheRegistry;
+}
+
+void TransformRegistry::registerTransform(const TransformInfo *Info) {
+  assert(Info != 0 && "TransformInfo is NULL");
+  if (StringInfoMap.find(Info->getName()) !=
+      StringInfoMap.end()) {
+    assert(!"Transform registered multiple times!");
+  }
+  StringInfoMap[Info->getName()] = Info;
+}
+
Index: cpp11-migrate/TransformRegistry.h
===================================================================
--- /dev/null
+++ cpp11-migrate/TransformRegistry.h
@@ -0,0 +1,44 @@
+//===-- cpp11-migrate/TransformRegistry.h - Registration Def'n --*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+/// \file
+/// \brief This file provides the definition for TransformRegistry.h.
+///
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_TOOLS_CLANG_TOOLS_EXTRA_CPP11_MIGRATE_TRANSFORM_REGISTRY_H
+#define LLVM_TOOLS_CLANG_TOOLS_EXTRA_CPP11_MIGRATE_TRANSFORM_REGISTRY_H
+
+#include "llvm/ADT/StringMap.h"
+#include <vector>
+
+class TransformInfo;
+
+class TransformRegistry {
+public:
+  typedef llvm::StringMap<const TransformInfo *> StringMapType;
+  typedef StringMapType::const_iterator const_iterator;
+
+public:
+
+  /// Returns a pointer to the global TransformRegistry.
+  ///
+  /// No longer valid after llvm_shutdown.
+  static TransformRegistry *getRegistry();
+
+  /// Register a Transform with the registry.
+  void registerTransform(const TransformInfo *TI);
+
+  const_iterator begin() const { return StringInfoMap.begin(); }
+  const_iterator end() const { return StringInfoMap.end(); }
+  
+private:
+  StringMapType StringInfoMap;
+};
+
+
+#endif // LLVM_TOOLS_CLANG_TOOLS_EXTRA_CPP11_MIGRATE_TRANSFORM_REGISTRY_H
Index: cpp11-migrate/TransformSupport.h
===================================================================
--- /dev/null
+++ cpp11-migrate/TransformSupport.h
@@ -0,0 +1,52 @@
+//===-- cpp11-migrate/TransformSupport.h - Transform registration*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+/// \file
+/// \brief This file provides definitions for classes involved with transform
+/// registration.
+///
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_TOOLS_CLANG_TOOLS_EXTRA_CPP11_MIGRATE_TRANSFORM_SUPPORT_H
+#define LLVM_TOOLS_CLANG_TOOLS_EXTRA_CPP11_MIGRATE_TRANSFORM_SUPPORT_H
+
+#include "TransformRegistry.h"
+
+class Transform;
+
+class TransformInfo {
+public:
+  typedef Transform *(*TransformCreator)();
+
+public:
+  TransformInfo(const char *Name, const char *Desc, TransformCreator Creator)
+    : Name(Name), Desc(Desc), Creator(Creator) {}
+
+  const char *getName() const { return Name; }
+  const char *getDesc() const { return Desc; }
+  TransformCreator getCreator() const { return Creator; }
+
+
+private:
+  const char *Name;
+  const char *Desc;
+  TransformCreator Creator;
+};
+
+template <typename TransformType>
+Transform *createTransform() { return new TransformType(); }
+
+template <typename TransformType>
+class RegisterTransform : public TransformInfo {
+public:
+  RegisterTransform(const char *Name, const char *Description)
+   : TransformInfo(Name, Description, &createTransform<TransformType>) {
+    TransformRegistry::getRegistry()->registerTransform(this);
+  }
+};
+
+#endif // LLVM_TOOLS_CLANG_TOOLS_EXTRA_CPP11_MIGRATE_TRANSFORM_SUPPORT_H
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to