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