Skip to content

Commit b814f8a

Browse files
committed
[Support] Accept Twine in debugHeader
Accept `Twine`s in `circt::debugHeader`, which makes using the function a bit easier. Also, rely on the `raw_ostream`'s buffer location tracking to figure out how many padding characters need to be added.
1 parent 441c015 commit b814f8a

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

include/circt/Support/Debug.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace circt {
2525
///
2626
/// This is commonly used for generating a header in debug information. The
2727
/// format is modeled after LLVM/MLIR/CIRCT source file headers.
28-
llvm::raw_ostream &debugHeader(llvm::StringRef str, int width = 80);
28+
llvm::raw_ostream &debugHeader(const llvm::Twine &str, unsigned width = 80);
2929

3030
/// Write a boilerplate header for a pass to the debug stream. This generates
3131
/// output like the following if the pass's name is "FooPass":
@@ -34,13 +34,13 @@ llvm::raw_ostream &debugHeader(llvm::StringRef str, int width = 80);
3434
///
3535
/// This is commonly used to generate a header in debug when a pass starts
3636
/// running.
37-
llvm::raw_ostream &debugPassHeader(const mlir::Pass *pass, int width = 80);
37+
llvm::raw_ostream &debugPassHeader(const mlir::Pass *pass, unsigned width = 80);
3838

3939
/// Write a boilerplate footer to the debug stream to indicate that a pass has
4040
/// ended. This produces text like the following:
4141
///
4242
/// ===-----------------------------------===
43-
llvm::raw_ostream &debugFooter(int width = 80);
43+
llvm::raw_ostream &debugFooter(unsigned width = 80);
4444

4545
} // namespace circt
4646

lib/Support/Debug.cpp

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===- Debug.cpp - Debug Utilities ------------------------------*- C++ -*-===//
1+
//===----------------------------------------------------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -11,39 +11,38 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "circt/Support/Debug.h"
14-
1514
#include "llvm/ADT/Twine.h"
1615
#include "llvm/Support/Debug.h"
1716

18-
llvm::raw_ostream &circt::debugHeader(llvm::StringRef str, int width) {
19-
auto &dbgStream = llvm::dbgs();
20-
dbgStream << "===- " << str << " ";
21-
width -= 6 + str.size();
22-
if (width > 3) {
23-
dbgStream << std::string(width - 3, '-');
24-
width = 3;
17+
llvm::raw_ostream &circt::debugHeader(const llvm::Twine &str, unsigned width) {
18+
auto &os = llvm::dbgs();
19+
auto start = os.tell();
20+
os << "===- " << str << " ";
21+
auto endAct = os.tell() + 4; // 4 for "-==="
22+
auto endExp = start + width;
23+
while (endAct < endExp) {
24+
os << '-';
25+
++endAct;
2526
}
26-
if (width > 0)
27-
dbgStream << std::string(width, '=');
28-
return dbgStream;
27+
os << "-===";
28+
return os;
2929
}
3030

31-
llvm::raw_ostream &circt::debugPassHeader(const mlir::Pass *pass, int width) {
32-
return debugHeader((llvm::Twine("Running ") + pass->getName()).str());
31+
llvm::raw_ostream &circt::debugPassHeader(const mlir::Pass *pass,
32+
unsigned width) {
33+
return debugHeader(llvm::Twine("Running ") + pass->getName());
3334
}
3435

35-
llvm::raw_ostream &circt::debugFooter(int width) {
36-
auto &dbgStream = llvm::dbgs();
37-
if (width > 3) {
38-
int startWidth = std::min(width - 3, 3);
39-
dbgStream << std::string(startWidth, '=');
40-
width -= startWidth;
41-
}
42-
if (width > 3) {
43-
dbgStream << std::string(width - 3, '-');
44-
width = 3;
36+
llvm::raw_ostream &circt::debugFooter(unsigned width) {
37+
auto &os = llvm::dbgs();
38+
auto start = os.tell();
39+
os << "===";
40+
auto endAct = os.tell() + 3; // 3 for trailing "==="
41+
auto endExp = start + width;
42+
while (endAct < endExp) {
43+
os << '-';
44+
++endAct;
4545
}
46-
if (width > 0)
47-
dbgStream << std::string(width, '=');
48-
return dbgStream;
46+
os << "===";
47+
return os;
4948
}

0 commit comments

Comments
 (0)