diff --git a/llvm/include/llvm/Support/SourceMgr.h b/llvm/include/llvm/Support/SourceMgr.h index ffd6bbfaf1113..7b40100336403 100644 --- a/llvm/include/llvm/Support/SourceMgr.h +++ b/llvm/include/llvm/Support/SourceMgr.h @@ -329,7 +329,7 @@ class SMDiagnostic { ArrayRef getFixIts() const { return FixIts; } void print(const char *ProgName, raw_ostream &S, bool ShowColors = true, - bool ShowKindLabel = true) const; + bool ShowKindLabel = true, bool ShowLocation = true) const; }; } // end namespace llvm diff --git a/llvm/lib/Support/SourceMgr.cpp b/llvm/lib/Support/SourceMgr.cpp index 4b938d08234cc..b8bf02d59675d 100644 --- a/llvm/lib/Support/SourceMgr.cpp +++ b/llvm/lib/Support/SourceMgr.cpp @@ -498,7 +498,7 @@ static void printSourceLine(raw_ostream &S, StringRef LineContents) { static bool isNonASCII(char c) { return c & 0x80; } void SMDiagnostic::print(const char *ProgName, raw_ostream &OS, bool ShowColors, - bool ShowKindLabel) const { + bool ShowKindLabel, bool ShowLocation) const { ColorMode Mode = ShowColors ? ColorMode::Auto : ColorMode::Disable; { @@ -507,7 +507,7 @@ void SMDiagnostic::print(const char *ProgName, raw_ostream &OS, bool ShowColors, if (ProgName && ProgName[0]) S << ProgName << ": "; - if (!Filename.empty()) { + if (ShowLocation && !Filename.empty()) { if (Filename == "-") S << ""; else diff --git a/llvm/unittests/Support/SourceMgrTest.cpp b/llvm/unittests/Support/SourceMgrTest.cpp index 9fab912a6622b..f288a69a345a4 100644 --- a/llvm/unittests/Support/SourceMgrTest.cpp +++ b/llvm/unittests/Support/SourceMgrTest.cpp @@ -580,3 +580,15 @@ TEST_F(SourceMgrTest, AddIncludedFile) { "Included from //path/to/includes/search-path-file:3:\n", Output); } + +TEST_F(SourceMgrTest, PrintWithoutLoc) { + raw_string_ostream OS(Output); + auto Diag = + llvm::SMDiagnostic("file.in", llvm::SourceMgr::DK_Error, "message"); + Diag.print(nullptr, OS); + OS.flush(); + EXPECT_EQ("file.in: error: message\n", Output); + Output.clear(); + Diag.print(nullptr, OS, false, false, false); + EXPECT_EQ("message\n", Output); +}