Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 1 | //===--- SourceCode.h - Manipulating source code as strings -----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Various code that examines C++ source code without using heavy AST machinery |
| 11 | // (and often not even the lexer). To be used sparingly! |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SOURCECODE_H |
| 15 | #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SOURCECODE_H |
| 16 | #include "Protocol.h" |
Kadir Cetinkaya | 2f84d91 | 2018-08-08 08:59:29 +0000 | [diff] [blame] | 17 | #include "clang/Basic/Diagnostic.h" |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceLocation.h" |
Eric Liu | 9133ecd | 2018-05-11 12:12:08 +0000 | [diff] [blame] | 19 | #include "clang/Tooling/Core/Replacement.h" |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 20 | |
| 21 | namespace clang { |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 22 | class SourceManager; |
| 23 | |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 24 | namespace clangd { |
| 25 | |
Sam McCall | 7189112 | 2018-10-23 11:51:53 +0000 | [diff] [blame^] | 26 | // Counts the number of UTF-16 code units needed to represent a string (LSP |
| 27 | // specifies string lengths in UTF-16 code units). |
| 28 | size_t lspLength(StringRef Code); |
| 29 | |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 30 | /// Turn a [line, column] pair into an offset in Code. |
Simon Marchi | 766338a | 2018-03-21 14:36:46 +0000 | [diff] [blame] | 31 | /// |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 32 | /// If P.character exceeds the line length, returns the offset at end-of-line. |
| 33 | /// (If !AllowColumnsBeyondLineLength, then returns an error instead). |
| 34 | /// If the line number is out of range, returns an error. |
Simon Marchi | 766338a | 2018-03-21 14:36:46 +0000 | [diff] [blame] | 35 | /// |
| 36 | /// The returned value is in the range [0, Code.size()]. |
| 37 | llvm::Expected<size_t> |
| 38 | positionToOffset(llvm::StringRef Code, Position P, |
| 39 | bool AllowColumnsBeyondLineLength = true); |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 40 | |
| 41 | /// Turn an offset in Code into a [line, column] pair. |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 42 | /// The offset must be in range [0, Code.size()]. |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 43 | Position offsetToPosition(llvm::StringRef Code, size_t Offset); |
| 44 | |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 45 | /// Turn a SourceLocation into a [line, column] pair. |
Simon Marchi | 766338a | 2018-03-21 14:36:46 +0000 | [diff] [blame] | 46 | /// FIXME: This should return an error if the location is invalid. |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 47 | Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc); |
| 48 | |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 49 | // Converts a half-open clang source range to an LSP range. |
| 50 | // Note that clang also uses closed source ranges, which this can't handle! |
| 51 | Range halfOpenToRange(const SourceManager &SM, CharSourceRange R); |
| 52 | |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 53 | // Converts an offset to a clang line/column (1-based, columns are bytes). |
| 54 | // The offset must be in range [0, Code.size()]. |
| 55 | // Prefer to use SourceManager if one is available. |
| 56 | std::pair<size_t, size_t> offsetToClangLineColumn(llvm::StringRef Code, |
| 57 | size_t Offset); |
| 58 | |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 59 | /// From "a::b::c", return {"a::b::", "c"}. Scope is empty if there's no |
| 60 | /// qualifier. |
| 61 | std::pair<llvm::StringRef, llvm::StringRef> |
| 62 | splitQualifiedName(llvm::StringRef QName); |
| 63 | |
Eric Liu | 9133ecd | 2018-05-11 12:12:08 +0000 | [diff] [blame] | 64 | TextEdit replacementToEdit(StringRef Code, const tooling::Replacement &R); |
| 65 | |
| 66 | std::vector<TextEdit> replacementsToEdits(StringRef Code, |
| 67 | const tooling::Replacements &Repls); |
| 68 | |
Kadir Cetinkaya | 2f84d91 | 2018-08-08 08:59:29 +0000 | [diff] [blame] | 69 | TextEdit toTextEdit(const FixItHint &FixIt, const SourceManager &M, |
| 70 | const LangOptions &L); |
| 71 | |
Simon Marchi | 25f1f73 | 2018-08-10 22:27:53 +0000 | [diff] [blame] | 72 | /// Get the real/canonical path of \p F. This means: |
| 73 | /// |
| 74 | /// - Absolute path |
| 75 | /// - Symlinks resolved |
| 76 | /// - No "." or ".." component |
| 77 | /// - No duplicate or trailing directory separator |
| 78 | /// |
| 79 | /// This function should be used when sending paths to clients, so that paths |
| 80 | /// are normalized as much as possible. |
| 81 | llvm::Optional<std::string> getRealPath(const FileEntry *F, |
| 82 | const SourceManager &SourceMgr); |
Kadir Cetinkaya | a9c9d00 | 2018-08-13 08:23:01 +0000 | [diff] [blame] | 83 | |
| 84 | bool IsRangeConsecutive(const Range &Left, const Range &Right); |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 85 | } // namespace clangd |
| 86 | } // namespace clang |
| 87 | #endif |