LLVM 22.0.0git
Debug.h
Go to the documentation of this file.
1//===- llvm/Support/Debug.h - Easy way to add debug output ------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements a handy way of adding debugging information to your
10// code, without it being enabled all of the time, and without having to add
11// command line options to enable it.
12//
13// In particular, just wrap your code with the LLVM_DEBUG() macro, and it will
14// be enabled automatically if you specify '-debug' on the command-line.
15// LLVM_DEBUG() requires the DEBUG_TYPE macro to be defined. Set it to "foo"
16// specify that your debug code belongs to class "foo". Be careful that you only
17// do this after including Debug.h and not around any #include of headers.
18// Headers should define and undef the macro around the code that needs to use
19// the LLVM_DEBUG() macro. Then, on the command line, you can specify
20// '-debug-only=foo' to enable JUST the debug information for the foo class.
21//
22// When compiling without assertions, the -debug-* options and all code in
23// LLVM_DEBUG() statements disappears, so it does not affect the runtime of the
24// code.
25//
26//===----------------------------------------------------------------------===//
27
28#ifndef LLVM_SUPPORT_DEBUG_H
29#define LLVM_SUPPORT_DEBUG_H
30
32
33namespace llvm {
34
35class raw_ostream;
36
37#ifndef NDEBUG
38
39/// isCurrentDebugType - Return true if the specified string is the debug type
40/// specified on the command line, or if none was specified on the command line
41/// with the -debug-only=X option.
42/// An optional level can be provided to control the verbosity of the output.
43/// If the provided level is not 0 and user specified a level below the provided
44/// level, return false.
45LLVM_ABI bool isCurrentDebugType(const char *Type, int Level = 0);
46
47/// Overload allowing to swap the order of the Type and Level arguments.
48LLVM_ABI inline bool isCurrentDebugType(int Level, const char *Type) {
49 return isCurrentDebugType(Type, Level);
50}
51
52/// setCurrentDebugType - Set the current debug type, as if the -debug-only=X
53/// option were specified. Note that DebugFlag also needs to be set to true for
54/// debug output to be produced.
55/// The debug type format is "type[:level]", where the level is an optional
56/// integer. If a level is provided, the debug output is enabled only if the
57/// user specified a level at least as high as the provided level.
58/// 0 is a special level that acts as an opt-out for this specific debug type
59/// without affecting the other debug output.
60LLVM_ABI void setCurrentDebugType(const char *Type);
61
62/// setCurrentDebugTypes - Set the current debug type, as if the
63/// -debug-only=X,Y,Z option were specified. Note that DebugFlag
64/// also needs to be set to true for debug output to be produced.
65///
66LLVM_ABI void setCurrentDebugTypes(const char **Types, unsigned Count);
67
68/// DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug
69/// information. If the '-debug' option is specified on the commandline, and if
70/// this is a debug build, then the code specified as the option to the macro
71/// will be executed. Otherwise it will not be. Example:
72///
73/// DEBUG_WITH_TYPE("bitset", dbgs() << "Bitset contains: " << Bitset << "\n");
74///
75/// This will emit the debug information if -debug is present, and -debug-only
76/// is not specified, or is specified as "bitset".
77#define DEBUG_WITH_TYPE(TYPE, ...) \
78 do { \
79 if (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE, 1)) { \
80 __VA_ARGS__; \
81 } \
82 } while (false)
83
84#else
85#define isCurrentDebugType(X) (false)
86#define setCurrentDebugType(X) do { (void)(X); } while (false)
87#define setCurrentDebugTypes(X, N) do { (void)(X); (void)(N); } while (false)
88#define DEBUG_WITH_TYPE(TYPE, ...) \
89 do { \
90 } while (false)
91#endif
92
93/// This boolean is set to true if the '-debug' command line option
94/// is specified. This should probably not be referenced directly, instead, use
95/// the DEBUG macro below.
96///
97LLVM_ABI extern bool DebugFlag;
98
99/// EnableDebugBuffering - This defaults to false. If true, the debug
100/// stream will install signal handlers to dump any buffered debug
101/// output. It allows clients to selectively allow the debug stream
102/// to install signal handlers if they are certain there will be no
103/// conflict.
104///
106
107/// dbgs() - This returns a reference to a raw_ostream for debugging
108/// messages. If debugging is disabled it returns errs(). Use it
109/// like: dbgs() << "foo" << "bar";
110LLVM_ABI raw_ostream &dbgs();
111
112// DEBUG macro - This macro should be used by passes to emit debug information.
113// If the '-debug' option is specified on the commandline, and if this is a
114// debug build, then the code specified as the option to the macro will be
115// executed. Otherwise it will not be. Example:
116//
117// LLVM_DEBUG(dbgs() << "Bitset contains: " << Bitset << "\n");
118//
119#define LLVM_DEBUG(...) DEBUG_WITH_TYPE(DEBUG_TYPE, __VA_ARGS__)
120
121} // end namespace llvm
122
123#endif // LLVM_SUPPORT_DEBUG_H
RelocType Type
Definition: COFFYAML.cpp:410
#define LLVM_ABI
Definition: Compiler.h:213
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI bool EnableDebugBuffering
EnableDebugBuffering - This defaults to false.
Definition: Debug.cpp:240
LLVM_ABI bool isCurrentDebugType(const char *Type, int Level=0)
isCurrentDebugType - Return true if the specified string is the debug type specified on the command l...
Definition: Debug.cpp:81
LLVM_ABI bool DebugFlag
This boolean is set to true if the '-debug' command line option is specified.
Definition: Debug.cpp:68
LLVM_ABI void setCurrentDebugTypes(const char **Types, unsigned Count)
setCurrentDebugTypes - Set the current debug type, as if the -debug-only=X,Y,Z option were specified.
Definition: Debug.cpp:111
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
LLVM_ABI void setCurrentDebugType(const char *Type)
setCurrentDebugType - Set the current debug type, as if the -debug-only=X option were specified.
Definition: Debug.cpp:107