forked from OSchip/llvm-project
Make SourceMgr::PrintMessage() testable and add unit tests
llvm-svn: 191558
This commit is contained in:
parent
c2bed42904
commit
8f944628ac
|
@ -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.
|
||||
|
|
|
@ -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<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, 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<SMRange> Ranges,
|
||||
ArrayRef<SMFixIt> FixIts, bool ShowColors) const {
|
||||
PrintMessage(llvm::errs(), Loc, Kind, Msg, Ranges, FixIts, ShowColors);
|
||||
}
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// SMDiagnostic Implementation
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
|
|
@ -28,6 +28,7 @@ add_llvm_unittest(SupportTests
|
|||
ProcessTest.cpp
|
||||
ProgramTest.cpp
|
||||
RegexTest.cpp
|
||||
SourceMgrTest.cpp
|
||||
SwapByteOrderTest.cpp
|
||||
TimeValueTest.cpp
|
||||
UnicodeTest.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<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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue