• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- DiagTool.cpp - 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 #include "DiagTool.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/StringMap.h"
17 #include <vector>
18 
19 using namespace diagtool;
20 
DiagTool(llvm::StringRef toolCmd,llvm::StringRef toolDesc)21 DiagTool::DiagTool(llvm::StringRef toolCmd,
22                    llvm::StringRef toolDesc)
23   : cmd(toolCmd), description(toolDesc) {}
24 
~DiagTool()25 DiagTool::~DiagTool() {}
26 
27 typedef llvm::StringMap<DiagTool *> ToolMap;
getTools(void * v)28 static inline ToolMap *getTools(void *v) { return static_cast<ToolMap*>(v); }
29 
DiagTools()30 DiagTools::DiagTools() : tools(new ToolMap()) {}
~DiagTools()31 DiagTools::~DiagTools() { delete getTools(tools); }
32 
getTool(llvm::StringRef toolCmd)33 DiagTool *DiagTools::getTool(llvm::StringRef toolCmd) {
34   ToolMap::iterator it = getTools(tools)->find(toolCmd);
35   return (it == getTools(tools)->end()) ? nullptr : it->getValue();
36 }
37 
registerTool(DiagTool * tool)38 void DiagTools::registerTool(DiagTool *tool) {
39   (*getTools(tools))[tool->getName()] = tool;
40 }
41 
printCommands(llvm::raw_ostream & out)42 void DiagTools::printCommands(llvm::raw_ostream &out) {
43   std::vector<llvm::StringRef> toolNames;
44   unsigned maxName = 0;
45   for (ToolMap::iterator it = getTools(tools)->begin(),
46        ei = getTools(tools)->end(); it != ei; ++it) {
47     toolNames.push_back(it->getKey());
48     unsigned len = it->getKey().size();
49     if (len > maxName)
50       maxName = len;
51   }
52   std::sort(toolNames.begin(), toolNames.end());
53 
54   for (std::vector<llvm::StringRef>::iterator it = toolNames.begin(),
55        ei = toolNames.end(); it != ei; ++it) {
56 
57     out << "  " << (*it);
58     unsigned spaces = (maxName + 3) - (it->size());
59     for (unsigned i = 0; i < spaces; ++i)
60       out << ' ';
61 
62     out << getTool(*it)->getDescription() << '\n';
63   }
64 }
65 
66 namespace diagtool {
67   llvm::ManagedStatic<DiagTools> diagTools;
68 }
69