llvm.org GIT mirror llvm / 8a93c3a
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 Dmitri Gribenko 6 years ago
4 changed file(s) with 178 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
143143 ///
144144 /// @param ShowColors - Display colored messages if output is a terminal and
145145 /// the default error handler is used.
146 void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind,
147 const Twine &Msg,
148 ArrayRef Ranges = None,
149 ArrayRef FixIts = None,
150 bool ShowColors = true) const;
151
152 /// Emits a diagnostic to llvm::errs().
146153 void PrintMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg,
147154 ArrayRef Ranges = None,
148155 ArrayRef FixIts = None,
149156 bool ShowColors = true) const;
150
151157
152158 /// GetMessage - Return an SMDiagnostic at the specified location with the
153159 /// specified string.
210210 LineStr, ColRanges, FixIts);
211211 }
212212
213 void SourceMgr::PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
213 void SourceMgr::PrintMessage(raw_ostream &OS, SMLoc Loc,
214 SourceMgr::DiagKind Kind,
214215 const Twine &Msg, ArrayRef Ranges,
215216 ArrayRef FixIts, bool ShowColors) const {
216217 SMDiagnostic Diagnostic = GetMessage(Loc, Kind, Msg, Ranges, FixIts);
221222 return;
222223 }
223224
224 raw_ostream &OS = errs();
225
226225 if (Loc != SMLoc()) {
227226 int CurBuf = FindBufferContainingLoc(Loc);
228227 assert(CurBuf != -1 && "Invalid or unspecified location!");
230229 }
231230
232231 Diagnostic.print(0, OS, ShowColors);
232 }
233
234 void SourceMgr::PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
235 const Twine &Msg, ArrayRef Ranges,
236 ArrayRef FixIts, bool ShowColors) const {
237 PrintMessage(llvm::errs(), Loc, Kind, Msg, Ranges, FixIts, ShowColors);
233238 }
234239
235240 //===----------------------------------------------------------------------===//
2727 ProcessTest.cpp
2828 ProgramTest.cpp
2929 RegexTest.cpp
30 SourceMgrTest.cpp
3031 SwapByteOrderTest.cpp
3132 TimeValueTest.cpp
3233 UnicodeTest.cpp
0 //===- unittests/Support/SourceMgrTest.cpp - SourceMgr tests --------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Support/SourceMgr.h"
10 #include "llvm/Support/MemoryBuffer.h"
11 #include "llvm/Support/raw_ostream.h"
12 #include "gtest/gtest.h"
13
14 using namespace llvm;
15
16 namespace {
17
18 class SourceMgrTest : public testing::Test {
19 public:
20 SourceMgr SM;
21 unsigned MainBufferID;
22 std::string Output;
23
24 void setMainBuffer(StringRef Text, StringRef BufferName) {
25 MemoryBuffer *MainBuffer = MemoryBuffer::getMemBuffer(Text, BufferName);
26 MainBufferID = SM.AddNewSourceBuffer(MainBuffer, llvm::SMLoc());
27 }
28
29 SMLoc getLoc(unsigned Offset) {
30 return SMLoc::getFromPointer(
31 SM.getMemoryBuffer(MainBufferID)->getBufferStart() + Offset);
32 }
33
34 SMRange getRange(unsigned Offset, unsigned Length) {
35 return SMRange(getLoc(Offset), getLoc(Offset + Length));
36 }
37
38 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
39 const Twine &Msg, ArrayRef Ranges,
40 ArrayRef FixIts) {
41 raw_string_ostream OS(Output);
42 SM.PrintMessage(OS, Loc, Kind, Msg, Ranges, FixIts);
43 }
44 };
45
46 } // unnamed namespace
47
48 TEST_F(SourceMgrTest, BasicError) {
49 setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
50 printMessage(getLoc(4), SourceMgr::DK_Error, "message", None, None);
51
52 EXPECT_EQ("file.in:1:5: error: message\n"
53 "aaa bbb\n"
54 " ^\n",
55 Output);
56 }
57
58 TEST_F(SourceMgrTest, BasicWarning) {
59 setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
60 printMessage(getLoc(4), SourceMgr::DK_Warning, "message", None, None);
61
62 EXPECT_EQ("file.in:1:5: warning: message\n"
63 "aaa bbb\n"
64 " ^\n",
65 Output);
66 }
67
68 TEST_F(SourceMgrTest, BasicNote) {
69 setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
70 printMessage(getLoc(4), SourceMgr::DK_Note, "message", None, None);
71
72 EXPECT_EQ("file.in:1:5: note: message\n"
73 "aaa bbb\n"
74 " ^\n",
75 Output);
76 }
77
78 TEST_F(SourceMgrTest, LocationAtEndOfLine) {
79 setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
80 printMessage(getLoc(6), SourceMgr::DK_Error, "message", None, None);
81
82 EXPECT_EQ("file.in:1:7: error: message\n"
83 "aaa bbb\n"
84 " ^\n",
85 Output);
86 }
87
88 TEST_F(SourceMgrTest, LocationAtNewline) {
89 setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
90 printMessage(getLoc(7), SourceMgr::DK_Error, "message", None, None);
91
92 EXPECT_EQ("file.in:1:8: error: message\n"
93 "aaa bbb\n"
94 " ^\n",
95 Output);
96 }
97
98 TEST_F(SourceMgrTest, BasicRange) {
99 setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
100 printMessage(getLoc(4), SourceMgr::DK_Error, "message", getRange(4, 3), None);
101
102 EXPECT_EQ("file.in:1:5: error: message\n"
103 "aaa bbb\n"
104 " ^~~\n",
105 Output);
106 }
107
108 TEST_F(SourceMgrTest, RangeWithTab) {
109 setMainBuffer("aaa\tbbb\nccc ddd\n", "file.in");
110 printMessage(getLoc(4), SourceMgr::DK_Error, "message", getRange(3, 3), None);
111
112 EXPECT_EQ("file.in:1:5: error: message\n"
113 "aaa bbb\n"
114 " ~~~~~^~\n",
115 Output);
116 }
117
118 TEST_F(SourceMgrTest, MultiLineRange) {
119 setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
120 printMessage(getLoc(4), SourceMgr::DK_Error, "message", getRange(4, 7), None);
121
122 EXPECT_EQ("file.in:1:5: error: message\n"
123 "aaa bbb\n"
124 " ^~~\n",
125 Output);
126 }
127
128 TEST_F(SourceMgrTest, MultipleRanges) {
129 setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
130 SMRange Ranges[] = { getRange(0, 3), getRange(4, 3) };
131 printMessage(getLoc(4), SourceMgr::DK_Error, "message", Ranges, None);
132
133 EXPECT_EQ("file.in:1:5: error: message\n"
134 "aaa bbb\n"
135 "~~~ ^~~\n",
136 Output);
137 }
138
139 TEST_F(SourceMgrTest, OverlappingRanges) {
140 setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
141 SMRange Ranges[] = { getRange(0, 3), getRange(2, 4) };
142 printMessage(getLoc(4), SourceMgr::DK_Error, "message", Ranges, None);
143
144 EXPECT_EQ("file.in:1:5: error: message\n"
145 "aaa bbb\n"
146 "~~~~^~\n",
147 Output);
148 }
149
150 TEST_F(SourceMgrTest, BasicFixit) {
151 setMainBuffer("aaa bbb\nccc ddd\n", "file.in");
152 printMessage(getLoc(4), SourceMgr::DK_Error, "message", None,
153 makeArrayRef(SMFixIt(getRange(4, 3), "zzz")));
154
155 EXPECT_EQ("file.in:1:5: error: message\n"
156 "aaa bbb\n"
157 " ^~~\n"
158 " zzz\n",
159 Output);
160 }
161