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 "refactor/Tweak.h" 15 #include "clang/AST/ASTContext.h" 16 #include "clang/Basic/LangOptions.h" 17 #include "clang/Basic/SourceLocation.h" 18 #include "clang/Basic/SourceManager.h" 19 #include "clang/Basic/TokenKinds.h" 20 #include "clang/Driver/Types.h" 21 #include "clang/Format/Format.h" 22 #include "clang/Lex/Lexer.h" 23 #include "clang/Lex/Preprocessor.h" 24 #include "clang/Lex/Token.h" 25 #include "clang/Tooling/Core/Replacement.h" 26 #include "llvm/ADT/ArrayRef.h" 27 #include "llvm/ADT/None.h" 28 #include "llvm/ADT/STLExtras.h" 29 #include "llvm/ADT/StringExtras.h" 30 #include "llvm/ADT/StringMap.h" 31 #include "llvm/ADT/StringRef.h" 32 #include "llvm/Support/Compiler.h" 33 #include "llvm/Support/Errc.h" 34 #include "llvm/Support/Error.h" 35 #include "llvm/Support/ErrorHandling.h" 36 #include "llvm/Support/LineIterator.h" 37 #include "llvm/Support/MemoryBuffer.h" 38 #include "llvm/Support/Path.h" 39 #include "llvm/Support/SHA1.h" 40 #include "llvm/Support/VirtualFileSystem.h" 41 #include "llvm/Support/xxhash.h" 42 #include <algorithm> 43 #include <cstddef> 44 #include <string> 45 #include <vector> 46 47 namespace clang { 48 namespace clangd { 49 50 // 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. 56 template <typename Callback> 57 static bool iterateCodepoints(llvm::StringRef U8, const Callback &CB) { 58 // 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. 60 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. 69 size_t UTF8Length = llvm::countLeadingOnes(C); 70 // 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 83 // 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. 87 static size_t measureUnits(llvm::StringRef U8, int Units, OffsetEncoding Enc, 88 bool &Valid) { 89 Valid = Units >= 0; 90 if (Units <= 0) 91 return 0; 92 size_t Result = 0; 93 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 } 116 // Don't return an out-of-range index if we overran. 117 if (Result > U8.size()) { 118 Valid = false; 119 return U8.size(); 120 } 121 return Result; 122 } 123 124 Key<OffsetEncoding> kCurrentOffsetEncoding; 125 static OffsetEncoding lspEncoding() { 126 auto *Enc = Context::current().get(kCurrentOffsetEncoding); 127 return Enc ? *Enc : OffsetEncoding::UTF16; 128 } 129 130 // Like most strings in clangd, the input is UTF-8 encoded. 131 size_t lspLength(llvm::StringRef Code) { 132 size_t Count = 0; 133 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 } 152 return Count; 153 } 154 155 llvm::Expected<size_t> positionToOffset(llvm::StringRef Code, Position P, 156 bool AllowColumnsBeyondLineLength) { 157 if (P.line < 0) 158 return llvm::make_error<llvm::StringError>( 159 llvm::formatv("Line value can't be negative ({0})", P.line), 160 llvm::errc::invalid_argument); 161 if (P.character < 0) 162 return llvm::make_error<llvm::StringError>( 163 llvm::formatv("Character value can't be negative ({0})", P.character), 164 llvm::errc::invalid_argument); 165 size_t StartOfLine = 0; 166 for (int I = 0; I != P.line; ++I) { 167 size_t NextNL = Code.find('\n', StartOfLine); 168 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); 172 StartOfLine = NextNL + 1; 173 } 174 StringRef Line = 175 Code.substr(StartOfLine).take_until([](char C) { return C == '\n'; }); 176 177 // P.character may be in UTF-16, transcode if necessary. 178 bool Valid; 179 size_t ByteInLine = measureUnits(Line, P.character, lspEncoding(), Valid); 180 if (!Valid && !AllowColumnsBeyondLineLength) 181 return llvm::make_error<llvm::StringError>( 182 llvm::formatv("{0} offset {1} is invalid for line {2}", lspEncoding(), 183 P.character, P.line), 184 llvm::errc::invalid_argument); 185 return StartOfLine + ByteInLine; 186 } 187 188 Position offsetToPosition(llvm::StringRef Code, size_t Offset) { 189 Offset = std::min(Code.size(), Offset); 190 llvm::StringRef Before = Code.substr(0, Offset); 191 int Lines = Before.count('\n'); 192 size_t PrevNL = Before.rfind('\n'); 193 size_t StartOfLine = (PrevNL == llvm::StringRef::npos) ? 0 : (PrevNL + 1); 194 Position Pos; 195 Pos.line = Lines; 196 Pos.character = lspLength(Before.substr(StartOfLine)); 197 return Pos; 198 } 199 200 Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc) { 201 // 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); 205 Position P; 206 P.line = static_cast<int>(SM.getLineNumber(FID, Offset)) - 1; 207 bool Invalid = false; 208 llvm::StringRef Code = SM.getBufferData(FID, &Invalid); 209 if (!Invalid) { 210 auto ColumnInBytes = SM.getColumnNumber(FID, Offset) - 1; 211 auto LineSoFar = Code.substr(Offset - ColumnInBytes, ColumnInBytes); 212 P.character = lspLength(LineSoFar); 213 } 214 return P; 215 } 216 217 bool 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 227 llvm::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 238 namespace { 239 240 enum TokenFlavor { Identifier, Operator, Whitespace, Other }; 241 242 bool 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 256 TokenFlavor 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 277 SourceLocation 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 287 // 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. 297 SourceLocation InputLoc = SM.getComposedLoc(FID, *Offset); 298 if (*Offset == 0) // Case 1 or 4. 299 return InputLoc; 300 SourceLocation Before = SM.getComposedLoc(FID, *Offset - 1); 301 SourceLocation BeforeTokBeginning = 302 Lexer::GetBeginningOfToken(Before, SM, LangOpts); 303 TokenFlavor BeforeKind = getTokenFlavor(BeforeTokBeginning, SM, LangOpts); 304 305 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; 338 } 339 340 bool 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 355 bool 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 370 bool halfOpenRangeTouches(const SourceManager &Mgr, SourceRange R, 371 SourceLocation L) { 372 return L == R.getEnd() || halfOpenRangeContains(Mgr, R, L); 373 } 374 375 SourceLocation 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 397 static 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 413 static 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 421 static 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. 429 static 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. 438 static SourceRange unionTokenRange(SourceRange R1, SourceRange R2, 439 const SourceManager &SM, 440 const LangOptions &LangOpts) { 441 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); 451 } 452 453 // 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. 456 static 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(); 484 } 485 486 // Find an expansion range (not necessarily immediate) the ends of which are in 487 // the same file id. 488 static SourceRange 489 getExpansionTokenRangeInSameFile(SourceLocation Loc, const SourceManager &SM, 490 const LangOptions &LangOpts) { 491 return rangeInCommonFile( 492 toTokenRange(SM.getImmediateExpansionRange(Loc), SM, LangOpts), SM, 493 LangOpts); 494 } 495 496 // Returns the file range for a given Location as a Token Range 497 // 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. 502 // - We want to split '>>' tokens as the lexer parses the '>>' in nested 503 // template instantiations as a '>>' instead of two '>'s. 504 // There is also getExpansionRange but it simply calls 505 // getImmediateExpansionRange on the begin and ends separately which is wrong. 506 static SourceRange getTokenFileRange(SourceLocation Loc, 507 const SourceManager &SM, 508 const LangOptions &LangOpts) { 509 SourceRange FileRange = Loc; 510 while (!FileRange.getBegin().isFileID()) { 511 if (SM.isMacroArgExpansion(FileRange.getBegin())) { 512 FileRange = unionTokenRange( 513 SM.getImmediateSpellingLoc(FileRange.getBegin()), 514 SM.getImmediateSpellingLoc(FileRange.getEnd()), SM, LangOpts); 515 assert(SM.isWrittenInSameFile(FileRange.getBegin(), FileRange.getEnd())); 516 } else { 517 SourceRange ExpansionRangeForBegin = 518 getExpansionTokenRangeInSameFile(FileRange.getBegin(), SM, LangOpts); 519 SourceRange ExpansionRangeForEnd = 520 getExpansionTokenRangeInSameFile(FileRange.getEnd(), SM, LangOpts); 521 if (ExpansionRangeForBegin.isInvalid() || 522 ExpansionRangeForEnd.isInvalid()) 523 return SourceRange(); 524 assert(SM.isWrittenInSameFile(ExpansionRangeForBegin.getBegin(), 525 ExpansionRangeForEnd.getBegin()) && 526 "Both Expansion ranges should be in same file."); 527 FileRange = unionTokenRange(ExpansionRangeForBegin, ExpansionRangeForEnd, 528 SM, LangOpts); 529 } 530 } 531 return FileRange; 532 } 533 534 bool isInsideMainFile(SourceLocation Loc, const SourceManager &SM) { 535 return Loc.isValid() && SM.isWrittenInMainFile(SM.getExpansionLoc(Loc)); 536 } 537 538 llvm::Optional<SourceRange> toHalfOpenFileRange(const SourceManager &SM, 539 const LangOptions &LangOpts, 540 SourceRange R) { 541 SourceRange R1 = getTokenFileRange(R.getBegin(), SM, LangOpts); 542 if (!isValidFileRange(SM, R1)) 543 return llvm::None; 544 545 SourceRange R2 = getTokenFileRange(R.getEnd(), SM, LangOpts); 546 if (!isValidFileRange(SM, R2)) 547 return llvm::None; 548 549 SourceRange Result = 550 rangeInCommonFile(unionTokenRange(R1, R2, SM, LangOpts), SM, LangOpts); 551 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 557 return Result; 558 } 559 560 llvm::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 571 llvm::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 581 Range 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 589 std::pair<size_t, size_t> offsetToClangLineColumn(llvm::StringRef Code, 590 size_t Offset) { 591 Offset = std::min(Code.size(), Offset); 592 llvm::StringRef Before = Code.substr(0, Offset); 593 int Lines = Before.count('\n'); 594 size_t PrevNL = Before.rfind('\n'); 595 size_t StartOfLine = (PrevNL == llvm::StringRef::npos) ? 0 : (PrevNL + 1); 596 return {Lines + 1, Offset - StartOfLine + 1}; 597 } 598 599 std::pair<StringRef, StringRef> splitQualifiedName(StringRef QName) { 600 size_t Pos = QName.rfind("::"); 601 if (Pos == llvm::StringRef::npos) 602 return {llvm::StringRef(), QName}; 603 return {QName.substr(0, Pos + 2), QName.substr(Pos + 2)}; 604 } 605 606 TextEdit replacementToEdit(llvm::StringRef Code, 607 const tooling::Replacement &R) { 608 Range ReplacementRange = { 609 offsetToPosition(Code, R.getOffset()), 610 offsetToPosition(Code, R.getOffset() + R.getLength())}; 611 return {ReplacementRange, R.getReplacementText()}; 612 } 613 614 std::vector<TextEdit> replacementsToEdits(llvm::StringRef Code, 615 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 622 llvm::Optional<std::string> getCanonicalPath(const FileEntry *F, 623 const SourceManager &SourceMgr) { 624 if (!F) 625 return None; 626 627 llvm::SmallString<128> FilePath = F->getName(); 628 if (!llvm::sys::path::is_absolute(FilePath)) { 629 if (auto EC = 630 SourceMgr.getFileManager().getVirtualFileSystem().makeAbsolute( 631 FilePath)) { 632 elog("Could not turn relative path '{0}' to absolute: {1}", FilePath, 633 EC.message()); 634 return None; 635 } 636 } 637 638 // Handle the symbolic link path case where the current working directory 639 // (getCurrentWorkingDirectory) is a symlink. We always want to the real 640 // 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" 649 if (auto Dir = SourceMgr.getFileManager().getDirectory( 650 llvm::sys::path::parent_path(FilePath))) { 651 llvm::SmallString<128> RealPath; 652 llvm::StringRef DirName = SourceMgr.getFileManager().getCanonicalName(*Dir); 653 llvm::sys::path::append(RealPath, DirName, 654 llvm::sys::path::filename(FilePath)); 655 return RealPath.str().str(); 656 } 657 658 return FilePath.str().str(); 659 } 660 661 TextEdit 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 670 bool isRangeConsecutive(const Range &Left, const Range &Right) { 671 return Left.end.line == Right.start.line && 672 Left.end.character == Right.start.character; 673 } 674 675 FileDigest digest(llvm::StringRef Content) { 676 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; 683 } 684 685 llvm::Optional<FileDigest> digestFile(const SourceManager &SM, FileID FID) { 686 bool Invalid = false; 687 llvm::StringRef Content = SM.getBufferData(FID, &Invalid); 688 if (Invalid) 689 return None; 690 return digest(Content); 691 } 692 693 format::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 706 llvm::Expected<tooling::Replacements> 707 cleanupAndFormat(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 715 static void 716 lex(llvm::StringRef Code, const LangOptions &LangOpts, 717 llvm::function_ref<void(const clang::Token &, const SourceManager &SM)> 718 Action) { 719 // FIXME: InMemoryFileAdapter crashes unless the buffer is null terminated! 720 std::string NullTerminatedCode = Code.str(); 721 SourceManagerForFile FileSM("dummy.cpp", NullTerminatedCode); 722 auto &SM = FileSM.get(); 723 auto FID = SM.getMainFileID(); 724 // Create a raw lexer (with no associated preprocessor object). 725 Lexer Lex(FID, SM.getBuffer(FID), SM, LangOpts); 726 Token Tok; 727 728 while (!Lex.LexFromRawLexer(Tok)) 729 Action(Tok, SM); 730 // LexFromRawLexer returns true after it lexes last token, so we still have 731 // one more token to report. 732 Action(Tok, SM); 733 } 734 735 llvm::StringMap<unsigned> collectIdentifiers(llvm::StringRef Content, 736 const format::FormatStyle &Style) { 737 llvm::StringMap<unsigned> Identifiers; 738 auto LangOpt = format::getFormattingLangOpts(Style); 739 lex(Content, LangOpt, [&](const clang::Token &Tok, const SourceManager &) { 740 if (Tok.getKind() == tok::raw_identifier) 741 ++Identifiers[Tok.getRawIdentifier()]; 742 }); 743 return Identifiers; 744 } 745 746 std::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 763 namespace { 764 struct 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; 773 }; 774 // Scans C++ source code for constructs that change the visible namespaces. 775 void parseNamespaceEvents(llvm::StringRef Code, 776 const format::FormatStyle &Style, 777 llvm::function_ref<void(NamespaceEvent)> Callback) { 778 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 794 NamespaceEvent Event; 795 lex(Code, format::getFormattingLangOpts(Style), 796 [&](const clang::Token &Tok,const SourceManager &SM) { 797 Event.Pos = sourceLocToPosition(SM, Tok.getLocation()); 798 switch (Tok.getKind()) { 799 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: 804 NSName.append(Tok.getRawIdentifier()); 805 State = UsingNamespaceName; 806 break; 807 case Namespace: 808 case NamespaceName: 809 NSName.append(Tok.getRawIdentifier()); 810 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); 850 Event.Trigger = NamespaceEvent::BeginNamespace; 851 Event.Payload = llvm::join(Enclosing, "::"); 852 Callback(Event); 853 } 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(); 866 Event.Trigger = NamespaceEvent::EndNamespace; 867 Event.Payload = llvm::join(Enclosing, "::"); 868 Callback(Event); 869 } 870 BraceStack.pop_back(); 871 } 872 break; 873 case tok::semi: 874 if (State == UsingNamespaceName) { 875 // Parsed: using namespace <name> ; 876 Event.Trigger = NamespaceEvent::UsingDirective; 877 Event.Payload = std::move(NSName); 878 Callback(Event); 879 } 880 State = Default; 881 break; 882 default: 883 State = Default; 884 break; 885 } 886 }); 887 } 888 889 // Returns the prefix namespaces of NS: {"" ... NS}. 890 llvm::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 901 std::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 907 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 }); 928 929 std::vector<std::string> Found; 930 for (llvm::StringRef Enclosing : ancestorNamespaces(Current)) { 931 Found.push_back(Enclosing); 932 auto It = UsingDirectives.find(Enclosing); 933 if (It != UsingDirectives.end()) 934 for (const auto &Used : It->second) 935 Found.push_back(Used.getKey()); 936 } 937 938 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 949 llvm::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 987 llvm::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 1013 llvm::Expected<std::string> Edit::apply() const { 1014 return tooling::applyAllReplacements(InitialCode, Replacements); 1015 } 1016 1017 std::vector<TextEdit> Edit::asTextEdits() const { 1018 return replacementsToEdits(InitialCode, Replacements); 1019 } 1020 1021 bool 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 1057 llvm::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 1065 EligibleRegion 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 1118 bool 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 1130 } // namespace clangd 1131 } // namespace clang 1132