mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-06-13 22:24:07 +00:00
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
This commit is contained in:
@ -144,11 +144,17 @@ public:
|
|||||||
///
|
///
|
||||||
/// @param ShowColors - Display colored messages if output is a terminal and
|
/// @param ShowColors - Display colored messages if output is a terminal and
|
||||||
/// the default error handler is used.
|
/// 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<SMRange> Ranges = None,
|
||||||
ArrayRef<SMFixIt> FixIts = None,
|
ArrayRef<SMFixIt> FixIts = None,
|
||||||
bool ShowColors = true) const;
|
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
|
/// GetMessage - Return an SMDiagnostic at the specified location with the
|
||||||
/// specified string.
|
/// specified string.
|
||||||
|
@ -211,7 +211,8 @@ SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
|
|||||||
LineStr, ColRanges, FixIts);
|
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,
|
const Twine &Msg, ArrayRef<SMRange> Ranges,
|
||||||
ArrayRef<SMFixIt> FixIts, bool ShowColors) const {
|
ArrayRef<SMFixIt> FixIts, bool ShowColors) const {
|
||||||
SMDiagnostic Diagnostic = GetMessage(Loc, Kind, Msg, Ranges, FixIts);
|
SMDiagnostic Diagnostic = GetMessage(Loc, Kind, Msg, Ranges, FixIts);
|
||||||
@ -222,8 +223,6 @@ void SourceMgr::PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
raw_ostream &OS = errs();
|
|
||||||
|
|
||||||
if (Loc != SMLoc()) {
|
if (Loc != SMLoc()) {
|
||||||
int CurBuf = FindBufferContainingLoc(Loc);
|
int CurBuf = FindBufferContainingLoc(Loc);
|
||||||
assert(CurBuf != -1 && "Invalid or unspecified location!");
|
assert(CurBuf != -1 && "Invalid or unspecified location!");
|
||||||
@ -233,6 +232,12 @@ void SourceMgr::PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
|
|||||||
Diagnostic.print(0, OS, ShowColors);
|
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
|
// SMDiagnostic Implementation
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
@ -28,6 +28,7 @@ add_llvm_unittest(SupportTests
|
|||||||
ProcessTest.cpp
|
ProcessTest.cpp
|
||||||
ProgramTest.cpp
|
ProgramTest.cpp
|
||||||
RegexTest.cpp
|
RegexTest.cpp
|
||||||
|
SourceMgrTest.cpp
|
||||||
SwapByteOrderTest.cpp
|
SwapByteOrderTest.cpp
|
||||||
TimeValueTest.cpp
|
TimeValueTest.cpp
|
||||||
UnicodeTest.cpp
|
UnicodeTest.cpp
|
||||||
|
162
unittests/Support/SourceMgrTest.cpp
Normal file
162
unittests/Support/SourceMgrTest.cpp
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
|
Reference in New Issue
Block a user