xref: /minix3/external/bsd/llvm/dist/clang/tools/diagtool/DiagTool.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc //===- DiagTool.cpp - Classes for defining diagtool tools -------------------===//
2*f4a2713aSLionel Sambuc //
3*f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4*f4a2713aSLionel Sambuc //
5*f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7*f4a2713aSLionel Sambuc //
8*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9*f4a2713aSLionel Sambuc //
10*f4a2713aSLionel Sambuc // This file implements the boilerplate for defining diagtool tools.
11*f4a2713aSLionel Sambuc //
12*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13*f4a2713aSLionel Sambuc 
14*f4a2713aSLionel Sambuc #include "DiagTool.h"
15*f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
16*f4a2713aSLionel Sambuc #include "llvm/ADT/StringMap.h"
17*f4a2713aSLionel Sambuc #include <vector>
18*f4a2713aSLionel Sambuc 
19*f4a2713aSLionel Sambuc using namespace diagtool;
20*f4a2713aSLionel Sambuc 
21*f4a2713aSLionel Sambuc DiagTool::DiagTool(llvm::StringRef toolCmd,
22*f4a2713aSLionel Sambuc                    llvm::StringRef toolDesc)
23*f4a2713aSLionel Sambuc   : cmd(toolCmd), description(toolDesc) {}
24*f4a2713aSLionel Sambuc 
25*f4a2713aSLionel Sambuc DiagTool::~DiagTool() {}
26*f4a2713aSLionel Sambuc 
27*f4a2713aSLionel Sambuc typedef llvm::StringMap<DiagTool *> ToolMap;
28*f4a2713aSLionel Sambuc static inline ToolMap *getTools(void *v) { return static_cast<ToolMap*>(v); }
29*f4a2713aSLionel Sambuc 
30*f4a2713aSLionel Sambuc DiagTools::DiagTools() : tools(new ToolMap()) {}
31*f4a2713aSLionel Sambuc DiagTools::~DiagTools() { delete getTools(tools); }
32*f4a2713aSLionel Sambuc 
33*f4a2713aSLionel Sambuc DiagTool *DiagTools::getTool(llvm::StringRef toolCmd) {
34*f4a2713aSLionel Sambuc   ToolMap::iterator it = getTools(tools)->find(toolCmd);
35*f4a2713aSLionel Sambuc   return (it == getTools(tools)->end()) ? 0 : it->getValue();
36*f4a2713aSLionel Sambuc }
37*f4a2713aSLionel Sambuc 
38*f4a2713aSLionel Sambuc void DiagTools::registerTool(DiagTool *tool) {
39*f4a2713aSLionel Sambuc   getTools(tools)->GetOrCreateValue(tool->getName(), tool);
40*f4a2713aSLionel Sambuc }
41*f4a2713aSLionel Sambuc 
42*f4a2713aSLionel Sambuc void DiagTools::printCommands(llvm::raw_ostream &out) {
43*f4a2713aSLionel Sambuc   std::vector<llvm::StringRef> toolNames;
44*f4a2713aSLionel Sambuc   unsigned maxName = 0;
45*f4a2713aSLionel Sambuc   for (ToolMap::iterator it = getTools(tools)->begin(),
46*f4a2713aSLionel Sambuc        ei = getTools(tools)->end(); it != ei; ++it) {
47*f4a2713aSLionel Sambuc     toolNames.push_back(it->getKey());
48*f4a2713aSLionel Sambuc     unsigned len = it->getKey().size();
49*f4a2713aSLionel Sambuc     if (len > maxName)
50*f4a2713aSLionel Sambuc       maxName = len;
51*f4a2713aSLionel Sambuc   }
52*f4a2713aSLionel Sambuc   std::sort(toolNames.begin(), toolNames.end());
53*f4a2713aSLionel Sambuc 
54*f4a2713aSLionel Sambuc   for (std::vector<llvm::StringRef>::iterator it = toolNames.begin(),
55*f4a2713aSLionel Sambuc        ei = toolNames.end(); it != ei; ++it) {
56*f4a2713aSLionel Sambuc 
57*f4a2713aSLionel Sambuc     out << "  " << (*it);
58*f4a2713aSLionel Sambuc     unsigned spaces = (maxName + 3) - (it->size());
59*f4a2713aSLionel Sambuc     for (unsigned i = 0; i < spaces; ++i)
60*f4a2713aSLionel Sambuc       out << ' ';
61*f4a2713aSLionel Sambuc 
62*f4a2713aSLionel Sambuc     out << getTool(*it)->getDescription() << '\n';
63*f4a2713aSLionel Sambuc   }
64*f4a2713aSLionel Sambuc }
65*f4a2713aSLionel Sambuc 
66*f4a2713aSLionel Sambuc namespace diagtool {
67*f4a2713aSLionel Sambuc   llvm::ManagedStatic<DiagTools> diagTools;
68*f4a2713aSLionel Sambuc }
69