Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 1 | //===--- SourceCode.h - Manipulating source code as strings -----*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | #include "SourceCode.h" |
| 9 | |
Marc-Andre Laperle | 1be6970 | 2018-07-05 19:35:01 +0000 | [diff] [blame] | 10 | #include "Logger.h" |
| 11 | #include "clang/AST/ASTContext.h" |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 12 | #include "clang/Basic/SourceManager.h" |
Marc-Andre Laperle | 1be6970 | 2018-07-05 19:35:01 +0000 | [diff] [blame] | 13 | #include "clang/Lex/Lexer.h" |
Ilya Biryukov | 4399878 | 2019-01-31 21:30:05 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/None.h" |
| 15 | #include "llvm/ADT/StringRef.h" |
Simon Marchi | 766338a | 2018-03-21 14:36:46 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Errc.h" |
| 17 | #include "llvm/Support/Error.h" |
Marc-Andre Laperle | 1be6970 | 2018-07-05 19:35:01 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Path.h" |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 19 | |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 20 | namespace clang { |
| 21 | namespace clangd { |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 22 | |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 23 | // Here be dragons. LSP positions use columns measured in *UTF-16 code units*! |
| 24 | // Clangd uses UTF-8 and byte-offsets internally, so conversion is nontrivial. |
| 25 | |
| 26 | // Iterates over unicode codepoints in the (UTF-8) string. For each, |
| 27 | // invokes CB(UTF-8 length, UTF-16 length), and breaks if it returns true. |
| 28 | // Returns true if CB returned true, false if we hit the end of string. |
| 29 | template <typename Callback> |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 30 | static bool iterateCodepoints(llvm::StringRef U8, const Callback &CB) { |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 31 | for (size_t I = 0; I < U8.size();) { |
| 32 | unsigned char C = static_cast<unsigned char>(U8[I]); |
| 33 | if (LLVM_LIKELY(!(C & 0x80))) { // ASCII character. |
| 34 | if (CB(1, 1)) |
| 35 | return true; |
| 36 | ++I; |
| 37 | continue; |
| 38 | } |
| 39 | // This convenient property of UTF-8 holds for all non-ASCII characters. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 40 | size_t UTF8Length = llvm::countLeadingOnes(C); |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 41 | // 0xxx is ASCII, handled above. 10xxx is a trailing byte, invalid here. |
| 42 | // 11111xxx is not valid UTF-8 at all. Assert because it's probably our bug. |
| 43 | assert((UTF8Length >= 2 && UTF8Length <= 4) && |
| 44 | "Invalid UTF-8, or transcoding bug?"); |
| 45 | I += UTF8Length; // Skip over all trailing bytes. |
| 46 | // A codepoint takes two UTF-16 code unit if it's astral (outside BMP). |
| 47 | // Astral codepoints are encoded as 4 bytes in UTF-8 (11110xxx ...) |
| 48 | if (CB(UTF8Length, UTF8Length == 4 ? 2 : 1)) |
| 49 | return true; |
| 50 | } |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | // Returns the offset into the string that matches \p Units UTF-16 code units. |
| 55 | // Conceptually, this converts to UTF-16, truncates to CodeUnits, converts back |
| 56 | // to UTF-8, and returns the length in bytes. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 57 | static size_t measureUTF16(llvm::StringRef U8, int U16Units, bool &Valid) { |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 58 | size_t Result = 0; |
| 59 | Valid = U16Units == 0 || iterateCodepoints(U8, [&](int U8Len, int U16Len) { |
| 60 | Result += U8Len; |
| 61 | U16Units -= U16Len; |
| 62 | return U16Units <= 0; |
| 63 | }); |
| 64 | if (U16Units < 0) // Offset was into the middle of a surrogate pair. |
| 65 | Valid = false; |
| 66 | // Don't return an out-of-range index if we overran. |
| 67 | return std::min(Result, U8.size()); |
| 68 | } |
| 69 | |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 70 | // Like most strings in clangd, the input is UTF-8 encoded. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 71 | size_t lspLength(llvm::StringRef Code) { |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 72 | // A codepoint takes two UTF-16 code unit if it's astral (outside BMP). |
| 73 | // Astral codepoints are encoded as 4 bytes in UTF-8, starting with 11110xxx. |
| 74 | size_t Count = 0; |
Sam McCall | 7189112 | 2018-10-23 11:51:53 +0000 | [diff] [blame] | 75 | iterateCodepoints(Code, [&](int U8Len, int U16Len) { |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 76 | Count += U16Len; |
| 77 | return false; |
| 78 | }); |
| 79 | return Count; |
| 80 | } |
| 81 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 82 | llvm::Expected<size_t> positionToOffset(llvm::StringRef Code, Position P, |
| 83 | bool AllowColumnsBeyondLineLength) { |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 84 | if (P.line < 0) |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 85 | return llvm::make_error<llvm::StringError>( |
| 86 | llvm::formatv("Line value can't be negative ({0})", P.line), |
| 87 | llvm::errc::invalid_argument); |
Simon Marchi | 766338a | 2018-03-21 14:36:46 +0000 | [diff] [blame] | 88 | if (P.character < 0) |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 89 | return llvm::make_error<llvm::StringError>( |
| 90 | llvm::formatv("Character value can't be negative ({0})", P.character), |
| 91 | llvm::errc::invalid_argument); |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 92 | size_t StartOfLine = 0; |
| 93 | for (int I = 0; I != P.line; ++I) { |
| 94 | size_t NextNL = Code.find('\n', StartOfLine); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 95 | if (NextNL == llvm::StringRef::npos) |
| 96 | return llvm::make_error<llvm::StringError>( |
| 97 | llvm::formatv("Line value is out of range ({0})", P.line), |
| 98 | llvm::errc::invalid_argument); |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 99 | StartOfLine = NextNL + 1; |
| 100 | } |
Simon Marchi | 766338a | 2018-03-21 14:36:46 +0000 | [diff] [blame] | 101 | |
| 102 | size_t NextNL = Code.find('\n', StartOfLine); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 103 | if (NextNL == llvm::StringRef::npos) |
Simon Marchi | 766338a | 2018-03-21 14:36:46 +0000 | [diff] [blame] | 104 | NextNL = Code.size(); |
| 105 | |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 106 | bool Valid; |
| 107 | size_t ByteOffsetInLine = measureUTF16( |
| 108 | Code.substr(StartOfLine, NextNL - StartOfLine), P.character, Valid); |
| 109 | if (!Valid && !AllowColumnsBeyondLineLength) |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 110 | return llvm::make_error<llvm::StringError>( |
| 111 | llvm::formatv("UTF-16 offset {0} is invalid for line {1}", P.character, |
| 112 | P.line), |
| 113 | llvm::errc::invalid_argument); |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 114 | return StartOfLine + ByteOffsetInLine; |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 117 | Position offsetToPosition(llvm::StringRef Code, size_t Offset) { |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 118 | Offset = std::min(Code.size(), Offset); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 119 | llvm::StringRef Before = Code.substr(0, Offset); |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 120 | int Lines = Before.count('\n'); |
| 121 | size_t PrevNL = Before.rfind('\n'); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 122 | size_t StartOfLine = (PrevNL == llvm::StringRef::npos) ? 0 : (PrevNL + 1); |
Ilya Biryukov | 7beea3a | 2018-02-14 10:52:04 +0000 | [diff] [blame] | 123 | Position Pos; |
| 124 | Pos.line = Lines; |
Sam McCall | 7189112 | 2018-10-23 11:51:53 +0000 | [diff] [blame] | 125 | Pos.character = lspLength(Before.substr(StartOfLine)); |
Ilya Biryukov | 7beea3a | 2018-02-14 10:52:04 +0000 | [diff] [blame] | 126 | return Pos; |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 129 | Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc) { |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 130 | // We use the SourceManager's line tables, but its column number is in bytes. |
| 131 | FileID FID; |
| 132 | unsigned Offset; |
| 133 | std::tie(FID, Offset) = SM.getDecomposedSpellingLoc(Loc); |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 134 | Position P; |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 135 | P.line = static_cast<int>(SM.getLineNumber(FID, Offset)) - 1; |
| 136 | bool Invalid = false; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 137 | llvm::StringRef Code = SM.getBufferData(FID, &Invalid); |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 138 | if (!Invalid) { |
| 139 | auto ColumnInBytes = SM.getColumnNumber(FID, Offset) - 1; |
| 140 | auto LineSoFar = Code.substr(Offset - ColumnInBytes, ColumnInBytes); |
Sam McCall | 7189112 | 2018-10-23 11:51:53 +0000 | [diff] [blame] | 141 | P.character = lspLength(LineSoFar); |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 142 | } |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 143 | return P; |
| 144 | } |
| 145 | |
Ilya Biryukov | 4399878 | 2019-01-31 21:30:05 +0000 | [diff] [blame] | 146 | bool isValidFileRange(const SourceManager &Mgr, SourceRange R) { |
| 147 | if (!R.getBegin().isValid() || !R.getEnd().isValid()) |
| 148 | return false; |
| 149 | |
| 150 | FileID BeginFID; |
| 151 | size_t BeginOffset = 0; |
| 152 | std::tie(BeginFID, BeginOffset) = Mgr.getDecomposedLoc(R.getBegin()); |
| 153 | |
| 154 | FileID EndFID; |
| 155 | size_t EndOffset = 0; |
| 156 | std::tie(EndFID, EndOffset) = Mgr.getDecomposedLoc(R.getEnd()); |
| 157 | |
| 158 | return BeginFID.isValid() && BeginFID == EndFID && BeginOffset <= EndOffset; |
| 159 | } |
| 160 | |
| 161 | bool halfOpenRangeContains(const SourceManager &Mgr, SourceRange R, |
| 162 | SourceLocation L) { |
| 163 | assert(isValidFileRange(Mgr, R)); |
| 164 | |
| 165 | FileID BeginFID; |
| 166 | size_t BeginOffset = 0; |
| 167 | std::tie(BeginFID, BeginOffset) = Mgr.getDecomposedLoc(R.getBegin()); |
| 168 | size_t EndOffset = Mgr.getFileOffset(R.getEnd()); |
| 169 | |
| 170 | FileID LFid; |
| 171 | size_t LOffset; |
| 172 | std::tie(LFid, LOffset) = Mgr.getDecomposedLoc(L); |
| 173 | return BeginFID == LFid && BeginOffset <= LOffset && LOffset < EndOffset; |
| 174 | } |
| 175 | |
| 176 | bool halfOpenRangeTouches(const SourceManager &Mgr, SourceRange R, |
| 177 | SourceLocation L) { |
| 178 | return L == R.getEnd() || halfOpenRangeContains(Mgr, R, L); |
| 179 | } |
| 180 | |
| 181 | llvm::Optional<SourceRange> toHalfOpenFileRange(const SourceManager &Mgr, |
| 182 | const LangOptions &LangOpts, |
| 183 | SourceRange R) { |
| 184 | auto Begin = Mgr.getFileLoc(R.getBegin()); |
| 185 | if (Begin.isInvalid()) |
| 186 | return llvm::None; |
| 187 | auto End = Mgr.getFileLoc(R.getEnd()); |
| 188 | if (End.isInvalid()) |
| 189 | return llvm::None; |
| 190 | End = Lexer::getLocForEndOfToken(End, 0, Mgr, LangOpts); |
| 191 | |
| 192 | SourceRange Result(Begin, End); |
| 193 | if (!isValidFileRange(Mgr, Result)) |
| 194 | return llvm::None; |
| 195 | return Result; |
| 196 | } |
| 197 | |
| 198 | llvm::StringRef toSourceCode(const SourceManager &SM, SourceRange R) { |
| 199 | assert(isValidFileRange(SM, R)); |
| 200 | bool Invalid = false; |
| 201 | auto *Buf = SM.getBuffer(SM.getFileID(R.getBegin()), &Invalid); |
| 202 | assert(!Invalid); |
| 203 | |
| 204 | size_t BeginOffset = SM.getFileOffset(R.getBegin()); |
| 205 | size_t EndOffset = SM.getFileOffset(R.getEnd()); |
| 206 | return Buf->getBuffer().substr(BeginOffset, EndOffset - BeginOffset); |
| 207 | } |
| 208 | |
Ilya Biryukov | cce67a3 | 2019-01-29 14:17:36 +0000 | [diff] [blame] | 209 | llvm::Expected<SourceLocation> sourceLocationInMainFile(const SourceManager &SM, |
| 210 | Position P) { |
| 211 | llvm::StringRef Code = SM.getBuffer(SM.getMainFileID())->getBuffer(); |
| 212 | auto Offset = |
| 213 | positionToOffset(Code, P, /*AllowColumnBeyondLineLength=*/false); |
| 214 | if (!Offset) |
| 215 | return Offset.takeError(); |
| 216 | return SM.getLocForStartOfFile(SM.getMainFileID()).getLocWithOffset(*Offset); |
| 217 | } |
| 218 | |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 219 | Range halfOpenToRange(const SourceManager &SM, CharSourceRange R) { |
| 220 | // Clang is 1-based, LSP uses 0-based indexes. |
| 221 | Position Begin = sourceLocToPosition(SM, R.getBegin()); |
| 222 | Position End = sourceLocToPosition(SM, R.getEnd()); |
| 223 | |
| 224 | return {Begin, End}; |
| 225 | } |
| 226 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 227 | std::pair<size_t, size_t> offsetToClangLineColumn(llvm::StringRef Code, |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 228 | size_t Offset) { |
| 229 | Offset = std::min(Code.size(), Offset); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 230 | llvm::StringRef Before = Code.substr(0, Offset); |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 231 | int Lines = Before.count('\n'); |
| 232 | size_t PrevNL = Before.rfind('\n'); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 233 | size_t StartOfLine = (PrevNL == llvm::StringRef::npos) ? 0 : (PrevNL + 1); |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 234 | return {Lines + 1, Offset - StartOfLine + 1}; |
| 235 | } |
| 236 | |
Ilya Biryukov | 4399878 | 2019-01-31 21:30:05 +0000 | [diff] [blame] | 237 | std::pair<StringRef, StringRef> splitQualifiedName(StringRef QName) { |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 238 | size_t Pos = QName.rfind("::"); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 239 | if (Pos == llvm::StringRef::npos) |
| 240 | return {llvm::StringRef(), QName}; |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 241 | return {QName.substr(0, Pos + 2), QName.substr(Pos + 2)}; |
| 242 | } |
| 243 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 244 | TextEdit replacementToEdit(llvm::StringRef Code, |
| 245 | const tooling::Replacement &R) { |
Eric Liu | 9133ecd | 2018-05-11 12:12:08 +0000 | [diff] [blame] | 246 | Range ReplacementRange = { |
| 247 | offsetToPosition(Code, R.getOffset()), |
| 248 | offsetToPosition(Code, R.getOffset() + R.getLength())}; |
| 249 | return {ReplacementRange, R.getReplacementText()}; |
| 250 | } |
| 251 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 252 | std::vector<TextEdit> replacementsToEdits(llvm::StringRef Code, |
Eric Liu | 9133ecd | 2018-05-11 12:12:08 +0000 | [diff] [blame] | 253 | const tooling::Replacements &Repls) { |
| 254 | std::vector<TextEdit> Edits; |
| 255 | for (const auto &R : Repls) |
| 256 | Edits.push_back(replacementToEdit(Code, R)); |
| 257 | return Edits; |
| 258 | } |
| 259 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 260 | llvm::Optional<std::string> getCanonicalPath(const FileEntry *F, |
| 261 | const SourceManager &SourceMgr) { |
Kadir Cetinkaya | dd67793 | 2018-12-19 10:46:21 +0000 | [diff] [blame] | 262 | if (!F) |
| 263 | return None; |
Simon Marchi | 25f1f73 | 2018-08-10 22:27:53 +0000 | [diff] [blame] | 264 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 265 | llvm::SmallString<128> FilePath = F->getName(); |
| 266 | if (!llvm::sys::path::is_absolute(FilePath)) { |
Kadir Cetinkaya | dd67793 | 2018-12-19 10:46:21 +0000 | [diff] [blame] | 267 | if (auto EC = |
| 268 | SourceMgr.getFileManager().getVirtualFileSystem()->makeAbsolute( |
| 269 | FilePath)) { |
| 270 | elog("Could not turn relative path '{0}' to absolute: {1}", FilePath, |
| 271 | EC.message()); |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 272 | return None; |
Marc-Andre Laperle | 1be6970 | 2018-07-05 19:35:01 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
Simon Marchi | 25f1f73 | 2018-08-10 22:27:53 +0000 | [diff] [blame] | 275 | |
Kadir Cetinkaya | dd67793 | 2018-12-19 10:46:21 +0000 | [diff] [blame] | 276 | // Handle the symbolic link path case where the current working directory |
| 277 | // (getCurrentWorkingDirectory) is a symlink./ We always want to the real |
| 278 | // file path (instead of the symlink path) for the C++ symbols. |
| 279 | // |
| 280 | // Consider the following example: |
| 281 | // |
| 282 | // src dir: /project/src/foo.h |
| 283 | // current working directory (symlink): /tmp/build -> /project/src/ |
| 284 | // |
| 285 | // The file path of Symbol is "/project/src/foo.h" instead of |
| 286 | // "/tmp/build/foo.h" |
| 287 | if (const DirectoryEntry *Dir = SourceMgr.getFileManager().getDirectory( |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 288 | llvm::sys::path::parent_path(FilePath))) { |
| 289 | llvm::SmallString<128> RealPath; |
| 290 | llvm::StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir); |
| 291 | llvm::sys::path::append(RealPath, DirName, |
| 292 | llvm::sys::path::filename(FilePath)); |
Kadir Cetinkaya | dd67793 | 2018-12-19 10:46:21 +0000 | [diff] [blame] | 293 | return RealPath.str().str(); |
Simon Marchi | 25f1f73 | 2018-08-10 22:27:53 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Kadir Cetinkaya | dd67793 | 2018-12-19 10:46:21 +0000 | [diff] [blame] | 296 | return FilePath.str().str(); |
Marc-Andre Laperle | 1be6970 | 2018-07-05 19:35:01 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Kadir Cetinkaya | 2f84d91 | 2018-08-08 08:59:29 +0000 | [diff] [blame] | 299 | TextEdit toTextEdit(const FixItHint &FixIt, const SourceManager &M, |
| 300 | const LangOptions &L) { |
| 301 | TextEdit Result; |
| 302 | Result.range = |
| 303 | halfOpenToRange(M, Lexer::makeFileCharRange(FixIt.RemoveRange, M, L)); |
| 304 | Result.newText = FixIt.CodeToInsert; |
| 305 | return Result; |
| 306 | } |
| 307 | |
Haojian Wu | aa3ed5a | 2019-01-25 15:14:03 +0000 | [diff] [blame] | 308 | bool isRangeConsecutive(const Range &Left, const Range &Right) { |
Kadir Cetinkaya | a9c9d00 | 2018-08-13 08:23:01 +0000 | [diff] [blame] | 309 | return Left.end.line == Right.start.line && |
| 310 | Left.end.character == Right.start.character; |
| 311 | } |
| 312 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 313 | FileDigest digest(llvm::StringRef Content) { |
Kadir Cetinkaya | d08eab4 | 2018-11-27 16:08:53 +0000 | [diff] [blame] | 314 | return llvm::SHA1::hash({(const uint8_t *)Content.data(), Content.size()}); |
| 315 | } |
| 316 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 317 | llvm::Optional<FileDigest> digestFile(const SourceManager &SM, FileID FID) { |
Kadir Cetinkaya | d08eab4 | 2018-11-27 16:08:53 +0000 | [diff] [blame] | 318 | bool Invalid = false; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 319 | llvm::StringRef Content = SM.getBufferData(FID, &Invalid); |
Kadir Cetinkaya | d08eab4 | 2018-11-27 16:08:53 +0000 | [diff] [blame] | 320 | if (Invalid) |
| 321 | return None; |
| 322 | return digest(Content); |
| 323 | } |
| 324 | |
Eric Liu | dd66277 | 2019-01-28 14:01:55 +0000 | [diff] [blame] | 325 | format::FormatStyle getFormatStyleForFile(llvm::StringRef File, |
| 326 | llvm::StringRef Content, |
| 327 | llvm::vfs::FileSystem *FS) { |
| 328 | auto Style = format::getStyle(format::DefaultFormatStyle, File, |
| 329 | format::DefaultFallbackStyle, Content, FS); |
| 330 | if (!Style) { |
| 331 | log("getStyle() failed for file {0}: {1}. Fallback is LLVM style.", File, |
| 332 | Style.takeError()); |
| 333 | Style = format::getLLVMStyle(); |
| 334 | } |
| 335 | return *Style; |
| 336 | } |
| 337 | |
Haojian Wu | 12e194c | 2019-02-06 15:24:50 +0000 | [diff] [blame] | 338 | llvm::Expected<tooling::Replacements> |
| 339 | cleanupAndFormat(StringRef Code, const tooling::Replacements &Replaces, |
| 340 | const format::FormatStyle &Style) { |
| 341 | auto CleanReplaces = cleanupAroundReplacements(Code, Replaces, Style); |
| 342 | if (!CleanReplaces) |
| 343 | return CleanReplaces; |
| 344 | return formatReplacements(Code, std::move(*CleanReplaces), Style); |
| 345 | } |
| 346 | |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 347 | } // namespace clangd |
| 348 | } // namespace clang |