Chris Lattner | 2dd93ed | 2003-08-01 22:12:40 +0000 | [diff] [blame^] | 1 | //===- Debug.h - An easy way to add debug output to your code ---*- C++ -*-===// |
| 2 | // |
| 3 | // This file implements a handle way of adding debugging information to your |
| 4 | // code, without it being enabled all of the time, and without having to add |
| 5 | // command line options to enable it. |
| 6 | // |
| 7 | // In particular, just wrap your code with the DEBUG() macro, and it will be |
| 8 | // enabled automatically if you specify '-debug' on the command-line. |
| 9 | // Alternatively, you can also use the SET_DEBUG_TYPE("foo") macro to specify |
| 10 | // that your debug code belongs to class "foo". Then, on the command line, you |
| 11 | // can specify '-debug-only=foo' to enable JUST the debug information for the |
| 12 | // foo class. |
| 13 | // |
| 14 | // When compiling in release mode, the -debug-* options and all code in DEBUG() |
| 15 | // statements disappears, so it does not effect the runtime of the code. |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #ifndef SUPPORT_DEBUG_H |
| 20 | #define SUPPORT_DEBUG_H |
| 21 | |
| 22 | // DebugFlag - This boolean is set to true if the '-debug' command line option |
| 23 | // is specified. This should probably not be referenced directly, instead, use |
| 24 | // the DEBUG macro below. |
| 25 | // |
| 26 | extern bool DebugFlag; |
| 27 | |
| 28 | // isCurrentDebugType - Return true if the specified string is the debug type |
| 29 | // specified on the command line, or if none was specified on the command line |
| 30 | // with the -debug-only=X option. |
| 31 | // |
| 32 | bool isCurrentDebugType(const char *Type); |
| 33 | |
| 34 | // DEBUG macro - This macro should be used by passes to emit debug information. |
| 35 | // In the '-debug' option is specified on the commandline, and if this is a |
| 36 | // debug build, then the code specified as the option to the macro will be |
| 37 | // executed. Otherwise it will not be. Example: |
| 38 | // |
| 39 | // DEBUG(cerr << "Bitset contains: " << Bitset << "\n"); |
| 40 | // |
| 41 | |
| 42 | #ifndef DEBUG_TYPE |
| 43 | #define DEBUG_TYPE "" |
| 44 | #endif |
| 45 | |
| 46 | #ifdef NDEBUG |
| 47 | #define DEBUG(X) |
| 48 | #else |
| 49 | #define DEBUG(X) \ |
| 50 | do { if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) { X; } } while (0) |
| 51 | #endif |
| 52 | |
| 53 | #endif |