diff options
Diffstat (limited to 'clang-tools-extra/clangd/SourceCode.cpp')
| -rw-r--r-- | clang-tools-extra/clangd/SourceCode.cpp | 92 |
1 files changed, 47 insertions, 45 deletions
diff --git a/clang-tools-extra/clangd/SourceCode.cpp b/clang-tools-extra/clangd/SourceCode.cpp index ce22676dc23..343fc70113f 100644 --- a/clang-tools-extra/clangd/SourceCode.cpp +++ b/clang-tools-extra/clangd/SourceCode.cpp @@ -16,7 +16,6 @@ #include "llvm/Support/Error.h" #include "llvm/Support/Path.h" -using namespace llvm; namespace clang { namespace clangd { @@ -27,7 +26,7 @@ namespace clangd { // invokes CB(UTF-8 length, UTF-16 length), and breaks if it returns true. // Returns true if CB returned true, false if we hit the end of string. template <typename Callback> -static bool iterateCodepoints(StringRef U8, const Callback &CB) { +static bool iterateCodepoints(llvm::StringRef U8, const Callback &CB) { for (size_t I = 0; I < U8.size();) { unsigned char C = static_cast<unsigned char>(U8[I]); if (LLVM_LIKELY(!(C & 0x80))) { // ASCII character. @@ -37,7 +36,7 @@ static bool iterateCodepoints(StringRef U8, const Callback &CB) { continue; } // This convenient property of UTF-8 holds for all non-ASCII characters. - size_t UTF8Length = countLeadingOnes(C); + size_t UTF8Length = llvm::countLeadingOnes(C); // 0xxx is ASCII, handled above. 10xxx is a trailing byte, invalid here. // 11111xxx is not valid UTF-8 at all. Assert because it's probably our bug. assert((UTF8Length >= 2 && UTF8Length <= 4) && @@ -54,7 +53,7 @@ static bool iterateCodepoints(StringRef U8, const Callback &CB) { // Returns the offset into the string that matches \p Units UTF-16 code units. // Conceptually, this converts to UTF-16, truncates to CodeUnits, converts back // to UTF-8, and returns the length in bytes. -static size_t measureUTF16(StringRef U8, int U16Units, bool &Valid) { +static size_t measureUTF16(llvm::StringRef U8, int U16Units, bool &Valid) { size_t Result = 0; Valid = U16Units == 0 || iterateCodepoints(U8, [&](int U8Len, int U16Len) { Result += U8Len; @@ -68,7 +67,7 @@ static size_t measureUTF16(StringRef U8, int U16Units, bool &Valid) { } // Like most strings in clangd, the input is UTF-8 encoded. -size_t lspLength(StringRef Code) { +size_t lspLength(llvm::StringRef Code) { // A codepoint takes two UTF-16 code unit if it's astral (outside BMP). // Astral codepoints are encoded as 4 bytes in UTF-8, starting with 11110xxx. size_t Count = 0; @@ -79,47 +78,47 @@ size_t lspLength(StringRef Code) { return Count; } -Expected<size_t> positionToOffset(StringRef Code, Position P, - bool AllowColumnsBeyondLineLength) { +llvm::Expected<size_t> positionToOffset(llvm::StringRef Code, Position P, + bool AllowColumnsBeyondLineLength) { if (P.line < 0) - return make_error<StringError>( - formatv("Line value can't be negative ({0})", P.line), - errc::invalid_argument); + return llvm::make_error<llvm::StringError>( + llvm::formatv("Line value can't be negative ({0})", P.line), + llvm::errc::invalid_argument); if (P.character < 0) - return make_error<StringError>( - formatv("Character value can't be negative ({0})", P.character), - errc::invalid_argument); + return llvm::make_error<llvm::StringError>( + llvm::formatv("Character value can't be negative ({0})", P.character), + llvm::errc::invalid_argument); size_t StartOfLine = 0; for (int I = 0; I != P.line; ++I) { size_t NextNL = Code.find('\n', StartOfLine); - if (NextNL == StringRef::npos) - return make_error<StringError>( - formatv("Line value is out of range ({0})", P.line), - errc::invalid_argument); + if (NextNL == llvm::StringRef::npos) + return llvm::make_error<llvm::StringError>( + llvm::formatv("Line value is out of range ({0})", P.line), + llvm::errc::invalid_argument); StartOfLine = NextNL + 1; } size_t NextNL = Code.find('\n', StartOfLine); - if (NextNL == StringRef::npos) + if (NextNL == llvm::StringRef::npos) NextNL = Code.size(); bool Valid; size_t ByteOffsetInLine = measureUTF16( Code.substr(StartOfLine, NextNL - StartOfLine), P.character, Valid); if (!Valid && !AllowColumnsBeyondLineLength) - return make_error<StringError>( - formatv("UTF-16 offset {0} is invalid for line {1}", P.character, - P.line), - errc::invalid_argument); + return llvm::make_error<llvm::StringError>( + llvm::formatv("UTF-16 offset {0} is invalid for line {1}", P.character, + P.line), + llvm::errc::invalid_argument); return StartOfLine + ByteOffsetInLine; } -Position offsetToPosition(StringRef Code, size_t Offset) { +Position offsetToPosition(llvm::StringRef Code, size_t Offset) { Offset = std::min(Code.size(), Offset); - StringRef Before = Code.substr(0, Offset); + llvm::StringRef Before = Code.substr(0, Offset); int Lines = Before.count('\n'); size_t PrevNL = Before.rfind('\n'); - size_t StartOfLine = (PrevNL == StringRef::npos) ? 0 : (PrevNL + 1); + size_t StartOfLine = (PrevNL == llvm::StringRef::npos) ? 0 : (PrevNL + 1); Position Pos; Pos.line = Lines; Pos.character = lspLength(Before.substr(StartOfLine)); @@ -134,7 +133,7 @@ Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc) { Position P; P.line = static_cast<int>(SM.getLineNumber(FID, Offset)) - 1; bool Invalid = false; - StringRef Code = SM.getBufferData(FID, &Invalid); + llvm::StringRef Code = SM.getBufferData(FID, &Invalid); if (!Invalid) { auto ColumnInBytes = SM.getColumnNumber(FID, Offset) - 1; auto LineSoFar = Code.substr(Offset - ColumnInBytes, ColumnInBytes); @@ -151,31 +150,33 @@ Range halfOpenToRange(const SourceManager &SM, CharSourceRange R) { return {Begin, End}; } -std::pair<size_t, size_t> offsetToClangLineColumn(StringRef Code, +std::pair<size_t, size_t> offsetToClangLineColumn(llvm::StringRef Code, size_t Offset) { Offset = std::min(Code.size(), Offset); - StringRef Before = Code.substr(0, Offset); + llvm::StringRef Before = Code.substr(0, Offset); int Lines = Before.count('\n'); size_t PrevNL = Before.rfind('\n'); - size_t StartOfLine = (PrevNL == StringRef::npos) ? 0 : (PrevNL + 1); + size_t StartOfLine = (PrevNL == llvm::StringRef::npos) ? 0 : (PrevNL + 1); return {Lines + 1, Offset - StartOfLine + 1}; } -std::pair<StringRef, StringRef> splitQualifiedName(StringRef QName) { +std::pair<llvm::StringRef, llvm::StringRef> +splitQualifiedName(llvm::StringRef QName) { size_t Pos = QName.rfind("::"); - if (Pos == StringRef::npos) - return {StringRef(), QName}; + if (Pos == llvm::StringRef::npos) + return {llvm::StringRef(), QName}; return {QName.substr(0, Pos + 2), QName.substr(Pos + 2)}; } -TextEdit replacementToEdit(StringRef Code, const tooling::Replacement &R) { +TextEdit replacementToEdit(llvm::StringRef Code, + const tooling::Replacement &R) { Range ReplacementRange = { offsetToPosition(Code, R.getOffset()), offsetToPosition(Code, R.getOffset() + R.getLength())}; return {ReplacementRange, R.getReplacementText()}; } -std::vector<TextEdit> replacementsToEdits(StringRef Code, +std::vector<TextEdit> replacementsToEdits(llvm::StringRef Code, const tooling::Replacements &Repls) { std::vector<TextEdit> Edits; for (const auto &R : Repls) @@ -183,13 +184,13 @@ std::vector<TextEdit> replacementsToEdits(StringRef Code, return Edits; } -Optional<std::string> getCanonicalPath(const FileEntry *F, - const SourceManager &SourceMgr) { +llvm::Optional<std::string> getCanonicalPath(const FileEntry *F, + const SourceManager &SourceMgr) { if (!F) return None; - SmallString<128> FilePath = F->getName(); - if (!sys::path::is_absolute(FilePath)) { + llvm::SmallString<128> FilePath = F->getName(); + if (!llvm::sys::path::is_absolute(FilePath)) { if (auto EC = SourceMgr.getFileManager().getVirtualFileSystem()->makeAbsolute( FilePath)) { @@ -211,10 +212,11 @@ Optional<std::string> getCanonicalPath(const FileEntry *F, // The file path of Symbol is "/project/src/foo.h" instead of // "/tmp/build/foo.h" if (const DirectoryEntry *Dir = SourceMgr.getFileManager().getDirectory( - sys::path::parent_path(FilePath))) { - SmallString<128> RealPath; - StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir); - sys::path::append(RealPath, DirName, sys::path::filename(FilePath)); + llvm::sys::path::parent_path(FilePath))) { + llvm::SmallString<128> RealPath; + llvm::StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir); + llvm::sys::path::append(RealPath, DirName, + llvm::sys::path::filename(FilePath)); return RealPath.str().str(); } @@ -235,13 +237,13 @@ bool IsRangeConsecutive(const Range &Left, const Range &Right) { Left.end.character == Right.start.character; } -FileDigest digest(StringRef Content) { +FileDigest digest(llvm::StringRef Content) { return llvm::SHA1::hash({(const uint8_t *)Content.data(), Content.size()}); } -Optional<FileDigest> digestFile(const SourceManager &SM, FileID FID) { +llvm::Optional<FileDigest> digestFile(const SourceManager &SM, FileID FID) { bool Invalid = false; - StringRef Content = SM.getBufferData(FID, &Invalid); + llvm::StringRef Content = SM.getBufferData(FID, &Invalid); if (Invalid) return None; return digest(Content); |

