Debug.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/Support/Debug.h - Easy way to add debug output ------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file implements a handy way of adding debugging information to your
  15. // code, without it being enabled all of the time, and without having to add
  16. // command line options to enable it.
  17. //
  18. // In particular, just wrap your code with the LLVM_DEBUG() macro, and it will
  19. // be enabled automatically if you specify '-debug' on the command-line.
  20. // LLVM_DEBUG() requires the DEBUG_TYPE macro to be defined. Set it to "foo"
  21. // specify that your debug code belongs to class "foo". Be careful that you only
  22. // do this after including Debug.h and not around any #include of headers.
  23. // Headers should define and undef the macro acround the code that needs to use
  24. // the LLVM_DEBUG() macro. Then, on the command line, you can specify
  25. // '-debug-only=foo' to enable JUST the debug information for the foo class.
  26. //
  27. // When compiling without assertions, the -debug-* options and all code in
  28. // LLVM_DEBUG() statements disappears, so it does not affect the runtime of the
  29. // code.
  30. //
  31. //===----------------------------------------------------------------------===//
  32. #ifndef LLVM_SUPPORT_DEBUG_H
  33. #define LLVM_SUPPORT_DEBUG_H
  34. namespace llvm {
  35. class raw_ostream;
  36. #ifndef NDEBUG
  37. /// isCurrentDebugType - Return true if the specified string is the debug type
  38. /// specified on the command line, or if none was specified on the command line
  39. /// with the -debug-only=X option.
  40. ///
  41. bool isCurrentDebugType(const char *Type);
  42. /// setCurrentDebugType - Set the current debug type, as if the -debug-only=X
  43. /// option were specified. Note that DebugFlag also needs to be set to true for
  44. /// debug output to be produced.
  45. ///
  46. void setCurrentDebugType(const char *Type);
  47. /// setCurrentDebugTypes - Set the current debug type, as if the
  48. /// -debug-only=X,Y,Z option were specified. Note that DebugFlag
  49. /// also needs to be set to true for debug output to be produced.
  50. ///
  51. void setCurrentDebugTypes(const char **Types, unsigned Count);
  52. /// DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug
  53. /// information. In the '-debug' option is specified on the commandline, and if
  54. /// this is a debug build, then the code specified as the option to the macro
  55. /// will be executed. Otherwise it will not be. Example:
  56. ///
  57. /// DEBUG_WITH_TYPE("bitset", dbgs() << "Bitset contains: " << Bitset << "\n");
  58. ///
  59. /// This will emit the debug information if -debug is present, and -debug-only
  60. /// is not specified, or is specified as "bitset".
  61. #define DEBUG_WITH_TYPE(TYPE, X) \
  62. do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE)) { X; } \
  63. } while (false)
  64. #else
  65. #define isCurrentDebugType(X) (false)
  66. #define setCurrentDebugType(X)
  67. #define setCurrentDebugTypes(X, N)
  68. #define DEBUG_WITH_TYPE(TYPE, X) do { } while (false)
  69. #endif
  70. /// This boolean is set to true if the '-debug' command line option
  71. /// is specified. This should probably not be referenced directly, instead, use
  72. /// the DEBUG macro below.
  73. ///
  74. extern bool DebugFlag;
  75. /// EnableDebugBuffering - This defaults to false. If true, the debug
  76. /// stream will install signal handlers to dump any buffered debug
  77. /// output. It allows clients to selectively allow the debug stream
  78. /// to install signal handlers if they are certain there will be no
  79. /// conflict.
  80. ///
  81. extern bool EnableDebugBuffering;
  82. /// dbgs() - This returns a reference to a raw_ostream for debugging
  83. /// messages. If debugging is disabled it returns errs(). Use it
  84. /// like: dbgs() << "foo" << "bar";
  85. raw_ostream &dbgs();
  86. // DEBUG macro - This macro should be used by passes to emit debug information.
  87. // In the '-debug' option is specified on the commandline, and if this is a
  88. // debug build, then the code specified as the option to the macro will be
  89. // executed. Otherwise it will not be. Example:
  90. //
  91. // LLVM_DEBUG(dbgs() << "Bitset contains: " << Bitset << "\n");
  92. //
  93. #define LLVM_DEBUG(X) DEBUG_WITH_TYPE(DEBUG_TYPE, X)
  94. } // end namespace llvm
  95. #endif // LLVM_SUPPORT_DEBUG_H
  96. #ifdef __GNUC__
  97. #pragma GCC diagnostic pop
  98. #endif