LLVM API Documentation
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 effect 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/Streams.h" 00030 00031 namespace llvm { 00032 00033 // DebugFlag - This boolean is set to true if the '-debug' command line option 00034 // is specified. This should probably not be referenced directly, instead, use 00035 // the DEBUG macro below. 00036 // 00037 extern bool DebugFlag; 00038 00039 // isCurrentDebugType - Return true if the specified string is the debug type 00040 // specified on the command line, or if none was specified on the command line 00041 // with the -debug-only=X option. 00042 // 00043 bool isCurrentDebugType(const char *Type); 00044 00045 // DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug 00046 // information. In the '-debug' option is specified on the commandline, and if 00047 // this is a debug build, then the code specified as the option to the macro 00048 // will be executed. Otherwise it will not be. Example: 00049 // 00050 // DEBUG_WITH_TYPE("bitset", errs() << "Bitset contains: " << Bitset << "\n"); 00051 // 00052 // This will emit the debug information if -debug is present, and -debug-only is 00053 // not specified, or is specified as "bitset". 00054 00055 #ifdef NDEBUG 00056 #define DEBUG_WITH_TYPE(TYPE, X) do { } while (0) 00057 #else 00058 #define DEBUG_WITH_TYPE(TYPE, X) \ 00059 do { if (DebugFlag && isCurrentDebugType(TYPE)) { X; } } while (0) 00060 #endif 00061 00062 // DEBUG macro - This macro should be used by passes to emit debug information. 00063 // In the '-debug' option is specified on the commandline, and if this is a 00064 // debug build, then the code specified as the option to the macro will be 00065 // executed. Otherwise it will not be. Example: 00066 // 00067 // DEBUG(errs() << "Bitset contains: " << Bitset << "\n"); 00068 // 00069 00070 #ifndef DEBUG_TYPE 00071 #define DEBUG_TYPE "" 00072 #endif 00073 00074 #define DEBUG(X) DEBUG_WITH_TYPE(DEBUG_TYPE, X) 00075 00076 /// getNullOutputStream - Return a null string that does not output 00077 /// anything. This hides the static variable from other modules. 00078 /// 00079 OStream &getNullOutputStream(); 00080 00081 /// getErrorOutputStream - Returns the error output stream (std::cerr). This 00082 /// places the std::c* I/O streams into one .cpp file and relieves the whole 00083 /// program from having to have hundreds of static c'tor/d'tors for them. 00084 /// 00085 OStream &getErrorOutputStream(const char *DebugType); 00086 00087 #ifdef NDEBUG 00088 #define DOUT llvm::getNullOutputStream() 00089 #else 00090 #define DOUT llvm::getErrorOutputStream(DEBUG_TYPE) 00091 #endif 00092 00093 } // End llvm namespace 00094 00095 #endif