1 //===--- SourceCode.h - Manipulating source code as strings -----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 #include "SourceCode.h"
9
10 #include "FuzzyMatch.h"
11 #include "Preamble.h"
12 #include "Protocol.h"
13 #include "refactor/Tweak.h"
14 #include "support/Context.h"
15 #include "support/Logger.h"
16 #include "support/Threading.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/Basic/LangOptions.h"
19 #include "clang/Basic/SourceLocation.h"
20 #include "clang/Basic/SourceManager.h"
21 #include "clang/Basic/TokenKinds.h"
22 #include "clang/Driver/Types.h"
23 #include "clang/Format/Format.h"
24 #include "clang/Lex/Lexer.h"
25 #include "clang/Lex/Preprocessor.h"
26 #include "clang/Lex/Token.h"
27 #include "clang/Tooling/Core/Replacement.h"
28 #include "clang/Tooling/Syntax/Tokens.h"
29 #include "llvm/ADT/ArrayRef.h"
30 #include "llvm/ADT/None.h"
31 #include "llvm/ADT/STLExtras.h"
32 #include "llvm/ADT/StringExtras.h"
33 #include "llvm/ADT/StringMap.h"
34 #include "llvm/ADT/StringRef.h"
35 #include "llvm/Support/Compiler.h"
36 #include "llvm/Support/Errc.h"
37 #include "llvm/Support/Error.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include "llvm/Support/LineIterator.h"
40 #include "llvm/Support/MemoryBuffer.h"
41 #include "llvm/Support/Path.h"
42 #include "llvm/Support/SHA1.h"
43 #include "llvm/Support/VirtualFileSystem.h"
44 #include "llvm/Support/xxhash.h"
45 #include <algorithm>
46 #include <cstddef>
47 #include <string>
48 #include <vector>
49
50 namespace clang {
51 namespace clangd {
52
53 // Here be dragons. LSP positions use columns measured in *UTF-16 code units*!
54 // Clangd uses UTF-8 and byte-offsets internally, so conversion is nontrivial.
55
56 // Iterates over unicode codepoints in the (UTF-8) string. For each,
57 // invokes CB(UTF-8 length, UTF-16 length), and breaks if it returns true.
58 // Returns true if CB returned true, false if we hit the end of string.
59 //
60 // If the string is not valid UTF-8, we log this error and "decode" the
61 // text in some arbitrary way. This is pretty sad, but this tends to happen deep
62 // within indexing of headers where clang misdetected the encoding, and
63 // propagating the error all the way back up is (probably?) not be worth it.
64 template <typename Callback>
iterateCodepoints(llvm::StringRef U8,const Callback & CB)65 static bool iterateCodepoints(llvm::StringRef U8, const Callback &CB) {
66 bool LoggedInvalid = false;
67 // A codepoint takes two UTF-16 code unit if it's astral (outside BMP).
68 // Astral codepoints are encoded as 4 bytes in UTF-8, starting with 11110xxx.
69 for (size_t I = 0; I < U8.size();) {
70 unsigned char C = static_cast<unsigned char>(U8[I]);
71 if (LLVM_LIKELY(!(C & 0x80))) { // ASCII character.
72 if (CB(1, 1))
73 return true;
74 ++I;
75 continue;
76 }
77 // This convenient property of UTF-8 holds for all non-ASCII characters.
78 size_t UTF8Length = llvm::countLeadingOnes(C);
79 // 0xxx is ASCII, handled above. 10xxx is a trailing byte, invalid here.
80 // 11111xxx is not valid UTF-8 at all, maybe some ISO-8859-*.
81 if (LLVM_UNLIKELY(UTF8Length < 2 || UTF8Length > 4)) {
82 if (!LoggedInvalid) {
83 elog("File has invalid UTF-8 near offset {0}: {1}", I, llvm::toHex(U8));
84 LoggedInvalid = true;
85 }
86 // We can't give a correct result, but avoid returning something wild.
87 // Pretend this is a valid ASCII byte, for lack of better options.
88 // (Too late to get ISO-8859-* right, we've skipped some bytes already).
89 if (CB(1, 1))
90 return true;
91 ++I;
92 continue;
93 }
94 I += UTF8Length; // Skip over all trailing bytes.
95 // A codepoint takes two UTF-16 code unit if it's astral (outside BMP).
96 // Astral codepoints are encoded as 4 bytes in UTF-8 (11110xxx ...)
97 if (CB(UTF8Length, UTF8Length == 4 ? 2 : 1))
98 return true;
99 }
100 return false;
101 }
102
103 // Returns the byte offset into the string that is an offset of \p Units in
104 // the specified encoding.
105 // Conceptually, this converts to the encoding, truncates to CodeUnits,
106 // converts back to UTF-8, and returns the length in bytes.
measureUnits(llvm::StringRef U8,int Units,OffsetEncoding Enc,bool & Valid)107 static size_t measureUnits(llvm::StringRef U8, int Units, OffsetEncoding Enc,
108 bool &Valid) {
109 Valid = Units >= 0;
110 if (Units <= 0)
111 return 0;
112 size_t Result = 0;
113 switch (Enc) {
114 case OffsetEncoding::UTF8:
115 Result = Units;
116 break;
117 case OffsetEncoding::UTF16:
118 Valid = iterateCodepoints(U8, [&](int U8Len, int U16Len) {
119 Result += U8Len;
120 Units -= U16Len;
121 return Units <= 0;
122 });
123 if (Units < 0) // Offset in the middle of a surrogate pair.
124 Valid = false;
125 break;
126 case OffsetEncoding::UTF32:
127 Valid = iterateCodepoints(U8, [&](int U8Len, int U16Len) {
128 Result += U8Len;
129 Units--;
130 return Units <= 0;
131 });
132 break;
133 case OffsetEncoding::UnsupportedEncoding:
134 llvm_unreachable("unsupported encoding");
135 }
136 // Don't return an out-of-range index if we overran.
137 if (Result > U8.size()) {
138 Valid = false;
139 return U8.size();
140 }
141 return Result;
142 }
143
144 Key<OffsetEncoding> kCurrentOffsetEncoding;
lspEncoding()145 static OffsetEncoding lspEncoding() {
146 auto *Enc = Context::current().get(kCurrentOffsetEncoding);
147 return Enc ? *Enc : OffsetEncoding::UTF16;
148 }
149
150 // Like most strings in clangd, the input is UTF-8 encoded.
lspLength(llvm::StringRef Code)151 size_t lspLength(llvm::StringRef Code) {
152 size_t Count = 0;
153 switch (lspEncoding()) {
154 case OffsetEncoding::UTF8:
155 Count = Code.size();
156 break;
157 case OffsetEncoding::UTF16:
158 iterateCodepoints(Code, [&](int U8Len, int U16Len) {
159 Count += U16Len;
160 return false;
161 });
162 break;
163 case OffsetEncoding::UTF32:
164 iterateCodepoints(Code, [&](int U8Len, int U16Len) {
165 ++Count;
166 return false;
167 });
168 break;
169 case OffsetEncoding::UnsupportedEncoding:
170 llvm_unreachable("unsupported encoding");
171 }
172 return Count;
173 }
174
positionToOffset(llvm::StringRef Code,Position P,bool AllowColumnsBeyondLineLength)175 llvm::Expected<size_t> positionToOffset(llvm::StringRef Code, Position P,
176 bool AllowColumnsBeyondLineLength) {
177 if (P.line < 0)
178 return error(llvm::errc::invalid_argument,
179 "Line value can't be negative ({0})", P.line);
180 if (P.character < 0)
181 return error(llvm::errc::invalid_argument,
182 "Character value can't be negative ({0})", P.character);
183 size_t StartOfLine = 0;
184 for (int I = 0; I != P.line; ++I) {
185 size_t NextNL = Code.find('\n', StartOfLine);
186 if (NextNL == llvm::StringRef::npos)
187 return error(llvm::errc::invalid_argument,
188 "Line value is out of range ({0})", P.line);
189 StartOfLine = NextNL + 1;
190 }
191 StringRef Line =
192 Code.substr(StartOfLine).take_until([](char C) { return C == '\n'; });
193
194 // P.character may be in UTF-16, transcode if necessary.
195 bool Valid;
196 size_t ByteInLine = measureUnits(Line, P.character, lspEncoding(), Valid);
197 if (!Valid && !AllowColumnsBeyondLineLength)
198 return error(llvm::errc::invalid_argument,
199 "{0} offset {1} is invalid for line {2}", lspEncoding(),
200 P.character, P.line);
201 return StartOfLine + ByteInLine;
202 }
203
offsetToPosition(llvm::StringRef Code,size_t Offset)204 Position offsetToPosition(llvm::StringRef Code, size_t Offset) {
205 Offset = std::min(Code.size(), Offset);
206 llvm::StringRef Before = Code.substr(0, Offset);
207 int Lines = Before.count('\n');
208 size_t PrevNL = Before.rfind('\n');
209 size_t StartOfLine = (PrevNL == llvm::StringRef::npos) ? 0 : (PrevNL + 1);
210 Position Pos;
211 Pos.line = Lines;
212 Pos.character = lspLength(Before.substr(StartOfLine));
213 return Pos;
214 }
215
sourceLocToPosition(const SourceManager & SM,SourceLocation Loc)216 Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc) {
217 // We use the SourceManager's line tables, but its column number is in bytes.
218 FileID FID;
219 unsigned Offset;
220 std::tie(FID, Offset) = SM.getDecomposedSpellingLoc(Loc);
221 Position P;
222 P.line = static_cast<int>(SM.getLineNumber(FID, Offset)) - 1;
223 bool Invalid = false;
224 llvm::StringRef Code = SM.getBufferData(FID, &Invalid);
225 if (!Invalid) {
226 auto ColumnInBytes = SM.getColumnNumber(FID, Offset) - 1;
227 auto LineSoFar = Code.substr(Offset - ColumnInBytes, ColumnInBytes);
228 P.character = lspLength(LineSoFar);
229 }
230 return P;
231 }
232
isSpelledInSource(SourceLocation Loc,const SourceManager & SM)233 bool isSpelledInSource(SourceLocation Loc, const SourceManager &SM) {
234 if (Loc.isMacroID()) {
235 std::string PrintLoc = SM.getSpellingLoc(Loc).printToString(SM);
236 if (llvm::StringRef(PrintLoc).startswith("<scratch") ||
237 llvm::StringRef(PrintLoc).startswith("<command line>"))
238 return false;
239 }
240 return true;
241 }
242
isValidFileRange(const SourceManager & Mgr,SourceRange R)243 bool isValidFileRange(const SourceManager &Mgr, SourceRange R) {
244 if (!R.getBegin().isValid() || !R.getEnd().isValid())
245 return false;
246
247 FileID BeginFID;
248 size_t BeginOffset = 0;
249 std::tie(BeginFID, BeginOffset) = Mgr.getDecomposedLoc(R.getBegin());
250
251 FileID EndFID;
252 size_t EndOffset = 0;
253 std::tie(EndFID, EndOffset) = Mgr.getDecomposedLoc(R.getEnd());
254
255 return BeginFID.isValid() && BeginFID == EndFID && BeginOffset <= EndOffset;
256 }
257
includeHashLoc(FileID IncludedFile,const SourceManager & SM)258 SourceLocation includeHashLoc(FileID IncludedFile, const SourceManager &SM) {
259 assert(SM.getLocForEndOfFile(IncludedFile).isFileID());
260 FileID IncludingFile;
261 unsigned Offset;
262 std::tie(IncludingFile, Offset) =
263 SM.getDecomposedExpansionLoc(SM.getIncludeLoc(IncludedFile));
264 bool Invalid = false;
265 llvm::StringRef Buf = SM.getBufferData(IncludingFile, &Invalid);
266 if (Invalid)
267 return SourceLocation();
268 // Now buf is "...\n#include <foo>\n..."
269 // and Offset points here: ^
270 // Rewind to the preceding # on the line.
271 assert(Offset < Buf.size());
272 for (;; --Offset) {
273 if (Buf[Offset] == '#')
274 return SM.getComposedLoc(IncludingFile, Offset);
275 if (Buf[Offset] == '\n' || Offset == 0) // no hash, what's going on?
276 return SourceLocation();
277 }
278 }
279
getTokenLengthAtLoc(SourceLocation Loc,const SourceManager & SM,const LangOptions & LangOpts)280 static unsigned getTokenLengthAtLoc(SourceLocation Loc, const SourceManager &SM,
281 const LangOptions &LangOpts) {
282 Token TheTok;
283 if (Lexer::getRawToken(Loc, TheTok, SM, LangOpts))
284 return 0;
285 // FIXME: Here we check whether the token at the location is a greatergreater
286 // (>>) token and consider it as a single greater (>). This is to get it
287 // working for templates but it isn't correct for the right shift operator. We
288 // can avoid this by using half open char ranges in getFileRange() but getting
289 // token ending is not well supported in macroIDs.
290 if (TheTok.is(tok::greatergreater))
291 return 1;
292 return TheTok.getLength();
293 }
294
295 // Returns location of the last character of the token at a given loc
getLocForTokenEnd(SourceLocation BeginLoc,const SourceManager & SM,const LangOptions & LangOpts)296 static SourceLocation getLocForTokenEnd(SourceLocation BeginLoc,
297 const SourceManager &SM,
298 const LangOptions &LangOpts) {
299 unsigned Len = getTokenLengthAtLoc(BeginLoc, SM, LangOpts);
300 return BeginLoc.getLocWithOffset(Len ? Len - 1 : 0);
301 }
302
303 // Returns location of the starting of the token at a given EndLoc
getLocForTokenBegin(SourceLocation EndLoc,const SourceManager & SM,const LangOptions & LangOpts)304 static SourceLocation getLocForTokenBegin(SourceLocation EndLoc,
305 const SourceManager &SM,
306 const LangOptions &LangOpts) {
307 return EndLoc.getLocWithOffset(
308 -(signed)getTokenLengthAtLoc(EndLoc, SM, LangOpts));
309 }
310
311 // Converts a char source range to a token range.
toTokenRange(CharSourceRange Range,const SourceManager & SM,const LangOptions & LangOpts)312 static SourceRange toTokenRange(CharSourceRange Range, const SourceManager &SM,
313 const LangOptions &LangOpts) {
314 if (!Range.isTokenRange())
315 Range.setEnd(getLocForTokenBegin(Range.getEnd(), SM, LangOpts));
316 return Range.getAsRange();
317 }
318 // Returns the union of two token ranges.
319 // To find the maximum of the Ends of the ranges, we compare the location of the
320 // last character of the token.
unionTokenRange(SourceRange R1,SourceRange R2,const SourceManager & SM,const LangOptions & LangOpts)321 static SourceRange unionTokenRange(SourceRange R1, SourceRange R2,
322 const SourceManager &SM,
323 const LangOptions &LangOpts) {
324 SourceLocation Begin =
325 SM.isBeforeInTranslationUnit(R1.getBegin(), R2.getBegin())
326 ? R1.getBegin()
327 : R2.getBegin();
328 SourceLocation End =
329 SM.isBeforeInTranslationUnit(getLocForTokenEnd(R1.getEnd(), SM, LangOpts),
330 getLocForTokenEnd(R2.getEnd(), SM, LangOpts))
331 ? R2.getEnd()
332 : R1.getEnd();
333 return SourceRange(Begin, End);
334 }
335
336 // Given a range whose endpoints may be in different expansions or files,
337 // tries to find a range within a common file by following up the expansion and
338 // include location in each.
rangeInCommonFile(SourceRange R,const SourceManager & SM,const LangOptions & LangOpts)339 static SourceRange rangeInCommonFile(SourceRange R, const SourceManager &SM,
340 const LangOptions &LangOpts) {
341 // Fast path for most common cases.
342 if (SM.isWrittenInSameFile(R.getBegin(), R.getEnd()))
343 return R;
344 // Record the stack of expansion locations for the beginning, keyed by FileID.
345 llvm::DenseMap<FileID, SourceLocation> BeginExpansions;
346 for (SourceLocation Begin = R.getBegin(); Begin.isValid();
347 Begin = Begin.isFileID()
348 ? includeHashLoc(SM.getFileID(Begin), SM)
349 : SM.getImmediateExpansionRange(Begin).getBegin()) {
350 BeginExpansions[SM.getFileID(Begin)] = Begin;
351 }
352 // Move up the stack of expansion locations for the end until we find the
353 // location in BeginExpansions with that has the same file id.
354 for (SourceLocation End = R.getEnd(); End.isValid();
355 End = End.isFileID() ? includeHashLoc(SM.getFileID(End), SM)
356 : toTokenRange(SM.getImmediateExpansionRange(End),
357 SM, LangOpts)
358 .getEnd()) {
359 auto It = BeginExpansions.find(SM.getFileID(End));
360 if (It != BeginExpansions.end()) {
361 if (SM.getFileOffset(It->second) > SM.getFileOffset(End))
362 return SourceLocation();
363 return {It->second, End};
364 }
365 }
366 return SourceRange();
367 }
368
369 // Find an expansion range (not necessarily immediate) the ends of which are in
370 // the same file id.
371 static SourceRange
getExpansionTokenRangeInSameFile(SourceLocation Loc,const SourceManager & SM,const LangOptions & LangOpts)372 getExpansionTokenRangeInSameFile(SourceLocation Loc, const SourceManager &SM,
373 const LangOptions &LangOpts) {
374 return rangeInCommonFile(
375 toTokenRange(SM.getImmediateExpansionRange(Loc), SM, LangOpts), SM,
376 LangOpts);
377 }
378
379 // Returns the file range for a given Location as a Token Range
380 // This is quite similar to getFileLoc in SourceManager as both use
381 // getImmediateExpansionRange and getImmediateSpellingLoc (for macro IDs).
382 // However:
383 // - We want to maintain the full range information as we move from one file to
384 // the next. getFileLoc only uses the BeginLoc of getImmediateExpansionRange.
385 // - We want to split '>>' tokens as the lexer parses the '>>' in nested
386 // template instantiations as a '>>' instead of two '>'s.
387 // There is also getExpansionRange but it simply calls
388 // getImmediateExpansionRange on the begin and ends separately which is wrong.
getTokenFileRange(SourceLocation Loc,const SourceManager & SM,const LangOptions & LangOpts)389 static SourceRange getTokenFileRange(SourceLocation Loc,
390 const SourceManager &SM,
391 const LangOptions &LangOpts) {
392 SourceRange FileRange = Loc;
393 while (!FileRange.getBegin().isFileID()) {
394 if (SM.isMacroArgExpansion(FileRange.getBegin())) {
395 FileRange = unionTokenRange(
396 SM.getImmediateSpellingLoc(FileRange.getBegin()),
397 SM.getImmediateSpellingLoc(FileRange.getEnd()), SM, LangOpts);
398 assert(SM.isWrittenInSameFile(FileRange.getBegin(), FileRange.getEnd()));
399 } else {
400 SourceRange ExpansionRangeForBegin =
401 getExpansionTokenRangeInSameFile(FileRange.getBegin(), SM, LangOpts);
402 SourceRange ExpansionRangeForEnd =
403 getExpansionTokenRangeInSameFile(FileRange.getEnd(), SM, LangOpts);
404 if (ExpansionRangeForBegin.isInvalid() ||
405 ExpansionRangeForEnd.isInvalid())
406 return SourceRange();
407 assert(SM.isWrittenInSameFile(ExpansionRangeForBegin.getBegin(),
408 ExpansionRangeForEnd.getBegin()) &&
409 "Both Expansion ranges should be in same file.");
410 FileRange = unionTokenRange(ExpansionRangeForBegin, ExpansionRangeForEnd,
411 SM, LangOpts);
412 }
413 }
414 return FileRange;
415 }
416
isInsideMainFile(SourceLocation Loc,const SourceManager & SM)417 bool isInsideMainFile(SourceLocation Loc, const SourceManager &SM) {
418 if (!Loc.isValid())
419 return false;
420 FileID FID = SM.getFileID(SM.getExpansionLoc(Loc));
421 return FID == SM.getMainFileID() || FID == SM.getPreambleFileID();
422 }
423
toHalfOpenFileRange(const SourceManager & SM,const LangOptions & LangOpts,SourceRange R)424 llvm::Optional<SourceRange> toHalfOpenFileRange(const SourceManager &SM,
425 const LangOptions &LangOpts,
426 SourceRange R) {
427 SourceRange R1 = getTokenFileRange(R.getBegin(), SM, LangOpts);
428 if (!isValidFileRange(SM, R1))
429 return llvm::None;
430
431 SourceRange R2 = getTokenFileRange(R.getEnd(), SM, LangOpts);
432 if (!isValidFileRange(SM, R2))
433 return llvm::None;
434
435 SourceRange Result =
436 rangeInCommonFile(unionTokenRange(R1, R2, SM, LangOpts), SM, LangOpts);
437 unsigned TokLen = getTokenLengthAtLoc(Result.getEnd(), SM, LangOpts);
438 // Convert from closed token range to half-open (char) range
439 Result.setEnd(Result.getEnd().getLocWithOffset(TokLen));
440 if (!isValidFileRange(SM, Result))
441 return llvm::None;
442
443 return Result;
444 }
445
toSourceCode(const SourceManager & SM,SourceRange R)446 llvm::StringRef toSourceCode(const SourceManager &SM, SourceRange R) {
447 assert(isValidFileRange(SM, R));
448 auto Buf = SM.getBufferOrNone(SM.getFileID(R.getBegin()));
449 assert(Buf);
450
451 size_t BeginOffset = SM.getFileOffset(R.getBegin());
452 size_t EndOffset = SM.getFileOffset(R.getEnd());
453 return Buf->getBuffer().substr(BeginOffset, EndOffset - BeginOffset);
454 }
455
sourceLocationInMainFile(const SourceManager & SM,Position P)456 llvm::Expected<SourceLocation> sourceLocationInMainFile(const SourceManager &SM,
457 Position P) {
458 llvm::StringRef Code = SM.getBufferOrFake(SM.getMainFileID()).getBuffer();
459 auto Offset =
460 positionToOffset(Code, P, /*AllowColumnBeyondLineLength=*/false);
461 if (!Offset)
462 return Offset.takeError();
463 return SM.getLocForStartOfFile(SM.getMainFileID()).getLocWithOffset(*Offset);
464 }
465
halfOpenToRange(const SourceManager & SM,CharSourceRange R)466 Range halfOpenToRange(const SourceManager &SM, CharSourceRange R) {
467 // Clang is 1-based, LSP uses 0-based indexes.
468 Position Begin = sourceLocToPosition(SM, R.getBegin());
469 Position End = sourceLocToPosition(SM, R.getEnd());
470
471 return {Begin, End};
472 }
473
offsetToClangLineColumn(llvm::StringRef Code,size_t Offset)474 std::pair<size_t, size_t> offsetToClangLineColumn(llvm::StringRef Code,
475 size_t Offset) {
476 Offset = std::min(Code.size(), Offset);
477 llvm::StringRef Before = Code.substr(0, Offset);
478 int Lines = Before.count('\n');
479 size_t PrevNL = Before.rfind('\n');
480 size_t StartOfLine = (PrevNL == llvm::StringRef::npos) ? 0 : (PrevNL + 1);
481 return {Lines + 1, Offset - StartOfLine + 1};
482 }
483
splitQualifiedName(StringRef QName)484 std::pair<StringRef, StringRef> splitQualifiedName(StringRef QName) {
485 size_t Pos = QName.rfind("::");
486 if (Pos == llvm::StringRef::npos)
487 return {llvm::StringRef(), QName};
488 return {QName.substr(0, Pos + 2), QName.substr(Pos + 2)};
489 }
490
replacementToEdit(llvm::StringRef Code,const tooling::Replacement & R)491 TextEdit replacementToEdit(llvm::StringRef Code,
492 const tooling::Replacement &R) {
493 Range ReplacementRange = {
494 offsetToPosition(Code, R.getOffset()),
495 offsetToPosition(Code, R.getOffset() + R.getLength())};
496 return {ReplacementRange, std::string(R.getReplacementText())};
497 }
498
replacementsToEdits(llvm::StringRef Code,const tooling::Replacements & Repls)499 std::vector<TextEdit> replacementsToEdits(llvm::StringRef Code,
500 const tooling::Replacements &Repls) {
501 std::vector<TextEdit> Edits;
502 for (const auto &R : Repls)
503 Edits.push_back(replacementToEdit(Code, R));
504 return Edits;
505 }
506
getCanonicalPath(const FileEntry * F,const SourceManager & SourceMgr)507 llvm::Optional<std::string> getCanonicalPath(const FileEntry *F,
508 const SourceManager &SourceMgr) {
509 if (!F)
510 return None;
511
512 llvm::SmallString<128> FilePath = F->getName();
513 if (!llvm::sys::path::is_absolute(FilePath)) {
514 if (auto EC =
515 SourceMgr.getFileManager().getVirtualFileSystem().makeAbsolute(
516 FilePath)) {
517 elog("Could not turn relative path '{0}' to absolute: {1}", FilePath,
518 EC.message());
519 return None;
520 }
521 }
522
523 // Handle the symbolic link path case where the current working directory
524 // (getCurrentWorkingDirectory) is a symlink. We always want to the real
525 // file path (instead of the symlink path) for the C++ symbols.
526 //
527 // Consider the following example:
528 //
529 // src dir: /project/src/foo.h
530 // current working directory (symlink): /tmp/build -> /project/src/
531 //
532 // The file path of Symbol is "/project/src/foo.h" instead of
533 // "/tmp/build/foo.h"
534 if (auto Dir = SourceMgr.getFileManager().getDirectory(
535 llvm::sys::path::parent_path(FilePath))) {
536 llvm::SmallString<128> RealPath;
537 llvm::StringRef DirName = SourceMgr.getFileManager().getCanonicalName(*Dir);
538 llvm::sys::path::append(RealPath, DirName,
539 llvm::sys::path::filename(FilePath));
540 return RealPath.str().str();
541 }
542
543 return FilePath.str().str();
544 }
545
toTextEdit(const FixItHint & FixIt,const SourceManager & M,const LangOptions & L)546 TextEdit toTextEdit(const FixItHint &FixIt, const SourceManager &M,
547 const LangOptions &L) {
548 TextEdit Result;
549 Result.range =
550 halfOpenToRange(M, Lexer::makeFileCharRange(FixIt.RemoveRange, M, L));
551 Result.newText = FixIt.CodeToInsert;
552 return Result;
553 }
554
digest(llvm::StringRef Content)555 FileDigest digest(llvm::StringRef Content) {
556 uint64_t Hash{llvm::xxHash64(Content)};
557 FileDigest Result;
558 for (unsigned I = 0; I < Result.size(); ++I) {
559 Result[I] = uint8_t(Hash);
560 Hash >>= 8;
561 }
562 return Result;
563 }
564
digestFile(const SourceManager & SM,FileID FID)565 llvm::Optional<FileDigest> digestFile(const SourceManager &SM, FileID FID) {
566 bool Invalid = false;
567 llvm::StringRef Content = SM.getBufferData(FID, &Invalid);
568 if (Invalid)
569 return None;
570 return digest(Content);
571 }
572
getFormatStyleForFile(llvm::StringRef File,llvm::StringRef Content,const ThreadsafeFS & TFS)573 format::FormatStyle getFormatStyleForFile(llvm::StringRef File,
574 llvm::StringRef Content,
575 const ThreadsafeFS &TFS) {
576 auto Style = format::getStyle(format::DefaultFormatStyle, File,
577 format::DefaultFallbackStyle, Content,
578 TFS.view(/*CWD=*/llvm::None).get());
579 if (!Style) {
580 log("getStyle() failed for file {0}: {1}. Fallback is LLVM style.", File,
581 Style.takeError());
582 return format::getLLVMStyle();
583 }
584 return *Style;
585 }
586
587 llvm::Expected<tooling::Replacements>
cleanupAndFormat(StringRef Code,const tooling::Replacements & Replaces,const format::FormatStyle & Style)588 cleanupAndFormat(StringRef Code, const tooling::Replacements &Replaces,
589 const format::FormatStyle &Style) {
590 auto CleanReplaces = cleanupAroundReplacements(Code, Replaces, Style);
591 if (!CleanReplaces)
592 return CleanReplaces;
593 return formatReplacements(Code, std::move(*CleanReplaces), Style);
594 }
595
596 static void
lex(llvm::StringRef Code,const LangOptions & LangOpts,llvm::function_ref<void (const syntax::Token &,const SourceManager & SM)> Action)597 lex(llvm::StringRef Code, const LangOptions &LangOpts,
598 llvm::function_ref<void(const syntax::Token &, const SourceManager &SM)>
599 Action) {
600 // FIXME: InMemoryFileAdapter crashes unless the buffer is null terminated!
601 std::string NullTerminatedCode = Code.str();
602 SourceManagerForFile FileSM("dummy.cpp", NullTerminatedCode);
603 auto &SM = FileSM.get();
604 for (const auto &Tok : syntax::tokenize(SM.getMainFileID(), SM, LangOpts))
605 Action(Tok, SM);
606 }
607
collectIdentifiers(llvm::StringRef Content,const format::FormatStyle & Style)608 llvm::StringMap<unsigned> collectIdentifiers(llvm::StringRef Content,
609 const format::FormatStyle &Style) {
610 llvm::StringMap<unsigned> Identifiers;
611 auto LangOpt = format::getFormattingLangOpts(Style);
612 lex(Content, LangOpt, [&](const syntax::Token &Tok, const SourceManager &SM) {
613 if (Tok.kind() == tok::identifier)
614 ++Identifiers[Tok.text(SM)];
615 // FIXME: Should this function really return keywords too ?
616 else if (const auto *Keyword = tok::getKeywordSpelling(Tok.kind()))
617 ++Identifiers[Keyword];
618 });
619 return Identifiers;
620 }
621
collectIdentifierRanges(llvm::StringRef Identifier,llvm::StringRef Content,const LangOptions & LangOpts)622 std::vector<Range> collectIdentifierRanges(llvm::StringRef Identifier,
623 llvm::StringRef Content,
624 const LangOptions &LangOpts) {
625 std::vector<Range> Ranges;
626 lex(Content, LangOpts,
627 [&](const syntax::Token &Tok, const SourceManager &SM) {
628 if (Tok.kind() != tok::identifier || Tok.text(SM) != Identifier)
629 return;
630 Ranges.push_back(halfOpenToRange(SM, Tok.range(SM).toCharRange(SM)));
631 });
632 return Ranges;
633 }
634
isKeyword(llvm::StringRef NewName,const LangOptions & LangOpts)635 bool isKeyword(llvm::StringRef NewName, const LangOptions &LangOpts) {
636 // Keywords are initialized in constructor.
637 clang::IdentifierTable KeywordsTable(LangOpts);
638 return KeywordsTable.find(NewName) != KeywordsTable.end();
639 }
640
641 namespace {
642 struct NamespaceEvent {
643 enum {
644 BeginNamespace, // namespace <ns> {. Payload is resolved <ns>.
645 EndNamespace, // } // namespace <ns>. Payload is resolved *outer*
646 // namespace.
647 UsingDirective // using namespace <ns>. Payload is unresolved <ns>.
648 } Trigger;
649 std::string Payload;
650 Position Pos;
651 };
652 // Scans C++ source code for constructs that change the visible namespaces.
parseNamespaceEvents(llvm::StringRef Code,const LangOptions & LangOpts,llvm::function_ref<void (NamespaceEvent)> Callback)653 void parseNamespaceEvents(llvm::StringRef Code, const LangOptions &LangOpts,
654 llvm::function_ref<void(NamespaceEvent)> Callback) {
655
656 // Stack of enclosing namespaces, e.g. {"clang", "clangd"}
657 std::vector<std::string> Enclosing; // Contains e.g. "clang", "clangd"
658 // Stack counts open braces. true if the brace opened a namespace.
659 std::vector<bool> BraceStack;
660
661 enum {
662 Default,
663 Namespace, // just saw 'namespace'
664 NamespaceName, // just saw 'namespace' NSName
665 Using, // just saw 'using'
666 UsingNamespace, // just saw 'using namespace'
667 UsingNamespaceName, // just saw 'using namespace' NSName
668 } State = Default;
669 std::string NSName;
670
671 NamespaceEvent Event;
672 lex(Code, LangOpts, [&](const syntax::Token &Tok, const SourceManager &SM) {
673 Event.Pos = sourceLocToPosition(SM, Tok.location());
674 switch (Tok.kind()) {
675 case tok::kw_using:
676 State = State == Default ? Using : Default;
677 break;
678 case tok::kw_namespace:
679 switch (State) {
680 case Using:
681 State = UsingNamespace;
682 break;
683 case Default:
684 State = Namespace;
685 break;
686 default:
687 State = Default;
688 break;
689 }
690 break;
691 case tok::identifier:
692 switch (State) {
693 case UsingNamespace:
694 NSName.clear();
695 LLVM_FALLTHROUGH;
696 case UsingNamespaceName:
697 NSName.append(Tok.text(SM).str());
698 State = UsingNamespaceName;
699 break;
700 case Namespace:
701 NSName.clear();
702 LLVM_FALLTHROUGH;
703 case NamespaceName:
704 NSName.append(Tok.text(SM).str());
705 State = NamespaceName;
706 break;
707 case Using:
708 case Default:
709 State = Default;
710 break;
711 }
712 break;
713 case tok::coloncolon:
714 // This can come at the beginning or in the middle of a namespace
715 // name.
716 switch (State) {
717 case UsingNamespace:
718 NSName.clear();
719 LLVM_FALLTHROUGH;
720 case UsingNamespaceName:
721 NSName.append("::");
722 State = UsingNamespaceName;
723 break;
724 case NamespaceName:
725 NSName.append("::");
726 State = NamespaceName;
727 break;
728 case Namespace: // Not legal here.
729 case Using:
730 case Default:
731 State = Default;
732 break;
733 }
734 break;
735 case tok::l_brace:
736 // Record which { started a namespace, so we know when } ends one.
737 if (State == NamespaceName) {
738 // Parsed: namespace <name> {
739 BraceStack.push_back(true);
740 Enclosing.push_back(NSName);
741 Event.Trigger = NamespaceEvent::BeginNamespace;
742 Event.Payload = llvm::join(Enclosing, "::");
743 Callback(Event);
744 } else {
745 // This case includes anonymous namespaces (State = Namespace).
746 // For our purposes, they're not namespaces and we ignore them.
747 BraceStack.push_back(false);
748 }
749 State = Default;
750 break;
751 case tok::r_brace:
752 // If braces are unmatched, we're going to be confused, but don't
753 // crash.
754 if (!BraceStack.empty()) {
755 if (BraceStack.back()) {
756 // Parsed: } // namespace
757 Enclosing.pop_back();
758 Event.Trigger = NamespaceEvent::EndNamespace;
759 Event.Payload = llvm::join(Enclosing, "::");
760 Callback(Event);
761 }
762 BraceStack.pop_back();
763 }
764 break;
765 case tok::semi:
766 if (State == UsingNamespaceName) {
767 // Parsed: using namespace <name> ;
768 Event.Trigger = NamespaceEvent::UsingDirective;
769 Event.Payload = std::move(NSName);
770 Callback(Event);
771 }
772 State = Default;
773 break;
774 default:
775 State = Default;
776 break;
777 }
778 });
779 }
780
781 // Returns the prefix namespaces of NS: {"" ... NS}.
ancestorNamespaces(llvm::StringRef NS)782 llvm::SmallVector<llvm::StringRef, 8> ancestorNamespaces(llvm::StringRef NS) {
783 llvm::SmallVector<llvm::StringRef, 8> Results;
784 Results.push_back(NS.take_front(0));
785 NS.split(Results, "::", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
786 for (llvm::StringRef &R : Results)
787 R = NS.take_front(R.end() - NS.begin());
788 return Results;
789 }
790
791 } // namespace
792
visibleNamespaces(llvm::StringRef Code,const LangOptions & LangOpts)793 std::vector<std::string> visibleNamespaces(llvm::StringRef Code,
794 const LangOptions &LangOpts) {
795 std::string Current;
796 // Map from namespace to (resolved) namespaces introduced via using directive.
797 llvm::StringMap<llvm::StringSet<>> UsingDirectives;
798
799 parseNamespaceEvents(Code, LangOpts, [&](NamespaceEvent Event) {
800 llvm::StringRef NS = Event.Payload;
801 switch (Event.Trigger) {
802 case NamespaceEvent::BeginNamespace:
803 case NamespaceEvent::EndNamespace:
804 Current = std::move(Event.Payload);
805 break;
806 case NamespaceEvent::UsingDirective:
807 if (NS.consume_front("::"))
808 UsingDirectives[Current].insert(NS);
809 else {
810 for (llvm::StringRef Enclosing : ancestorNamespaces(Current)) {
811 if (Enclosing.empty())
812 UsingDirectives[Current].insert(NS);
813 else
814 UsingDirectives[Current].insert((Enclosing + "::" + NS).str());
815 }
816 }
817 break;
818 }
819 });
820
821 std::vector<std::string> Found;
822 for (llvm::StringRef Enclosing : ancestorNamespaces(Current)) {
823 Found.push_back(std::string(Enclosing));
824 auto It = UsingDirectives.find(Enclosing);
825 if (It != UsingDirectives.end())
826 for (const auto &Used : It->second)
827 Found.push_back(std::string(Used.getKey()));
828 }
829
830 llvm::sort(Found, [&](const std::string &LHS, const std::string &RHS) {
831 if (Current == RHS)
832 return false;
833 if (Current == LHS)
834 return true;
835 return LHS < RHS;
836 });
837 Found.erase(std::unique(Found.begin(), Found.end()), Found.end());
838 return Found;
839 }
840
collectWords(llvm::StringRef Content)841 llvm::StringSet<> collectWords(llvm::StringRef Content) {
842 // We assume short words are not significant.
843 // We may want to consider other stopwords, e.g. language keywords.
844 // (A very naive implementation showed no benefit, but lexing might do better)
845 static constexpr int MinWordLength = 4;
846
847 std::vector<CharRole> Roles(Content.size());
848 calculateRoles(Content, Roles);
849
850 llvm::StringSet<> Result;
851 llvm::SmallString<256> Word;
852 auto Flush = [&] {
853 if (Word.size() >= MinWordLength) {
854 for (char &C : Word)
855 C = llvm::toLower(C);
856 Result.insert(Word);
857 }
858 Word.clear();
859 };
860 for (unsigned I = 0; I < Content.size(); ++I) {
861 switch (Roles[I]) {
862 case Head:
863 Flush();
864 LLVM_FALLTHROUGH;
865 case Tail:
866 Word.push_back(Content[I]);
867 break;
868 case Unknown:
869 case Separator:
870 Flush();
871 break;
872 }
873 }
874 Flush();
875
876 return Result;
877 }
878
isLikelyIdentifier(llvm::StringRef Word,llvm::StringRef Before,llvm::StringRef After)879 static bool isLikelyIdentifier(llvm::StringRef Word, llvm::StringRef Before,
880 llvm::StringRef After) {
881 // `foo` is an identifier.
882 if (Before.endswith("`") && After.startswith("`"))
883 return true;
884 // In foo::bar, both foo and bar are identifiers.
885 if (Before.endswith("::") || After.startswith("::"))
886 return true;
887 // Doxygen tags like \c foo indicate identifiers.
888 // Don't search too far back.
889 // This duplicates clang's doxygen parser, revisit if it gets complicated.
890 Before = Before.take_back(100); // Don't search too far back.
891 auto Pos = Before.find_last_of("\\@");
892 if (Pos != llvm::StringRef::npos) {
893 llvm::StringRef Tag = Before.substr(Pos + 1).rtrim(' ');
894 if (Tag == "p" || Tag == "c" || Tag == "class" || Tag == "tparam" ||
895 Tag == "param" || Tag == "param[in]" || Tag == "param[out]" ||
896 Tag == "param[in,out]" || Tag == "retval" || Tag == "throw" ||
897 Tag == "throws" || Tag == "link")
898 return true;
899 }
900
901 // Word contains underscore.
902 // This handles things like snake_case and MACRO_CASE.
903 if (Word.contains('_')) {
904 return true;
905 }
906 // Word contains capital letter other than at beginning.
907 // This handles things like lowerCamel and UpperCamel.
908 // The check for also containing a lowercase letter is to rule out
909 // initialisms like "HTTP".
910 bool HasLower = Word.find_if(clang::isLowercase) != StringRef::npos;
911 bool HasUpper = Word.substr(1).find_if(clang::isUppercase) != StringRef::npos;
912 if (HasLower && HasUpper) {
913 return true;
914 }
915 // FIXME: consider mid-sentence Capitalization?
916 return false;
917 }
918
touching(SourceLocation SpelledLoc,const syntax::TokenBuffer & TB,const LangOptions & LangOpts)919 llvm::Optional<SpelledWord> SpelledWord::touching(SourceLocation SpelledLoc,
920 const syntax::TokenBuffer &TB,
921 const LangOptions &LangOpts) {
922 const auto &SM = TB.sourceManager();
923 auto Touching = syntax::spelledTokensTouching(SpelledLoc, TB);
924 for (const auto &T : Touching) {
925 // If the token is an identifier or a keyword, don't use any heuristics.
926 if (tok::isAnyIdentifier(T.kind()) || tok::getKeywordSpelling(T.kind())) {
927 SpelledWord Result;
928 Result.Location = T.location();
929 Result.Text = T.text(SM);
930 Result.LikelyIdentifier = tok::isAnyIdentifier(T.kind());
931 Result.PartOfSpelledToken = &T;
932 Result.SpelledToken = &T;
933 auto Expanded =
934 TB.expandedTokens(SM.getMacroArgExpandedLocation(T.location()));
935 if (Expanded.size() == 1 && Expanded.front().text(SM) == Result.Text)
936 Result.ExpandedToken = &Expanded.front();
937 return Result;
938 }
939 }
940 FileID File;
941 unsigned Offset;
942 std::tie(File, Offset) = SM.getDecomposedLoc(SpelledLoc);
943 bool Invalid = false;
944 llvm::StringRef Code = SM.getBufferData(File, &Invalid);
945 if (Invalid)
946 return llvm::None;
947 unsigned B = Offset, E = Offset;
948 while (B > 0 && isIdentifierBody(Code[B - 1]))
949 --B;
950 while (E < Code.size() && isIdentifierBody(Code[E]))
951 ++E;
952 if (B == E)
953 return llvm::None;
954
955 SpelledWord Result;
956 Result.Location = SM.getComposedLoc(File, B);
957 Result.Text = Code.slice(B, E);
958 Result.LikelyIdentifier =
959 isLikelyIdentifier(Result.Text, Code.substr(0, B), Code.substr(E)) &&
960 // should not be a keyword
961 tok::isAnyIdentifier(
962 IdentifierTable(LangOpts).get(Result.Text).getTokenID());
963 for (const auto &T : Touching)
964 if (T.location() <= Result.Location)
965 Result.PartOfSpelledToken = &T;
966 return Result;
967 }
968
locateMacroAt(const syntax::Token & SpelledTok,Preprocessor & PP)969 llvm::Optional<DefinedMacro> locateMacroAt(const syntax::Token &SpelledTok,
970 Preprocessor &PP) {
971 SourceLocation Loc = SpelledTok.location();
972 assert(Loc.isFileID());
973 const auto &SM = PP.getSourceManager();
974 IdentifierInfo *IdentifierInfo = PP.getIdentifierInfo(SpelledTok.text(SM));
975 if (!IdentifierInfo || !IdentifierInfo->hadMacroDefinition())
976 return None;
977
978 // Get the definition just before the searched location so that a macro
979 // referenced in a '#undef MACRO' can still be found. Note that we only do
980 // that if Loc is not pointing at start of file.
981 if (SM.getLocForStartOfFile(SM.getFileID(Loc)) != Loc)
982 Loc = Loc.getLocWithOffset(-1);
983 MacroDefinition MacroDef = PP.getMacroDefinitionAtLoc(IdentifierInfo, Loc);
984 if (auto *MI = MacroDef.getMacroInfo())
985 return DefinedMacro{
986 IdentifierInfo->getName(), MI,
987 translatePreamblePatchLocation(MI->getDefinitionLoc(), SM)};
988 return None;
989 }
990
apply() const991 llvm::Expected<std::string> Edit::apply() const {
992 return tooling::applyAllReplacements(InitialCode, Replacements);
993 }
994
asTextEdits() const995 std::vector<TextEdit> Edit::asTextEdits() const {
996 return replacementsToEdits(InitialCode, Replacements);
997 }
998
canApplyTo(llvm::StringRef Code) const999 bool Edit::canApplyTo(llvm::StringRef Code) const {
1000 // Create line iterators, since line numbers are important while applying our
1001 // edit we cannot skip blank lines.
1002 auto LHS = llvm::MemoryBuffer::getMemBuffer(Code);
1003 llvm::line_iterator LHSIt(*LHS, /*SkipBlanks=*/false);
1004
1005 auto RHS = llvm::MemoryBuffer::getMemBuffer(InitialCode);
1006 llvm::line_iterator RHSIt(*RHS, /*SkipBlanks=*/false);
1007
1008 // Compare the InitialCode we prepared the edit for with the Code we received
1009 // line by line to make sure there are no differences.
1010 // FIXME: This check is too conservative now, it should be enough to only
1011 // check lines around the replacements contained inside the Edit.
1012 while (!LHSIt.is_at_eof() && !RHSIt.is_at_eof()) {
1013 if (*LHSIt != *RHSIt)
1014 return false;
1015 ++LHSIt;
1016 ++RHSIt;
1017 }
1018
1019 // After we reach EOF for any of the files we make sure the other one doesn't
1020 // contain any additional content except empty lines, they should not
1021 // interfere with the edit we produced.
1022 while (!LHSIt.is_at_eof()) {
1023 if (!LHSIt->empty())
1024 return false;
1025 ++LHSIt;
1026 }
1027 while (!RHSIt.is_at_eof()) {
1028 if (!RHSIt->empty())
1029 return false;
1030 ++RHSIt;
1031 }
1032 return true;
1033 }
1034
reformatEdit(Edit & E,const format::FormatStyle & Style)1035 llvm::Error reformatEdit(Edit &E, const format::FormatStyle &Style) {
1036 if (auto NewEdits = cleanupAndFormat(E.InitialCode, E.Replacements, Style))
1037 E.Replacements = std::move(*NewEdits);
1038 else
1039 return NewEdits.takeError();
1040 return llvm::Error::success();
1041 }
1042
getEligiblePoints(llvm::StringRef Code,llvm::StringRef FullyQualifiedName,const LangOptions & LangOpts)1043 EligibleRegion getEligiblePoints(llvm::StringRef Code,
1044 llvm::StringRef FullyQualifiedName,
1045 const LangOptions &LangOpts) {
1046 EligibleRegion ER;
1047 // Start with global namespace.
1048 std::vector<std::string> Enclosing = {""};
1049 // FIXME: In addition to namespaces try to generate events for function
1050 // definitions as well. One might use a closing parantheses(")" followed by an
1051 // opening brace "{" to trigger the start.
1052 parseNamespaceEvents(Code, LangOpts, [&](NamespaceEvent Event) {
1053 // Using Directives only introduces declarations to current scope, they do
1054 // not change the current namespace, so skip them.
1055 if (Event.Trigger == NamespaceEvent::UsingDirective)
1056 return;
1057 // Do not qualify the global namespace.
1058 if (!Event.Payload.empty())
1059 Event.Payload.append("::");
1060
1061 std::string CurrentNamespace;
1062 if (Event.Trigger == NamespaceEvent::BeginNamespace) {
1063 Enclosing.emplace_back(std::move(Event.Payload));
1064 CurrentNamespace = Enclosing.back();
1065 // parseNameSpaceEvents reports the beginning position of a token; we want
1066 // to insert after '{', so increment by one.
1067 ++Event.Pos.character;
1068 } else {
1069 // Event.Payload points to outer namespace when exiting a scope, so use
1070 // the namespace we've last entered instead.
1071 CurrentNamespace = std::move(Enclosing.back());
1072 Enclosing.pop_back();
1073 assert(Enclosing.back() == Event.Payload);
1074 }
1075
1076 // Ignore namespaces that are not a prefix of the target.
1077 if (!FullyQualifiedName.startswith(CurrentNamespace))
1078 return;
1079
1080 // Prefer the namespace that shares the longest prefix with target.
1081 if (CurrentNamespace.size() > ER.EnclosingNamespace.size()) {
1082 ER.EligiblePoints.clear();
1083 ER.EnclosingNamespace = CurrentNamespace;
1084 }
1085 if (CurrentNamespace.size() == ER.EnclosingNamespace.size())
1086 ER.EligiblePoints.emplace_back(std::move(Event.Pos));
1087 });
1088 // If there were no shared namespaces just return EOF.
1089 if (ER.EligiblePoints.empty()) {
1090 assert(ER.EnclosingNamespace.empty());
1091 ER.EligiblePoints.emplace_back(offsetToPosition(Code, Code.size()));
1092 }
1093 return ER;
1094 }
1095
isHeaderFile(llvm::StringRef FileName,llvm::Optional<LangOptions> LangOpts)1096 bool isHeaderFile(llvm::StringRef FileName,
1097 llvm::Optional<LangOptions> LangOpts) {
1098 // Respect the langOpts, for non-file-extension cases, e.g. standard library
1099 // files.
1100 if (LangOpts && LangOpts->IsHeaderFile)
1101 return true;
1102 namespace types = clang::driver::types;
1103 auto Lang = types::lookupTypeForExtension(
1104 llvm::sys::path::extension(FileName).substr(1));
1105 return Lang != types::TY_INVALID && types::onlyPrecompileType(Lang);
1106 }
1107
isProtoFile(SourceLocation Loc,const SourceManager & SM)1108 bool isProtoFile(SourceLocation Loc, const SourceManager &SM) {
1109 auto FileName = SM.getFilename(Loc);
1110 if (!FileName.endswith(".proto.h") && !FileName.endswith(".pb.h"))
1111 return false;
1112 auto FID = SM.getFileID(Loc);
1113 // All proto generated headers should start with this line.
1114 static const char *PROTO_HEADER_COMMENT =
1115 "// Generated by the protocol buffer compiler. DO NOT EDIT!";
1116 // Double check that this is an actual protobuf header.
1117 return SM.getBufferData(FID).startswith(PROTO_HEADER_COMMENT);
1118 }
1119
1120 } // namespace clangd
1121 } // namespace clang
1122