LLVM API Documentation

Debug.h
Go to the documentation of this file.
00001 //===- llvm/Support/Debug.h - Easy way to add debug output ------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements a handy way of adding debugging information to your
00011 // code, without it being enabled all of the time, and without having to add
00012 // command line options to enable it.
00013 //
00014 // In particular, just wrap your code with the DEBUG() macro, and it will be
00015 // enabled automatically if you specify '-debug' on the command-line.
00016 // Alternatively, you can also use the SET_DEBUG_TYPE("foo") macro to specify
00017 // that your debug code belongs to class "foo".  Then, on the command line, you
00018 // can specify '-debug-only=foo' to enable JUST the debug information for the
00019 // foo class.
00020 //
00021 // When compiling without assertions, the -debug-* options and all code in
00022 // DEBUG() statements disappears, so it does not affect the runtime of the code.
00023 //
00024 //===----------------------------------------------------------------------===//
00025 
00026 #ifndef LLVM_SUPPORT_DEBUG_H
00027 #define LLVM_SUPPORT_DEBUG_H
00028 
00029 #include "llvm/Support/raw_ostream.h"
00030 
00031 namespace llvm {
00032 
00033 /// DEBUG_TYPE macro - Files can specify a DEBUG_TYPE as a string, which causes
00034 /// all of their DEBUG statements to be activatable with -debug-only=thatstring.
00035 #ifndef DEBUG_TYPE
00036 #define DEBUG_TYPE ""
00037 #endif
00038 
00039 #ifndef NDEBUG
00040 /// DebugFlag - This boolean is set to true if the '-debug' command line option
00041 /// is specified.  This should probably not be referenced directly, instead, use
00042 /// the DEBUG macro below.
00043 ///
00044 extern bool DebugFlag;
00045 
00046 /// isCurrentDebugType - Return true if the specified string is the debug type
00047 /// specified on the command line, or if none was specified on the command line
00048 /// with the -debug-only=X option.
00049 ///
00050 bool isCurrentDebugType(const char *Type);
00051 
00052 /// setCurrentDebugType - Set the current debug type, as if the -debug-only=X
00053 /// option were specified.  Note that DebugFlag also needs to be set to true for
00054 /// debug output to be produced.
00055 ///
00056 void setCurrentDebugType(const char *Type);
00057 
00058 /// DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug
00059 /// information.  In the '-debug' option is specified on the commandline, and if
00060 /// this is a debug build, then the code specified as the option to the macro
00061 /// will be executed.  Otherwise it will not be.  Example:
00062 ///
00063 /// DEBUG_WITH_TYPE("bitset", dbgs() << "Bitset contains: " << Bitset << "\n");
00064 ///
00065 /// This will emit the debug information if -debug is present, and -debug-only
00066 /// is not specified, or is specified as "bitset".
00067 #define DEBUG_WITH_TYPE(TYPE, X)                                        \
00068   do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE)) { X; } \
00069   } while (0)
00070 
00071 #else
00072 #define isCurrentDebugType(X) (false)
00073 #define setCurrentDebugType(X)
00074 #define DEBUG_WITH_TYPE(TYPE, X) do { } while (0)
00075 #endif
00076 
00077 /// EnableDebugBuffering - This defaults to false.  If true, the debug
00078 /// stream will install signal handlers to dump any buffered debug
00079 /// output.  It allows clients to selectively allow the debug stream
00080 /// to install signal handlers if they are certain there will be no
00081 /// conflict.
00082 ///
00083 extern bool EnableDebugBuffering;
00084 
00085 /// dbgs() - This returns a reference to a raw_ostream for debugging
00086 /// messages.  If debugging is disabled it returns errs().  Use it
00087 /// like: dbgs() << "foo" << "bar";
00088 raw_ostream &dbgs();
00089 
00090 // DEBUG macro - This macro should be used by passes to emit debug information.
00091 // In the '-debug' option is specified on the commandline, and if this is a
00092 // debug build, then the code specified as the option to the macro will be
00093 // executed.  Otherwise it will not be.  Example:
00094 //
00095 // DEBUG(dbgs() << "Bitset contains: " << Bitset << "\n");
00096 //
00097 #define DEBUG(X) DEBUG_WITH_TYPE(DEBUG_TYPE, X)
00098 
00099 } // End llvm namespace
00100 
00101 #endif