LLVM  6.0.0svn
DebugCounter.cpp
Go to the documentation of this file.
3 #include "llvm/Support/Format.h"
5 #include "llvm/Support/Options.h"
6 
7 using namespace llvm;
8 
9 namespace {
10 // This class overrides the default list implementation of printing so we
11 // can pretty print the list of debug counter options. This type of
12 // dynamic option is pretty rare (basically this and pass lists).
13 class DebugCounterList : public cl::list<std::string, DebugCounter> {
14 private:
16 
17 public:
18  template <class... Mods>
19  explicit DebugCounterList(Mods &&... Ms) : Base(std::forward<Mods>(Ms)...) {}
20 
21 private:
22  void printOptionInfo(size_t GlobalWidth) const override {
23  // This is a variant of from generic_parser_base::printOptionInfo. Sadly,
24  // it's not easy to make it more usable. We could get it to print these as
25  // options if we were a cl::opt and registered them, but lists don't have
26  // options, nor does the parser for std::string. The other mechanisms for
27  // options are global and would pollute the global namespace with our
28  // counters. Rather than go that route, we have just overridden the
29  // printing, which only a few things call anyway.
30  outs() << " -" << ArgStr;
31  // All of the other options in CommandLine.cpp use ArgStr.size() + 6 for
32  // width, so we do the same.
33  Option::printHelpStr(HelpStr, GlobalWidth, ArgStr.size() + 6);
34  const auto &CounterInstance = DebugCounter::instance();
35  for (auto Name : CounterInstance) {
36  const auto Info =
37  CounterInstance.getCounterInfo(CounterInstance.getCounterId(Name));
38  size_t NumSpaces = GlobalWidth - Info.first.size() - 8;
39  outs() << " =" << Info.first;
40  outs().indent(NumSpaces) << " - " << Info.second << '\n';
41  }
42  }
43 };
44 } // namespace
45 
46 // Create our command line option.
47 static DebugCounterList DebugCounterOption(
48  "debug-counter",
49  cl::desc("Comma separated list of debug counter skip and count"),
51 
53 
55 
56 // This is called by the command line parser when it sees a value for the
57 // debug-counter option defined above.
58 void DebugCounter::push_back(const std::string &Val) {
59  if (Val.empty())
60  return;
61  // The strings should come in as counter=value
62  auto CounterPair = StringRef(Val).split('=');
63  if (CounterPair.second.empty()) {
64  errs() << "DebugCounter Error: " << Val << " does not have an = in it\n";
65  return;
66  }
67  // Now we have counter=value.
68  // First, process value.
69  long CounterVal;
70  if (CounterPair.second.getAsInteger(0, CounterVal)) {
71  errs() << "DebugCounter Error: " << CounterPair.second
72  << " is not a number\n";
73  return;
74  }
75  // Now we need to see if this is the skip or the count, remove the suffix, and
76  // add it to the counter values.
77  if (CounterPair.first.endswith("-skip")) {
78  auto CounterName = CounterPair.first.drop_back(5);
79  unsigned CounterID = RegisteredCounters.idFor(CounterName);
80  if (!CounterID) {
81  errs() << "DebugCounter Error: " << CounterName
82  << " is not a registered counter\n";
83  return;
84  }
85 
86  auto Res = Counters.insert({CounterID, {0, -1}});
87  Res.first->second.first = CounterVal;
88  } else if (CounterPair.first.endswith("-count")) {
89  auto CounterName = CounterPair.first.drop_back(6);
90  unsigned CounterID = RegisteredCounters.idFor(CounterName);
91  if (!CounterID) {
92  errs() << "DebugCounter Error: " << CounterName
93  << " is not a registered counter\n";
94  return;
95  }
96 
97  auto Res = Counters.insert({CounterID, {0, -1}});
98  Res.first->second.second = CounterVal;
99  } else {
100  errs() << "DebugCounter Error: " << CounterPair.first
101  << " does not end with -skip or -count\n";
102  }
103 }
104 
106  OS << "Counters and values:\n";
107  for (const auto &KV : Counters)
108  OS << left_justify(RegisteredCounters[KV.first], 32) << ": {"
109  << KV.second.first << "," << KV.second.second << "}\n";
110 }
111 
113  print(dbgs());
114 }
FormattedString left_justify(StringRef Str, unsigned Width)
left_justify - append spaces after string so total output is Width characters.
Definition: Format.h:145
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_DUMP_METHOD void dump() const
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:449
raw_ostream & indent(unsigned NumSpaces)
indent - Insert &#39;NumSpaces&#39; spaces.
static DebugCounter & instance()
Returns a reference to the singleton instance.
Definition: BitVector.h:920
static ManagedStatic< DebugCounter > DC
This file provides an implementation of debug counters.
static DebugCounterList DebugCounterOption("debug-counter", cl::desc("Comma separated list of debug counter skip and count"), cl::CommaSeparated, cl::ZeroOrMore, cl::location(DebugCounter::instance()))
raw_ostream & outs()
This returns a reference to a raw_ostream for standard output.
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:727
This file declares helper objects for defining debug options that can be configured via the command l...
void print(raw_ostream &OS) const
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
void push_back(const std::string &)
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:61
LocationClass< Ty > location(Ty &L)
Definition: CommandLine.h:422