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 "Context.h"
11 #include "FuzzyMatch.h"
12 #include "Logger.h"
13 #include "Protocol.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/Basic/SourceManager.h"
16 #include "clang/Basic/TokenKinds.h"
17 #include "clang/Format/Format.h"
18 #include "clang/Lex/Lexer.h"
19 #include "llvm/ADT/None.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/Errc.h"
24 #include "llvm/Support/Error.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/Path.h"
27 #include <algorithm>
28 
29 namespace clang {
30 namespace clangd {
31 
32 // Here be dragons. LSP positions use columns measured in *UTF-16 code units*!
33 // Clangd uses UTF-8 and byte-offsets internally, so conversion is nontrivial.
34 
35 // Iterates over unicode codepoints in the (UTF-8) string. For each,
36 // invokes CB(UTF-8 length, UTF-16 length), and breaks if it returns true.
37 // Returns true if CB returned true, false if we hit the end of string.
38 template <typename Callback>
39 static bool iterateCodepoints(llvm::StringRef U8, const Callback &CB) {
40   // A codepoint takes two UTF-16 code unit if it's astral (outside BMP).
41   // Astral codepoints are encoded as 4 bytes in UTF-8, starting with 11110xxx.
42   for (size_t I = 0; I < U8.size();) {
43     unsigned char C = static_cast<unsigned char>(U8[I]);
44     if (LLVM_LIKELY(!(C & 0x80))) { // ASCII character.
45       if (CB(1, 1))
46         return true;
47       ++I;
48       continue;
49     }
50     // This convenient property of UTF-8 holds for all non-ASCII characters.
51     size_t UTF8Length = llvm::countLeadingOnes(C);
52     // 0xxx is ASCII, handled above. 10xxx is a trailing byte, invalid here.
53     // 11111xxx is not valid UTF-8 at all. Assert because it's probably our bug.
54     assert((UTF8Length >= 2 && UTF8Length <= 4) &&
55            "Invalid UTF-8, or transcoding bug?");
56     I += UTF8Length; // Skip over all trailing bytes.
57     // A codepoint takes two UTF-16 code unit if it's astral (outside BMP).
58     // Astral codepoints are encoded as 4 bytes in UTF-8 (11110xxx ...)
59     if (CB(UTF8Length, UTF8Length == 4 ? 2 : 1))
60       return true;
61   }
62   return false;
63 }
64 
65 // Returns the byte offset into the string that is an offset of \p Units in
66 // the specified encoding.
67 // Conceptually, this converts to the encoding, truncates to CodeUnits,
68 // converts back to UTF-8, and returns the length in bytes.
69 static size_t measureUnits(llvm::StringRef U8, int Units, OffsetEncoding Enc,
70                            bool &Valid) {
71   Valid = Units >= 0;
72   if (Units <= 0)
73     return 0;
74   size_t Result = 0;
75   switch (Enc) {
76   case OffsetEncoding::UTF8:
77     Result = Units;
78     break;
79   case OffsetEncoding::UTF16:
80     Valid = iterateCodepoints(U8, [&](int U8Len, int U16Len) {
81       Result += U8Len;
82       Units -= U16Len;
83       return Units <= 0;
84     });
85     if (Units < 0) // Offset in the middle of a surrogate pair.
86       Valid = false;
87     break;
88   case OffsetEncoding::UTF32:
89     Valid = iterateCodepoints(U8, [&](int U8Len, int U16Len) {
90       Result += U8Len;
91       Units--;
92       return Units <= 0;
93     });
94     break;
95   case OffsetEncoding::UnsupportedEncoding:
96     llvm_unreachable("unsupported encoding");
97   }
98   // Don't return an out-of-range index if we overran.
99   if (Result > U8.size()) {
100     Valid = false;
101     return U8.size();
102   }
103   return Result;
104 }
105 
106 Key<OffsetEncoding> kCurrentOffsetEncoding;
107 static OffsetEncoding lspEncoding() {
108   auto *Enc = Context::current().get(kCurrentOffsetEncoding);
109   return Enc ? *Enc : OffsetEncoding::UTF16;
110 }
111 
112 // Like most strings in clangd, the input is UTF-8 encoded.
113 size_t lspLength(llvm::StringRef Code) {
114   size_t Count = 0;
115   switch (lspEncoding()) {
116   case OffsetEncoding::UTF8:
117     Count = Code.size();
118     break;
119   case OffsetEncoding::UTF16:
120     iterateCodepoints(Code, [&](int U8Len, int U16Len) {
121       Count += U16Len;
122       return false;
123     });
124     break;
125   case OffsetEncoding::UTF32:
126     iterateCodepoints(Code, [&](int U8Len, int U16Len) {
127       ++Count;
128       return false;
129     });
130     break;
131   case OffsetEncoding::UnsupportedEncoding:
132     llvm_unreachable("unsupported encoding");
133   }
134   return Count;
135 }
136 
137 llvm::Expected<size_t> positionToOffset(llvm::StringRef Code, Position P,
138                                         bool AllowColumnsBeyondLineLength) {
139   if (P.line < 0)
140     return llvm::make_error<llvm::StringError>(
141         llvm::formatv("Line value can't be negative ({0})", P.line),
142         llvm::errc::invalid_argument);
143   if (P.character < 0)
144     return llvm::make_error<llvm::StringError>(
145         llvm::formatv("Character value can't be negative ({0})", P.character),
146         llvm::errc::invalid_argument);
147   size_t StartOfLine = 0;
148   for (int I = 0; I != P.line; ++I) {
149     size_t NextNL = Code.find('\n', StartOfLine);
150     if (NextNL == llvm::StringRef::npos)
151       return llvm::make_error<llvm::StringError>(
152           llvm::formatv("Line value is out of range ({0})", P.line),
153           llvm::errc::invalid_argument);
154     StartOfLine = NextNL + 1;
155   }
156   StringRef Line =
157       Code.substr(StartOfLine).take_until([](char C) { return C == '\n'; });
158 
159   // P.character may be in UTF-16, transcode if necessary.
160   bool Valid;
161   size_t ByteInLine = measureUnits(Line, P.character, lspEncoding(), Valid);
162   if (!Valid && !AllowColumnsBeyondLineLength)
163     return llvm::make_error<llvm::StringError>(
164         llvm::formatv("{0} offset {1} is invalid for line {2}", lspEncoding(),
165                       P.character, P.line),
166         llvm::errc::invalid_argument);
167   return StartOfLine + ByteInLine;
168 }
169 
170 Position offsetToPosition(llvm::StringRef Code, size_t Offset) {
171   Offset = std::min(Code.size(), Offset);
172   llvm::StringRef Before = Code.substr(0, Offset);
173   int Lines = Before.count('\n');
174   size_t PrevNL = Before.rfind('\n');
175   size_t StartOfLine = (PrevNL == llvm::StringRef::npos) ? 0 : (PrevNL + 1);
176   Position Pos;
177   Pos.line = Lines;
178   Pos.character = lspLength(Before.substr(StartOfLine));
179   return Pos;
180 }
181 
182 Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc) {
183   // We use the SourceManager's line tables, but its column number is in bytes.
184   FileID FID;
185   unsigned Offset;
186   std::tie(FID, Offset) = SM.getDecomposedSpellingLoc(Loc);
187   Position P;
188   P.line = static_cast<int>(SM.getLineNumber(FID, Offset)) - 1;
189   bool Invalid = false;
190   llvm::StringRef Code = SM.getBufferData(FID, &Invalid);
191   if (!Invalid) {
192     auto ColumnInBytes = SM.getColumnNumber(FID, Offset) - 1;
193     auto LineSoFar = Code.substr(Offset - ColumnInBytes, ColumnInBytes);
194     P.character = lspLength(LineSoFar);
195   }
196   return P;
197 }
198 
199 bool isValidFileRange(const SourceManager &Mgr, SourceRange R) {
200   if (!R.getBegin().isValid() || !R.getEnd().isValid())
201     return false;
202 
203   FileID BeginFID;
204   size_t BeginOffset = 0;
205   std::tie(BeginFID, BeginOffset) = Mgr.getDecomposedLoc(R.getBegin());
206 
207   FileID EndFID;
208   size_t EndOffset = 0;
209   std::tie(EndFID, EndOffset) = Mgr.getDecomposedLoc(R.getEnd());
210 
211   return BeginFID.isValid() && BeginFID == EndFID && BeginOffset <= EndOffset;
212 }
213 
214 bool halfOpenRangeContains(const SourceManager &Mgr, SourceRange R,
215                            SourceLocation L) {
216   assert(isValidFileRange(Mgr, R));
217 
218   FileID BeginFID;
219   size_t BeginOffset = 0;
220   std::tie(BeginFID, BeginOffset) = Mgr.getDecomposedLoc(R.getBegin());
221   size_t EndOffset = Mgr.getFileOffset(R.getEnd());
222 
223   FileID LFid;
224   size_t LOffset;
225   std::tie(LFid, LOffset) = Mgr.getDecomposedLoc(L);
226   return BeginFID == LFid && BeginOffset <= LOffset && LOffset < EndOffset;
227 }
228 
229 bool halfOpenRangeTouches(const SourceManager &Mgr, SourceRange R,
230                           SourceLocation L) {
231   return L == R.getEnd() || halfOpenRangeContains(Mgr, R, L);
232 }
233 
234 llvm::Optional<SourceRange> toHalfOpenFileRange(const SourceManager &Mgr,
235                                                 const LangOptions &LangOpts,
236                                                 SourceRange R) {
237   auto Begin = Mgr.getFileLoc(R.getBegin());
238   if (Begin.isInvalid())
239     return llvm::None;
240   auto End = Mgr.getFileLoc(R.getEnd());
241   if (End.isInvalid())
242     return llvm::None;
243   End = Lexer::getLocForEndOfToken(End, 0, Mgr, LangOpts);
244 
245   SourceRange Result(Begin, End);
246   if (!isValidFileRange(Mgr, Result))
247     return llvm::None;
248   return Result;
249 }
250 
251 llvm::StringRef toSourceCode(const SourceManager &SM, SourceRange R) {
252   assert(isValidFileRange(SM, R));
253   bool Invalid = false;
254   auto *Buf = SM.getBuffer(SM.getFileID(R.getBegin()), &Invalid);
255   assert(!Invalid);
256 
257   size_t BeginOffset = SM.getFileOffset(R.getBegin());
258   size_t EndOffset = SM.getFileOffset(R.getEnd());
259   return Buf->getBuffer().substr(BeginOffset, EndOffset - BeginOffset);
260 }
261 
262 llvm::Expected<SourceLocation> sourceLocationInMainFile(const SourceManager &SM,
263                                                         Position P) {
264   llvm::StringRef Code = SM.getBuffer(SM.getMainFileID())->getBuffer();
265   auto Offset =
266       positionToOffset(Code, P, /*AllowColumnBeyondLineLength=*/false);
267   if (!Offset)
268     return Offset.takeError();
269   return SM.getLocForStartOfFile(SM.getMainFileID()).getLocWithOffset(*Offset);
270 }
271 
272 Range halfOpenToRange(const SourceManager &SM, CharSourceRange R) {
273   // Clang is 1-based, LSP uses 0-based indexes.
274   Position Begin = sourceLocToPosition(SM, R.getBegin());
275   Position End = sourceLocToPosition(SM, R.getEnd());
276 
277   return {Begin, End};
278 }
279 
280 std::pair<size_t, size_t> offsetToClangLineColumn(llvm::StringRef Code,
281                                                   size_t Offset) {
282   Offset = std::min(Code.size(), Offset);
283   llvm::StringRef Before = Code.substr(0, Offset);
284   int Lines = Before.count('\n');
285   size_t PrevNL = Before.rfind('\n');
286   size_t StartOfLine = (PrevNL == llvm::StringRef::npos) ? 0 : (PrevNL + 1);
287   return {Lines + 1, Offset - StartOfLine + 1};
288 }
289 
290 std::pair<StringRef, StringRef> splitQualifiedName(StringRef QName) {
291   size_t Pos = QName.rfind("::");
292   if (Pos == llvm::StringRef::npos)
293     return {llvm::StringRef(), QName};
294   return {QName.substr(0, Pos + 2), QName.substr(Pos + 2)};
295 }
296 
297 TextEdit replacementToEdit(llvm::StringRef Code,
298                            const tooling::Replacement &R) {
299   Range ReplacementRange = {
300       offsetToPosition(Code, R.getOffset()),
301       offsetToPosition(Code, R.getOffset() + R.getLength())};
302   return {ReplacementRange, R.getReplacementText()};
303 }
304 
305 std::vector<TextEdit> replacementsToEdits(llvm::StringRef Code,
306                                           const tooling::Replacements &Repls) {
307   std::vector<TextEdit> Edits;
308   for (const auto &R : Repls)
309     Edits.push_back(replacementToEdit(Code, R));
310   return Edits;
311 }
312 
313 llvm::Optional<std::string> getCanonicalPath(const FileEntry *F,
314                                              const SourceManager &SourceMgr) {
315   if (!F)
316     return None;
317 
318   llvm::SmallString<128> FilePath = F->getName();
319   if (!llvm::sys::path::is_absolute(FilePath)) {
320     if (auto EC =
321             SourceMgr.getFileManager().getVirtualFileSystem().makeAbsolute(
322                 FilePath)) {
323       elog("Could not turn relative path '{0}' to absolute: {1}", FilePath,
324            EC.message());
325       return None;
326     }
327   }
328 
329   // Handle the symbolic link path case where the current working directory
330   // (getCurrentWorkingDirectory) is a symlink./ We always want to the real
331   // file path (instead of the symlink path) for the  C++ symbols.
332   //
333   // Consider the following example:
334   //
335   //   src dir: /project/src/foo.h
336   //   current working directory (symlink): /tmp/build -> /project/src/
337   //
338   //  The file path of Symbol is "/project/src/foo.h" instead of
339   //  "/tmp/build/foo.h"
340   if (const DirectoryEntry *Dir = SourceMgr.getFileManager().getDirectory(
341           llvm::sys::path::parent_path(FilePath))) {
342     llvm::SmallString<128> RealPath;
343     llvm::StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir);
344     llvm::sys::path::append(RealPath, DirName,
345                             llvm::sys::path::filename(FilePath));
346     return RealPath.str().str();
347   }
348 
349   return FilePath.str().str();
350 }
351 
352 TextEdit toTextEdit(const FixItHint &FixIt, const SourceManager &M,
353                     const LangOptions &L) {
354   TextEdit Result;
355   Result.range =
356       halfOpenToRange(M, Lexer::makeFileCharRange(FixIt.RemoveRange, M, L));
357   Result.newText = FixIt.CodeToInsert;
358   return Result;
359 }
360 
361 bool isRangeConsecutive(const Range &Left, const Range &Right) {
362   return Left.end.line == Right.start.line &&
363          Left.end.character == Right.start.character;
364 }
365 
366 FileDigest digest(llvm::StringRef Content) {
367   return llvm::SHA1::hash({(const uint8_t *)Content.data(), Content.size()});
368 }
369 
370 llvm::Optional<FileDigest> digestFile(const SourceManager &SM, FileID FID) {
371   bool Invalid = false;
372   llvm::StringRef Content = SM.getBufferData(FID, &Invalid);
373   if (Invalid)
374     return None;
375   return digest(Content);
376 }
377 
378 format::FormatStyle getFormatStyleForFile(llvm::StringRef File,
379                                           llvm::StringRef Content,
380                                           llvm::vfs::FileSystem *FS) {
381   auto Style = format::getStyle(format::DefaultFormatStyle, File,
382                                 format::DefaultFallbackStyle, Content, FS);
383   if (!Style) {
384     log("getStyle() failed for file {0}: {1}. Fallback is LLVM style.", File,
385         Style.takeError());
386     Style = format::getLLVMStyle();
387   }
388   return *Style;
389 }
390 
391 llvm::Expected<tooling::Replacements>
392 cleanupAndFormat(StringRef Code, const tooling::Replacements &Replaces,
393                  const format::FormatStyle &Style) {
394   auto CleanReplaces = cleanupAroundReplacements(Code, Replaces, Style);
395   if (!CleanReplaces)
396     return CleanReplaces;
397   return formatReplacements(Code, std::move(*CleanReplaces), Style);
398 }
399 
400 template <typename Action>
401 static void lex(llvm::StringRef Code, const format::FormatStyle &Style,
402                 Action A) {
403   // FIXME: InMemoryFileAdapter crashes unless the buffer is null terminated!
404   std::string NullTerminatedCode = Code.str();
405   SourceManagerForFile FileSM("dummy.cpp", NullTerminatedCode);
406   auto &SM = FileSM.get();
407   auto FID = SM.getMainFileID();
408   Lexer Lex(FID, SM.getBuffer(FID), SM, format::getFormattingLangOpts(Style));
409   Token Tok;
410 
411   while (!Lex.LexFromRawLexer(Tok))
412     A(Tok);
413 }
414 
415 llvm::StringMap<unsigned> collectIdentifiers(llvm::StringRef Content,
416                                              const format::FormatStyle &Style) {
417   llvm::StringMap<unsigned> Identifiers;
418   lex(Content, Style, [&](const clang::Token &Tok) {
419     switch (Tok.getKind()) {
420     case tok::identifier:
421       ++Identifiers[Tok.getIdentifierInfo()->getName()];
422       break;
423     case tok::raw_identifier:
424       ++Identifiers[Tok.getRawIdentifier()];
425       break;
426     default:
427       break;
428     }
429   });
430   return Identifiers;
431 }
432 
433 namespace {
434 enum NamespaceEvent {
435   BeginNamespace, // namespace <ns> {.     Payload is resolved <ns>.
436   EndNamespace,   // } // namespace <ns>.  Payload is resolved *outer* namespace.
437   UsingDirective  // using namespace <ns>. Payload is unresolved <ns>.
438 };
439 // Scans C++ source code for constructs that change the visible namespaces.
440 void parseNamespaceEvents(
441     llvm::StringRef Code, const format::FormatStyle &Style,
442     llvm::function_ref<void(NamespaceEvent, llvm::StringRef)> Callback) {
443 
444   // Stack of enclosing namespaces, e.g. {"clang", "clangd"}
445   std::vector<std::string> Enclosing; // Contains e.g. "clang", "clangd"
446   // Stack counts open braces. true if the brace opened a namespace.
447   std::vector<bool> BraceStack;
448 
449   enum {
450     Default,
451     Namespace,          // just saw 'namespace'
452     NamespaceName,      // just saw 'namespace' NSName
453     Using,              // just saw 'using'
454     UsingNamespace,     // just saw 'using namespace'
455     UsingNamespaceName, // just saw 'using namespace' NSName
456   } State = Default;
457   std::string NSName;
458 
459   lex(Code, Style, [&](const clang::Token &Tok) {
460     switch(Tok.getKind()) {
461     case tok::raw_identifier:
462       // In raw mode, this could be a keyword or a name.
463       switch (State) {
464       case UsingNamespace:
465       case UsingNamespaceName:
466         NSName.append(Tok.getRawIdentifier());
467         State = UsingNamespaceName;
468         break;
469       case Namespace:
470       case NamespaceName:
471         NSName.append(Tok.getRawIdentifier());
472         State = NamespaceName;
473         break;
474       case Using:
475         State =
476             (Tok.getRawIdentifier() == "namespace") ? UsingNamespace : Default;
477         break;
478       case Default:
479         NSName.clear();
480         if (Tok.getRawIdentifier() == "namespace")
481           State = Namespace;
482         else if (Tok.getRawIdentifier() == "using")
483           State = Using;
484         break;
485       }
486       break;
487     case tok::coloncolon:
488       // This can come at the beginning or in the middle of a namespace name.
489       switch (State) {
490       case UsingNamespace:
491       case UsingNamespaceName:
492         NSName.append("::");
493         State = UsingNamespaceName;
494         break;
495       case NamespaceName:
496         NSName.append("::");
497         State = NamespaceName;
498         break;
499       case Namespace: // Not legal here.
500       case Using:
501       case Default:
502         State = Default;
503         break;
504       }
505       break;
506     case tok::l_brace:
507       // Record which { started a namespace, so we know when } ends one.
508       if (State == NamespaceName) {
509         // Parsed: namespace <name> {
510         BraceStack.push_back(true);
511         Enclosing.push_back(NSName);
512         Callback(BeginNamespace, llvm::join(Enclosing, "::"));
513       } else {
514         // This case includes anonymous namespaces (State = Namespace).
515         // For our purposes, they're not namespaces and we ignore them.
516         BraceStack.push_back(false);
517       }
518       State = Default;
519       break;
520     case tok::r_brace:
521       // If braces are unmatched, we're going to be confused, but don't crash.
522       if (!BraceStack.empty()) {
523         if (BraceStack.back()) {
524           // Parsed: } // namespace
525           Enclosing.pop_back();
526           Callback(EndNamespace, llvm::join(Enclosing, "::"));
527         }
528         BraceStack.pop_back();
529       }
530       break;
531     case tok::semi:
532       if (State == UsingNamespaceName)
533         // Parsed: using namespace <name> ;
534         Callback(UsingDirective, llvm::StringRef(NSName));
535       State = Default;
536       break;
537     default:
538       State = Default;
539       break;
540     }
541   });
542 }
543 
544 // Returns the prefix namespaces of NS: {"" ... NS}.
545 llvm::SmallVector<llvm::StringRef, 8> ancestorNamespaces(llvm::StringRef NS) {
546   llvm::SmallVector<llvm::StringRef, 8> Results;
547   Results.push_back(NS.take_front(0));
548   NS.split(Results, "::", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
549   for (llvm::StringRef &R : Results)
550     R = NS.take_front(R.end() - NS.begin());
551   return Results;
552 }
553 
554 } // namespace
555 
556 std::vector<std::string> visibleNamespaces(llvm::StringRef Code,
557                                            const format::FormatStyle &Style) {
558   std::string Current;
559   // Map from namespace to (resolved) namespaces introduced via using directive.
560   llvm::StringMap<llvm::StringSet<>> UsingDirectives;
561 
562   parseNamespaceEvents(Code, Style,
563                        [&](NamespaceEvent Event, llvm::StringRef NS) {
564                          switch (Event) {
565                          case BeginNamespace:
566                          case EndNamespace:
567                            Current = NS;
568                            break;
569                          case UsingDirective:
570                            if (NS.consume_front("::"))
571                              UsingDirectives[Current].insert(NS);
572                            else {
573                              for (llvm::StringRef Enclosing :
574                                   ancestorNamespaces(Current)) {
575                                if (Enclosing.empty())
576                                  UsingDirectives[Current].insert(NS);
577                                else
578                                  UsingDirectives[Current].insert(
579                                      (Enclosing + "::" + NS).str());
580                              }
581                            }
582                            break;
583                          }
584                        });
585 
586   std::vector<std::string> Found;
587   for (llvm::StringRef Enclosing : ancestorNamespaces(Current)) {
588     Found.push_back(Enclosing);
589     auto It = UsingDirectives.find(Enclosing);
590     if (It != UsingDirectives.end())
591       for (const auto& Used : It->second)
592         Found.push_back(Used.getKey());
593   }
594 
595 
596   llvm::sort(Found, [&](const std::string &LHS, const std::string &RHS) {
597     if (Current == RHS)
598       return false;
599     if (Current == LHS)
600       return true;
601     return LHS < RHS;
602   });
603   Found.erase(std::unique(Found.begin(), Found.end()), Found.end());
604   return Found;
605 }
606 
607 llvm::StringSet<> collectWords(llvm::StringRef Content) {
608   // We assume short words are not significant.
609   // We may want to consider other stopwords, e.g. language keywords.
610   // (A very naive implementation showed no benefit, but lexing might do better)
611   static constexpr int MinWordLength = 4;
612 
613   std::vector<CharRole> Roles(Content.size());
614   calculateRoles(Content, Roles);
615 
616   llvm::StringSet<> Result;
617   llvm::SmallString<256> Word;
618   auto Flush = [&] {
619     if (Word.size() >= MinWordLength) {
620       for (char &C : Word)
621         C = llvm::toLower(C);
622       Result.insert(Word);
623     }
624     Word.clear();
625   };
626   for (unsigned I = 0; I < Content.size(); ++I) {
627     switch (Roles[I]) {
628     case Head:
629       Flush();
630       LLVM_FALLTHROUGH;
631     case Tail:
632       Word.push_back(Content[I]);
633       break;
634     case Unknown:
635     case Separator:
636       Flush();
637       break;
638     }
639   }
640   Flush();
641 
642   return Result;
643 }
644 
645 } // namespace clangd
646 } // namespace clang
647