1 //===- DiagTool.h - Classes for defining diagtool tools -------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the boilerplate for defining diagtool tools. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef DIAGTOOL_DIAGTOOL_H 15 #define DIAGTOOL_DIAGTOOL_H 16 17 #include "llvm/ADT/StringRef.h" 18 #include "llvm/Support/ManagedStatic.h" 19 #include "llvm/Support/raw_ostream.h" 20 #include <string> 21 22 23 namespace diagtool { 24 25 class DiagTool { 26 const std::string cmd; 27 const std::string description; 28 public: 29 DiagTool(llvm::StringRef toolCmd, llvm::StringRef toolDesc); 30 virtual ~DiagTool(); 31 32 llvm::StringRef getName() const { return cmd; } 33 llvm::StringRef getDescription() const { return description; } 34 35 virtual int run(unsigned argc, char *argv[], llvm::raw_ostream &out) = 0; 36 }; 37 38 class DiagTools { 39 void *tools; 40 public: 41 DiagTools(); 42 ~DiagTools(); 43 44 DiagTool *getTool(llvm::StringRef toolCmd); 45 void registerTool(DiagTool *tool); 46 void printCommands(llvm::raw_ostream &out); 47 }; 48 49 extern llvm::ManagedStatic<DiagTools> diagTools; 50 51 template <typename DIAGTOOL> 52 class RegisterDiagTool { 53 public: 54 RegisterDiagTool() { diagTools->registerTool(new DIAGTOOL()); } 55 }; 56 57 } // end diagtool namespace 58 59 #define DEF_DIAGTOOL(NAME, DESC, CLSNAME)\ 60 namespace {\ 61 class CLSNAME : public diagtool::DiagTool {\ 62 public:\ 63 CLSNAME() : DiagTool(NAME, DESC) {}\ 64 virtual ~CLSNAME() {}\ 65 virtual int run(unsigned argc, char *argv[], llvm::raw_ostream &out);\ 66 };\ 67 diagtool::RegisterDiagTool<CLSNAME> Register##CLSNAME;\ 68 } 69 70 #endif 71