From 8a93c3ab2121f8c9f0739971070b042cd6aa8024 Mon Sep 17 00:00:00 2001 From: Dmitri Gribenko Date: Fri, 27 Sep 2013 21:09:25 +0000 Subject: [PATCH] Make SourceMgr::PrintMessage() testable and add unit tests git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@191558 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/SourceMgr.h | 8 +- lib/Support/SourceMgr.cpp | 11 +- unittests/Support/CMakeLists.txt | 1 + unittests/Support/SourceMgrTest.cpp | 162 ++++++++++++++++++++++++++++ 4 files changed, 178 insertions(+), 4 deletions(-) create mode 100644 unittests/Support/SourceMgrTest.cpp diff --git a/include/llvm/Support/SourceMgr.h b/include/llvm/Support/SourceMgr.h index 5b33d42f9eb..dd4897432d9 100644 --- a/include/llvm/Support/SourceMgr.h +++ b/include/llvm/Support/SourceMgr.h @@ -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 Ranges = None, ArrayRef FixIts = None, bool ShowColors = true) const; + /// Emits a diagnostic to llvm::errs(). + void PrintMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg, + ArrayRef Ranges = None, + ArrayRef FixIts = None, + bool ShowColors = true) const; /// GetMessage - Return an SMDiagnostic at the specified location with the /// specified string. diff --git a/lib/Support/SourceMgr.cpp b/lib/Support/SourceMgr.cpp index 51162dd342d..fbcd8f980c1 100644 --- a/lib/Support/SourceMgr.cpp +++ b/lib/Support/SourceMgr.cpp @@ -211,7 +211,8 @@ SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, SourceMgr::DiagKind Kind, 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 Ranges, ArrayRef FixIts, bool ShowColors) const { SMDiagnostic Diagnostic = GetMessage(Loc, Kind, Msg, Ranges, FixIts); @@ -222,8 +223,6 @@ void SourceMgr::PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, 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, SourceMgr::DiagKind Kind, Diagnostic.print(0, OS, ShowColors); } +void SourceMgr::PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, + const Twine &Msg, ArrayRef Ranges, + ArrayRef FixIts, bool ShowColors) const { + PrintMessage(llvm::errs(), Loc, Kind, Msg, Ranges, FixIts, ShowColors); +} + //===----------------------------------------------------------------------===// // SMDiagnostic Implementation //===----------------------------------------------------------------------===// diff --git a/unittests/Support/CMakeLists.txt b/unittests/Support/CMakeLists.txt index 0c5180c65fa..c2753629cb9 100644 --- a/unittests/Support/CMakeLists.txt +++ b/unittests/Support/CMakeLists.txt @@ -28,6 +28,7 @@ add_llvm_unittest(SupportTests ProcessTest.cpp ProgramTest.cpp RegexTest.cpp + SourceMgrTest.cpp SwapByteOrderTest.cpp TimeValueTest.cpp UnicodeTest.cpp diff --git a/unittests/Support/SourceMgrTest.cpp b/unittests/Support/SourceMgrTest.cpp new file mode 100644 index 00000000000..9bc0cf67b75 --- /dev/null +++ b/unittests/Support/SourceMgrTest.cpp @@ -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 Ranges, + ArrayRef 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); +} + -- 2.34.1