Debug.cpp revision 198090
1193323Sed//===-- Debug.cpp - An easy way to add debug output to your code ----------===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This file implements a handle way of adding debugging information to your
11193323Sed// code, without it being enabled all of the time, and without having to add
12193323Sed// command line options to enable it.
13193323Sed//
14193323Sed// In particular, just wrap your code with the DEBUG() macro, and it will be
15193323Sed// enabled automatically if you specify '-debug' on the command-line.
16193323Sed// Alternatively, you can also use the SET_DEBUG_TYPE("foo") macro to specify
17193323Sed// that your debug code belongs to class "foo".  Then, on the command line, you
18193323Sed// can specify '-debug-only=foo' to enable JUST the debug information for the
19193323Sed// foo class.
20193323Sed//
21193323Sed// When compiling in release mode, the -debug-* options and all code in DEBUG()
22193323Sed// statements disappears, so it does not effect the runtime of the code.
23193323Sed//
24193323Sed//===----------------------------------------------------------------------===//
25193323Sed
26193323Sed#include "llvm/Support/CommandLine.h"
27193323Sed#include "llvm/Support/Debug.h"
28193323Sedusing namespace llvm;
29193323Sed
30198090Srdivacky// All Debug.h functionality is a no-op in NDEBUG mode.
31198090Srdivacky#ifndef NDEBUG
32193323Sedbool llvm::DebugFlag;  // DebugFlag - Exported boolean set by the -debug option
33193323Sed
34198090Srdivacky// -debug - Command line option to enable the DEBUG statements in the passes.
35198090Srdivacky// This flag may only be enabled in debug builds.
36198090Srdivackystatic cl::opt<bool, true>
37198090SrdivackyDebug("debug", cl::desc("Enable debug output"), cl::Hidden,
38198090Srdivacky      cl::location(DebugFlag));
39193323Sed
40198090Srdivackystatic std::string CurrentDebugType;
41198090Srdivackystatic struct DebugOnlyOpt {
42198090Srdivacky  void operator=(const std::string &Val) const {
43198090Srdivacky    DebugFlag |= !Val.empty();
44198090Srdivacky    CurrentDebugType = Val;
45198090Srdivacky  }
46198090Srdivacky} DebugOnlyOptLoc;
47193323Sed
48198090Srdivackystatic cl::opt<DebugOnlyOpt, true, cl::parser<std::string> >
49198090SrdivackyDebugOnly("debug-only", cl::desc("Enable a specific type of debug output"),
50198090Srdivacky          cl::Hidden, cl::value_desc("debug string"),
51198090Srdivacky          cl::location(DebugOnlyOptLoc), cl::ValueRequired);
52193323Sed
53193323Sed// isCurrentDebugType - Return true if the specified string is the debug type
54193323Sed// specified on the command line, or if none was specified on the command line
55193323Sed// with the -debug-only=X option.
56193323Sed//
57193323Sedbool llvm::isCurrentDebugType(const char *DebugType) {
58193323Sed  return CurrentDebugType.empty() || DebugType == CurrentDebugType;
59198090Srdivacky}
60193323Sed#else
61198090Srdivacky// Avoid "has no symbols" warning.
62198090Srdivackyint Debug_dummy = 0;
63193323Sed#endif
64