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" |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 17 | #include "clang/Basic/SourceLocation.h" |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 18 | |
| 19 | namespace clang { |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 20 | class SourceManager; |
| 21 | |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 22 | namespace clangd { |
| 23 | |
| 24 | /// Turn a [line, column] pair into an offset in Code. |
Simon Marchi | 766338a | 2018-03-21 14:36:46 +0000 | [diff] [blame] | 25 | /// |
| 26 | /// If the character value is greater than the line length, the behavior depends |
| 27 | /// on AllowColumnsBeyondLineLength: |
| 28 | /// |
| 29 | /// - if true: default back to the end of the line |
| 30 | /// - if false: return an error |
| 31 | /// |
| 32 | /// If the line number is greater than the number of lines in the document, |
| 33 | /// always return an error. |
| 34 | /// |
| 35 | /// The returned value is in the range [0, Code.size()]. |
| 36 | llvm::Expected<size_t> |
| 37 | positionToOffset(llvm::StringRef Code, Position P, |
| 38 | bool AllowColumnsBeyondLineLength = true); |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 39 | |
| 40 | /// Turn an offset in Code into a [line, column] pair. |
Simon Marchi | 766338a | 2018-03-21 14:36:46 +0000 | [diff] [blame] | 41 | /// FIXME: This should return an error if the offset is invalid. |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 42 | Position offsetToPosition(llvm::StringRef Code, size_t Offset); |
| 43 | |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 44 | /// Turn a SourceLocation into a [line, column] pair. |
Simon Marchi | 766338a | 2018-03-21 14:36:46 +0000 | [diff] [blame] | 45 | /// FIXME: This should return an error if the location is invalid. |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 46 | Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc); |
| 47 | |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 48 | // Converts a half-open clang source range to an LSP range. |
| 49 | // Note that clang also uses closed source ranges, which this can't handle! |
| 50 | Range halfOpenToRange(const SourceManager &SM, CharSourceRange R); |
| 51 | |
Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame^] | 52 | /// From "a::b::c", return {"a::b::", "c"}. Scope is empty if there's no |
| 53 | /// qualifier. |
| 54 | std::pair<llvm::StringRef, llvm::StringRef> |
| 55 | splitQualifiedName(llvm::StringRef QName); |
| 56 | |
Sam McCall | b536a2a | 2017-12-19 12:23:48 +0000 | [diff] [blame] | 57 | } // namespace clangd |
| 58 | } // namespace clang |
| 59 | #endif |