1 //===- DiagTool.cpp - Classes for defining diagtool tools -------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the boilerplate for defining diagtool tools.
11 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/ADT/STLExtras.h"
18 using namespace diagtool
;
20 DiagTool::DiagTool(llvm::StringRef toolCmd
, llvm::StringRef toolDesc
)
21 : cmd(std::string(toolCmd
)), description(std::string(toolDesc
)) {}
23 DiagTool::~DiagTool() {}
25 typedef llvm::StringMap
<DiagTool
*> ToolMap
;
26 static inline ToolMap
*getTools(void *v
) { return static_cast<ToolMap
*>(v
); }
28 DiagTools::DiagTools() : tools(new ToolMap()) {}
29 DiagTools::~DiagTools() { delete getTools(tools
); }
31 DiagTool
*DiagTools::getTool(llvm::StringRef toolCmd
) {
32 ToolMap::iterator it
= getTools(tools
)->find(toolCmd
);
33 return (it
== getTools(tools
)->end()) ? nullptr : it
->getValue();
36 void DiagTools::registerTool(DiagTool
*tool
) {
37 (*getTools(tools
))[tool
->getName()] = tool
;
40 void DiagTools::printCommands(llvm::raw_ostream
&out
) {
41 std::vector
<llvm::StringRef
> toolNames
;
43 for (ToolMap::iterator it
= getTools(tools
)->begin(),
44 ei
= getTools(tools
)->end(); it
!= ei
; ++it
) {
45 toolNames
.push_back(it
->getKey());
46 unsigned len
= it
->getKey().size();
50 llvm::sort(toolNames
);
52 for (std::vector
<llvm::StringRef
>::iterator it
= toolNames
.begin(),
53 ei
= toolNames
.end(); it
!= ei
; ++it
) {
56 unsigned spaces
= (maxName
+ 3) - (it
->size());
57 for (unsigned i
= 0; i
< spaces
; ++i
)
60 out
<< getTool(*it
)->getDescription() << '\n';
65 llvm::ManagedStatic
<DiagTools
> diagTools
;