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