-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[llvm][support] Add LDBG macro. #143704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[llvm][support] Add LDBG macro. #143704
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a4aff45
[llvm][support] Add LLVM_DLOG macro.
jpienaar a501b1c
Fix formatting
jpienaar 41fb4e9
Fix cmake
jpienaar 6b63244
Update to balance brackets and comment
jpienaar 3c1b1ca
Merge branch 'main' into dlog
jpienaar d6a36df
Update llvm/unittests/Support/DebugLogTest.cpp
jpienaar 8e63bc5
Remove varargs that was c++20 feature, renamed to LDBG to match other…
jpienaar 386243e
Add test for opt mode
jpienaar c5a23ce
Fix formatting
jpienaar fe7e9b5
Remove variadic in ndebug side too
jpienaar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| //===- llvm/Support/DebugLog.h - Logging like debug output ------*- C++ -*-===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // This file contains macros for logging like debug output. It builds upon the | ||
| // support in Debug.h but provides a utility function for common debug output | ||
| // style. | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_SUPPORT_DEBUGLOG_H | ||
| #define LLVM_SUPPORT_DEBUGLOG_H | ||
|
|
||
| #include "llvm/Support/Debug.h" | ||
| #include "llvm/Support/raw_ostream.h" | ||
|
|
||
| namespace llvm { | ||
| #ifndef NDEBUG | ||
|
|
||
| // Output with given inputs and trailing newline. E.g., | ||
| // LDBG() << "Bitset contains: " << Bitset; | ||
| // is equivalent to | ||
| // LLVM_DEBUG(dbgs() << DEBUG_TYPE << " [" << __FILE__ << ":" << __LINE__ | ||
| // << "] " << "Bitset contains: " << Bitset << "\n"); | ||
jpienaar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #define LDBG() DEBUGLOG_WITH_STREAM_AND_TYPE(llvm::dbgs(), DEBUG_TYPE) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we prefix this with |
||
|
|
||
| #define DEBUGLOG_WITH_STREAM_AND_TYPE(STREAM, TYPE) \ | ||
| for (bool _c = (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE)); _c; \ | ||
| _c = false) \ | ||
| ::llvm::impl::LogWithNewline(TYPE, __FILE__, __LINE__, (STREAM)) | ||
|
|
||
| namespace impl { | ||
| class LogWithNewline { | ||
| public: | ||
| LogWithNewline(const char *debug_type, const char *file, int line, | ||
| raw_ostream &os) | ||
| : os(os) { | ||
| if (debug_type) | ||
| os << debug_type << " "; | ||
| os << "[" << file << ":" << line << "] "; | ||
| } | ||
| ~LogWithNewline() { os << '\n'; } | ||
| template <typename T> raw_ostream &operator<<(const T &t) && { | ||
| return os << t; | ||
| } | ||
|
|
||
| // Prevent copying, as this class manages newline responsibility and is | ||
| // intended for use as a temporary. | ||
| LogWithNewline(const LogWithNewline &) = delete; | ||
| LogWithNewline &operator=(const LogWithNewline &) = delete; | ||
| LogWithNewline &operator=(LogWithNewline &&) = delete; | ||
|
|
||
| private: | ||
| raw_ostream &os; | ||
| }; | ||
| } // end namespace impl | ||
| #else | ||
| // As others in Debug, When compiling without assertions, the -debug-* options | ||
| // and all inputs too LDBG() are ignored. | ||
| #define LDBG() \ | ||
| for (bool _c = false; _c; _c = false) \ | ||
| ::llvm::nulls() | ||
| #endif | ||
| } // end namespace llvm | ||
|
|
||
| #endif // LLVM_SUPPORT_DEBUGLOG_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| //===- llvm/unittest/Support/DebugLogTest.cpp -----------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "llvm/Support/DebugLog.h" | ||
| #include "llvm/Support/raw_ostream.h" | ||
| #include "gmock/gmock.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| #include <string> | ||
| using namespace llvm; | ||
| using testing::Eq; | ||
| using testing::HasSubstr; | ||
|
|
||
| #ifndef NDEBUG | ||
jpienaar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| TEST(DebugLogTest, Basic) { | ||
| llvm::DebugFlag = true; | ||
| static const char *DT[] = {"A", "B"}; | ||
|
|
||
| // Clear debug types. | ||
| setCurrentDebugTypes(DT, 0); | ||
| { | ||
| std::string str; | ||
| raw_string_ostream os(str); | ||
| DEBUGLOG_WITH_STREAM_AND_TYPE(os, nullptr) << "NoType"; | ||
| EXPECT_TRUE(StringRef(os.str()).starts_with('[')); | ||
| EXPECT_TRUE(StringRef(os.str()).ends_with("NoType\n")); | ||
| } | ||
|
|
||
| setCurrentDebugTypes(DT, 2); | ||
jpienaar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| std::string str; | ||
| raw_string_ostream os(str); | ||
| DEBUGLOG_WITH_STREAM_AND_TYPE(os, "A") << "A"; | ||
| DEBUGLOG_WITH_STREAM_AND_TYPE(os, "B") << "B"; | ||
| EXPECT_THAT(os.str(), AllOf(HasSubstr("A\n"), HasSubstr("B\n"))); | ||
| } | ||
|
|
||
| setCurrentDebugType("A"); | ||
| { | ||
| std::string str; | ||
| raw_string_ostream os(str); | ||
| // Just check that the macro doesn't result in dangling else. | ||
| if (true) | ||
| DEBUGLOG_WITH_STREAM_AND_TYPE(os, "A") << "A"; | ||
| else | ||
| DEBUGLOG_WITH_STREAM_AND_TYPE(os, "A") << "B"; | ||
| DEBUGLOG_WITH_STREAM_AND_TYPE(os, "B") << "B"; | ||
| EXPECT_THAT(os.str(), AllOf(HasSubstr("A\n"), Not(HasSubstr("B\n")))); | ||
|
|
||
| int count = 0; | ||
| auto inc = [&]() { return ++count; }; | ||
| EXPECT_THAT(count, Eq(0)); | ||
| DEBUGLOG_WITH_STREAM_AND_TYPE(os, "A") << inc(); | ||
| EXPECT_THAT(count, Eq(1)); | ||
| DEBUGLOG_WITH_STREAM_AND_TYPE(os, "B") << inc(); | ||
| EXPECT_THAT(count, Eq(1)); | ||
| } | ||
| } | ||
| #else | ||
| TEST(DebugLogTest, Basic) { | ||
| // LDBG should be compiled out in NDEBUG, so just check it compiles and has | ||
| // no effect. | ||
| llvm::DebugFlag = true; | ||
| static const char *DT[] = {}; | ||
| setCurrentDebugTypes(DT, 0); | ||
| int count = 0; | ||
| auto inc = [&]() { return ++count; }; | ||
| EXPECT_THAT(count, Eq(0)); | ||
| LDBG() << inc(); | ||
| EXPECT_THAT(count, Eq(0)); | ||
| } | ||
| #endif | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you considered making this part of the Debug.h header directly?