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