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