diff options
Diffstat (limited to 'llvm')
-rw-r--r-- | llvm/include/llvm/Support/Debug.h | 35 | ||||
-rw-r--r-- | llvm/lib/Support/Debug.cpp | 31 | ||||
-rw-r--r-- | llvm/unittests/Support/DebugTest.cpp | 25 |
3 files changed, 89 insertions, 2 deletions
diff --git a/llvm/include/llvm/Support/Debug.h b/llvm/include/llvm/Support/Debug.h index 924d7b2..6720ef5c 100644 --- a/llvm/include/llvm/Support/Debug.h +++ b/llvm/include/llvm/Support/Debug.h @@ -54,6 +54,28 @@ void setCurrentDebugType(const char *Type); /// void setCurrentDebugTypes(const char **Types, unsigned Count); +/// appendDebugType - Set the current debug type, as if the +/// -debug-only=CurX,CurY,NewZ option were specified when called with NewZ. +/// The previous state is tracked, so popAppendedDebugTypes can be called to +/// restore the previous state. Note that DebugFlag also needs to be set to true +/// for debug output to be produced. +/// +void appendDebugType(const char *Type); + +/// appendDebugTypes - Set the current debug type, as if the +/// -debug-only=CurX,CurY,NewX,NewY option were specified when called with +/// [NewX, NewY]. The previous state is tracked, so popAppendedDebugTypes can be +/// called to restore the previous state. Note that DebugFlag also needs to be +/// set to true debug output to be produced. +/// +void appendDebugTypes(const char **Types, unsigned Count); + +/// popAppendedDebugTypes - Restores CurDebugType to the state before the last +/// call to appendDebugType(s). Asserts and returns if the previous state was +/// empty or was reset by setCurrentDebugType(s). +/// +void popAppendedDebugTypes(); + /// DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug /// information. If the '-debug' option is specified on the commandline, and if /// this is a debug build, then the code specified as the option to the macro @@ -77,6 +99,19 @@ void setCurrentDebugTypes(const char **Types, unsigned Count); #define DEBUG_WITH_TYPE(TYPE, ...) \ do { \ } while (false) +#define appendDebugType(X) \ + do { \ + (void)(X); \ + } while (false) +#define appendDebugTypes(X, N) \ + do { \ + (void)(X); \ + (void)(N); \ + } while (false) +#define popAppendedDebugTypes() \ + do { \ + ; \ + } while (false) #endif /// This boolean is set to true if the '-debug' command line option diff --git a/llvm/lib/Support/Debug.cpp b/llvm/lib/Support/Debug.cpp index 5bb04d0..2582be7 100644 --- a/llvm/lib/Support/Debug.cpp +++ b/llvm/lib/Support/Debug.cpp @@ -35,6 +35,9 @@ #undef isCurrentDebugType #undef setCurrentDebugType #undef setCurrentDebugTypes +#undef appendDebugType +#undef appendDebugTypes +#undef popAppendedDebugTypes using namespace llvm; @@ -45,6 +48,32 @@ namespace llvm { bool DebugFlag = false; static ManagedStatic<std::vector<std::string>> CurrentDebugType; +static ManagedStatic<std::vector<int>> AppendedDebugTypeSizes; + +/// Appends to the CurrentDebugState by keeping its old state and adding the +/// new state. +void appendDebugTypes(const char **Types, unsigned Count) { + AppendedDebugTypeSizes->push_back(CurrentDebugType->size()); + for (size_t T = 0; T < Count; ++T) + CurrentDebugType->push_back(Types[T]); +} + +/// Appends to the CurrentDebugState by keeping its old state and adding the +/// new state. +void appendDebugType(const char *Type) { appendDebugTypes(&Type, 1); } + +/// Restore to the state before the latest call to appendDebugTypes. This can be +/// done multiple times. +void popAppendedDebugTypes() { + assert(AppendedDebugTypeSizes->size() > 0 && + "Popping from DebugTypes without any previous appending."); + + if (!AppendedDebugTypeSizes->size()) + return; + + CurrentDebugType->resize(AppendedDebugTypeSizes->back()); + AppendedDebugTypeSizes->pop_back(); +} /// Return true if the specified string is the debug type /// specified on the command line, or if none was specified on the command line @@ -72,6 +101,8 @@ void setCurrentDebugType(const char *Type) { } void setCurrentDebugTypes(const char **Types, unsigned Count) { + assert(AppendedDebugTypeSizes->size() == 0 && + "Resetting CurrentDebugType when it was previously appended to"); CurrentDebugType->clear(); llvm::append_range(*CurrentDebugType, ArrayRef(Types, Count)); } diff --git a/llvm/unittests/Support/DebugTest.cpp b/llvm/unittests/Support/DebugTest.cpp index e8b7548..3874142 100644 --- a/llvm/unittests/Support/DebugTest.cpp +++ b/llvm/unittests/Support/DebugTest.cpp @@ -19,8 +19,8 @@ using namespace llvm; TEST(DebugTest, Basic) { std::string s1, s2; raw_string_ostream os1(s1), os2(s2); - static const char *DT[] = {"A", "B"}; - + static const char *DT[] = {"A", "B"}; + llvm::DebugFlag = true; setCurrentDebugTypes(DT, 2); DEBUG_WITH_TYPE("A", os1 << "A"); @@ -50,4 +50,25 @@ TEST(DebugTest, CommaInDebugBlock) { }); EXPECT_EQ("ZYX", os1.str()); } + +TEST(DebugTest, AppendAndPop) { + std::string s1, s2, s3; + raw_string_ostream os1(s1), os2(s2), os3(s3); + + llvm::DebugFlag = true; + appendDebugType("A"); + DEBUG_WITH_TYPE("A", os1 << "A"); + DEBUG_WITH_TYPE("B", os1 << "B"); + EXPECT_EQ("A", os1.str()); + + appendDebugType("B"); + DEBUG_WITH_TYPE("A", os2 << "A"); + DEBUG_WITH_TYPE("B", os2 << "B"); + EXPECT_EQ("AB", os2.str()); + + popAppendedDebugTypes(); + DEBUG_WITH_TYPE("A", os3 << "A"); + DEBUG_WITH_TYPE("B", os3 << "B"); + EXPECT_EQ("A", os3.str()); +} #endif |