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" |
Ilya Biryukov | 4399878 | 2019-01-31 21:30:05 +0000 | [diff] [blame] | 17 | #include "clang/Basic/LangOptions.h" |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceLocation.h" |
Kadir Cetinkaya | d08eab4 | 2018-11-27 16:08:53 +0000 | [diff] [blame] | 19 | #include "clang/Basic/SourceManager.h" |
Eric Liu | dd66277 | 2019-01-28 14:01:55 +0000 | [diff] [blame] | 20 | #include "clang/Format/Format.h" |
Eric Liu | 9133ecd | 2018-05-11 12:12:08 +0000 | [diff] [blame] | 21 | #include "clang/Tooling/Core/Replacement.h" |
Eric Liu | dd66277 | 2019-01-28 14:01:55 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringRef.h" |
Kadir Cetinkaya | d08eab4 | 2018-11-27 16:08:53 +0000 | [diff] [blame] | 23 | #include "llvm/Support/SHA1.h" |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 24 | |
| 25 | namespace clang { |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 26 | class SourceManager; |
| 27 | |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 28 | namespace clangd { |
| 29 | |
Kadir Cetinkaya | d08eab4 | 2018-11-27 16:08:53 +0000 | [diff] [blame] | 30 | // We tend to generate digests for source codes in a lot of different places. |
| 31 | // This represents the type for those digests to prevent us hard coding details |
| 32 | // of hashing function at every place that needs to store this information. |
| 33 | using FileDigest = decltype(llvm::SHA1::hash({})); |
| 34 | FileDigest digest(StringRef Content); |
| 35 | Optional<FileDigest> digestFile(const SourceManager &SM, FileID FID); |
| 36 | |
Sam McCall | 7189112 | 2018-10-23 11:51:53 +0000 | [diff] [blame] | 37 | // Counts the number of UTF-16 code units needed to represent a string (LSP |
| 38 | // specifies string lengths in UTF-16 code units). |
| 39 | size_t lspLength(StringRef Code); |
| 40 | |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 41 | /// Turn a [line, column] pair into an offset in Code. |
Simon Marchi | 766338a | 2018-03-21 14:36:46 +0000 | [diff] [blame] | 42 | /// |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 43 | /// If P.character exceeds the line length, returns the offset at end-of-line. |
| 44 | /// (If !AllowColumnsBeyondLineLength, then returns an error instead). |
| 45 | /// If the line number is out of range, returns an error. |
Simon Marchi | 766338a | 2018-03-21 14:36:46 +0000 | [diff] [blame] | 46 | /// |
| 47 | /// The returned value is in the range [0, Code.size()]. |
| 48 | llvm::Expected<size_t> |
| 49 | positionToOffset(llvm::StringRef Code, Position P, |
Fangrui Song | 8ebb854 | 2019-02-07 15:38:14 +0000 | [diff] [blame] | 50 | bool AllowColumnsBeyondLineLength = true); |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 51 | |
| 52 | /// Turn an offset in Code into a [line, column] pair. |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 53 | /// The offset must be in range [0, Code.size()]. |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 54 | Position offsetToPosition(llvm::StringRef Code, size_t Offset); |
| 55 | |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 56 | /// Turn a SourceLocation into a [line, column] pair. |
Simon Marchi | 766338a | 2018-03-21 14:36:46 +0000 | [diff] [blame] | 57 | /// FIXME: This should return an error if the location is invalid. |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 58 | Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc); |
| 59 | |
Ilya Biryukov | cce67a3 | 2019-01-29 14:17:36 +0000 | [diff] [blame] | 60 | /// Return the file location, corresponding to \p P. Note that one should take |
| 61 | /// care to avoid comparing the result with expansion locations. |
| 62 | llvm::Expected<SourceLocation> sourceLocationInMainFile(const SourceManager &SM, |
| 63 | Position P); |
| 64 | |
Ilya Biryukov | 4399878 | 2019-01-31 21:30:05 +0000 | [diff] [blame] | 65 | /// Turns a token range into a half-open range and checks its correctness. |
| 66 | /// The resulting range will have only valid source location on both sides, both |
| 67 | /// of which are file locations. |
| 68 | /// |
| 69 | /// File locations always point to a particular offset in a file, i.e. they |
| 70 | /// never refer to a location inside a macro expansion. Turning locations from |
| 71 | /// macro expansions into file locations is ambiguous - one can use |
| 72 | /// SourceManager::{getExpansion|getFile|getSpelling}Loc. This function |
| 73 | /// calls SourceManager::getFileLoc on both ends of \p R to do the conversion. |
| 74 | /// |
| 75 | /// User input (e.g. cursor position) is expressed as a file location, so this |
| 76 | /// function can be viewed as a way to normalize the ranges used in the clang |
| 77 | /// AST so that they are comparable with ranges coming from the user input. |
| 78 | llvm::Optional<SourceRange> toHalfOpenFileRange(const SourceManager &Mgr, |
| 79 | const LangOptions &LangOpts, |
| 80 | SourceRange R); |
| 81 | |
| 82 | /// Returns true iff all of the following conditions hold: |
| 83 | /// - start and end locations are valid, |
| 84 | /// - start and end locations are file locations from the same file |
| 85 | /// (i.e. expansion locations are not taken into account). |
| 86 | /// - start offset <= end offset. |
| 87 | /// FIXME: introduce a type for source range with this invariant. |
| 88 | bool isValidFileRange(const SourceManager &Mgr, SourceRange R); |
| 89 | |
| 90 | /// Returns true iff \p L is contained in \p R. |
| 91 | /// EXPECTS: isValidFileRange(R) == true, L is a file location. |
| 92 | bool halfOpenRangeContains(const SourceManager &Mgr, SourceRange R, |
| 93 | SourceLocation L); |
| 94 | |
| 95 | /// Returns true iff \p L is contained in \p R or \p L is equal to the end point |
| 96 | /// of \p R. |
| 97 | /// EXPECTS: isValidFileRange(R) == true, L is a file location. |
| 98 | bool halfOpenRangeTouches(const SourceManager &Mgr, SourceRange R, |
| 99 | SourceLocation L); |
| 100 | |
| 101 | /// Returns the source code covered by the source range. |
| 102 | /// EXPECTS: isValidFileRange(R) == true. |
| 103 | llvm::StringRef toSourceCode(const SourceManager &SM, SourceRange R); |
| 104 | |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 105 | // Converts a half-open clang source range to an LSP range. |
| 106 | // Note that clang also uses closed source ranges, which this can't handle! |
| 107 | Range halfOpenToRange(const SourceManager &SM, CharSourceRange R); |
| 108 | |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 109 | // Converts an offset to a clang line/column (1-based, columns are bytes). |
| 110 | // The offset must be in range [0, Code.size()]. |
| 111 | // Prefer to use SourceManager if one is available. |
| 112 | std::pair<size_t, size_t> offsetToClangLineColumn(llvm::StringRef Code, |
Fangrui Song | 8ebb854 | 2019-02-07 15:38:14 +0000 | [diff] [blame] | 113 | size_t Offset); |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 114 | |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 115 | /// From "a::b::c", return {"a::b::", "c"}. Scope is empty if there's no |
| 116 | /// qualifier. |
| 117 | std::pair<llvm::StringRef, llvm::StringRef> |
| 118 | splitQualifiedName(llvm::StringRef QName); |
| 119 | |
Eric Liu | 9133ecd | 2018-05-11 12:12:08 +0000 | [diff] [blame] | 120 | TextEdit replacementToEdit(StringRef Code, const tooling::Replacement &R); |
| 121 | |
| 122 | std::vector<TextEdit> replacementsToEdits(StringRef Code, |
Fangrui Song | 8ebb854 | 2019-02-07 15:38:14 +0000 | [diff] [blame] | 123 | const tooling::Replacements &Repls); |
Eric Liu | 9133ecd | 2018-05-11 12:12:08 +0000 | [diff] [blame] | 124 | |
Kadir Cetinkaya | 2f84d91 | 2018-08-08 08:59:29 +0000 | [diff] [blame] | 125 | TextEdit toTextEdit(const FixItHint &FixIt, const SourceManager &M, |
Fangrui Song | 8ebb854 | 2019-02-07 15:38:14 +0000 | [diff] [blame] | 126 | const LangOptions &L); |
Kadir Cetinkaya | 2f84d91 | 2018-08-08 08:59:29 +0000 | [diff] [blame] | 127 | |
Kadir Cetinkaya | dd67793 | 2018-12-19 10:46:21 +0000 | [diff] [blame] | 128 | /// Get the canonical path of \p F. This means: |
Simon Marchi | 25f1f73 | 2018-08-10 22:27:53 +0000 | [diff] [blame] | 129 | /// |
| 130 | /// - Absolute path |
| 131 | /// - Symlinks resolved |
| 132 | /// - No "." or ".." component |
| 133 | /// - No duplicate or trailing directory separator |
| 134 | /// |
Kadir Cetinkaya | dd67793 | 2018-12-19 10:46:21 +0000 | [diff] [blame] | 135 | /// This function should be used when paths needs to be used outside the |
| 136 | /// component that generate it, so that paths are normalized as much as |
| 137 | /// possible. |
| 138 | llvm::Optional<std::string> getCanonicalPath(const FileEntry *F, |
Fangrui Song | 8ebb854 | 2019-02-07 15:38:14 +0000 | [diff] [blame] | 139 | const SourceManager &SourceMgr); |
Kadir Cetinkaya | a9c9d00 | 2018-08-13 08:23:01 +0000 | [diff] [blame] | 140 | |
Haojian Wu | aa3ed5a | 2019-01-25 15:14:03 +0000 | [diff] [blame] | 141 | bool isRangeConsecutive(const Range &Left, const Range &Right); |
Eric Liu | dd66277 | 2019-01-28 14:01:55 +0000 | [diff] [blame] | 142 | |
| 143 | format::FormatStyle getFormatStyleForFile(llvm::StringRef File, |
| 144 | llvm::StringRef Content, |
| 145 | llvm::vfs::FileSystem *FS); |
| 146 | |
Haojian Wu | 12e194c | 2019-02-06 15:24:50 +0000 | [diff] [blame] | 147 | // Cleanup and format the given replacements. |
| 148 | llvm::Expected<tooling::Replacements> |
| 149 | cleanupAndFormat(StringRef Code, const tooling::Replacements &Replaces, |
| 150 | const format::FormatStyle &Style); |
| 151 | |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 152 | } // namespace clangd |
| 153 | } // namespace clang |
| 154 | #endif |