1 //===- unittests/Support/SourceMgrTest.cpp - SourceMgr tests --------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/SourceMgr.h"
10 #include "llvm/Support/MemoryBuffer.h"
11 #include "llvm/Support/raw_ostream.h"
12 #include "gtest/gtest.h"
18 class SourceMgrTest
: public testing::Test
{
21 unsigned MainBufferID
;
24 void setMainBuffer(StringRef Text
, StringRef BufferName
) {
25 std::unique_ptr
<MemoryBuffer
> MainBuffer
=
26 MemoryBuffer::getMemBuffer(Text
, BufferName
);
27 MainBufferID
= SM
.AddNewSourceBuffer(std::move(MainBuffer
), llvm::SMLoc());
30 SMLoc
getLoc(unsigned Offset
) {
31 return SMLoc::getFromPointer(
32 SM
.getMemoryBuffer(MainBufferID
)->getBufferStart() + Offset
);
35 SMRange
getRange(unsigned Offset
, unsigned Length
) {
36 return SMRange(getLoc(Offset
), getLoc(Offset
+ Length
));
39 void printMessage(SMLoc Loc
, SourceMgr::DiagKind Kind
,
40 const Twine
&Msg
, ArrayRef
<SMRange
> Ranges
,
41 ArrayRef
<SMFixIt
> FixIts
) {
42 raw_string_ostream
OS(Output
);
43 SM
.PrintMessage(OS
, Loc
, Kind
, Msg
, Ranges
, FixIts
);
47 } // unnamed namespace
49 TEST_F(SourceMgrTest
, BasicError
) {
50 setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
51 printMessage(getLoc(4), SourceMgr::DK_Error
, "message", None
, None
);
53 EXPECT_EQ("file.in:1:5: error: message\n"
59 TEST_F(SourceMgrTest
, BasicWarning
) {
60 setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
61 printMessage(getLoc(4), SourceMgr::DK_Warning
, "message", None
, None
);
63 EXPECT_EQ("file.in:1:5: warning: message\n"
69 TEST_F(SourceMgrTest
, BasicRemark
) {
70 setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
71 printMessage(getLoc(4), SourceMgr::DK_Remark
, "message", None
, None
);
73 EXPECT_EQ("file.in:1:5: remark: message\n"
79 TEST_F(SourceMgrTest
, BasicNote
) {
80 setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
81 printMessage(getLoc(4), SourceMgr::DK_Note
, "message", None
, None
);
83 EXPECT_EQ("file.in:1:5: note: message\n"
89 TEST_F(SourceMgrTest
, LocationAtEndOfLine
) {
90 setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
91 printMessage(getLoc(6), SourceMgr::DK_Error
, "message", None
, None
);
93 EXPECT_EQ("file.in:1:7: error: message\n"
99 TEST_F(SourceMgrTest
, LocationAtNewline
) {
100 setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
101 printMessage(getLoc(7), SourceMgr::DK_Error
, "message", None
, None
);
103 EXPECT_EQ("file.in:1:8: error: message\n"
109 TEST_F(SourceMgrTest
, LocationAtEmptyBuffer
) {
110 setMainBuffer("", "file.in");
111 printMessage(getLoc(0), SourceMgr::DK_Error
, "message", None
, None
);
113 EXPECT_EQ("file.in:1:1: error: message\n"
119 TEST_F(SourceMgrTest
, LocationJustOnSoleNewline
) {
120 setMainBuffer("\n", "file.in");
121 printMessage(getLoc(0), SourceMgr::DK_Error
, "message", None
, None
);
123 EXPECT_EQ("file.in:1:1: error: message\n"
129 TEST_F(SourceMgrTest
, LocationJustAfterSoleNewline
) {
130 setMainBuffer("\n", "file.in");
131 printMessage(getLoc(1), SourceMgr::DK_Error
, "message", None
, None
);
133 EXPECT_EQ("file.in:2:1: error: message\n"
139 TEST_F(SourceMgrTest
, LocationJustAfterNonNewline
) {
140 setMainBuffer("123", "file.in");
141 printMessage(getLoc(3), SourceMgr::DK_Error
, "message", None
, None
);
143 EXPECT_EQ("file.in:1:4: error: message\n"
149 TEST_F(SourceMgrTest
, LocationOnFirstLineOfMultiline
) {
150 setMainBuffer("1234\n6789\n", "file.in");
151 printMessage(getLoc(3), SourceMgr::DK_Error
, "message", None
, None
);
153 EXPECT_EQ("file.in:1:4: error: message\n"
159 TEST_F(SourceMgrTest
, LocationOnEOLOfFirstLineOfMultiline
) {
160 setMainBuffer("1234\n6789\n", "file.in");
161 printMessage(getLoc(4), SourceMgr::DK_Error
, "message", None
, None
);
163 EXPECT_EQ("file.in:1:5: error: message\n"
169 TEST_F(SourceMgrTest
, LocationOnSecondLineOfMultiline
) {
170 setMainBuffer("1234\n6789\n", "file.in");
171 printMessage(getLoc(5), SourceMgr::DK_Error
, "message", None
, None
);
173 EXPECT_EQ("file.in:2:1: error: message\n"
179 TEST_F(SourceMgrTest
, LocationOnSecondLineOfMultilineNoSecondEOL
) {
180 setMainBuffer("1234\n6789", "file.in");
181 printMessage(getLoc(5), SourceMgr::DK_Error
, "message", None
, None
);
183 EXPECT_EQ("file.in:2:1: error: message\n"
189 TEST_F(SourceMgrTest
, LocationOnEOLOfSecondSecondLineOfMultiline
) {
190 setMainBuffer("1234\n6789\n", "file.in");
191 printMessage(getLoc(9), SourceMgr::DK_Error
, "message", None
, None
);
193 EXPECT_EQ("file.in:2:5: error: message\n"
199 #define STRING_LITERAL_253_BYTES \
200 "1234567890\n1234567890\n" \
201 "1234567890\n1234567890\n" \
202 "1234567890\n1234567890\n" \
203 "1234567890\n1234567890\n" \
204 "1234567890\n1234567890\n" \
205 "1234567890\n1234567890\n" \
206 "1234567890\n1234567890\n" \
207 "1234567890\n1234567890\n" \
208 "1234567890\n1234567890\n" \
209 "1234567890\n1234567890\n" \
210 "1234567890\n1234567890\n" \
213 //===----------------------------------------------------------------------===//
214 // 255-byte buffer tests
215 //===----------------------------------------------------------------------===//
217 TEST_F(SourceMgrTest
, LocationBeforeEndOf255ByteBuffer
) {
218 setMainBuffer(STRING_LITERAL_253_BYTES
// first 253 bytes
219 "12" // + 2 = 255 bytes
221 printMessage(getLoc(253), SourceMgr::DK_Error
, "message", None
, None
);
222 EXPECT_EQ("file.in:24:1: error: message\n"
228 TEST_F(SourceMgrTest
, LocationAtEndOf255ByteBuffer
) {
229 setMainBuffer(STRING_LITERAL_253_BYTES
// first 253 bytes
230 "12" // + 2 = 255 bytes
232 printMessage(getLoc(254), SourceMgr::DK_Error
, "message", None
, None
);
233 EXPECT_EQ("file.in:24:2: error: message\n"
239 TEST_F(SourceMgrTest
, LocationPastEndOf255ByteBuffer
) {
240 setMainBuffer(STRING_LITERAL_253_BYTES
// first 253 bytes
241 "12" // + 2 = 255 bytes
243 printMessage(getLoc(255), SourceMgr::DK_Error
, "message", None
, None
);
244 EXPECT_EQ("file.in:24:3: error: message\n"
250 TEST_F(SourceMgrTest
, LocationBeforeEndOf255ByteBufferEndingInNewline
) {
251 setMainBuffer(STRING_LITERAL_253_BYTES
// first 253 bytes
252 "1\n" // + 2 = 255 bytes
254 printMessage(getLoc(253), SourceMgr::DK_Error
, "message", None
, None
);
255 EXPECT_EQ("file.in:24:1: error: message\n"
261 TEST_F(SourceMgrTest
, LocationAtEndOf255ByteBufferEndingInNewline
) {
262 setMainBuffer(STRING_LITERAL_253_BYTES
// first 253 bytes
263 "1\n" // + 2 = 255 bytes
265 printMessage(getLoc(254), SourceMgr::DK_Error
, "message", None
, None
);
266 EXPECT_EQ("file.in:24:2: error: message\n"
272 TEST_F(SourceMgrTest
, LocationPastEndOf255ByteBufferEndingInNewline
) {
273 setMainBuffer(STRING_LITERAL_253_BYTES
// first 253 bytes
274 "1\n" // + 2 = 255 bytes
276 printMessage(getLoc(255), SourceMgr::DK_Error
, "message", None
, None
);
277 EXPECT_EQ("file.in:25:1: error: message\n"
283 //===----------------------------------------------------------------------===//
284 // 256-byte buffer tests
285 //===----------------------------------------------------------------------===//
287 TEST_F(SourceMgrTest
, LocationBeforeEndOf256ByteBuffer
) {
288 setMainBuffer(STRING_LITERAL_253_BYTES
// first 253 bytes
289 "123" // + 3 = 256 bytes
291 printMessage(getLoc(254), SourceMgr::DK_Error
, "message", None
, None
);
292 EXPECT_EQ("file.in:24:2: error: message\n"
298 TEST_F(SourceMgrTest
, LocationAtEndOf256ByteBuffer
) {
299 setMainBuffer(STRING_LITERAL_253_BYTES
// first 253 bytes
300 "123" // + 3 = 256 bytes
302 printMessage(getLoc(255), SourceMgr::DK_Error
, "message", None
, None
);
303 EXPECT_EQ("file.in:24:3: error: message\n"
309 TEST_F(SourceMgrTest
, LocationPastEndOf256ByteBuffer
) {
310 setMainBuffer(STRING_LITERAL_253_BYTES
// first 253 bytes
311 "123" // + 3 = 256 bytes
313 printMessage(getLoc(256), SourceMgr::DK_Error
, "message", None
, None
);
314 EXPECT_EQ("file.in:24:4: error: message\n"
320 TEST_F(SourceMgrTest
, LocationBeforeEndOf256ByteBufferEndingInNewline
) {
321 setMainBuffer(STRING_LITERAL_253_BYTES
// first 253 bytes
322 "12\n" // + 3 = 256 bytes
324 printMessage(getLoc(254), SourceMgr::DK_Error
, "message", None
, None
);
325 EXPECT_EQ("file.in:24:2: error: message\n"
331 TEST_F(SourceMgrTest
, LocationAtEndOf256ByteBufferEndingInNewline
) {
332 setMainBuffer(STRING_LITERAL_253_BYTES
// first 253 bytes
333 "12\n" // + 3 = 256 bytes
335 printMessage(getLoc(255), SourceMgr::DK_Error
, "message", None
, None
);
336 EXPECT_EQ("file.in:24:3: error: message\n"
342 TEST_F(SourceMgrTest
, LocationPastEndOf256ByteBufferEndingInNewline
) {
343 setMainBuffer(STRING_LITERAL_253_BYTES
// first 253 bytes
344 "12\n" // + 3 = 256 bytes
346 printMessage(getLoc(256), SourceMgr::DK_Error
, "message", None
, None
);
347 EXPECT_EQ("file.in:25:1: error: message\n"
353 //===----------------------------------------------------------------------===//
354 // 257-byte buffer tests
355 //===----------------------------------------------------------------------===//
357 TEST_F(SourceMgrTest
, LocationBeforeEndOf257ByteBuffer
) {
358 setMainBuffer(STRING_LITERAL_253_BYTES
// first 253 bytes
359 "1234" // + 4 = 257 bytes
361 printMessage(getLoc(255), SourceMgr::DK_Error
, "message", None
, None
);
362 EXPECT_EQ("file.in:24:3: error: message\n"
368 TEST_F(SourceMgrTest
, LocationAtEndOf257ByteBuffer
) {
369 setMainBuffer(STRING_LITERAL_253_BYTES
// first 253 bytes
370 "1234" // + 4 = 257 bytes
372 printMessage(getLoc(256), SourceMgr::DK_Error
, "message", None
, None
);
373 EXPECT_EQ("file.in:24:4: error: message\n"
379 TEST_F(SourceMgrTest
, LocationPastEndOf257ByteBuffer
) {
380 setMainBuffer(STRING_LITERAL_253_BYTES
// first 253 bytes
381 "1234" // + 4 = 257 bytes
383 printMessage(getLoc(257), SourceMgr::DK_Error
, "message", None
, None
);
384 EXPECT_EQ("file.in:24:5: error: message\n"
390 TEST_F(SourceMgrTest
, LocationBeforeEndOf257ByteBufferEndingInNewline
) {
391 setMainBuffer(STRING_LITERAL_253_BYTES
// first 253 bytes
392 "123\n" // + 4 = 257 bytes
394 printMessage(getLoc(255), SourceMgr::DK_Error
, "message", None
, None
);
395 EXPECT_EQ("file.in:24:3: error: message\n"
401 TEST_F(SourceMgrTest
, LocationAtEndOf257ByteBufferEndingInNewline
) {
402 setMainBuffer(STRING_LITERAL_253_BYTES
// first 253 bytes
403 "123\n" // + 4 = 257 bytes
405 printMessage(getLoc(256), SourceMgr::DK_Error
, "message", None
, None
);
406 EXPECT_EQ("file.in:24:4: error: message\n"
412 TEST_F(SourceMgrTest
, LocationPastEndOf257ByteBufferEndingInNewline
) {
413 setMainBuffer(STRING_LITERAL_253_BYTES
// first 253 bytes
414 "123\n" // + 4 = 257 bytes
416 printMessage(getLoc(257), SourceMgr::DK_Error
, "message", None
, None
);
417 EXPECT_EQ("file.in:25:1: error: message\n"
423 TEST_F(SourceMgrTest
, BasicRange
) {
424 setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
425 printMessage(getLoc(4), SourceMgr::DK_Error
, "message", getRange(4, 3), None
);
427 EXPECT_EQ("file.in:1:5: error: message\n"
433 TEST_F(SourceMgrTest
, RangeWithTab
) {
434 setMainBuffer("aaa\tbbb\nccc ddd\n", "file.in");
435 printMessage(getLoc(4), SourceMgr::DK_Error
, "message", getRange(3, 3), None
);
437 EXPECT_EQ("file.in:1:5: error: message\n"
443 TEST_F(SourceMgrTest
, MultiLineRange
) {
444 setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
445 printMessage(getLoc(4), SourceMgr::DK_Error
, "message", getRange(4, 7), None
);
447 EXPECT_EQ("file.in:1:5: error: message\n"
453 TEST_F(SourceMgrTest
, MultipleRanges
) {
454 setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
455 SMRange Ranges
[] = { getRange(0, 3), getRange(4, 3) };
456 printMessage(getLoc(4), SourceMgr::DK_Error
, "message", Ranges
, None
);
458 EXPECT_EQ("file.in:1:5: error: message\n"
464 TEST_F(SourceMgrTest
, OverlappingRanges
) {
465 setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
466 SMRange Ranges
[] = { getRange(0, 3), getRange(2, 4) };
467 printMessage(getLoc(4), SourceMgr::DK_Error
, "message", Ranges
, None
);
469 EXPECT_EQ("file.in:1:5: error: message\n"
475 TEST_F(SourceMgrTest
, BasicFixit
) {
476 setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
477 printMessage(getLoc(4), SourceMgr::DK_Error
, "message", None
,
478 makeArrayRef(SMFixIt(getRange(4, 3), "zzz")));
480 EXPECT_EQ("file.in:1:5: error: message\n"
487 TEST_F(SourceMgrTest
, FixitForTab
) {
488 setMainBuffer("aaa\tbbb\nccc ddd\n", "file.in");
489 printMessage(getLoc(3), SourceMgr::DK_Error
, "message", None
,
490 makeArrayRef(SMFixIt(getRange(3, 1), "zzz")));
492 EXPECT_EQ("file.in:1:4: error: message\n"