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