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