1193323Sed//===- llvm/Support/Debug.h - Easy way to add debug output ------*- C++ -*-===//
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 handy 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//
21198090Srdivacky// When compiling without assertions, the -debug-* options and all code in
22245431Sdim// DEBUG() statements disappears, so it does not affect the runtime of the code.
23193323Sed//
24193323Sed//===----------------------------------------------------------------------===//
25193323Sed
26193323Sed#ifndef LLVM_SUPPORT_DEBUG_H
27193323Sed#define LLVM_SUPPORT_DEBUG_H
28193323Sed
29263509Sdim#include "llvm/Support/raw_ostream.h"
30263509Sdim
31193323Sednamespace llvm {
32193323Sed
33198892Srdivacky/// DEBUG_TYPE macro - Files can specify a DEBUG_TYPE as a string, which causes
34198892Srdivacky/// all of their DEBUG statements to be activatable with -debug-only=thatstring.
35198892Srdivacky#ifndef DEBUG_TYPE
36198892Srdivacky#define DEBUG_TYPE ""
37198892Srdivacky#endif
38235633Sdim
39198090Srdivacky#ifndef NDEBUG
40198892Srdivacky/// DebugFlag - This boolean is set to true if the '-debug' command line option
41198892Srdivacky/// is specified.  This should probably not be referenced directly, instead, use
42198892Srdivacky/// the DEBUG macro below.
43198892Srdivacky///
44193323Sedextern bool DebugFlag;
45235633Sdim
46198892Srdivacky/// isCurrentDebugType - Return true if the specified string is the debug type
47198892Srdivacky/// specified on the command line, or if none was specified on the command line
48198892Srdivacky/// with the -debug-only=X option.
49198892Srdivacky///
50198892Srdivackybool isCurrentDebugType(const char *Type);
51193323Sed
52245431Sdim/// setCurrentDebugType - Set the current debug type, as if the -debug-only=X
53198892Srdivacky/// option were specified.  Note that DebugFlag also needs to be set to true for
54198892Srdivacky/// debug output to be produced.
55198892Srdivacky///
56245431Sdimvoid setCurrentDebugType(const char *Type);
57235633Sdim
58198892Srdivacky/// DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug
59198892Srdivacky/// information.  In the '-debug' option is specified on the commandline, and if
60198892Srdivacky/// this is a debug build, then the code specified as the option to the macro
61198892Srdivacky/// will be executed.  Otherwise it will not be.  Example:
62198892Srdivacky///
63201360Srdivacky/// DEBUG_WITH_TYPE("bitset", dbgs() << "Bitset contains: " << Bitset << "\n");
64198892Srdivacky///
65198892Srdivacky/// This will emit the debug information if -debug is present, and -debug-only
66198892Srdivacky/// is not specified, or is specified as "bitset".
67198892Srdivacky#define DEBUG_WITH_TYPE(TYPE, X)                                        \
68200581Srdivacky  do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE)) { X; } \
69200581Srdivacky  } while (0)
70198892Srdivacky
71198090Srdivacky#else
72198090Srdivacky#define isCurrentDebugType(X) (false)
73245431Sdim#define setCurrentDebugType(X)
74198090Srdivacky#define DEBUG_WITH_TYPE(TYPE, X) do { } while (0)
75198090Srdivacky#endif
76198090Srdivacky
77201360Srdivacky/// EnableDebugBuffering - This defaults to false.  If true, the debug
78201360Srdivacky/// stream will install signal handlers to dump any buffered debug
79201360Srdivacky/// output.  It allows clients to selectively allow the debug stream
80201360Srdivacky/// to install signal handlers if they are certain there will be no
81201360Srdivacky/// conflict.
82201360Srdivacky///
83201360Srdivackyextern bool EnableDebugBuffering;
84201360Srdivacky
85201360Srdivacky/// dbgs() - This returns a reference to a raw_ostream for debugging
86201360Srdivacky/// messages.  If debugging is disabled it returns errs().  Use it
87201360Srdivacky/// like: dbgs() << "foo" << "bar";
88201360Srdivackyraw_ostream &dbgs();
89201360Srdivacky
90193323Sed// DEBUG macro - This macro should be used by passes to emit debug information.
91193323Sed// In the '-debug' option is specified on the commandline, and if this is a
92193323Sed// debug build, then the code specified as the option to the macro will be
93193323Sed// executed.  Otherwise it will not be.  Example:
94193323Sed//
95201360Srdivacky// DEBUG(dbgs() << "Bitset contains: " << Bitset << "\n");
96193323Sed//
97198090Srdivacky#define DEBUG(X) DEBUG_WITH_TYPE(DEBUG_TYPE, X)
98201360Srdivacky
99193323Sed} // End llvm namespace
100193323Sed
101193323Sed#endif
102