Skip to content

Commit a4accdf

Browse files
authored
[Support] Add option to print SMDiagnostic into a buffer without the filename and location info (#92050)
1 parent 8070b2d commit a4accdf

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

llvm/include/llvm/Support/SourceMgr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ class SMDiagnostic {
317317
ArrayRef<SMFixIt> getFixIts() const { return FixIts; }
318318

319319
void print(const char *ProgName, raw_ostream &S, bool ShowColors = true,
320-
bool ShowKindLabel = true) const;
320+
bool ShowKindLabel = true, bool ShowLocation = true) const;
321321
};
322322

323323
} // end namespace llvm

llvm/lib/Support/SourceMgr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ static void printSourceLine(raw_ostream &S, StringRef LineContents) {
482482
static bool isNonASCII(char c) { return c & 0x80; }
483483

484484
void SMDiagnostic::print(const char *ProgName, raw_ostream &OS, bool ShowColors,
485-
bool ShowKindLabel) const {
485+
bool ShowKindLabel, bool ShowLocation) const {
486486
ColorMode Mode = ShowColors ? ColorMode::Auto : ColorMode::Disable;
487487

488488
{
@@ -491,7 +491,7 @@ void SMDiagnostic::print(const char *ProgName, raw_ostream &OS, bool ShowColors,
491491
if (ProgName && ProgName[0])
492492
S << ProgName << ": ";
493493

494-
if (!Filename.empty()) {
494+
if (ShowLocation && !Filename.empty()) {
495495
if (Filename == "-")
496496
S << "<stdin>";
497497
else

llvm/unittests/Support/SourceMgrTest.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,3 +532,14 @@ TEST_F(SourceMgrTest, FixitForTab) {
532532
Output);
533533
}
534534

535+
TEST_F(SourceMgrTest, PrintWithoutLoc) {
536+
raw_string_ostream OS(Output);
537+
auto Diag =
538+
llvm::SMDiagnostic("file.in", llvm::SourceMgr::DK_Error, "message");
539+
Diag.print(nullptr, OS);
540+
OS.flush();
541+
EXPECT_EQ("file.in: error: message\n", Output);
542+
Output.clear();
543+
Diag.print(nullptr, OS, false, false, false);
544+
EXPECT_EQ("message\n", Output);
545+
}

0 commit comments

Comments
 (0)