LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Debug.cpp

Go to the documentation of this file.
00001 //===-- Debug.cpp - An easy way to add debug output to your code ----------===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source 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/Debug.h"
00027 #include "llvm/Support/CommandLine.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   cl::opt<bool, true>
00037   Debug("debug", cl::desc("Enable debug output"), cl::Hidden,
00038         cl::location(DebugFlag));
00039 
00040   std::string CurrentDebugType;
00041   struct DebugOnlyOpt {
00042     void operator=(const std::string &Val) const {
00043       DebugFlag |= !Val.empty();
00044       CurrentDebugType = Val;
00045     }
00046   } DebugOnlyOptLoc;
00047 
00048   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 }