LLVM  mainline
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 define the DEBUG_TYPE macro to "foo" specify
00017 // that your debug code belongs to class "foo". Be careful that you only do
00018 // this after including Debug.h and not around any #include of headers. Headers
00019 // should define and undef the macro acround the code that needs to use the
00020 // DEBUG() macro. Then, on the command line, you can specify '-debug-only=foo'
00021 // to enable JUST the debug information for the foo class.
00022 //
00023 // When compiling without assertions, the -debug-* options and all code in
00024 // DEBUG() statements disappears, so it does not affect the runtime of the code.
00025 //
00026 //===----------------------------------------------------------------------===//
00027 
00028 #ifndef LLVM_SUPPORT_DEBUG_H
00029 #define LLVM_SUPPORT_DEBUG_H
00030 
00031 namespace llvm {
00032 class raw_ostream;
00033 
00034 #ifndef NDEBUG
00035 /// DebugFlag - This boolean is set to true if the '-debug' command line option
00036 /// is specified.  This should probably not be referenced directly, instead, use
00037 /// the DEBUG macro below.
00038 ///
00039 extern bool DebugFlag;
00040 
00041 /// isCurrentDebugType - Return true if the specified string is the debug type
00042 /// specified on the command line, or if none was specified on the command line
00043 /// with the -debug-only=X option.
00044 ///
00045 bool isCurrentDebugType(const char *Type);
00046 
00047 /// setCurrentDebugType - Set the current debug type, as if the -debug-only=X
00048 /// option were specified.  Note that DebugFlag also needs to be set to true for
00049 /// debug output to be produced.
00050 ///
00051 void setCurrentDebugType(const char *Type);
00052 
00053 /// DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug
00054 /// information.  In the '-debug' option is specified on the commandline, and if
00055 /// this is a debug build, then the code specified as the option to the macro
00056 /// will be executed.  Otherwise it will not be.  Example:
00057 ///
00058 /// DEBUG_WITH_TYPE("bitset", dbgs() << "Bitset contains: " << Bitset << "\n");
00059 ///
00060 /// This will emit the debug information if -debug is present, and -debug-only
00061 /// is not specified, or is specified as "bitset".
00062 #define DEBUG_WITH_TYPE(TYPE, X)                                        \
00063   do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE)) { X; } \
00064   } while (0)
00065 
00066 #else
00067 #define isCurrentDebugType(X) (false)
00068 #define setCurrentDebugType(X)
00069 #define DEBUG_WITH_TYPE(TYPE, X) do { } while (0)
00070 #endif
00071 
00072 /// EnableDebugBuffering - This defaults to false.  If true, the debug
00073 /// stream will install signal handlers to dump any buffered debug
00074 /// output.  It allows clients to selectively allow the debug stream
00075 /// to install signal handlers if they are certain there will be no
00076 /// conflict.
00077 ///
00078 extern bool EnableDebugBuffering;
00079 
00080 /// dbgs() - This returns a reference to a raw_ostream for debugging
00081 /// messages.  If debugging is disabled it returns errs().  Use it
00082 /// like: dbgs() << "foo" << "bar";
00083 raw_ostream &dbgs();
00084 
00085 // DEBUG macro - This macro should be used by passes to emit debug information.
00086 // In the '-debug' option is specified on the commandline, and if this is a
00087 // debug build, then the code specified as the option to the macro will be
00088 // executed.  Otherwise it will not be.  Example:
00089 //
00090 // DEBUG(dbgs() << "Bitset contains: " << Bitset << "\n");
00091 //
00092 #define DEBUG(X) DEBUG_WITH_TYPE(DEBUG_TYPE, X)
00093 
00094 } // End llvm namespace
00095 
00096 #endif