blob: 36e72a2ded5abf20a0734a725b21a69158891f33 [file] [log] [blame]
Ted Kremenek2dc651d2011-08-09 03:39:19 +00001//===- 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/StringMap.h"
16#include "llvm/ADT/SmallString.h"
17#include <vector>
18
19using namespace diagtool;
20
21DiagTool::DiagTool(llvm::StringRef toolCmd,
22 llvm::StringRef toolDesc)
23 : cmd(toolCmd), description(toolDesc) {}
24
25DiagTool::~DiagTool() {}
26
27typedef llvm::StringMap<DiagTool *> ToolMap;
28static inline ToolMap *getTools(void *v) { return static_cast<ToolMap*>(v); }
29
30DiagTools::DiagTools() : tools(new ToolMap()) {}
31DiagTools::~DiagTools() { delete getTools(tools); }
32
33DiagTool *DiagTools::getTool(llvm::StringRef toolCmd) {
34 ToolMap::iterator it = getTools(tools)->find(toolCmd);
35 return (it == getTools(tools)->end()) ? 0 : it->getValue();
36}
37
38void DiagTools::registerTool(DiagTool *tool) {
39 getTools(tools)->GetOrCreateValue(tool->getName(), tool);
40}
41
42void 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
66namespace diagtool {
Nick Lewycky83f06e82011-08-12 01:14:22 +000067 llvm::ManagedStatic<DiagTools> diagTools;
Ted Kremenek2dc651d2011-08-09 03:39:19 +000068}