[llvm] r191558 - Make SourceMgr::PrintMessage() testable and add unit tests

Dmitri Gribenko gribozavr at gmail.com
Fri Sep 27 14:09:26 PDT 2013


Author: gribozavr
Date: Fri Sep 27 16:09:25 2013
New Revision: 191558

URL: http://llvm.org/viewvc/llvm-project?rev=191558&view=rev
Log:
Make SourceMgr::PrintMessage() testable and add unit tests

Added:
    llvm/trunk/unittests/Support/SourceMgrTest.cpp
Modified:
    llvm/trunk/include/llvm/Support/SourceMgr.h
    llvm/trunk/lib/Support/SourceMgr.cpp
    llvm/trunk/unittests/Support/CMakeLists.txt

Modified: llvm/trunk/include/llvm/Support/SourceMgr.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/SourceMgr.h?rev=191558&r1=191557&r2=191558&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/SourceMgr.h (original)
+++ llvm/trunk/include/llvm/Support/SourceMgr.h Fri Sep 27 16:09:25 2013
@@ -144,11 +144,17 @@ public:
   ///
   /// @param ShowColors - Display colored messages if output is a terminal and
   /// the default error handler is used.
-  void PrintMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg,
+  void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind,
+                    const Twine &Msg,
                     ArrayRef<SMRange> Ranges = None,
                     ArrayRef<SMFixIt> FixIts = None,
                     bool ShowColors = true) const;
 
+  /// Emits a diagnostic to llvm::errs().
+  void PrintMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg,
+                    ArrayRef<SMRange> Ranges = None,
+                    ArrayRef<SMFixIt> FixIts = None,
+                    bool ShowColors = true) const;
 
   /// GetMessage - Return an SMDiagnostic at the specified location with the
   /// specified string.

Modified: llvm/trunk/lib/Support/SourceMgr.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/SourceMgr.cpp?rev=191558&r1=191557&r2=191558&view=diff
==============================================================================
--- llvm/trunk/lib/Support/SourceMgr.cpp (original)
+++ llvm/trunk/lib/Support/SourceMgr.cpp Fri Sep 27 16:09:25 2013
@@ -211,7 +211,8 @@ SMDiagnostic SourceMgr::GetMessage(SMLoc
                       LineStr, ColRanges, FixIts);
 }
 
-void SourceMgr::PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
+void SourceMgr::PrintMessage(raw_ostream &OS, SMLoc Loc,
+                             SourceMgr::DiagKind Kind,
                              const Twine &Msg, ArrayRef<SMRange> Ranges,
                              ArrayRef<SMFixIt> FixIts, bool ShowColors) const {
   SMDiagnostic Diagnostic = GetMessage(Loc, Kind, Msg, Ranges, FixIts);
@@ -222,8 +223,6 @@ void SourceMgr::PrintMessage(SMLoc Loc,
     return;
   }
 
-  raw_ostream &OS = errs();
-
   if (Loc != SMLoc()) {
     int CurBuf = FindBufferContainingLoc(Loc);
     assert(CurBuf != -1 && "Invalid or unspecified location!");
@@ -233,6 +232,12 @@ void SourceMgr::PrintMessage(SMLoc Loc,
   Diagnostic.print(0, OS, ShowColors);
 }
 
+void SourceMgr::PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
+                             const Twine &Msg, ArrayRef<SMRange> Ranges,
+                             ArrayRef<SMFixIt> FixIts, bool ShowColors) const {
+  PrintMessage(llvm::errs(), Loc, Kind, Msg, Ranges, FixIts, ShowColors);
+}
+
 //===----------------------------------------------------------------------===//
 // SMDiagnostic Implementation
 //===----------------------------------------------------------------------===//

Modified: llvm/trunk/unittests/Support/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/CMakeLists.txt?rev=191558&r1=191557&r2=191558&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/CMakeLists.txt (original)
+++ llvm/trunk/unittests/Support/CMakeLists.txt Fri Sep 27 16:09:25 2013
@@ -28,6 +28,7 @@ add_llvm_unittest(SupportTests
   ProcessTest.cpp
   ProgramTest.cpp
   RegexTest.cpp
+  SourceMgrTest.cpp
   SwapByteOrderTest.cpp
   TimeValueTest.cpp
   UnicodeTest.cpp

Added: llvm/trunk/unittests/Support/SourceMgrTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/SourceMgrTest.cpp?rev=191558&view=auto
==============================================================================
--- llvm/trunk/unittests/Support/SourceMgrTest.cpp (added)
+++ llvm/trunk/unittests/Support/SourceMgrTest.cpp Fri Sep 27 16:09:25 2013
@@ -0,0 +1,162 @@
+//===- unittests/Support/SourceMgrTest.cpp - SourceMgr tests --------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/SourceMgr.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/raw_ostream.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+namespace {
+
+class SourceMgrTest : public testing::Test {
+public:
+  SourceMgr SM;
+  unsigned MainBufferID;
+  std::string Output;
+
+  void setMainBuffer(StringRef Text, StringRef BufferName) {
+    MemoryBuffer *MainBuffer = MemoryBuffer::getMemBuffer(Text, BufferName);
+    MainBufferID = SM.AddNewSourceBuffer(MainBuffer, llvm::SMLoc());
+  }
+
+  SMLoc getLoc(unsigned Offset) {
+    return SMLoc::getFromPointer(
+        SM.getMemoryBuffer(MainBufferID)->getBufferStart() + Offset);
+  }
+
+  SMRange getRange(unsigned Offset, unsigned Length) {
+    return SMRange(getLoc(Offset), getLoc(Offset + Length));
+  }
+
+  void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
+                    const Twine &Msg, ArrayRef<SMRange> Ranges,
+                    ArrayRef<SMFixIt> FixIts) {
+    raw_string_ostream OS(Output);
+    SM.PrintMessage(OS, Loc, Kind, Msg, Ranges, FixIts);
+  }
+};
+
+} // unnamed namespace
+
+TEST_F(SourceMgrTest, BasicError) {
+  setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
+  printMessage(getLoc(4), SourceMgr::DK_Error, "message", None, None);
+
+  EXPECT_EQ("file.in:1:5: error: message\n"
+            "aaa bbb\n"
+            "    ^\n",
+            Output);
+}
+
+TEST_F(SourceMgrTest, BasicWarning) {
+  setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
+  printMessage(getLoc(4), SourceMgr::DK_Warning, "message", None, None);
+
+  EXPECT_EQ("file.in:1:5: warning: message\n"
+            "aaa bbb\n"
+            "    ^\n",
+            Output);
+}
+
+TEST_F(SourceMgrTest, BasicNote) {
+  setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
+  printMessage(getLoc(4), SourceMgr::DK_Note, "message", None, None);
+
+  EXPECT_EQ("file.in:1:5: note: message\n"
+            "aaa bbb\n"
+            "    ^\n",
+            Output);
+}
+
+TEST_F(SourceMgrTest, LocationAtEndOfLine) {
+  setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
+  printMessage(getLoc(6), SourceMgr::DK_Error, "message", None, None);
+
+  EXPECT_EQ("file.in:1:7: error: message\n"
+            "aaa bbb\n"
+            "      ^\n",
+            Output);
+}
+
+TEST_F(SourceMgrTest, LocationAtNewline) {
+  setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
+  printMessage(getLoc(7), SourceMgr::DK_Error, "message", None, None);
+
+  EXPECT_EQ("file.in:1:8: error: message\n"
+            "aaa bbb\n"
+            "       ^\n",
+            Output);
+}
+
+TEST_F(SourceMgrTest, BasicRange) {
+  setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
+  printMessage(getLoc(4), SourceMgr::DK_Error, "message", getRange(4, 3), None);
+
+  EXPECT_EQ("file.in:1:5: error: message\n"
+            "aaa bbb\n"
+            "    ^~~\n",
+            Output);
+}
+
+TEST_F(SourceMgrTest, RangeWithTab) {
+  setMainBuffer("aaa\tbbb\nccc ddd\n", "file.in");
+  printMessage(getLoc(4), SourceMgr::DK_Error, "message", getRange(3, 3), None);
+
+  EXPECT_EQ("file.in:1:5: error: message\n"
+            "aaa     bbb\n"
+            "   ~~~~~^~\n",
+            Output);
+}
+
+TEST_F(SourceMgrTest, MultiLineRange) {
+  setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
+  printMessage(getLoc(4), SourceMgr::DK_Error, "message", getRange(4, 7), None);
+
+  EXPECT_EQ("file.in:1:5: error: message\n"
+            "aaa bbb\n"
+            "    ^~~\n",
+            Output);
+}
+
+TEST_F(SourceMgrTest, MultipleRanges) {
+  setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
+  SMRange Ranges[] = { getRange(0, 3), getRange(4, 3) };
+  printMessage(getLoc(4), SourceMgr::DK_Error, "message", Ranges, None);
+
+  EXPECT_EQ("file.in:1:5: error: message\n"
+            "aaa bbb\n"
+            "~~~ ^~~\n",
+            Output);
+}
+
+TEST_F(SourceMgrTest, OverlappingRanges) {
+  setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
+  SMRange Ranges[] = { getRange(0, 3), getRange(2, 4) };
+  printMessage(getLoc(4), SourceMgr::DK_Error, "message", Ranges, None);
+
+  EXPECT_EQ("file.in:1:5: error: message\n"
+            "aaa bbb\n"
+            "~~~~^~\n",
+            Output);
+}
+
+TEST_F(SourceMgrTest, BasicFixit) {
+  setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
+  printMessage(getLoc(4), SourceMgr::DK_Error, "message", None,
+               makeArrayRef(SMFixIt(getRange(4, 3), "zzz")));
+
+  EXPECT_EQ("file.in:1:5: error: message\n"
+            "aaa bbb\n"
+            "    ^~~\n"
+            "    zzz\n",
+            Output);
+}
+





More information about the llvm-commits mailing list