blob: b1e69f38c323d86a99f39436f3c670238aad7c70 [file] [log] [blame]
Ted Kremenekf88d3352011-08-09 03:39:19 +00001//===- 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"
Nick Lewycky1c5f3fa2011-08-12 01:14:22 +000018#include "llvm/Support/ManagedStatic.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000019#include "llvm/Support/raw_ostream.h"
Ted Kremenekf88d3352011-08-09 03:39:19 +000020#include <string>
21
22
23namespace diagtool {
24
25class DiagTool {
26 const std::string cmd;
27 const std::string description;
28public:
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
38class DiagTools {
39 void *tools;
40public:
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
Nick Lewycky1c5f3fa2011-08-12 01:14:22 +000049extern llvm::ManagedStatic<DiagTools> diagTools;
50
Ted Kremenekf88d3352011-08-09 03:39:19 +000051template <typename DIAGTOOL>
52class RegisterDiagTool {
53public:
Nick Lewycky1c5f3fa2011-08-12 01:14:22 +000054 RegisterDiagTool() { diagTools->registerTool(new DIAGTOOL()); }
Ted Kremenekf88d3352011-08-09 03:39:19 +000055};
56
57} // end diagtool namespace
58
59#define DEF_DIAGTOOL(NAME, DESC, CLSNAME)\
60namespace {\
61class CLSNAME : public diagtool::DiagTool {\
62public:\
63 CLSNAME() : DiagTool(NAME, DESC) {}\
64 virtual ~CLSNAME() {}\
Craig Topper36835562014-03-15 07:47:46 +000065 int run(unsigned argc, char *argv[], llvm::raw_ostream &out) override;\
Ted Kremenekf88d3352011-08-09 03:39:19 +000066};\
67diagtool::RegisterDiagTool<CLSNAME> Register##CLSNAME;\
68}
69
70#endif