Home | History | Annotate | Download | only in diagtool
      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 
     21 DiagTool::DiagTool(llvm::StringRef toolCmd,
     22                    llvm::StringRef toolDesc)
     23   : cmd(toolCmd), description(toolDesc) {}
     24 
     25 DiagTool::~DiagTool() {}
     26 
     27 typedef llvm::StringMap<DiagTool *> ToolMap;
     28 static inline ToolMap *getTools(void *v) { return static_cast<ToolMap*>(v); }
     29 
     30 DiagTools::DiagTools() : tools(new ToolMap()) {}
     31 DiagTools::~DiagTools() { delete getTools(tools); }
     32 
     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 
     38 void DiagTools::registerTool(DiagTool *tool) {
     39   getTools(tools)->GetOrCreateValue(tool->getName(), tool);
     40 }
     41 
     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