• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
22 namespace diagtool {
23 
24 class DiagTool {
25   const std::string cmd;
26   const std::string description;
27 public:
28   DiagTool(llvm::StringRef toolCmd, llvm::StringRef toolDesc);
29   virtual ~DiagTool();
30 
getName()31   llvm::StringRef getName() const { return cmd; }
getDescription()32   llvm::StringRef getDescription() const { return description; }
33 
34   virtual int run(unsigned argc, char *argv[], llvm::raw_ostream &out) = 0;
35 };
36 
37 class DiagTools {
38   void *tools;
39 public:
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 
48 extern llvm::ManagedStatic<DiagTools> diagTools;
49 
50 template <typename DIAGTOOL>
51 class RegisterDiagTool {
52 public:
RegisterDiagTool()53   RegisterDiagTool() { diagTools->registerTool(new DIAGTOOL()); }
54 };
55 
56 } // end diagtool namespace
57 
58 #define DEF_DIAGTOOL(NAME, DESC, CLSNAME)\
59 namespace {\
60 class CLSNAME : public diagtool::DiagTool {\
61 public:\
62   CLSNAME() : DiagTool(NAME, DESC) {}\
63   virtual ~CLSNAME() {}\
64   int run(unsigned argc, char *argv[], llvm::raw_ostream &out) override;\
65 };\
66 diagtool::RegisterDiagTool<CLSNAME> Register##CLSNAME;\
67 }
68 
69 #endif
70