LLVM API Documentation
00001 //===-- Debug.cpp - An easy way to add debug output to your code ----------===// 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 handle 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 in release mode, the -debug-* options and all code in DEBUG() 00022 // statements disappears, so it does not effect the runtime of the code. 00023 // 00024 //===----------------------------------------------------------------------===// 00025 00026 #include "llvm/Support/CommandLine.h" 00027 #include "llvm/Support/Debug.h" 00028 using namespace llvm; 00029 00030 bool llvm::DebugFlag; // DebugFlag - Exported boolean set by the -debug option 00031 00032 namespace { 00033 #ifndef NDEBUG 00034 // -debug - Command line option to enable the DEBUG statements in the passes. 00035 // This flag may only be enabled in debug builds. 00036 static cl::opt<bool, true> 00037 Debug("debug", cl::desc("Enable debug output"), cl::Hidden, 00038 cl::location(DebugFlag)); 00039 00040 static std::string CurrentDebugType; 00041 static struct DebugOnlyOpt { 00042 void operator=(const std::string &Val) const { 00043 DebugFlag |= !Val.empty(); 00044 CurrentDebugType = Val; 00045 } 00046 } DebugOnlyOptLoc; 00047 00048 static cl::opt<DebugOnlyOpt, true, cl::parser<std::string> > 00049 DebugOnly("debug-only", cl::desc("Enable a specific type of debug output"), 00050 cl::Hidden, cl::value_desc("debug string"), 00051 cl::location(DebugOnlyOptLoc), cl::ValueRequired); 00052 #endif 00053 } 00054 00055 // isCurrentDebugType - Return true if the specified string is the debug type 00056 // specified on the command line, or if none was specified on the command line 00057 // with the -debug-only=X option. 00058 // 00059 bool llvm::isCurrentDebugType(const char *DebugType) { 00060 #ifndef NDEBUG 00061 return CurrentDebugType.empty() || DebugType == CurrentDebugType; 00062 #else 00063 return false; 00064 #endif 00065 } 00066 00067 // getErrorOutputStream - Returns the error output stream (std::cerr). This 00068 // places the std::c* I/O streams into one .cpp file and relieves the whole 00069 // program from having to have hundreds of static c'tor/d'tors for them. 00070 // 00071 OStream &llvm::getErrorOutputStream(const char *DebugType) { 00072 static OStream cnoout(0); 00073 if (DebugFlag && isCurrentDebugType(DebugType)) 00074 return cerr; 00075 else 00076 return cnoout; 00077 }