1f4a2713aSLionel Sambuc //===- unittests/Support/SourceMgrTest.cpp - SourceMgr tests --------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc #include "llvm/Support/SourceMgr.h"
11f4a2713aSLionel Sambuc #include "llvm/Support/MemoryBuffer.h"
12f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
13f4a2713aSLionel Sambuc #include "gtest/gtest.h"
14f4a2713aSLionel Sambuc 
15f4a2713aSLionel Sambuc using namespace llvm;
16f4a2713aSLionel Sambuc 
17f4a2713aSLionel Sambuc namespace {
18f4a2713aSLionel Sambuc 
19f4a2713aSLionel Sambuc class SourceMgrTest : public testing::Test {
20f4a2713aSLionel Sambuc public:
21f4a2713aSLionel Sambuc   SourceMgr SM;
22f4a2713aSLionel Sambuc   unsigned MainBufferID;
23f4a2713aSLionel Sambuc   std::string Output;
24f4a2713aSLionel Sambuc 
setMainBuffer(StringRef Text,StringRef BufferName)25f4a2713aSLionel Sambuc   void setMainBuffer(StringRef Text, StringRef BufferName) {
26*0a6a1f1dSLionel Sambuc     std::unique_ptr<MemoryBuffer> MainBuffer =
27*0a6a1f1dSLionel Sambuc         MemoryBuffer::getMemBuffer(Text, BufferName);
28*0a6a1f1dSLionel Sambuc     MainBufferID = SM.AddNewSourceBuffer(std::move(MainBuffer), llvm::SMLoc());
29f4a2713aSLionel Sambuc   }
30f4a2713aSLionel Sambuc 
getLoc(unsigned Offset)31f4a2713aSLionel Sambuc   SMLoc getLoc(unsigned Offset) {
32f4a2713aSLionel Sambuc     return SMLoc::getFromPointer(
33f4a2713aSLionel Sambuc         SM.getMemoryBuffer(MainBufferID)->getBufferStart() + Offset);
34f4a2713aSLionel Sambuc   }
35f4a2713aSLionel Sambuc 
getRange(unsigned Offset,unsigned Length)36f4a2713aSLionel Sambuc   SMRange getRange(unsigned Offset, unsigned Length) {
37f4a2713aSLionel Sambuc     return SMRange(getLoc(Offset), getLoc(Offset + Length));
38f4a2713aSLionel Sambuc   }
39f4a2713aSLionel Sambuc 
printMessage(SMLoc Loc,SourceMgr::DiagKind Kind,const Twine & Msg,ArrayRef<SMRange> Ranges,ArrayRef<SMFixIt> FixIts)40f4a2713aSLionel Sambuc   void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
41f4a2713aSLionel Sambuc                     const Twine &Msg, ArrayRef<SMRange> Ranges,
42f4a2713aSLionel Sambuc                     ArrayRef<SMFixIt> FixIts) {
43f4a2713aSLionel Sambuc     raw_string_ostream OS(Output);
44f4a2713aSLionel Sambuc     SM.PrintMessage(OS, Loc, Kind, Msg, Ranges, FixIts);
45f4a2713aSLionel Sambuc   }
46f4a2713aSLionel Sambuc };
47f4a2713aSLionel Sambuc 
48f4a2713aSLionel Sambuc } // unnamed namespace
49f4a2713aSLionel Sambuc 
TEST_F(SourceMgrTest,BasicError)50f4a2713aSLionel Sambuc TEST_F(SourceMgrTest, BasicError) {
51f4a2713aSLionel Sambuc   setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
52f4a2713aSLionel Sambuc   printMessage(getLoc(4), SourceMgr::DK_Error, "message", None, None);
53f4a2713aSLionel Sambuc 
54f4a2713aSLionel Sambuc   EXPECT_EQ("file.in:1:5: error: message\n"
55f4a2713aSLionel Sambuc             "aaa bbb\n"
56f4a2713aSLionel Sambuc             "    ^\n",
57f4a2713aSLionel Sambuc             Output);
58f4a2713aSLionel Sambuc }
59f4a2713aSLionel Sambuc 
TEST_F(SourceMgrTest,BasicWarning)60f4a2713aSLionel Sambuc TEST_F(SourceMgrTest, BasicWarning) {
61f4a2713aSLionel Sambuc   setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
62f4a2713aSLionel Sambuc   printMessage(getLoc(4), SourceMgr::DK_Warning, "message", None, None);
63f4a2713aSLionel Sambuc 
64f4a2713aSLionel Sambuc   EXPECT_EQ("file.in:1:5: warning: message\n"
65f4a2713aSLionel Sambuc             "aaa bbb\n"
66f4a2713aSLionel Sambuc             "    ^\n",
67f4a2713aSLionel Sambuc             Output);
68f4a2713aSLionel Sambuc }
69f4a2713aSLionel Sambuc 
TEST_F(SourceMgrTest,BasicNote)70f4a2713aSLionel Sambuc TEST_F(SourceMgrTest, BasicNote) {
71f4a2713aSLionel Sambuc   setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
72f4a2713aSLionel Sambuc   printMessage(getLoc(4), SourceMgr::DK_Note, "message", None, None);
73f4a2713aSLionel Sambuc 
74f4a2713aSLionel Sambuc   EXPECT_EQ("file.in:1:5: note: message\n"
75f4a2713aSLionel Sambuc             "aaa bbb\n"
76f4a2713aSLionel Sambuc             "    ^\n",
77f4a2713aSLionel Sambuc             Output);
78f4a2713aSLionel Sambuc }
79f4a2713aSLionel Sambuc 
TEST_F(SourceMgrTest,LocationAtEndOfLine)80f4a2713aSLionel Sambuc TEST_F(SourceMgrTest, LocationAtEndOfLine) {
81f4a2713aSLionel Sambuc   setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
82f4a2713aSLionel Sambuc   printMessage(getLoc(6), SourceMgr::DK_Error, "message", None, None);
83f4a2713aSLionel Sambuc 
84f4a2713aSLionel Sambuc   EXPECT_EQ("file.in:1:7: error: message\n"
85f4a2713aSLionel Sambuc             "aaa bbb\n"
86f4a2713aSLionel Sambuc             "      ^\n",
87f4a2713aSLionel Sambuc             Output);
88f4a2713aSLionel Sambuc }
89f4a2713aSLionel Sambuc 
TEST_F(SourceMgrTest,LocationAtNewline)90f4a2713aSLionel Sambuc TEST_F(SourceMgrTest, LocationAtNewline) {
91f4a2713aSLionel Sambuc   setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
92f4a2713aSLionel Sambuc   printMessage(getLoc(7), SourceMgr::DK_Error, "message", None, None);
93f4a2713aSLionel Sambuc 
94f4a2713aSLionel Sambuc   EXPECT_EQ("file.in:1:8: error: message\n"
95f4a2713aSLionel Sambuc             "aaa bbb\n"
96f4a2713aSLionel Sambuc             "       ^\n",
97f4a2713aSLionel Sambuc             Output);
98f4a2713aSLionel Sambuc }
99f4a2713aSLionel Sambuc 
TEST_F(SourceMgrTest,BasicRange)100f4a2713aSLionel Sambuc TEST_F(SourceMgrTest, BasicRange) {
101f4a2713aSLionel Sambuc   setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
102f4a2713aSLionel Sambuc   printMessage(getLoc(4), SourceMgr::DK_Error, "message", getRange(4, 3), None);
103f4a2713aSLionel Sambuc 
104f4a2713aSLionel Sambuc   EXPECT_EQ("file.in:1:5: error: message\n"
105f4a2713aSLionel Sambuc             "aaa bbb\n"
106f4a2713aSLionel Sambuc             "    ^~~\n",
107f4a2713aSLionel Sambuc             Output);
108f4a2713aSLionel Sambuc }
109f4a2713aSLionel Sambuc 
TEST_F(SourceMgrTest,RangeWithTab)110f4a2713aSLionel Sambuc TEST_F(SourceMgrTest, RangeWithTab) {
111f4a2713aSLionel Sambuc   setMainBuffer("aaa\tbbb\nccc ddd\n", "file.in");
112f4a2713aSLionel Sambuc   printMessage(getLoc(4), SourceMgr::DK_Error, "message", getRange(3, 3), None);
113f4a2713aSLionel Sambuc 
114f4a2713aSLionel Sambuc   EXPECT_EQ("file.in:1:5: error: message\n"
115f4a2713aSLionel Sambuc             "aaa     bbb\n"
116f4a2713aSLionel Sambuc             "   ~~~~~^~\n",
117f4a2713aSLionel Sambuc             Output);
118f4a2713aSLionel Sambuc }
119f4a2713aSLionel Sambuc 
TEST_F(SourceMgrTest,MultiLineRange)120f4a2713aSLionel Sambuc TEST_F(SourceMgrTest, MultiLineRange) {
121f4a2713aSLionel Sambuc   setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
122f4a2713aSLionel Sambuc   printMessage(getLoc(4), SourceMgr::DK_Error, "message", getRange(4, 7), None);
123f4a2713aSLionel Sambuc 
124f4a2713aSLionel Sambuc   EXPECT_EQ("file.in:1:5: error: message\n"
125f4a2713aSLionel Sambuc             "aaa bbb\n"
126f4a2713aSLionel Sambuc             "    ^~~\n",
127f4a2713aSLionel Sambuc             Output);
128f4a2713aSLionel Sambuc }
129f4a2713aSLionel Sambuc 
TEST_F(SourceMgrTest,MultipleRanges)130f4a2713aSLionel Sambuc TEST_F(SourceMgrTest, MultipleRanges) {
131f4a2713aSLionel Sambuc   setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
132f4a2713aSLionel Sambuc   SMRange Ranges[] = { getRange(0, 3), getRange(4, 3) };
133f4a2713aSLionel Sambuc   printMessage(getLoc(4), SourceMgr::DK_Error, "message", Ranges, None);
134f4a2713aSLionel Sambuc 
135f4a2713aSLionel Sambuc   EXPECT_EQ("file.in:1:5: error: message\n"
136f4a2713aSLionel Sambuc             "aaa bbb\n"
137f4a2713aSLionel Sambuc             "~~~ ^~~\n",
138f4a2713aSLionel Sambuc             Output);
139f4a2713aSLionel Sambuc }
140f4a2713aSLionel Sambuc 
TEST_F(SourceMgrTest,OverlappingRanges)141f4a2713aSLionel Sambuc TEST_F(SourceMgrTest, OverlappingRanges) {
142f4a2713aSLionel Sambuc   setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
143f4a2713aSLionel Sambuc   SMRange Ranges[] = { getRange(0, 3), getRange(2, 4) };
144f4a2713aSLionel Sambuc   printMessage(getLoc(4), SourceMgr::DK_Error, "message", Ranges, None);
145f4a2713aSLionel Sambuc 
146f4a2713aSLionel Sambuc   EXPECT_EQ("file.in:1:5: error: message\n"
147f4a2713aSLionel Sambuc             "aaa bbb\n"
148f4a2713aSLionel Sambuc             "~~~~^~\n",
149f4a2713aSLionel Sambuc             Output);
150f4a2713aSLionel Sambuc }
151f4a2713aSLionel Sambuc 
TEST_F(SourceMgrTest,BasicFixit)152f4a2713aSLionel Sambuc TEST_F(SourceMgrTest, BasicFixit) {
153f4a2713aSLionel Sambuc   setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
154f4a2713aSLionel Sambuc   printMessage(getLoc(4), SourceMgr::DK_Error, "message", None,
155f4a2713aSLionel Sambuc                makeArrayRef(SMFixIt(getRange(4, 3), "zzz")));
156f4a2713aSLionel Sambuc 
157f4a2713aSLionel Sambuc   EXPECT_EQ("file.in:1:5: error: message\n"
158f4a2713aSLionel Sambuc             "aaa bbb\n"
159f4a2713aSLionel Sambuc             "    ^~~\n"
160f4a2713aSLionel Sambuc             "    zzz\n",
161f4a2713aSLionel Sambuc             Output);
162f4a2713aSLionel Sambuc }
163f4a2713aSLionel Sambuc 
TEST_F(SourceMgrTest,FixitForTab)164f4a2713aSLionel Sambuc TEST_F(SourceMgrTest, FixitForTab) {
165f4a2713aSLionel Sambuc   setMainBuffer("aaa\tbbb\nccc ddd\n", "file.in");
166f4a2713aSLionel Sambuc   printMessage(getLoc(3), SourceMgr::DK_Error, "message", None,
167f4a2713aSLionel Sambuc                makeArrayRef(SMFixIt(getRange(3, 1), "zzz")));
168f4a2713aSLionel Sambuc 
169f4a2713aSLionel Sambuc   EXPECT_EQ("file.in:1:4: error: message\n"
170f4a2713aSLionel Sambuc             "aaa     bbb\n"
171f4a2713aSLionel Sambuc             "   ^^^^^\n"
172f4a2713aSLionel Sambuc             "   zzz\n",
173f4a2713aSLionel Sambuc             Output);
174f4a2713aSLionel Sambuc }
175f4a2713aSLionel Sambuc 
176