blob: 5ac738055b541fad6a561e779ae493aa6e15e256 [file] [log] [blame]
Sam McCallb536a2a2017-12-19 12:23:48 +00001//===--- SourceCode.h - Manipulating source code as strings -----*- 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//===----------------------------------------------------------------------===//
8#include "SourceCode.h"
9
Sam McCalla69698f2019-03-27 17:47:49 +000010#include "Context.h"
Sam McCall9fb22b22019-05-06 10:25:10 +000011#include "FuzzyMatch.h"
Marc-Andre Laperle1be69702018-07-05 19:35:01 +000012#include "Logger.h"
Sam McCalla69698f2019-03-27 17:47:49 +000013#include "Protocol.h"
Kadir Cetinkaya5b270932019-09-09 12:28:44 +000014#include "refactor/Tweak.h"
Marc-Andre Laperle1be69702018-07-05 19:35:01 +000015#include "clang/AST/ASTContext.h"
Shaurya Gupta0d26d6f2019-07-12 11:42:31 +000016#include "clang/Basic/LangOptions.h"
17#include "clang/Basic/SourceLocation.h"
Marc-Andre Laperle63a10982018-02-21 02:39:08 +000018#include "clang/Basic/SourceManager.h"
Sam McCallc316b222019-04-26 07:45:49 +000019#include "clang/Basic/TokenKinds.h"
Haojian Wu509efe52019-11-13 16:30:07 +010020#include "clang/Driver/Types.h"
Sam McCallc316b222019-04-26 07:45:49 +000021#include "clang/Format/Format.h"
Marc-Andre Laperle1be69702018-07-05 19:35:01 +000022#include "clang/Lex/Lexer.h"
Haojian Wu9d34f452019-07-01 09:26:48 +000023#include "clang/Lex/Preprocessor.h"
Kadir Cetinkayad62e3ed2019-09-25 11:35:38 +020024#include "clang/Lex/Token.h"
Kadir Cetinkaya5b270932019-09-09 12:28:44 +000025#include "clang/Tooling/Core/Replacement.h"
26#include "llvm/ADT/ArrayRef.h"
Ilya Biryukov43998782019-01-31 21:30:05 +000027#include "llvm/ADT/None.h"
Kadir Cetinkayad62e3ed2019-09-25 11:35:38 +020028#include "llvm/ADT/STLExtras.h"
Sam McCallc316b222019-04-26 07:45:49 +000029#include "llvm/ADT/StringExtras.h"
Kadir Cetinkaya5b270932019-09-09 12:28:44 +000030#include "llvm/ADT/StringMap.h"
Ilya Biryukov43998782019-01-31 21:30:05 +000031#include "llvm/ADT/StringRef.h"
Sam McCall9fb22b22019-05-06 10:25:10 +000032#include "llvm/Support/Compiler.h"
Simon Marchi766338a2018-03-21 14:36:46 +000033#include "llvm/Support/Errc.h"
34#include "llvm/Support/Error.h"
Sam McCall8b25d222019-03-28 14:37:51 +000035#include "llvm/Support/ErrorHandling.h"
Kadir Cetinkaya5b270932019-09-09 12:28:44 +000036#include "llvm/Support/LineIterator.h"
37#include "llvm/Support/MemoryBuffer.h"
Marc-Andre Laperle1be69702018-07-05 19:35:01 +000038#include "llvm/Support/Path.h"
Kadir Cetinkaya5b270932019-09-09 12:28:44 +000039#include "llvm/Support/SHA1.h"
40#include "llvm/Support/VirtualFileSystem.h"
Sam McCall674d8a92019-07-08 11:33:17 +000041#include "llvm/Support/xxhash.h"
Sam McCallc316b222019-04-26 07:45:49 +000042#include <algorithm>
Kadir Cetinkayad62e3ed2019-09-25 11:35:38 +020043#include <cstddef>
44#include <string>
45#include <vector>
Marc-Andre Laperle63a10982018-02-21 02:39:08 +000046
Sam McCallb536a2a2017-12-19 12:23:48 +000047namespace clang {
48namespace clangd {
Sam McCallb536a2a2017-12-19 12:23:48 +000049
Sam McCalla4962cc2018-04-27 11:59:28 +000050// Here be dragons. LSP positions use columns measured in *UTF-16 code units*!
51// Clangd uses UTF-8 and byte-offsets internally, so conversion is nontrivial.
52
53// Iterates over unicode codepoints in the (UTF-8) string. For each,
54// invokes CB(UTF-8 length, UTF-16 length), and breaks if it returns true.
55// Returns true if CB returned true, false if we hit the end of string.
56template <typename Callback>
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000057static bool iterateCodepoints(llvm::StringRef U8, const Callback &CB) {
Sam McCall8b25d222019-03-28 14:37:51 +000058 // A codepoint takes two UTF-16 code unit if it's astral (outside BMP).
59 // Astral codepoints are encoded as 4 bytes in UTF-8, starting with 11110xxx.
Sam McCalla4962cc2018-04-27 11:59:28 +000060 for (size_t I = 0; I < U8.size();) {
61 unsigned char C = static_cast<unsigned char>(U8[I]);
62 if (LLVM_LIKELY(!(C & 0x80))) { // ASCII character.
63 if (CB(1, 1))
64 return true;
65 ++I;
66 continue;
67 }
68 // This convenient property of UTF-8 holds for all non-ASCII characters.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000069 size_t UTF8Length = llvm::countLeadingOnes(C);
Sam McCalla4962cc2018-04-27 11:59:28 +000070 // 0xxx is ASCII, handled above. 10xxx is a trailing byte, invalid here.
71 // 11111xxx is not valid UTF-8 at all. Assert because it's probably our bug.
72 assert((UTF8Length >= 2 && UTF8Length <= 4) &&
73 "Invalid UTF-8, or transcoding bug?");
74 I += UTF8Length; // Skip over all trailing bytes.
75 // A codepoint takes two UTF-16 code unit if it's astral (outside BMP).
76 // Astral codepoints are encoded as 4 bytes in UTF-8 (11110xxx ...)
77 if (CB(UTF8Length, UTF8Length == 4 ? 2 : 1))
78 return true;
79 }
80 return false;
81}
82
Sam McCall8b25d222019-03-28 14:37:51 +000083// Returns the byte offset into the string that is an offset of \p Units in
84// the specified encoding.
85// Conceptually, this converts to the encoding, truncates to CodeUnits,
86// converts back to UTF-8, and returns the length in bytes.
87static size_t measureUnits(llvm::StringRef U8, int Units, OffsetEncoding Enc,
88 bool &Valid) {
89 Valid = Units >= 0;
90 if (Units <= 0)
91 return 0;
Sam McCalla4962cc2018-04-27 11:59:28 +000092 size_t Result = 0;
Sam McCall8b25d222019-03-28 14:37:51 +000093 switch (Enc) {
94 case OffsetEncoding::UTF8:
95 Result = Units;
96 break;
97 case OffsetEncoding::UTF16:
98 Valid = iterateCodepoints(U8, [&](int U8Len, int U16Len) {
99 Result += U8Len;
100 Units -= U16Len;
101 return Units <= 0;
102 });
103 if (Units < 0) // Offset in the middle of a surrogate pair.
104 Valid = false;
105 break;
106 case OffsetEncoding::UTF32:
107 Valid = iterateCodepoints(U8, [&](int U8Len, int U16Len) {
108 Result += U8Len;
109 Units--;
110 return Units <= 0;
111 });
112 break;
113 case OffsetEncoding::UnsupportedEncoding:
114 llvm_unreachable("unsupported encoding");
115 }
Sam McCalla4962cc2018-04-27 11:59:28 +0000116 // Don't return an out-of-range index if we overran.
Sam McCall8b25d222019-03-28 14:37:51 +0000117 if (Result > U8.size()) {
118 Valid = false;
119 return U8.size();
120 }
121 return Result;
Sam McCalla4962cc2018-04-27 11:59:28 +0000122}
123
Sam McCalla69698f2019-03-27 17:47:49 +0000124Key<OffsetEncoding> kCurrentOffsetEncoding;
Sam McCall8b25d222019-03-28 14:37:51 +0000125static OffsetEncoding lspEncoding() {
Sam McCalla69698f2019-03-27 17:47:49 +0000126 auto *Enc = Context::current().get(kCurrentOffsetEncoding);
Sam McCall8b25d222019-03-28 14:37:51 +0000127 return Enc ? *Enc : OffsetEncoding::UTF16;
Sam McCalla69698f2019-03-27 17:47:49 +0000128}
129
Sam McCalla4962cc2018-04-27 11:59:28 +0000130// Like most strings in clangd, the input is UTF-8 encoded.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000131size_t lspLength(llvm::StringRef Code) {
Sam McCalla4962cc2018-04-27 11:59:28 +0000132 size_t Count = 0;
Sam McCall8b25d222019-03-28 14:37:51 +0000133 switch (lspEncoding()) {
134 case OffsetEncoding::UTF8:
135 Count = Code.size();
136 break;
137 case OffsetEncoding::UTF16:
138 iterateCodepoints(Code, [&](int U8Len, int U16Len) {
139 Count += U16Len;
140 return false;
141 });
142 break;
143 case OffsetEncoding::UTF32:
144 iterateCodepoints(Code, [&](int U8Len, int U16Len) {
145 ++Count;
146 return false;
147 });
148 break;
149 case OffsetEncoding::UnsupportedEncoding:
150 llvm_unreachable("unsupported encoding");
151 }
Sam McCalla4962cc2018-04-27 11:59:28 +0000152 return Count;
153}
154
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000155llvm::Expected<size_t> positionToOffset(llvm::StringRef Code, Position P,
156 bool AllowColumnsBeyondLineLength) {
Sam McCallb536a2a2017-12-19 12:23:48 +0000157 if (P.line < 0)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000158 return llvm::make_error<llvm::StringError>(
159 llvm::formatv("Line value can't be negative ({0})", P.line),
160 llvm::errc::invalid_argument);
Simon Marchi766338a2018-03-21 14:36:46 +0000161 if (P.character < 0)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000162 return llvm::make_error<llvm::StringError>(
163 llvm::formatv("Character value can't be negative ({0})", P.character),
164 llvm::errc::invalid_argument);
Sam McCallb536a2a2017-12-19 12:23:48 +0000165 size_t StartOfLine = 0;
166 for (int I = 0; I != P.line; ++I) {
167 size_t NextNL = Code.find('\n', StartOfLine);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000168 if (NextNL == llvm::StringRef::npos)
169 return llvm::make_error<llvm::StringError>(
170 llvm::formatv("Line value is out of range ({0})", P.line),
171 llvm::errc::invalid_argument);
Sam McCallb536a2a2017-12-19 12:23:48 +0000172 StartOfLine = NextNL + 1;
173 }
Sam McCalla69698f2019-03-27 17:47:49 +0000174 StringRef Line =
175 Code.substr(StartOfLine).take_until([](char C) { return C == '\n'; });
Simon Marchi766338a2018-03-21 14:36:46 +0000176
Sam McCall8b25d222019-03-28 14:37:51 +0000177 // P.character may be in UTF-16, transcode if necessary.
Sam McCalla4962cc2018-04-27 11:59:28 +0000178 bool Valid;
Sam McCall8b25d222019-03-28 14:37:51 +0000179 size_t ByteInLine = measureUnits(Line, P.character, lspEncoding(), Valid);
Sam McCalla4962cc2018-04-27 11:59:28 +0000180 if (!Valid && !AllowColumnsBeyondLineLength)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000181 return llvm::make_error<llvm::StringError>(
Sam McCall8b25d222019-03-28 14:37:51 +0000182 llvm::formatv("{0} offset {1} is invalid for line {2}", lspEncoding(),
183 P.character, P.line),
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000184 llvm::errc::invalid_argument);
Sam McCall8b25d222019-03-28 14:37:51 +0000185 return StartOfLine + ByteInLine;
Sam McCallb536a2a2017-12-19 12:23:48 +0000186}
187
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000188Position offsetToPosition(llvm::StringRef Code, size_t Offset) {
Sam McCallb536a2a2017-12-19 12:23:48 +0000189 Offset = std::min(Code.size(), Offset);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000190 llvm::StringRef Before = Code.substr(0, Offset);
Sam McCallb536a2a2017-12-19 12:23:48 +0000191 int Lines = Before.count('\n');
192 size_t PrevNL = Before.rfind('\n');
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000193 size_t StartOfLine = (PrevNL == llvm::StringRef::npos) ? 0 : (PrevNL + 1);
Ilya Biryukov7beea3a2018-02-14 10:52:04 +0000194 Position Pos;
195 Pos.line = Lines;
Sam McCall71891122018-10-23 11:51:53 +0000196 Pos.character = lspLength(Before.substr(StartOfLine));
Ilya Biryukov7beea3a2018-02-14 10:52:04 +0000197 return Pos;
Sam McCallb536a2a2017-12-19 12:23:48 +0000198}
199
Marc-Andre Laperle63a10982018-02-21 02:39:08 +0000200Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc) {
Sam McCalla4962cc2018-04-27 11:59:28 +0000201 // We use the SourceManager's line tables, but its column number is in bytes.
202 FileID FID;
203 unsigned Offset;
204 std::tie(FID, Offset) = SM.getDecomposedSpellingLoc(Loc);
Marc-Andre Laperle63a10982018-02-21 02:39:08 +0000205 Position P;
Sam McCalla4962cc2018-04-27 11:59:28 +0000206 P.line = static_cast<int>(SM.getLineNumber(FID, Offset)) - 1;
207 bool Invalid = false;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000208 llvm::StringRef Code = SM.getBufferData(FID, &Invalid);
Sam McCalla4962cc2018-04-27 11:59:28 +0000209 if (!Invalid) {
210 auto ColumnInBytes = SM.getColumnNumber(FID, Offset) - 1;
211 auto LineSoFar = Code.substr(Offset - ColumnInBytes, ColumnInBytes);
Sam McCall71891122018-10-23 11:51:53 +0000212 P.character = lspLength(LineSoFar);
Sam McCalla4962cc2018-04-27 11:59:28 +0000213 }
Marc-Andre Laperle63a10982018-02-21 02:39:08 +0000214 return P;
215}
216
Sam McCall95738072019-08-06 20:25:59 +0000217bool isSpelledInSource(SourceLocation Loc, const SourceManager &SM) {
218 if (Loc.isMacroID()) {
219 std::string PrintLoc = SM.getSpellingLoc(Loc).printToString(SM);
220 if (llvm::StringRef(PrintLoc).startswith("<scratch") ||
221 llvm::StringRef(PrintLoc).startswith("<command line>"))
222 return false;
223 }
224 return true;
225}
226
Haojian Wu92c32572019-06-25 08:01:46 +0000227llvm::Optional<Range> getTokenRange(const SourceManager &SM,
228 const LangOptions &LangOpts,
229 SourceLocation TokLoc) {
230 if (!TokLoc.isValid())
231 return llvm::None;
232 SourceLocation End = Lexer::getLocForEndOfToken(TokLoc, 0, SM, LangOpts);
233 if (!End.isValid())
234 return llvm::None;
235 return halfOpenToRange(SM, CharSourceRange::getCharRange(TokLoc, End));
236}
237
Haojian Wu9f2bf662019-10-01 11:03:56 +0000238namespace {
239
240enum TokenFlavor { Identifier, Operator, Whitespace, Other };
241
242bool isOverloadedOperator(const Token &Tok) {
243 switch (Tok.getKind()) {
244#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemOnly) \
245 case tok::Token:
246#define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemOnly)
247#include "clang/Basic/OperatorKinds.def"
248 return true;
249
250 default:
251 break;
252 }
253 return false;
254}
255
256TokenFlavor getTokenFlavor(SourceLocation Loc, const SourceManager &SM,
257 const LangOptions &LangOpts) {
258 Token Tok;
259 Tok.setKind(tok::NUM_TOKENS);
260 if (Lexer::getRawToken(Loc, Tok, SM, LangOpts,
261 /*IgnoreWhiteSpace*/ false))
262 return Other;
263
264 // getRawToken will return false without setting Tok when the token is
265 // whitespace, so if the flag is not set, we are sure this is a whitespace.
266 if (Tok.is(tok::TokenKind::NUM_TOKENS))
267 return Whitespace;
268 if (Tok.is(tok::TokenKind::raw_identifier))
269 return Identifier;
270 if (isOverloadedOperator(Tok))
271 return Operator;
272 return Other;
273}
274
275} // namespace
276
Sam McCall19cefc22019-09-03 15:34:47 +0000277SourceLocation getBeginningOfIdentifier(const Position &Pos,
278 const SourceManager &SM,
279 const LangOptions &LangOpts) {
280 FileID FID = SM.getMainFileID();
281 auto Offset = positionToOffset(SM.getBufferData(FID), Pos);
282 if (!Offset) {
283 log("getBeginningOfIdentifier: {0}", Offset.takeError());
284 return SourceLocation();
285 }
286
Haojian Wu9f2bf662019-10-01 11:03:56 +0000287 // GetBeginningOfToken(InputLoc) is almost what we want, but does the wrong
288 // thing if the cursor is at the end of the token (identifier or operator).
289 // The cases are:
290 // 1) at the beginning of the token
291 // 2) at the middle of the token
292 // 3) at the end of the token
293 // 4) anywhere outside the identifier or operator
294 // To distinguish all cases, we lex both at the
295 // GetBeginningOfToken(InputLoc-1) and GetBeginningOfToken(InputLoc), for
296 // cases 1 and 4, we just return the original location.
Sam McCall19cefc22019-09-03 15:34:47 +0000297 SourceLocation InputLoc = SM.getComposedLoc(FID, *Offset);
Haojian Wu9f2bf662019-10-01 11:03:56 +0000298 if (*Offset == 0) // Case 1 or 4.
Sam McCallb2a984c02019-09-04 10:15:27 +0000299 return InputLoc;
Sam McCall19cefc22019-09-03 15:34:47 +0000300 SourceLocation Before = SM.getComposedLoc(FID, *Offset - 1);
Haojian Wu9f2bf662019-10-01 11:03:56 +0000301 SourceLocation BeforeTokBeginning =
302 Lexer::GetBeginningOfToken(Before, SM, LangOpts);
303 TokenFlavor BeforeKind = getTokenFlavor(BeforeTokBeginning, SM, LangOpts);
Sam McCall19cefc22019-09-03 15:34:47 +0000304
Haojian Wu9f2bf662019-10-01 11:03:56 +0000305 SourceLocation CurrentTokBeginning =
306 Lexer::GetBeginningOfToken(InputLoc, SM, LangOpts);
307 TokenFlavor CurrentKind = getTokenFlavor(CurrentTokBeginning, SM, LangOpts);
308
309 // At the middle of the token.
310 if (BeforeTokBeginning == CurrentTokBeginning) {
311 // For interesting token, we return the beginning of the token.
312 if (CurrentKind == Identifier || CurrentKind == Operator)
313 return CurrentTokBeginning;
314 // otherwise, we return the original loc.
315 return InputLoc;
316 }
317
318 // Whitespace is not interesting.
319 if (BeforeKind == Whitespace)
320 return CurrentTokBeginning;
321 if (CurrentKind == Whitespace)
322 return BeforeTokBeginning;
323
324 // The cursor is at the token boundary, e.g. "Before^Current", we prefer
325 // identifiers to other tokens.
326 if (CurrentKind == Identifier)
327 return CurrentTokBeginning;
328 if (BeforeKind == Identifier)
329 return BeforeTokBeginning;
330 // Then prefer overloaded operators to other tokens.
331 if (CurrentKind == Operator)
332 return CurrentTokBeginning;
333 if (BeforeKind == Operator)
334 return BeforeTokBeginning;
335
336 // Non-interesting case, we just return the original location.
337 return InputLoc;
Sam McCall19cefc22019-09-03 15:34:47 +0000338}
339
Ilya Biryukov43998782019-01-31 21:30:05 +0000340bool isValidFileRange(const SourceManager &Mgr, SourceRange R) {
341 if (!R.getBegin().isValid() || !R.getEnd().isValid())
342 return false;
343
344 FileID BeginFID;
345 size_t BeginOffset = 0;
346 std::tie(BeginFID, BeginOffset) = Mgr.getDecomposedLoc(R.getBegin());
347
348 FileID EndFID;
349 size_t EndOffset = 0;
350 std::tie(EndFID, EndOffset) = Mgr.getDecomposedLoc(R.getEnd());
351
352 return BeginFID.isValid() && BeginFID == EndFID && BeginOffset <= EndOffset;
353}
354
355bool halfOpenRangeContains(const SourceManager &Mgr, SourceRange R,
356 SourceLocation L) {
357 assert(isValidFileRange(Mgr, R));
358
359 FileID BeginFID;
360 size_t BeginOffset = 0;
361 std::tie(BeginFID, BeginOffset) = Mgr.getDecomposedLoc(R.getBegin());
362 size_t EndOffset = Mgr.getFileOffset(R.getEnd());
363
364 FileID LFid;
365 size_t LOffset;
366 std::tie(LFid, LOffset) = Mgr.getDecomposedLoc(L);
367 return BeginFID == LFid && BeginOffset <= LOffset && LOffset < EndOffset;
368}
369
370bool halfOpenRangeTouches(const SourceManager &Mgr, SourceRange R,
371 SourceLocation L) {
372 return L == R.getEnd() || halfOpenRangeContains(Mgr, R, L);
373}
374
Sam McCallc791d852019-08-27 08:44:06 +0000375SourceLocation includeHashLoc(FileID IncludedFile, const SourceManager &SM) {
376 assert(SM.getLocForEndOfFile(IncludedFile).isFileID());
377 FileID IncludingFile;
378 unsigned Offset;
379 std::tie(IncludingFile, Offset) =
380 SM.getDecomposedExpansionLoc(SM.getIncludeLoc(IncludedFile));
381 bool Invalid = false;
382 llvm::StringRef Buf = SM.getBufferData(IncludingFile, &Invalid);
383 if (Invalid)
384 return SourceLocation();
385 // Now buf is "...\n#include <foo>\n..."
386 // and Offset points here: ^
387 // Rewind to the preceding # on the line.
388 assert(Offset < Buf.size());
389 for (;; --Offset) {
390 if (Buf[Offset] == '#')
391 return SM.getComposedLoc(IncludingFile, Offset);
392 if (Buf[Offset] == '\n' || Offset == 0) // no hash, what's going on?
393 return SourceLocation();
394 }
395}
396
Shaurya Gupta0d26d6f2019-07-12 11:42:31 +0000397static unsigned getTokenLengthAtLoc(SourceLocation Loc, const SourceManager &SM,
398 const LangOptions &LangOpts) {
399 Token TheTok;
400 if (Lexer::getRawToken(Loc, TheTok, SM, LangOpts))
401 return 0;
402 // FIXME: Here we check whether the token at the location is a greatergreater
403 // (>>) token and consider it as a single greater (>). This is to get it
404 // working for templates but it isn't correct for the right shift operator. We
405 // can avoid this by using half open char ranges in getFileRange() but getting
406 // token ending is not well supported in macroIDs.
407 if (TheTok.is(tok::greatergreater))
408 return 1;
409 return TheTok.getLength();
410}
411
412// Returns location of the last character of the token at a given loc
413static SourceLocation getLocForTokenEnd(SourceLocation BeginLoc,
414 const SourceManager &SM,
415 const LangOptions &LangOpts) {
416 unsigned Len = getTokenLengthAtLoc(BeginLoc, SM, LangOpts);
417 return BeginLoc.getLocWithOffset(Len ? Len - 1 : 0);
418}
419
420// Returns location of the starting of the token at a given EndLoc
421static SourceLocation getLocForTokenBegin(SourceLocation EndLoc,
422 const SourceManager &SM,
423 const LangOptions &LangOpts) {
424 return EndLoc.getLocWithOffset(
425 -(signed)getTokenLengthAtLoc(EndLoc, SM, LangOpts));
426}
427
428// Converts a char source range to a token range.
429static SourceRange toTokenRange(CharSourceRange Range, const SourceManager &SM,
430 const LangOptions &LangOpts) {
431 if (!Range.isTokenRange())
432 Range.setEnd(getLocForTokenBegin(Range.getEnd(), SM, LangOpts));
433 return Range.getAsRange();
434}
435// Returns the union of two token ranges.
436// To find the maximum of the Ends of the ranges, we compare the location of the
437// last character of the token.
438static SourceRange unionTokenRange(SourceRange R1, SourceRange R2,
439 const SourceManager &SM,
440 const LangOptions &LangOpts) {
Sam McCallc791d852019-08-27 08:44:06 +0000441 SourceLocation Begin =
442 SM.isBeforeInTranslationUnit(R1.getBegin(), R2.getBegin())
443 ? R1.getBegin()
444 : R2.getBegin();
445 SourceLocation End =
446 SM.isBeforeInTranslationUnit(getLocForTokenEnd(R1.getEnd(), SM, LangOpts),
447 getLocForTokenEnd(R2.getEnd(), SM, LangOpts))
448 ? R2.getEnd()
449 : R1.getEnd();
450 return SourceRange(Begin, End);
Shaurya Gupta0d26d6f2019-07-12 11:42:31 +0000451}
452
Sam McCallc791d852019-08-27 08:44:06 +0000453// Given a range whose endpoints may be in different expansions or files,
454// tries to find a range within a common file by following up the expansion and
455// include location in each.
456static SourceRange rangeInCommonFile(SourceRange R, const SourceManager &SM,
457 const LangOptions &LangOpts) {
458 // Fast path for most common cases.
459 if (SM.isWrittenInSameFile(R.getBegin(), R.getEnd()))
460 return R;
461 // Record the stack of expansion locations for the beginning, keyed by FileID.
462 llvm::DenseMap<FileID, SourceLocation> BeginExpansions;
463 for (SourceLocation Begin = R.getBegin(); Begin.isValid();
464 Begin = Begin.isFileID()
465 ? includeHashLoc(SM.getFileID(Begin), SM)
466 : SM.getImmediateExpansionRange(Begin).getBegin()) {
467 BeginExpansions[SM.getFileID(Begin)] = Begin;
468 }
469 // Move up the stack of expansion locations for the end until we find the
470 // location in BeginExpansions with that has the same file id.
471 for (SourceLocation End = R.getEnd(); End.isValid();
472 End = End.isFileID() ? includeHashLoc(SM.getFileID(End), SM)
473 : toTokenRange(SM.getImmediateExpansionRange(End),
474 SM, LangOpts)
475 .getEnd()) {
476 auto It = BeginExpansions.find(SM.getFileID(End));
477 if (It != BeginExpansions.end()) {
478 if (SM.getFileOffset(It->second) > SM.getFileOffset(End))
479 return SourceLocation();
480 return {It->second, End};
481 }
482 }
483 return SourceRange();
Shaurya Gupta8fbb6ce2019-08-06 17:01:12 +0000484}
485
486// Find an expansion range (not necessarily immediate) the ends of which are in
487// the same file id.
488static SourceRange
489getExpansionTokenRangeInSameFile(SourceLocation Loc, const SourceManager &SM,
490 const LangOptions &LangOpts) {
Sam McCallc791d852019-08-27 08:44:06 +0000491 return rangeInCommonFile(
492 toTokenRange(SM.getImmediateExpansionRange(Loc), SM, LangOpts), SM,
493 LangOpts);
Shaurya Gupta8fbb6ce2019-08-06 17:01:12 +0000494}
Sam McCallc791d852019-08-27 08:44:06 +0000495
Shaurya Gupta8fbb6ce2019-08-06 17:01:12 +0000496// Returns the file range for a given Location as a Token Range
Shaurya Gupta0d26d6f2019-07-12 11:42:31 +0000497// This is quite similar to getFileLoc in SourceManager as both use
498// getImmediateExpansionRange and getImmediateSpellingLoc (for macro IDs).
499// However:
500// - We want to maintain the full range information as we move from one file to
501// the next. getFileLoc only uses the BeginLoc of getImmediateExpansionRange.
Shaurya Gupta8fbb6ce2019-08-06 17:01:12 +0000502// - We want to split '>>' tokens as the lexer parses the '>>' in nested
503// template instantiations as a '>>' instead of two '>'s.
Shaurya Gupta0d26d6f2019-07-12 11:42:31 +0000504// There is also getExpansionRange but it simply calls
505// getImmediateExpansionRange on the begin and ends separately which is wrong.
506static SourceRange getTokenFileRange(SourceLocation Loc,
507 const SourceManager &SM,
508 const LangOptions &LangOpts) {
509 SourceRange FileRange = Loc;
510 while (!FileRange.getBegin().isFileID()) {
Shaurya Gupta0d26d6f2019-07-12 11:42:31 +0000511 if (SM.isMacroArgExpansion(FileRange.getBegin())) {
Shaurya Gupta8fbb6ce2019-08-06 17:01:12 +0000512 FileRange = unionTokenRange(
513 SM.getImmediateSpellingLoc(FileRange.getBegin()),
514 SM.getImmediateSpellingLoc(FileRange.getEnd()), SM, LangOpts);
Sam McCallc791d852019-08-27 08:44:06 +0000515 assert(SM.isWrittenInSameFile(FileRange.getBegin(), FileRange.getEnd()));
Shaurya Gupta0d26d6f2019-07-12 11:42:31 +0000516 } else {
Shaurya Gupta8fbb6ce2019-08-06 17:01:12 +0000517 SourceRange ExpansionRangeForBegin =
518 getExpansionTokenRangeInSameFile(FileRange.getBegin(), SM, LangOpts);
519 SourceRange ExpansionRangeForEnd =
520 getExpansionTokenRangeInSameFile(FileRange.getEnd(), SM, LangOpts);
Sam McCallc791d852019-08-27 08:44:06 +0000521 if (ExpansionRangeForBegin.isInvalid() ||
522 ExpansionRangeForEnd.isInvalid())
523 return SourceRange();
524 assert(SM.isWrittenInSameFile(ExpansionRangeForBegin.getBegin(),
525 ExpansionRangeForEnd.getBegin()) &&
Shaurya Gupta8fbb6ce2019-08-06 17:01:12 +0000526 "Both Expansion ranges should be in same file.");
Shaurya Gupta0d26d6f2019-07-12 11:42:31 +0000527 FileRange = unionTokenRange(ExpansionRangeForBegin, ExpansionRangeForEnd,
528 SM, LangOpts);
529 }
530 }
531 return FileRange;
532}
533
Haojian Wu6ae86ea2019-07-19 08:33:39 +0000534bool isInsideMainFile(SourceLocation Loc, const SourceManager &SM) {
535 return Loc.isValid() && SM.isWrittenInMainFile(SM.getExpansionLoc(Loc));
536}
537
Shaurya Gupta0d26d6f2019-07-12 11:42:31 +0000538llvm::Optional<SourceRange> toHalfOpenFileRange(const SourceManager &SM,
Ilya Biryukov43998782019-01-31 21:30:05 +0000539 const LangOptions &LangOpts,
540 SourceRange R) {
Shaurya Gupta0d26d6f2019-07-12 11:42:31 +0000541 SourceRange R1 = getTokenFileRange(R.getBegin(), SM, LangOpts);
542 if (!isValidFileRange(SM, R1))
Ilya Biryukov43998782019-01-31 21:30:05 +0000543 return llvm::None;
Ilya Biryukov43998782019-01-31 21:30:05 +0000544
Shaurya Gupta0d26d6f2019-07-12 11:42:31 +0000545 SourceRange R2 = getTokenFileRange(R.getEnd(), SM, LangOpts);
546 if (!isValidFileRange(SM, R2))
Ilya Biryukov43998782019-01-31 21:30:05 +0000547 return llvm::None;
Shaurya Gupta0d26d6f2019-07-12 11:42:31 +0000548
Sam McCallc791d852019-08-27 08:44:06 +0000549 SourceRange Result =
550 rangeInCommonFile(unionTokenRange(R1, R2, SM, LangOpts), SM, LangOpts);
Shaurya Gupta0d26d6f2019-07-12 11:42:31 +0000551 unsigned TokLen = getTokenLengthAtLoc(Result.getEnd(), SM, LangOpts);
552 // Convert from closed token range to half-open (char) range
553 Result.setEnd(Result.getEnd().getLocWithOffset(TokLen));
554 if (!isValidFileRange(SM, Result))
555 return llvm::None;
556
Ilya Biryukov43998782019-01-31 21:30:05 +0000557 return Result;
558}
559
560llvm::StringRef toSourceCode(const SourceManager &SM, SourceRange R) {
561 assert(isValidFileRange(SM, R));
562 bool Invalid = false;
563 auto *Buf = SM.getBuffer(SM.getFileID(R.getBegin()), &Invalid);
564 assert(!Invalid);
565
566 size_t BeginOffset = SM.getFileOffset(R.getBegin());
567 size_t EndOffset = SM.getFileOffset(R.getEnd());
568 return Buf->getBuffer().substr(BeginOffset, EndOffset - BeginOffset);
569}
570
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000571llvm::Expected<SourceLocation> sourceLocationInMainFile(const SourceManager &SM,
572 Position P) {
573 llvm::StringRef Code = SM.getBuffer(SM.getMainFileID())->getBuffer();
574 auto Offset =
575 positionToOffset(Code, P, /*AllowColumnBeyondLineLength=*/false);
576 if (!Offset)
577 return Offset.takeError();
578 return SM.getLocForStartOfFile(SM.getMainFileID()).getLocWithOffset(*Offset);
579}
580
Ilya Biryukov71028b82018-03-12 15:28:22 +0000581Range halfOpenToRange(const SourceManager &SM, CharSourceRange R) {
582 // Clang is 1-based, LSP uses 0-based indexes.
583 Position Begin = sourceLocToPosition(SM, R.getBegin());
584 Position End = sourceLocToPosition(SM, R.getEnd());
585
586 return {Begin, End};
587}
588
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000589std::pair<size_t, size_t> offsetToClangLineColumn(llvm::StringRef Code,
Sam McCalla4962cc2018-04-27 11:59:28 +0000590 size_t Offset) {
591 Offset = std::min(Code.size(), Offset);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000592 llvm::StringRef Before = Code.substr(0, Offset);
Sam McCalla4962cc2018-04-27 11:59:28 +0000593 int Lines = Before.count('\n');
594 size_t PrevNL = Before.rfind('\n');
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000595 size_t StartOfLine = (PrevNL == llvm::StringRef::npos) ? 0 : (PrevNL + 1);
Sam McCalla4962cc2018-04-27 11:59:28 +0000596 return {Lines + 1, Offset - StartOfLine + 1};
597}
598
Ilya Biryukov43998782019-01-31 21:30:05 +0000599std::pair<StringRef, StringRef> splitQualifiedName(StringRef QName) {
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000600 size_t Pos = QName.rfind("::");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000601 if (Pos == llvm::StringRef::npos)
602 return {llvm::StringRef(), QName};
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000603 return {QName.substr(0, Pos + 2), QName.substr(Pos + 2)};
604}
605
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000606TextEdit replacementToEdit(llvm::StringRef Code,
607 const tooling::Replacement &R) {
Eric Liu9133ecd2018-05-11 12:12:08 +0000608 Range ReplacementRange = {
609 offsetToPosition(Code, R.getOffset()),
610 offsetToPosition(Code, R.getOffset() + R.getLength())};
Benjamin Krameradcd0262020-01-28 20:23:46 +0100611 return {ReplacementRange, std::string(R.getReplacementText())};
Eric Liu9133ecd2018-05-11 12:12:08 +0000612}
613
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000614std::vector<TextEdit> replacementsToEdits(llvm::StringRef Code,
Eric Liu9133ecd2018-05-11 12:12:08 +0000615 const tooling::Replacements &Repls) {
616 std::vector<TextEdit> Edits;
617 for (const auto &R : Repls)
618 Edits.push_back(replacementToEdit(Code, R));
619 return Edits;
620}
621
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000622llvm::Optional<std::string> getCanonicalPath(const FileEntry *F,
623 const SourceManager &SourceMgr) {
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000624 if (!F)
625 return None;
Simon Marchi25f1f732018-08-10 22:27:53 +0000626
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000627 llvm::SmallString<128> FilePath = F->getName();
628 if (!llvm::sys::path::is_absolute(FilePath)) {
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000629 if (auto EC =
Duncan P. N. Exon Smithdb8a7422019-03-26 22:32:06 +0000630 SourceMgr.getFileManager().getVirtualFileSystem().makeAbsolute(
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000631 FilePath)) {
632 elog("Could not turn relative path '{0}' to absolute: {1}", FilePath,
633 EC.message());
Sam McCallc008af62018-10-20 15:30:37 +0000634 return None;
Marc-Andre Laperle1be69702018-07-05 19:35:01 +0000635 }
636 }
Simon Marchi25f1f732018-08-10 22:27:53 +0000637
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000638 // Handle the symbolic link path case where the current working directory
Kadir Cetinkaya5b270932019-09-09 12:28:44 +0000639 // (getCurrentWorkingDirectory) is a symlink. We always want to the real
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000640 // file path (instead of the symlink path) for the C++ symbols.
641 //
642 // Consider the following example:
643 //
644 // src dir: /project/src/foo.h
645 // current working directory (symlink): /tmp/build -> /project/src/
646 //
647 // The file path of Symbol is "/project/src/foo.h" instead of
648 // "/tmp/build/foo.h"
Harlan Haskinsa02f8572019-08-01 21:32:01 +0000649 if (auto Dir = SourceMgr.getFileManager().getDirectory(
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000650 llvm::sys::path::parent_path(FilePath))) {
651 llvm::SmallString<128> RealPath;
Harlan Haskinsa02f8572019-08-01 21:32:01 +0000652 llvm::StringRef DirName = SourceMgr.getFileManager().getCanonicalName(*Dir);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000653 llvm::sys::path::append(RealPath, DirName,
654 llvm::sys::path::filename(FilePath));
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000655 return RealPath.str().str();
Simon Marchi25f1f732018-08-10 22:27:53 +0000656 }
657
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000658 return FilePath.str().str();
Marc-Andre Laperle1be69702018-07-05 19:35:01 +0000659}
660
Kadir Cetinkaya2f84d912018-08-08 08:59:29 +0000661TextEdit toTextEdit(const FixItHint &FixIt, const SourceManager &M,
662 const LangOptions &L) {
663 TextEdit Result;
664 Result.range =
665 halfOpenToRange(M, Lexer::makeFileCharRange(FixIt.RemoveRange, M, L));
666 Result.newText = FixIt.CodeToInsert;
667 return Result;
668}
669
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000670bool isRangeConsecutive(const Range &Left, const Range &Right) {
Kadir Cetinkayaa9c9d002018-08-13 08:23:01 +0000671 return Left.end.line == Right.start.line &&
672 Left.end.character == Right.start.character;
673}
674
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000675FileDigest digest(llvm::StringRef Content) {
Sam McCall674d8a92019-07-08 11:33:17 +0000676 uint64_t Hash{llvm::xxHash64(Content)};
677 FileDigest Result;
678 for (unsigned I = 0; I < Result.size(); ++I) {
679 Result[I] = uint8_t(Hash);
680 Hash >>= 8;
681 }
682 return Result;
Kadir Cetinkayad08eab42018-11-27 16:08:53 +0000683}
684
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000685llvm::Optional<FileDigest> digestFile(const SourceManager &SM, FileID FID) {
Kadir Cetinkayad08eab42018-11-27 16:08:53 +0000686 bool Invalid = false;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000687 llvm::StringRef Content = SM.getBufferData(FID, &Invalid);
Kadir Cetinkayad08eab42018-11-27 16:08:53 +0000688 if (Invalid)
689 return None;
690 return digest(Content);
691}
692
Eric Liudd662772019-01-28 14:01:55 +0000693format::FormatStyle getFormatStyleForFile(llvm::StringRef File,
694 llvm::StringRef Content,
695 llvm::vfs::FileSystem *FS) {
696 auto Style = format::getStyle(format::DefaultFormatStyle, File,
697 format::DefaultFallbackStyle, Content, FS);
698 if (!Style) {
699 log("getStyle() failed for file {0}: {1}. Fallback is LLVM style.", File,
700 Style.takeError());
701 Style = format::getLLVMStyle();
702 }
703 return *Style;
704}
705
Haojian Wu12e194c2019-02-06 15:24:50 +0000706llvm::Expected<tooling::Replacements>
707cleanupAndFormat(StringRef Code, const tooling::Replacements &Replaces,
708 const format::FormatStyle &Style) {
709 auto CleanReplaces = cleanupAroundReplacements(Code, Replaces, Style);
710 if (!CleanReplaces)
711 return CleanReplaces;
712 return formatReplacements(Code, std::move(*CleanReplaces), Style);
713}
714
Haojian Wuc5e4cf42019-11-07 10:53:19 +0100715static void
716lex(llvm::StringRef Code, const LangOptions &LangOpts,
717 llvm::function_ref<void(const clang::Token &, const SourceManager &SM)>
718 Action) {
Sam McCallc316b222019-04-26 07:45:49 +0000719 // FIXME: InMemoryFileAdapter crashes unless the buffer is null terminated!
720 std::string NullTerminatedCode = Code.str();
721 SourceManagerForFile FileSM("dummy.cpp", NullTerminatedCode);
Eric Liu00d99bd2019-04-11 09:36:36 +0000722 auto &SM = FileSM.get();
723 auto FID = SM.getMainFileID();
Haojian Wu7ea4c6f2019-10-30 13:21:47 +0100724 // Create a raw lexer (with no associated preprocessor object).
725 Lexer Lex(FID, SM.getBuffer(FID), SM, LangOpts);
Eric Liu00d99bd2019-04-11 09:36:36 +0000726 Token Tok;
727
Sam McCallc316b222019-04-26 07:45:49 +0000728 while (!Lex.LexFromRawLexer(Tok))
Haojian Wu7ea4c6f2019-10-30 13:21:47 +0100729 Action(Tok, SM);
Kadir Cetinkaya194117f2019-09-25 14:12:05 +0000730 // LexFromRawLexer returns true after it lexes last token, so we still have
731 // one more token to report.
Haojian Wu7ea4c6f2019-10-30 13:21:47 +0100732 Action(Tok, SM);
Sam McCallc316b222019-04-26 07:45:49 +0000733}
734
735llvm::StringMap<unsigned> collectIdentifiers(llvm::StringRef Content,
736 const format::FormatStyle &Style) {
Eric Liu00d99bd2019-04-11 09:36:36 +0000737 llvm::StringMap<unsigned> Identifiers;
Haojian Wu7ea4c6f2019-10-30 13:21:47 +0100738 auto LangOpt = format::getFormattingLangOpts(Style);
739 lex(Content, LangOpt, [&](const clang::Token &Tok, const SourceManager &) {
740 if (Tok.getKind() == tok::raw_identifier)
Eric Liu00d99bd2019-04-11 09:36:36 +0000741 ++Identifiers[Tok.getRawIdentifier()];
Sam McCallc316b222019-04-26 07:45:49 +0000742 });
Eric Liu00d99bd2019-04-11 09:36:36 +0000743 return Identifiers;
744}
745
Haojian Wu7ea4c6f2019-10-30 13:21:47 +0100746std::vector<Range> collectIdentifierRanges(llvm::StringRef Identifier,
747 llvm::StringRef Content,
748 const LangOptions &LangOpts) {
749 std::vector<Range> Ranges;
750 lex(Content, LangOpts, [&](const clang::Token &Tok, const SourceManager &SM) {
751 if (Tok.getKind() != tok::raw_identifier)
752 return;
753 if (Tok.getRawIdentifier() != Identifier)
754 return;
755 auto Range = getTokenRange(SM, LangOpts, Tok.getLocation());
756 if (!Range)
757 return;
758 Ranges.push_back(*Range);
759 });
760 return Ranges;
761}
762
Sam McCallc316b222019-04-26 07:45:49 +0000763namespace {
Kadir Cetinkayad62e3ed2019-09-25 11:35:38 +0200764struct NamespaceEvent {
765 enum {
766 BeginNamespace, // namespace <ns> {. Payload is resolved <ns>.
767 EndNamespace, // } // namespace <ns>. Payload is resolved *outer*
768 // namespace.
769 UsingDirective // using namespace <ns>. Payload is unresolved <ns>.
770 } Trigger;
771 std::string Payload;
772 Position Pos;
Sam McCallc316b222019-04-26 07:45:49 +0000773};
774// Scans C++ source code for constructs that change the visible namespaces.
Kadir Cetinkayad62e3ed2019-09-25 11:35:38 +0200775void parseNamespaceEvents(llvm::StringRef Code,
776 const format::FormatStyle &Style,
777 llvm::function_ref<void(NamespaceEvent)> Callback) {
Sam McCallc316b222019-04-26 07:45:49 +0000778
779 // Stack of enclosing namespaces, e.g. {"clang", "clangd"}
780 std::vector<std::string> Enclosing; // Contains e.g. "clang", "clangd"
781 // Stack counts open braces. true if the brace opened a namespace.
782 std::vector<bool> BraceStack;
783
784 enum {
785 Default,
786 Namespace, // just saw 'namespace'
787 NamespaceName, // just saw 'namespace' NSName
788 Using, // just saw 'using'
789 UsingNamespace, // just saw 'using namespace'
790 UsingNamespaceName, // just saw 'using namespace' NSName
791 } State = Default;
792 std::string NSName;
793
Kadir Cetinkayad62e3ed2019-09-25 11:35:38 +0200794 NamespaceEvent Event;
Haojian Wu7ea4c6f2019-10-30 13:21:47 +0100795 lex(Code, format::getFormattingLangOpts(Style),
796 [&](const clang::Token &Tok,const SourceManager &SM) {
797 Event.Pos = sourceLocToPosition(SM, Tok.getLocation());
Kadir Cetinkayad62e3ed2019-09-25 11:35:38 +0200798 switch (Tok.getKind()) {
Sam McCallc316b222019-04-26 07:45:49 +0000799 case tok::raw_identifier:
800 // In raw mode, this could be a keyword or a name.
801 switch (State) {
802 case UsingNamespace:
803 case UsingNamespaceName:
Benjamin Krameradcd0262020-01-28 20:23:46 +0100804 NSName.append(std::string(Tok.getRawIdentifier()));
Sam McCallc316b222019-04-26 07:45:49 +0000805 State = UsingNamespaceName;
806 break;
807 case Namespace:
808 case NamespaceName:
Benjamin Krameradcd0262020-01-28 20:23:46 +0100809 NSName.append(std::string(Tok.getRawIdentifier()));
Sam McCallc316b222019-04-26 07:45:49 +0000810 State = NamespaceName;
811 break;
812 case Using:
813 State =
814 (Tok.getRawIdentifier() == "namespace") ? UsingNamespace : Default;
815 break;
816 case Default:
817 NSName.clear();
818 if (Tok.getRawIdentifier() == "namespace")
819 State = Namespace;
820 else if (Tok.getRawIdentifier() == "using")
821 State = Using;
822 break;
823 }
824 break;
825 case tok::coloncolon:
826 // This can come at the beginning or in the middle of a namespace name.
827 switch (State) {
828 case UsingNamespace:
829 case UsingNamespaceName:
830 NSName.append("::");
831 State = UsingNamespaceName;
832 break;
833 case NamespaceName:
834 NSName.append("::");
835 State = NamespaceName;
836 break;
837 case Namespace: // Not legal here.
838 case Using:
839 case Default:
840 State = Default;
841 break;
842 }
843 break;
844 case tok::l_brace:
845 // Record which { started a namespace, so we know when } ends one.
846 if (State == NamespaceName) {
847 // Parsed: namespace <name> {
848 BraceStack.push_back(true);
849 Enclosing.push_back(NSName);
Kadir Cetinkayad62e3ed2019-09-25 11:35:38 +0200850 Event.Trigger = NamespaceEvent::BeginNamespace;
851 Event.Payload = llvm::join(Enclosing, "::");
852 Callback(Event);
Sam McCallc316b222019-04-26 07:45:49 +0000853 } else {
854 // This case includes anonymous namespaces (State = Namespace).
855 // For our purposes, they're not namespaces and we ignore them.
856 BraceStack.push_back(false);
857 }
858 State = Default;
859 break;
860 case tok::r_brace:
861 // If braces are unmatched, we're going to be confused, but don't crash.
862 if (!BraceStack.empty()) {
863 if (BraceStack.back()) {
864 // Parsed: } // namespace
865 Enclosing.pop_back();
Kadir Cetinkayad62e3ed2019-09-25 11:35:38 +0200866 Event.Trigger = NamespaceEvent::EndNamespace;
867 Event.Payload = llvm::join(Enclosing, "::");
868 Callback(Event);
Sam McCallc316b222019-04-26 07:45:49 +0000869 }
870 BraceStack.pop_back();
871 }
872 break;
873 case tok::semi:
Kadir Cetinkayad62e3ed2019-09-25 11:35:38 +0200874 if (State == UsingNamespaceName) {
Sam McCallc316b222019-04-26 07:45:49 +0000875 // Parsed: using namespace <name> ;
Kadir Cetinkayad62e3ed2019-09-25 11:35:38 +0200876 Event.Trigger = NamespaceEvent::UsingDirective;
877 Event.Payload = std::move(NSName);
878 Callback(Event);
879 }
Sam McCallc316b222019-04-26 07:45:49 +0000880 State = Default;
881 break;
882 default:
883 State = Default;
884 break;
885 }
886 });
887}
888
889// Returns the prefix namespaces of NS: {"" ... NS}.
890llvm::SmallVector<llvm::StringRef, 8> ancestorNamespaces(llvm::StringRef NS) {
891 llvm::SmallVector<llvm::StringRef, 8> Results;
892 Results.push_back(NS.take_front(0));
893 NS.split(Results, "::", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
894 for (llvm::StringRef &R : Results)
895 R = NS.take_front(R.end() - NS.begin());
896 return Results;
897}
898
899} // namespace
900
901std::vector<std::string> visibleNamespaces(llvm::StringRef Code,
902 const format::FormatStyle &Style) {
903 std::string Current;
904 // Map from namespace to (resolved) namespaces introduced via using directive.
905 llvm::StringMap<llvm::StringSet<>> UsingDirectives;
906
Kadir Cetinkayad62e3ed2019-09-25 11:35:38 +0200907 parseNamespaceEvents(Code, Style, [&](NamespaceEvent Event) {
908 llvm::StringRef NS = Event.Payload;
909 switch (Event.Trigger) {
910 case NamespaceEvent::BeginNamespace:
911 case NamespaceEvent::EndNamespace:
912 Current = std::move(Event.Payload);
913 break;
914 case NamespaceEvent::UsingDirective:
915 if (NS.consume_front("::"))
916 UsingDirectives[Current].insert(NS);
917 else {
918 for (llvm::StringRef Enclosing : ancestorNamespaces(Current)) {
919 if (Enclosing.empty())
920 UsingDirectives[Current].insert(NS);
921 else
922 UsingDirectives[Current].insert((Enclosing + "::" + NS).str());
923 }
924 }
925 break;
926 }
927 });
Sam McCallc316b222019-04-26 07:45:49 +0000928
929 std::vector<std::string> Found;
930 for (llvm::StringRef Enclosing : ancestorNamespaces(Current)) {
Benjamin Krameradcd0262020-01-28 20:23:46 +0100931 Found.push_back(std::string(Enclosing));
Sam McCallc316b222019-04-26 07:45:49 +0000932 auto It = UsingDirectives.find(Enclosing);
933 if (It != UsingDirectives.end())
Kadir Cetinkayad62e3ed2019-09-25 11:35:38 +0200934 for (const auto &Used : It->second)
Benjamin Krameradcd0262020-01-28 20:23:46 +0100935 Found.push_back(std::string(Used.getKey()));
Sam McCallc316b222019-04-26 07:45:49 +0000936 }
937
Sam McCallc316b222019-04-26 07:45:49 +0000938 llvm::sort(Found, [&](const std::string &LHS, const std::string &RHS) {
939 if (Current == RHS)
940 return false;
941 if (Current == LHS)
942 return true;
943 return LHS < RHS;
944 });
945 Found.erase(std::unique(Found.begin(), Found.end()), Found.end());
946 return Found;
947}
948
Sam McCall9fb22b22019-05-06 10:25:10 +0000949llvm::StringSet<> collectWords(llvm::StringRef Content) {
950 // We assume short words are not significant.
951 // We may want to consider other stopwords, e.g. language keywords.
952 // (A very naive implementation showed no benefit, but lexing might do better)
953 static constexpr int MinWordLength = 4;
954
955 std::vector<CharRole> Roles(Content.size());
956 calculateRoles(Content, Roles);
957
958 llvm::StringSet<> Result;
959 llvm::SmallString<256> Word;
960 auto Flush = [&] {
961 if (Word.size() >= MinWordLength) {
962 for (char &C : Word)
963 C = llvm::toLower(C);
964 Result.insert(Word);
965 }
966 Word.clear();
967 };
968 for (unsigned I = 0; I < Content.size(); ++I) {
969 switch (Roles[I]) {
970 case Head:
971 Flush();
972 LLVM_FALLTHROUGH;
973 case Tail:
974 Word.push_back(Content[I]);
975 break;
976 case Unknown:
977 case Separator:
978 Flush();
979 break;
980 }
981 }
982 Flush();
983
984 return Result;
985}
986
Haojian Wu9d34f452019-07-01 09:26:48 +0000987llvm::Optional<DefinedMacro> locateMacroAt(SourceLocation Loc,
988 Preprocessor &PP) {
989 const auto &SM = PP.getSourceManager();
990 const auto &LangOpts = PP.getLangOpts();
991 Token Result;
992 if (Lexer::getRawToken(SM.getSpellingLoc(Loc), Result, SM, LangOpts, false))
993 return None;
994 if (Result.is(tok::raw_identifier))
995 PP.LookUpIdentifierInfo(Result);
996 IdentifierInfo *IdentifierInfo = Result.getIdentifierInfo();
997 if (!IdentifierInfo || !IdentifierInfo->hadMacroDefinition())
998 return None;
999
1000 std::pair<FileID, unsigned int> DecLoc = SM.getDecomposedExpansionLoc(Loc);
1001 // Get the definition just before the searched location so that a macro
1002 // referenced in a '#undef MACRO' can still be found.
1003 SourceLocation BeforeSearchedLocation =
1004 SM.getMacroArgExpandedLocation(SM.getLocForStartOfFile(DecLoc.first)
1005 .getLocWithOffset(DecLoc.second - 1));
1006 MacroDefinition MacroDef =
1007 PP.getMacroDefinitionAtLoc(IdentifierInfo, BeforeSearchedLocation);
1008 if (auto *MI = MacroDef.getMacroInfo())
1009 return DefinedMacro{IdentifierInfo->getName(), MI};
1010 return None;
1011}
1012
Kadir Cetinkaya5b270932019-09-09 12:28:44 +00001013llvm::Expected<std::string> Edit::apply() const {
1014 return tooling::applyAllReplacements(InitialCode, Replacements);
1015}
1016
1017std::vector<TextEdit> Edit::asTextEdits() const {
1018 return replacementsToEdits(InitialCode, Replacements);
1019}
1020
1021bool Edit::canApplyTo(llvm::StringRef Code) const {
1022 // Create line iterators, since line numbers are important while applying our
1023 // edit we cannot skip blank lines.
1024 auto LHS = llvm::MemoryBuffer::getMemBuffer(Code);
1025 llvm::line_iterator LHSIt(*LHS, /*SkipBlanks=*/false);
1026
1027 auto RHS = llvm::MemoryBuffer::getMemBuffer(InitialCode);
1028 llvm::line_iterator RHSIt(*RHS, /*SkipBlanks=*/false);
1029
1030 // Compare the InitialCode we prepared the edit for with the Code we received
1031 // line by line to make sure there are no differences.
1032 // FIXME: This check is too conservative now, it should be enough to only
1033 // check lines around the replacements contained inside the Edit.
1034 while (!LHSIt.is_at_eof() && !RHSIt.is_at_eof()) {
1035 if (*LHSIt != *RHSIt)
1036 return false;
1037 ++LHSIt;
1038 ++RHSIt;
1039 }
1040
1041 // After we reach EOF for any of the files we make sure the other one doesn't
1042 // contain any additional content except empty lines, they should not
1043 // interfere with the edit we produced.
1044 while (!LHSIt.is_at_eof()) {
1045 if (!LHSIt->empty())
1046 return false;
1047 ++LHSIt;
1048 }
1049 while (!RHSIt.is_at_eof()) {
1050 if (!RHSIt->empty())
1051 return false;
1052 ++RHSIt;
1053 }
1054 return true;
1055}
1056
1057llvm::Error reformatEdit(Edit &E, const format::FormatStyle &Style) {
1058 if (auto NewEdits = cleanupAndFormat(E.InitialCode, E.Replacements, Style))
1059 E.Replacements = std::move(*NewEdits);
1060 else
1061 return NewEdits.takeError();
1062 return llvm::Error::success();
1063}
1064
Kadir Cetinkayad62e3ed2019-09-25 11:35:38 +02001065EligibleRegion getEligiblePoints(llvm::StringRef Code,
1066 llvm::StringRef FullyQualifiedName,
1067 const format::FormatStyle &Style) {
1068 EligibleRegion ER;
1069 // Start with global namespace.
1070 std::vector<std::string> Enclosing = {""};
1071 // FIXME: In addition to namespaces try to generate events for function
1072 // definitions as well. One might use a closing parantheses(")" followed by an
1073 // opening brace "{" to trigger the start.
1074 parseNamespaceEvents(Code, Style, [&](NamespaceEvent Event) {
1075 // Using Directives only introduces declarations to current scope, they do
1076 // not change the current namespace, so skip them.
1077 if (Event.Trigger == NamespaceEvent::UsingDirective)
1078 return;
1079 // Do not qualify the global namespace.
1080 if (!Event.Payload.empty())
1081 Event.Payload.append("::");
1082
1083 std::string CurrentNamespace;
1084 if (Event.Trigger == NamespaceEvent::BeginNamespace) {
1085 Enclosing.emplace_back(std::move(Event.Payload));
1086 CurrentNamespace = Enclosing.back();
1087 // parseNameSpaceEvents reports the beginning position of a token; we want
1088 // to insert after '{', so increment by one.
1089 ++Event.Pos.character;
1090 } else {
1091 // Event.Payload points to outer namespace when exiting a scope, so use
1092 // the namespace we've last entered instead.
1093 CurrentNamespace = std::move(Enclosing.back());
1094 Enclosing.pop_back();
1095 assert(Enclosing.back() == Event.Payload);
1096 }
1097
1098 // Ignore namespaces that are not a prefix of the target.
1099 if (!FullyQualifiedName.startswith(CurrentNamespace))
1100 return;
1101
1102 // Prefer the namespace that shares the longest prefix with target.
1103 if (CurrentNamespace.size() > ER.EnclosingNamespace.size()) {
1104 ER.EligiblePoints.clear();
1105 ER.EnclosingNamespace = CurrentNamespace;
1106 }
1107 if (CurrentNamespace.size() == ER.EnclosingNamespace.size())
1108 ER.EligiblePoints.emplace_back(std::move(Event.Pos));
1109 });
1110 // If there were no shared namespaces just return EOF.
1111 if (ER.EligiblePoints.empty()) {
1112 assert(ER.EnclosingNamespace.empty());
1113 ER.EligiblePoints.emplace_back(offsetToPosition(Code, Code.size()));
1114 }
1115 return ER;
1116}
1117
Haojian Wu509efe52019-11-13 16:30:07 +01001118bool isHeaderFile(llvm::StringRef FileName,
1119 llvm::Optional<LangOptions> LangOpts) {
1120 // Respect the langOpts, for non-file-extension cases, e.g. standard library
1121 // files.
1122 if (LangOpts && LangOpts->IsHeaderFile)
1123 return true;
1124 namespace types = clang::driver::types;
1125 auto Lang = types::lookupTypeForExtension(
1126 llvm::sys::path::extension(FileName).substr(1));
1127 return Lang != types::TY_INVALID && types::onlyPrecompileType(Lang);
1128}
1129
Haojian Wuf8865c02020-02-05 12:03:29 +01001130bool isProtoFile(SourceLocation Loc, const SourceManager &SM) {
1131 auto FileName = SM.getFilename(Loc);
1132 if (!FileName.endswith(".proto.h") && !FileName.endswith(".pb.h"))
1133 return false;
1134 auto FID = SM.getFileID(Loc);
1135 // All proto generated headers should start with this line.
1136 static const char *PROTO_HEADER_COMMENT =
1137 "// Generated by the protocol buffer compiler. DO NOT EDIT!";
1138 // Double check that this is an actual protobuf header.
1139 return SM.getBufferData(FID).startswith(PROTO_HEADER_COMMENT);
1140}
1141
Sam McCallb536a2a2017-12-19 12:23:48 +00001142} // namespace clangd
1143} // namespace clang