blob: 4366f36072eaaabe11b67d5eb85b2fce62adcc9b [file] [log] [blame]
Sam McCallb536a2a2017-12-19 12:23:48 +00001//===--- SourceCode.h - Manipulating source code as strings -----*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 McCallb536a2a2017-12-19 12:23:48 +00006//
7//===----------------------------------------------------------------------===//
8#include "SourceCode.h"
9
Marc-Andre Laperle1be69702018-07-05 19:35:01 +000010#include "Logger.h"
11#include "clang/AST/ASTContext.h"
Marc-Andre Laperle63a10982018-02-21 02:39:08 +000012#include "clang/Basic/SourceManager.h"
Marc-Andre Laperle1be69702018-07-05 19:35:01 +000013#include "clang/Lex/Lexer.h"
Ilya Biryukov43998782019-01-31 21:30:05 +000014#include "llvm/ADT/None.h"
15#include "llvm/ADT/StringRef.h"
Simon Marchi766338a2018-03-21 14:36:46 +000016#include "llvm/Support/Errc.h"
17#include "llvm/Support/Error.h"
Marc-Andre Laperle1be69702018-07-05 19:35:01 +000018#include "llvm/Support/Path.h"
Marc-Andre Laperle63a10982018-02-21 02:39:08 +000019
Sam McCallb536a2a2017-12-19 12:23:48 +000020namespace clang {
21namespace clangd {
Sam McCallb536a2a2017-12-19 12:23:48 +000022
Sam McCalla4962cc2018-04-27 11:59:28 +000023// 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.
29template <typename Callback>
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000030static bool iterateCodepoints(llvm::StringRef U8, const Callback &CB) {
Sam McCalla4962cc2018-04-27 11:59:28 +000031 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 Biryukovf2001aa2019-01-07 15:45:19 +000040 size_t UTF8Length = llvm::countLeadingOnes(C);
Sam McCalla4962cc2018-04-27 11:59:28 +000041 // 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 Biryukovf2001aa2019-01-07 15:45:19 +000057static size_t measureUTF16(llvm::StringRef U8, int U16Units, bool &Valid) {
Sam McCalla4962cc2018-04-27 11:59:28 +000058 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 McCalla4962cc2018-04-27 11:59:28 +000070// Like most strings in clangd, the input is UTF-8 encoded.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000071size_t lspLength(llvm::StringRef Code) {
Sam McCalla4962cc2018-04-27 11:59:28 +000072 // 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 McCall71891122018-10-23 11:51:53 +000075 iterateCodepoints(Code, [&](int U8Len, int U16Len) {
Sam McCalla4962cc2018-04-27 11:59:28 +000076 Count += U16Len;
77 return false;
78 });
79 return Count;
80}
81
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000082llvm::Expected<size_t> positionToOffset(llvm::StringRef Code, Position P,
83 bool AllowColumnsBeyondLineLength) {
Sam McCallb536a2a2017-12-19 12:23:48 +000084 if (P.line < 0)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000085 return llvm::make_error<llvm::StringError>(
86 llvm::formatv("Line value can't be negative ({0})", P.line),
87 llvm::errc::invalid_argument);
Simon Marchi766338a2018-03-21 14:36:46 +000088 if (P.character < 0)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000089 return llvm::make_error<llvm::StringError>(
90 llvm::formatv("Character value can't be negative ({0})", P.character),
91 llvm::errc::invalid_argument);
Sam McCallb536a2a2017-12-19 12:23:48 +000092 size_t StartOfLine = 0;
93 for (int I = 0; I != P.line; ++I) {
94 size_t NextNL = Code.find('\n', StartOfLine);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000095 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 McCallb536a2a2017-12-19 12:23:48 +000099 StartOfLine = NextNL + 1;
100 }
Simon Marchi766338a2018-03-21 14:36:46 +0000101
102 size_t NextNL = Code.find('\n', StartOfLine);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000103 if (NextNL == llvm::StringRef::npos)
Simon Marchi766338a2018-03-21 14:36:46 +0000104 NextNL = Code.size();
105
Sam McCalla4962cc2018-04-27 11:59:28 +0000106 bool Valid;
107 size_t ByteOffsetInLine = measureUTF16(
108 Code.substr(StartOfLine, NextNL - StartOfLine), P.character, Valid);
109 if (!Valid && !AllowColumnsBeyondLineLength)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000110 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 McCalla4962cc2018-04-27 11:59:28 +0000114 return StartOfLine + ByteOffsetInLine;
Sam McCallb536a2a2017-12-19 12:23:48 +0000115}
116
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000117Position offsetToPosition(llvm::StringRef Code, size_t Offset) {
Sam McCallb536a2a2017-12-19 12:23:48 +0000118 Offset = std::min(Code.size(), Offset);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000119 llvm::StringRef Before = Code.substr(0, Offset);
Sam McCallb536a2a2017-12-19 12:23:48 +0000120 int Lines = Before.count('\n');
121 size_t PrevNL = Before.rfind('\n');
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000122 size_t StartOfLine = (PrevNL == llvm::StringRef::npos) ? 0 : (PrevNL + 1);
Ilya Biryukov7beea3a2018-02-14 10:52:04 +0000123 Position Pos;
124 Pos.line = Lines;
Sam McCall71891122018-10-23 11:51:53 +0000125 Pos.character = lspLength(Before.substr(StartOfLine));
Ilya Biryukov7beea3a2018-02-14 10:52:04 +0000126 return Pos;
Sam McCallb536a2a2017-12-19 12:23:48 +0000127}
128
Marc-Andre Laperle63a10982018-02-21 02:39:08 +0000129Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc) {
Sam McCalla4962cc2018-04-27 11:59:28 +0000130 // 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 Laperle63a10982018-02-21 02:39:08 +0000134 Position P;
Sam McCalla4962cc2018-04-27 11:59:28 +0000135 P.line = static_cast<int>(SM.getLineNumber(FID, Offset)) - 1;
136 bool Invalid = false;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000137 llvm::StringRef Code = SM.getBufferData(FID, &Invalid);
Sam McCalla4962cc2018-04-27 11:59:28 +0000138 if (!Invalid) {
139 auto ColumnInBytes = SM.getColumnNumber(FID, Offset) - 1;
140 auto LineSoFar = Code.substr(Offset - ColumnInBytes, ColumnInBytes);
Sam McCall71891122018-10-23 11:51:53 +0000141 P.character = lspLength(LineSoFar);
Sam McCalla4962cc2018-04-27 11:59:28 +0000142 }
Marc-Andre Laperle63a10982018-02-21 02:39:08 +0000143 return P;
144}
145
Ilya Biryukov43998782019-01-31 21:30:05 +0000146bool 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
161bool 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
176bool halfOpenRangeTouches(const SourceManager &Mgr, SourceRange R,
177 SourceLocation L) {
178 return L == R.getEnd() || halfOpenRangeContains(Mgr, R, L);
179}
180
181llvm::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
198llvm::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 Biryukovcce67a32019-01-29 14:17:36 +0000209llvm::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 Biryukov71028b82018-03-12 15:28:22 +0000219Range 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 Biryukovf2001aa2019-01-07 15:45:19 +0000227std::pair<size_t, size_t> offsetToClangLineColumn(llvm::StringRef Code,
Sam McCalla4962cc2018-04-27 11:59:28 +0000228 size_t Offset) {
229 Offset = std::min(Code.size(), Offset);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000230 llvm::StringRef Before = Code.substr(0, Offset);
Sam McCalla4962cc2018-04-27 11:59:28 +0000231 int Lines = Before.count('\n');
232 size_t PrevNL = Before.rfind('\n');
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000233 size_t StartOfLine = (PrevNL == llvm::StringRef::npos) ? 0 : (PrevNL + 1);
Sam McCalla4962cc2018-04-27 11:59:28 +0000234 return {Lines + 1, Offset - StartOfLine + 1};
235}
236
Ilya Biryukov43998782019-01-31 21:30:05 +0000237std::pair<StringRef, StringRef> splitQualifiedName(StringRef QName) {
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000238 size_t Pos = QName.rfind("::");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000239 if (Pos == llvm::StringRef::npos)
240 return {llvm::StringRef(), QName};
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000241 return {QName.substr(0, Pos + 2), QName.substr(Pos + 2)};
242}
243
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000244TextEdit replacementToEdit(llvm::StringRef Code,
245 const tooling::Replacement &R) {
Eric Liu9133ecd2018-05-11 12:12:08 +0000246 Range ReplacementRange = {
247 offsetToPosition(Code, R.getOffset()),
248 offsetToPosition(Code, R.getOffset() + R.getLength())};
249 return {ReplacementRange, R.getReplacementText()};
250}
251
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000252std::vector<TextEdit> replacementsToEdits(llvm::StringRef Code,
Eric Liu9133ecd2018-05-11 12:12:08 +0000253 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 Biryukovf2001aa2019-01-07 15:45:19 +0000260llvm::Optional<std::string> getCanonicalPath(const FileEntry *F,
261 const SourceManager &SourceMgr) {
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000262 if (!F)
263 return None;
Simon Marchi25f1f732018-08-10 22:27:53 +0000264
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000265 llvm::SmallString<128> FilePath = F->getName();
266 if (!llvm::sys::path::is_absolute(FilePath)) {
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000267 if (auto EC =
Duncan P. N. Exon Smithdb8a7422019-03-26 22:32:06 +0000268 SourceMgr.getFileManager().getVirtualFileSystem().makeAbsolute(
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000269 FilePath)) {
270 elog("Could not turn relative path '{0}' to absolute: {1}", FilePath,
271 EC.message());
Sam McCallc008af62018-10-20 15:30:37 +0000272 return None;
Marc-Andre Laperle1be69702018-07-05 19:35:01 +0000273 }
274 }
Simon Marchi25f1f732018-08-10 22:27:53 +0000275
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000276 // 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 Biryukovf2001aa2019-01-07 15:45:19 +0000288 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 Cetinkayadd677932018-12-19 10:46:21 +0000293 return RealPath.str().str();
Simon Marchi25f1f732018-08-10 22:27:53 +0000294 }
295
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000296 return FilePath.str().str();
Marc-Andre Laperle1be69702018-07-05 19:35:01 +0000297}
298
Kadir Cetinkaya2f84d912018-08-08 08:59:29 +0000299TextEdit 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 Wuaa3ed5a2019-01-25 15:14:03 +0000308bool isRangeConsecutive(const Range &Left, const Range &Right) {
Kadir Cetinkayaa9c9d002018-08-13 08:23:01 +0000309 return Left.end.line == Right.start.line &&
310 Left.end.character == Right.start.character;
311}
312
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000313FileDigest digest(llvm::StringRef Content) {
Kadir Cetinkayad08eab42018-11-27 16:08:53 +0000314 return llvm::SHA1::hash({(const uint8_t *)Content.data(), Content.size()});
315}
316
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000317llvm::Optional<FileDigest> digestFile(const SourceManager &SM, FileID FID) {
Kadir Cetinkayad08eab42018-11-27 16:08:53 +0000318 bool Invalid = false;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000319 llvm::StringRef Content = SM.getBufferData(FID, &Invalid);
Kadir Cetinkayad08eab42018-11-27 16:08:53 +0000320 if (Invalid)
321 return None;
322 return digest(Content);
323}
324
Eric Liudd662772019-01-28 14:01:55 +0000325format::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 Wu12e194c2019-02-06 15:24:50 +0000338llvm::Expected<tooling::Replacements>
339cleanupAndFormat(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 McCallb536a2a2017-12-19 12:23:48 +0000347} // namespace clangd
348} // namespace clang