[llvm] a4accdf - [Support] Add option to print SMDiagnostic into a buffer without the filename and location info (#92050)

via llvm-commits llvm-commits at lists.llvm.org
Tue May 14 07:53:58 PDT 2024


Author: Artem Chikin
Date: 2024-05-14T07:53:53-07:00
New Revision: a4accdfe0c9415ad1bd3dac7dda8cb8bbcd1be2f

URL: https://github.com/llvm/llvm-project/commit/a4accdfe0c9415ad1bd3dac7dda8cb8bbcd1be2f
DIFF: https://github.com/llvm/llvm-project/commit/a4accdfe0c9415ad1bd3dac7dda8cb8bbcd1be2f.diff

LOG: [Support] Add option to print SMDiagnostic into a buffer without the filename and location info (#92050)

Added: 
    

Modified: 
    llvm/include/llvm/Support/SourceMgr.h
    llvm/lib/Support/SourceMgr.cpp
    llvm/unittests/Support/SourceMgrTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/SourceMgr.h b/llvm/include/llvm/Support/SourceMgr.h
index 6f5bee7f8cc22..7a4b6de1162da 100644
--- a/llvm/include/llvm/Support/SourceMgr.h
+++ b/llvm/include/llvm/Support/SourceMgr.h
@@ -317,7 +317,7 @@ class SMDiagnostic {
   ArrayRef<SMFixIt> 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 ebeff87c39549..6eaa8783a5319 100644
--- a/llvm/lib/Support/SourceMgr.cpp
+++ b/llvm/lib/Support/SourceMgr.cpp
@@ -482,7 +482,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;
 
   {
@@ -491,7 +491,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 << "<stdin>";
       else

diff  --git a/llvm/unittests/Support/SourceMgrTest.cpp b/llvm/unittests/Support/SourceMgrTest.cpp
index cbd17ff476890..75a0cfe35aaf0 100644
--- a/llvm/unittests/Support/SourceMgrTest.cpp
+++ b/llvm/unittests/Support/SourceMgrTest.cpp
@@ -532,3 +532,14 @@ TEST_F(SourceMgrTest, FixitForTab) {
             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);
+}


        


More information about the llvm-commits mailing list