blob: e49f64cef139e8d9d0a8a2ac65e49b60b7596f84 [file] [log] [blame]
Sam McCallb536a2a2017-12-19 12:23:48 +00001//===-- SourceCodeTests.cpp ------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Sam McCallb536a2a2017-12-19 12:23:48 +00006//
7//===----------------------------------------------------------------------===//
Ilya Biryukov060f7fe2019-01-29 14:31:19 +00008#include "Annotations.h"
Sam McCalla69698f2019-03-27 17:47:49 +00009#include "Context.h"
10#include "Protocol.h"
Sam McCallb536a2a2017-12-19 12:23:48 +000011#include "SourceCode.h"
Haojian Wu9d34f452019-07-01 09:26:48 +000012#include "TestTU.h"
Shaurya Gupta0d26d6f2019-07-12 11:42:31 +000013#include "clang/Basic/LangOptions.h"
Sam McCallc791d852019-08-27 08:44:06 +000014#include "clang/Basic/SourceLocation.h"
Eric Liu00d99bd2019-04-11 09:36:36 +000015#include "clang/Format/Format.h"
Simon Marchi766338a2018-03-21 14:36:46 +000016#include "llvm/Support/Error.h"
Sam McCallb536a2a2017-12-19 12:23:48 +000017#include "llvm/Support/raw_os_ostream.h"
Sam McCallc791d852019-08-27 08:44:06 +000018#include "llvm/Testing/Support/Annotations.h"
Simon Marchi766338a2018-03-21 14:36:46 +000019#include "llvm/Testing/Support/Error.h"
Sam McCallb536a2a2017-12-19 12:23:48 +000020#include "gmock/gmock.h"
21#include "gtest/gtest.h"
22
Sam McCallc008af62018-10-20 15:30:37 +000023namespace clang {
Sam McCallb536a2a2017-12-19 12:23:48 +000024namespace clangd {
Sam McCallb536a2a2017-12-19 12:23:48 +000025namespace {
26
Ilya Biryukov060f7fe2019-01-29 14:31:19 +000027using llvm::Failed;
28using llvm::HasValue;
Sam McCall9fb22b22019-05-06 10:25:10 +000029using ::testing::UnorderedElementsAreArray;
Ilya Biryukov060f7fe2019-01-29 14:31:19 +000030
Sam McCallb536a2a2017-12-19 12:23:48 +000031MATCHER_P2(Pos, Line, Col, "") {
Sam McCalla69698f2019-03-27 17:47:49 +000032 return arg.line == int(Line) && arg.character == int(Col);
Sam McCallb536a2a2017-12-19 12:23:48 +000033}
34
Haojian Wu9d34f452019-07-01 09:26:48 +000035MATCHER_P(MacroName, Name, "") { return arg.Name == Name; }
36
Ilya Biryukov7beea3a2018-02-14 10:52:04 +000037/// A helper to make tests easier to read.
38Position position(int line, int character) {
39 Position Pos;
40 Pos.line = line;
41 Pos.character = character;
42 return Pos;
43}
44
Kadir Cetinkayaa9c9d002018-08-13 08:23:01 +000045Range range(const std::pair<int, int> p1, const std::pair<int, int> p2) {
46 Range range;
47 range.start = position(p1.first, p1.second);
48 range.end = position(p2.first, p2.second);
49 return range;
50}
51
Sam McCall71891122018-10-23 11:51:53 +000052TEST(SourceCodeTests, lspLength) {
53 EXPECT_EQ(lspLength(""), 0UL);
54 EXPECT_EQ(lspLength("ascii"), 5UL);
55 // BMP
56 EXPECT_EQ(lspLength("↓"), 1UL);
57 EXPECT_EQ(lspLength("¥"), 1UL);
58 // astral
59 EXPECT_EQ(lspLength("😂"), 2UL);
Sam McCalla69698f2019-03-27 17:47:49 +000060
61 WithContextValue UTF8(kCurrentOffsetEncoding, OffsetEncoding::UTF8);
62 EXPECT_EQ(lspLength(""), 0UL);
63 EXPECT_EQ(lspLength("ascii"), 5UL);
64 // BMP
65 EXPECT_EQ(lspLength("↓"), 3UL);
66 EXPECT_EQ(lspLength("¥"), 2UL);
67 // astral
68 EXPECT_EQ(lspLength("😂"), 4UL);
Sam McCall8b25d222019-03-28 14:37:51 +000069
70 WithContextValue UTF32(kCurrentOffsetEncoding, OffsetEncoding::UTF32);
71 EXPECT_EQ(lspLength(""), 0UL);
72 EXPECT_EQ(lspLength("ascii"), 5UL);
73 // BMP
74 EXPECT_EQ(lspLength("↓"), 1UL);
75 EXPECT_EQ(lspLength("¥"), 1UL);
76 // astral
77 EXPECT_EQ(lspLength("😂"), 1UL);
Sam McCall71891122018-10-23 11:51:53 +000078}
79
Sam McCalla69698f2019-03-27 17:47:49 +000080// The = → 🡆 below are ASCII (1 byte), BMP (3 bytes), and astral (4 bytes).
81const char File[] = R"(0:0 = 0
821:0 8
832:0 🡆 18)";
84struct Line {
85 unsigned Number;
86 unsigned Offset;
87 unsigned Length;
88};
89Line FileLines[] = {Line{0, 0, 7}, Line{1, 8, 9}, Line{2, 18, 11}};
90
Sam McCallb536a2a2017-12-19 12:23:48 +000091TEST(SourceCodeTests, PositionToOffset) {
92 // line out of bounds
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000093 EXPECT_THAT_EXPECTED(positionToOffset(File, position(-1, 2)), llvm::Failed());
Sam McCallb536a2a2017-12-19 12:23:48 +000094 // first line
Simon Marchi766338a2018-03-21 14:36:46 +000095 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, -1)),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000096 llvm::Failed()); // out of range
Simon Marchi766338a2018-03-21 14:36:46 +000097 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 0)),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000098 llvm::HasValue(0)); // first character
Simon Marchi766338a2018-03-21 14:36:46 +000099 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 3)),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000100 llvm::HasValue(3)); // middle character
Simon Marchi766338a2018-03-21 14:36:46 +0000101 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 6)),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000102 llvm::HasValue(6)); // last character
Simon Marchi766338a2018-03-21 14:36:46 +0000103 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 7)),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000104 llvm::HasValue(7)); // the newline itself
Simon Marchi766338a2018-03-21 14:36:46 +0000105 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 7), false),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000106 llvm::HasValue(7));
Simon Marchi766338a2018-03-21 14:36:46 +0000107 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 8)),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000108 llvm::HasValue(7)); // out of range
Simon Marchi766338a2018-03-21 14:36:46 +0000109 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 8), false),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000110 llvm::Failed()); // out of range
Sam McCallb536a2a2017-12-19 12:23:48 +0000111 // middle line
Simon Marchi766338a2018-03-21 14:36:46 +0000112 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, -1)),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000113 llvm::Failed()); // out of range
Simon Marchi766338a2018-03-21 14:36:46 +0000114 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 0)),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000115 llvm::HasValue(8)); // first character
Simon Marchi766338a2018-03-21 14:36:46 +0000116 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 3)),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000117 llvm::HasValue(11)); // middle character
Simon Marchi766338a2018-03-21 14:36:46 +0000118 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 3), false),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000119 llvm::HasValue(11));
Simon Marchi766338a2018-03-21 14:36:46 +0000120 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 6)),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000121 llvm::HasValue(16)); // last character
Simon Marchi766338a2018-03-21 14:36:46 +0000122 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 7)),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000123 llvm::HasValue(17)); // the newline itself
Simon Marchi766338a2018-03-21 14:36:46 +0000124 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 8)),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000125 llvm::HasValue(17)); // out of range
Simon Marchi766338a2018-03-21 14:36:46 +0000126 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 8), false),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000127 llvm::Failed()); // out of range
Sam McCallb536a2a2017-12-19 12:23:48 +0000128 // last line
Simon Marchi766338a2018-03-21 14:36:46 +0000129 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, -1)),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000130 llvm::Failed()); // out of range
Simon Marchi766338a2018-03-21 14:36:46 +0000131 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 0)),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000132 llvm::HasValue(18)); // first character
Simon Marchi766338a2018-03-21 14:36:46 +0000133 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 3)),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000134 llvm::HasValue(21)); // middle character
Sam McCalla4962cc2018-04-27 11:59:28 +0000135 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 5), false),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000136 llvm::Failed()); // middle of surrogate pair
Sam McCalla4962cc2018-04-27 11:59:28 +0000137 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 5)),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000138 llvm::HasValue(26)); // middle of surrogate pair
Sam McCalla4962cc2018-04-27 11:59:28 +0000139 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 6), false),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000140 llvm::HasValue(26)); // end of surrogate pair
Simon Marchi766338a2018-03-21 14:36:46 +0000141 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 8)),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000142 llvm::HasValue(28)); // last character
Sam McCalla4962cc2018-04-27 11:59:28 +0000143 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 9)),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000144 llvm::HasValue(29)); // EOF
Sam McCalla4962cc2018-04-27 11:59:28 +0000145 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 10), false),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000146 llvm::Failed()); // out of range
Sam McCallb536a2a2017-12-19 12:23:48 +0000147 // line out of bounds
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000148 EXPECT_THAT_EXPECTED(positionToOffset(File, position(3, 0)), llvm::Failed());
149 EXPECT_THAT_EXPECTED(positionToOffset(File, position(3, 1)), llvm::Failed());
Sam McCalla69698f2019-03-27 17:47:49 +0000150
Sam McCall8b25d222019-03-28 14:37:51 +0000151 // Codepoints are similar, except near astral characters.
152 WithContextValue UTF32(kCurrentOffsetEncoding, OffsetEncoding::UTF32);
153 // line out of bounds
154 EXPECT_THAT_EXPECTED(positionToOffset(File, position(-1, 2)), llvm::Failed());
155 // first line
156 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, -1)),
157 llvm::Failed()); // out of range
158 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 0)),
159 llvm::HasValue(0)); // first character
160 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 3)),
161 llvm::HasValue(3)); // middle character
162 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 6)),
163 llvm::HasValue(6)); // last character
164 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 7)),
165 llvm::HasValue(7)); // the newline itself
166 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 7), false),
167 llvm::HasValue(7));
168 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 8)),
169 llvm::HasValue(7)); // out of range
170 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 8), false),
171 llvm::Failed()); // out of range
172 // middle line
173 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, -1)),
174 llvm::Failed()); // out of range
175 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 0)),
176 llvm::HasValue(8)); // first character
177 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 3)),
178 llvm::HasValue(11)); // middle character
179 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 3), false),
180 llvm::HasValue(11));
181 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 6)),
182 llvm::HasValue(16)); // last character
183 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 7)),
184 llvm::HasValue(17)); // the newline itself
185 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 8)),
186 llvm::HasValue(17)); // out of range
187 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 8), false),
188 llvm::Failed()); // out of range
189 // last line
190 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, -1)),
191 llvm::Failed()); // out of range
192 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 0)),
193 llvm::HasValue(18)); // first character
194 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 4)),
195 llvm::HasValue(22)); // Before astral character.
196 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 5), false),
197 llvm::HasValue(26)); // after astral character
198 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 7)),
199 llvm::HasValue(28)); // last character
200 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 8)),
201 llvm::HasValue(29)); // EOF
202 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 9), false),
203 llvm::Failed()); // out of range
204 // line out of bounds
205 EXPECT_THAT_EXPECTED(positionToOffset(File, position(3, 0)), llvm::Failed());
206 EXPECT_THAT_EXPECTED(positionToOffset(File, position(3, 1)), llvm::Failed());
207
Sam McCalla69698f2019-03-27 17:47:49 +0000208 // Test UTF-8, where transformations are trivial.
209 WithContextValue UTF8(kCurrentOffsetEncoding, OffsetEncoding::UTF8);
210 EXPECT_THAT_EXPECTED(positionToOffset(File, position(-1, 2)), llvm::Failed());
211 EXPECT_THAT_EXPECTED(positionToOffset(File, position(3, 0)), llvm::Failed());
212 for (Line L : FileLines) {
213 EXPECT_THAT_EXPECTED(positionToOffset(File, position(L.Number, -1)),
214 llvm::Failed()); // out of range
215 for (unsigned I = 0; I <= L.Length; ++I)
216 EXPECT_THAT_EXPECTED(positionToOffset(File, position(L.Number, I)),
217 llvm::HasValue(L.Offset + I));
218 EXPECT_THAT_EXPECTED(positionToOffset(File, position(L.Number, L.Length+1)),
219 llvm::HasValue(L.Offset + L.Length));
220 EXPECT_THAT_EXPECTED(
221 positionToOffset(File, position(L.Number, L.Length + 1), false),
222 llvm::Failed()); // out of range
223 }
Sam McCallb536a2a2017-12-19 12:23:48 +0000224}
225
226TEST(SourceCodeTests, OffsetToPosition) {
227 EXPECT_THAT(offsetToPosition(File, 0), Pos(0, 0)) << "start of file";
228 EXPECT_THAT(offsetToPosition(File, 3), Pos(0, 3)) << "in first line";
229 EXPECT_THAT(offsetToPosition(File, 6), Pos(0, 6)) << "end of first line";
230 EXPECT_THAT(offsetToPosition(File, 7), Pos(0, 7)) << "first newline";
231 EXPECT_THAT(offsetToPosition(File, 8), Pos(1, 0)) << "start of second line";
Sam McCalla4962cc2018-04-27 11:59:28 +0000232 EXPECT_THAT(offsetToPosition(File, 12), Pos(1, 4)) << "before BMP char";
233 EXPECT_THAT(offsetToPosition(File, 13), Pos(1, 5)) << "in BMP char";
234 EXPECT_THAT(offsetToPosition(File, 15), Pos(1, 5)) << "after BMP char";
235 EXPECT_THAT(offsetToPosition(File, 16), Pos(1, 6)) << "end of second line";
236 EXPECT_THAT(offsetToPosition(File, 17), Pos(1, 7)) << "second newline";
237 EXPECT_THAT(offsetToPosition(File, 18), Pos(2, 0)) << "start of last line";
238 EXPECT_THAT(offsetToPosition(File, 21), Pos(2, 3)) << "in last line";
239 EXPECT_THAT(offsetToPosition(File, 22), Pos(2, 4)) << "before astral char";
240 EXPECT_THAT(offsetToPosition(File, 24), Pos(2, 6)) << "in astral char";
241 EXPECT_THAT(offsetToPosition(File, 26), Pos(2, 6)) << "after astral char";
242 EXPECT_THAT(offsetToPosition(File, 28), Pos(2, 8)) << "end of last line";
243 EXPECT_THAT(offsetToPosition(File, 29), Pos(2, 9)) << "EOF";
244 EXPECT_THAT(offsetToPosition(File, 30), Pos(2, 9)) << "out of bounds";
Sam McCalla69698f2019-03-27 17:47:49 +0000245
Sam McCall8b25d222019-03-28 14:37:51 +0000246 // Codepoints are similar, except near astral characters.
247 WithContextValue UTF32(kCurrentOffsetEncoding, OffsetEncoding::UTF32);
248 EXPECT_THAT(offsetToPosition(File, 0), Pos(0, 0)) << "start of file";
249 EXPECT_THAT(offsetToPosition(File, 3), Pos(0, 3)) << "in first line";
250 EXPECT_THAT(offsetToPosition(File, 6), Pos(0, 6)) << "end of first line";
251 EXPECT_THAT(offsetToPosition(File, 7), Pos(0, 7)) << "first newline";
252 EXPECT_THAT(offsetToPosition(File, 8), Pos(1, 0)) << "start of second line";
253 EXPECT_THAT(offsetToPosition(File, 12), Pos(1, 4)) << "before BMP char";
254 EXPECT_THAT(offsetToPosition(File, 13), Pos(1, 5)) << "in BMP char";
255 EXPECT_THAT(offsetToPosition(File, 15), Pos(1, 5)) << "after BMP char";
256 EXPECT_THAT(offsetToPosition(File, 16), Pos(1, 6)) << "end of second line";
257 EXPECT_THAT(offsetToPosition(File, 17), Pos(1, 7)) << "second newline";
258 EXPECT_THAT(offsetToPosition(File, 18), Pos(2, 0)) << "start of last line";
259 EXPECT_THAT(offsetToPosition(File, 21), Pos(2, 3)) << "in last line";
260 EXPECT_THAT(offsetToPosition(File, 22), Pos(2, 4)) << "before astral char";
261 EXPECT_THAT(offsetToPosition(File, 24), Pos(2, 5)) << "in astral char";
262 EXPECT_THAT(offsetToPosition(File, 26), Pos(2, 5)) << "after astral char";
263 EXPECT_THAT(offsetToPosition(File, 28), Pos(2, 7)) << "end of last line";
264 EXPECT_THAT(offsetToPosition(File, 29), Pos(2, 8)) << "EOF";
265 EXPECT_THAT(offsetToPosition(File, 30), Pos(2, 8)) << "out of bounds";
266
Sam McCalla69698f2019-03-27 17:47:49 +0000267 WithContextValue UTF8(kCurrentOffsetEncoding, OffsetEncoding::UTF8);
268 for (Line L : FileLines) {
269 for (unsigned I = 0; I <= L.Length; ++I)
270 EXPECT_THAT(offsetToPosition(File, L.Offset + I), Pos(L.Number, I));
271 }
272 EXPECT_THAT(offsetToPosition(File, 30), Pos(2, 11)) << "out of bounds";
Sam McCallb536a2a2017-12-19 12:23:48 +0000273}
274
Kadir Cetinkayaa9c9d002018-08-13 08:23:01 +0000275TEST(SourceCodeTests, IsRangeConsecutive) {
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000276 EXPECT_TRUE(isRangeConsecutive(range({2, 2}, {2, 3}), range({2, 3}, {2, 4})));
Kadir Cetinkayaa9c9d002018-08-13 08:23:01 +0000277 EXPECT_FALSE(
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000278 isRangeConsecutive(range({0, 2}, {0, 3}), range({2, 3}, {2, 4})));
Kadir Cetinkayaa9c9d002018-08-13 08:23:01 +0000279 EXPECT_FALSE(
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000280 isRangeConsecutive(range({2, 2}, {2, 3}), range({2, 4}, {2, 5})));
Kadir Cetinkayaa9c9d002018-08-13 08:23:01 +0000281}
282
Ilya Biryukov060f7fe2019-01-29 14:31:19 +0000283TEST(SourceCodeTests, SourceLocationInMainFile) {
284 Annotations Source(R"cpp(
285 ^in^t ^foo
286 ^bar
287 ^baz ^() {} {} {} {} { }^
288)cpp");
289
290 SourceManagerForFile Owner("foo.cpp", Source.code());
291 SourceManager &SM = Owner.get();
292
293 SourceLocation StartOfFile = SM.getLocForStartOfFile(SM.getMainFileID());
294 EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(0, 0)),
295 HasValue(StartOfFile));
296 // End of file.
297 EXPECT_THAT_EXPECTED(
298 sourceLocationInMainFile(SM, position(4, 0)),
299 HasValue(StartOfFile.getLocWithOffset(Source.code().size())));
300 // Column number is too large.
301 EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(0, 1)), Failed());
302 EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(0, 100)),
303 Failed());
304 EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(4, 1)), Failed());
305 // Line number is too large.
306 EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(5, 0)), Failed());
307 // Check all positions mentioned in the test return valid results.
308 for (auto P : Source.points()) {
309 size_t Offset = llvm::cantFail(positionToOffset(Source.code(), P));
310 EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, P),
311 HasValue(StartOfFile.getLocWithOffset(Offset)));
312 }
313}
314
Sam McCall915f9782019-09-04 09:46:06 +0000315TEST(SourceCodeTests, GetBeginningOfIdentifier) {
Sam McCall19cefc22019-09-03 15:34:47 +0000316 std::string Preamble = R"cpp(
317struct Bar { int func(); };
318#define MACRO(X) void f() { X; }
319Bar* bar;
320 )cpp";
321 // First ^ is the expected beginning, last is the search position.
Haojian Wu9f2bf662019-10-01 11:03:56 +0000322 for (const std::string &Text : std::vector<std::string>{
Sam McCall19cefc22019-09-03 15:34:47 +0000323 "int ^f^oo();", // inside identifier
324 "int ^foo();", // beginning of identifier
325 "int ^foo^();", // end of identifier
326 "int foo(^);", // non-identifier
327 "^int foo();", // beginning of file (can't back up)
328 "int ^f0^0();", // after a digit (lexing at N-1 is wrong)
Haojian Wu9f2bf662019-10-01 11:03:56 +0000329 "/^/ comments", // non-interesting token
330 "void f(int abc) { abc ^ ++; }", // whitespace
331 "void f(int abc) { ^abc^++; }", // range of identifier
332 "void f(int abc) { ++^abc^; }", // range of identifier
333 "void f(int abc) { ++^abc; }", // range of identifier
334 "void f(int abc) { ^+^+abc; }", // range of operator
335 "void f(int abc) { ^abc^ ++; }", // range of identifier
336 "void f(int abc) { abc ^++^; }", // range of operator
337 "void f(int abc) { ^++^ abc; }", // range of operator
338 "void f(int abc) { ++ ^abc^; }", // range of identifier
339 "void f(int abc) { ^++^/**/abc; }", // range of operator
340 "void f(int abc) { ++/**/^abc; }", // range of identifier
341 "void f(int abc) { ^abc^/**/++; }", // range of identifier
342 "void f(int abc) { abc/**/^++; }", // range of operator
343 "void f() {^ }", // outside of identifier and operator
344 "int ^λλ^λ();", // UTF-8 handled properly when backing up
Sam McCall19cefc22019-09-03 15:34:47 +0000345
346 // identifier in macro arg
347 "MACRO(bar->^func())", // beginning of identifier
348 "MACRO(bar->^fun^c())", // inside identifier
349 "MACRO(bar->^func^())", // end of identifier
350 "MACRO(^bar->func())", // begin identifier
351 "MACRO(^bar^->func())", // end identifier
352 "^MACRO(bar->func())", // beginning of macro name
353 "^MAC^RO(bar->func())", // inside macro name
354 "^MACRO^(bar->func())", // end of macro name
355 }) {
356 std::string WithPreamble = Preamble + Text;
357 Annotations TestCase(WithPreamble);
358 auto AST = TestTU::withCode(TestCase.code()).build();
359 const auto &SourceMgr = AST.getSourceManager();
360 SourceLocation Actual = getBeginningOfIdentifier(
361 TestCase.points().back(), SourceMgr, AST.getASTContext().getLangOpts());
362 Position ActualPos = offsetToPosition(
363 TestCase.code(),
364 SourceMgr.getFileOffset(SourceMgr.getSpellingLoc(Actual)));
365 EXPECT_EQ(TestCase.points().front(), ActualPos) << Text;
366 }
367}
368
Eric Liu00d99bd2019-04-11 09:36:36 +0000369TEST(SourceCodeTests, CollectIdentifiers) {
370 auto Style = format::getLLVMStyle();
371 auto IDs = collectIdentifiers(R"cpp(
372 #include "a.h"
373 void foo() { int xyz; int abc = xyz; return foo(); }
374 )cpp",
375 Style);
376 EXPECT_EQ(IDs.size(), 7u);
377 EXPECT_EQ(IDs["include"], 1u);
378 EXPECT_EQ(IDs["void"], 1u);
379 EXPECT_EQ(IDs["int"], 2u);
380 EXPECT_EQ(IDs["xyz"], 2u);
381 EXPECT_EQ(IDs["abc"], 1u);
382 EXPECT_EQ(IDs["return"], 1u);
383 EXPECT_EQ(IDs["foo"], 2u);
384}
385
Sam McCall9fb22b22019-05-06 10:25:10 +0000386TEST(SourceCodeTests, CollectWords) {
387 auto Words = collectWords(R"cpp(
388 #define FIZZ_BUZZ
389 // this is a comment
390 std::string getSomeText() { return "magic word"; }
391 )cpp");
392 std::set<std::string> ActualWords(Words.keys().begin(), Words.keys().end());
393 std::set<std::string> ExpectedWords = {"define", "fizz", "buzz", "this",
394 "comment", "string", "some", "text",
395 "return", "magic", "word"};
396 EXPECT_EQ(ActualWords, ExpectedWords);
397}
398
Sam McCallc316b222019-04-26 07:45:49 +0000399TEST(SourceCodeTests, VisibleNamespaces) {
400 std::vector<std::pair<const char *, std::vector<std::string>>> Cases = {
401 {
402 R"cpp(
403 // Using directive resolved against enclosing namespaces.
404 using namespace foo;
405 namespace ns {
406 using namespace bar;
407 )cpp",
408 {"ns", "", "bar", "foo", "ns::bar"},
409 },
410 {
411 R"cpp(
412 // Don't include namespaces we've closed, ignore namespace aliases.
413 using namespace clang;
414 using std::swap;
415 namespace clang {
416 namespace clangd {}
417 namespace ll = ::llvm;
418 }
419 namespace clang {
420 )cpp",
421 {"clang", ""},
422 },
423 {
424 R"cpp(
425 // Using directives visible even if a namespace is reopened.
426 // Ignore anonymous namespaces.
427 namespace foo{ using namespace bar; }
428 namespace foo{ namespace {
429 )cpp",
430 {"foo", "", "bar", "foo::bar"},
431 },
432 {
433 R"cpp(
434 // Mismatched braces
435 namespace foo{}
436 }}}
437 namespace bar{
438 )cpp",
439 {"bar", ""},
440 },
441 {
442 R"cpp(
443 // Namespaces with multiple chunks.
444 namespace a::b {
445 using namespace c::d;
446 namespace e::f {
447 )cpp",
448 {
449 "a::b::e::f",
450 "",
451 "a",
452 "a::b",
453 "a::b::c::d",
454 "a::b::e",
455 "a::c::d",
456 "c::d",
457 },
458 },
Kadir Cetinkaya194117f2019-09-25 14:12:05 +0000459 {
460 "",
461 {""},
462 },
463 {
464 R"cpp(
465 // Parse until EOF
466 namespace bar{})cpp",
467 {""},
468 },
Sam McCallc316b222019-04-26 07:45:49 +0000469 };
470 for (const auto& Case : Cases) {
471 EXPECT_EQ(Case.second,
472 visibleNamespaces(Case.first, format::getLLVMStyle()))
473 << Case.first;
474 }
475}
476
Haojian Wu9d34f452019-07-01 09:26:48 +0000477TEST(SourceCodeTests, GetMacros) {
478 Annotations Code(R"cpp(
479 #define MACRO 123
480 int abc = MA^CRO;
481 )cpp");
482 TestTU TU = TestTU::withCode(Code.code());
483 auto AST = TU.build();
Sam McCall19cefc22019-09-03 15:34:47 +0000484 auto Loc = getBeginningOfIdentifier(Code.point(), AST.getSourceManager(),
485 AST.getASTContext().getLangOpts());
Haojian Wu9d34f452019-07-01 09:26:48 +0000486 auto Result = locateMacroAt(Loc, AST.getPreprocessor());
487 ASSERT_TRUE(Result);
488 EXPECT_THAT(*Result, MacroName("MACRO"));
489}
490
Haojian Wu6ae86ea2019-07-19 08:33:39 +0000491TEST(SourceCodeTests, IsInsideMainFile){
492 TestTU TU;
493 TU.HeaderCode = R"cpp(
494 #define DEFINE_CLASS(X) class X {};
495 #define DEFINE_YY DEFINE_CLASS(YY)
496
497 class Header1 {};
498 DEFINE_CLASS(Header2)
499 class Header {};
500 )cpp";
501 TU.Code = R"cpp(
502 class Main1 {};
503 DEFINE_CLASS(Main2)
504 DEFINE_YY
505 class Main {};
506 )cpp";
507 TU.ExtraArgs.push_back("-DHeader=Header3");
508 TU.ExtraArgs.push_back("-DMain=Main3");
509 auto AST = TU.build();
510 const auto& SM = AST.getSourceManager();
511 auto DeclLoc = [&AST](llvm::StringRef Name) {
512 return findDecl(AST, Name).getLocation();
513 };
514 for (const auto *HeaderDecl : {"Header1", "Header2", "Header3"})
515 EXPECT_FALSE(isInsideMainFile(DeclLoc(HeaderDecl), SM));
516
517 for (const auto *MainDecl : {"Main1", "Main2", "Main3", "YY"})
518 EXPECT_TRUE(isInsideMainFile(DeclLoc(MainDecl), SM));
519}
520
Shaurya Gupta0d26d6f2019-07-12 11:42:31 +0000521// Test for functions toHalfOpenFileRange and getHalfOpenFileRange
522TEST(SourceCodeTests, HalfOpenFileRange) {
523 // Each marked range should be the file range of the decl with the same name
524 // and each name should be unique.
525 Annotations Test(R"cpp(
526 #define FOO(X, Y) int Y = ++X
527 #define BAR(X) X + 1
528 #define ECHO(X) X
Shaurya Gupta8fbb6ce2019-08-06 17:01:12 +0000529
530 #define BUZZ BAZZ(ADD)
531 #define BAZZ(m) m(1)
532 #define ADD(a) int f = a + 1;
Shaurya Gupta0d26d6f2019-07-12 11:42:31 +0000533 template<typename T>
534 class P {};
Shaurya Gupta8fbb6ce2019-08-06 17:01:12 +0000535
536 int main() {
Shaurya Gupta0d26d6f2019-07-12 11:42:31 +0000537 $a[[P<P<P<P<P<int>>>>> a]];
538 $b[[int b = 1]];
539 $c[[FOO(b, c)]];
540 $d[[FOO(BAR(BAR(b)), d)]];
541 // FIXME: We might want to select everything inside the outer ECHO.
542 ECHO(ECHO($e[[int) ECHO(e]]));
Shaurya Gupta8fbb6ce2019-08-06 17:01:12 +0000543 // Shouldn't crash.
544 $f[[BUZZ]];
Shaurya Gupta0d26d6f2019-07-12 11:42:31 +0000545 }
546 )cpp");
547
548 ParsedAST AST = TestTU::withCode(Test.code()).build();
549 llvm::errs() << Test.code();
550 const SourceManager &SM = AST.getSourceManager();
551 const LangOptions &LangOpts = AST.getASTContext().getLangOpts();
552 // Turn a SourceLocation into a pair of positions
553 auto SourceRangeToRange = [&SM](SourceRange SrcRange) {
554 return Range{sourceLocToPosition(SM, SrcRange.getBegin()),
555 sourceLocToPosition(SM, SrcRange.getEnd())};
556 };
557 auto CheckRange = [&](llvm::StringRef Name) {
558 const NamedDecl &Decl = findUnqualifiedDecl(AST, Name);
559 auto FileRange = toHalfOpenFileRange(SM, LangOpts, Decl.getSourceRange());
560 SCOPED_TRACE("Checking range: " + Name);
561 ASSERT_NE(FileRange, llvm::None);
562 Range HalfOpenRange = SourceRangeToRange(*FileRange);
563 EXPECT_EQ(HalfOpenRange, Test.ranges(Name)[0]);
564 };
565
566 CheckRange("a");
567 CheckRange("b");
568 CheckRange("c");
569 CheckRange("d");
570 CheckRange("e");
Shaurya Gupta8fbb6ce2019-08-06 17:01:12 +0000571 CheckRange("f");
Shaurya Gupta0d26d6f2019-07-12 11:42:31 +0000572}
573
Sam McCallc791d852019-08-27 08:44:06 +0000574TEST(SourceCodeTests, HalfOpenFileRangePathologicalPreprocessor) {
575 const char *Case = R"cpp(
576#define MACRO while(1)
577 void test() {
578[[#include "Expand.inc"
579 br^eak]];
580 }
581 )cpp";
582 Annotations Test(Case);
583 auto TU = TestTU::withCode(Test.code());
584 TU.AdditionalFiles["Expand.inc"] = "MACRO\n";
585 auto AST = TU.build();
586
587 const auto &Func = cast<FunctionDecl>(findDecl(AST, "test"));
588 const auto &Body = cast<CompoundStmt>(Func.getBody());
589 const auto &Loop = cast<WhileStmt>(*Body->child_begin());
590 llvm::Optional<SourceRange> Range = toHalfOpenFileRange(
591 AST.getSourceManager(), AST.getASTContext().getLangOpts(),
592 Loop->getSourceRange());
593 ASSERT_TRUE(Range) << "Failed to get file range";
594 EXPECT_EQ(AST.getSourceManager().getFileOffset(Range->getBegin()),
595 Test.llvm::Annotations::range().Begin);
596 EXPECT_EQ(AST.getSourceManager().getFileOffset(Range->getEnd()),
597 Test.llvm::Annotations::range().End);
598}
599
600TEST(SourceCodeTests, IncludeHashLoc) {
601 const char *Case = R"cpp(
602$foo^#include "foo.inc"
603#define HEADER "bar.inc"
604 $bar^# include HEADER
605 )cpp";
606 Annotations Test(Case);
607 auto TU = TestTU::withCode(Test.code());
608 TU.AdditionalFiles["foo.inc"] = "int foo;\n";
609 TU.AdditionalFiles["bar.inc"] = "int bar;\n";
610 auto AST = TU.build();
611 const auto& SM = AST.getSourceManager();
612
613 FileID Foo = SM.getFileID(findDecl(AST, "foo").getLocation());
614 EXPECT_EQ(SM.getFileOffset(includeHashLoc(Foo, SM)),
615 Test.llvm::Annotations::point("foo"));
616 FileID Bar = SM.getFileID(findDecl(AST, "bar").getLocation());
617 EXPECT_EQ(SM.getFileOffset(includeHashLoc(Bar, SM)),
Sam McCall37a188b2019-08-27 09:27:00 +0000618 Test.llvm::Annotations::point("bar"));
Sam McCallc791d852019-08-27 08:44:06 +0000619}
620
Sam McCallb536a2a2017-12-19 12:23:48 +0000621} // namespace
622} // namespace clangd
623} // namespace clang