1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
//===- 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) {
std::unique_ptr<MemoryBuffer> MainBuffer =
MemoryBuffer::getMemBuffer(Text, BufferName);
MainBufferID = SM.AddNewSourceBuffer(std::move(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);
}
TEST_F(SourceMgrTest, FixitForTab) {
setMainBuffer("aaa\tbbb\nccc ddd\n", "file.in");
printMessage(getLoc(3), SourceMgr::DK_Error, "message", None,
makeArrayRef(SMFixIt(getRange(3, 1), "zzz")));
EXPECT_EQ("file.in:1:4: error: message\n"
"aaa bbb\n"
" ^^^^^\n"
" zzz\n",
Output);
}
|