blob: 43fd07015ec4467c8da01af11150f064f259835d [file] [log] [blame]
Sam McCallb536a2a2017-12-19 12:23:48 +00001//===--- 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 Laperle63a10982018-02-21 02:39:08 +000017#include "clang/Basic/SourceLocation.h"
Sam McCallb536a2a2017-12-19 12:23:48 +000018
19namespace clang {
Marc-Andre Laperle63a10982018-02-21 02:39:08 +000020class SourceManager;
21
Sam McCallb536a2a2017-12-19 12:23:48 +000022namespace clangd {
23
24/// Turn a [line, column] pair into an offset in Code.
Simon Marchi766338a2018-03-21 14:36:46 +000025///
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()].
36llvm::Expected<size_t>
37positionToOffset(llvm::StringRef Code, Position P,
38 bool AllowColumnsBeyondLineLength = true);
Sam McCallb536a2a2017-12-19 12:23:48 +000039
40/// Turn an offset in Code into a [line, column] pair.
Simon Marchi766338a2018-03-21 14:36:46 +000041/// FIXME: This should return an error if the offset is invalid.
Sam McCallb536a2a2017-12-19 12:23:48 +000042Position offsetToPosition(llvm::StringRef Code, size_t Offset);
43
Marc-Andre Laperle63a10982018-02-21 02:39:08 +000044/// Turn a SourceLocation into a [line, column] pair.
Simon Marchi766338a2018-03-21 14:36:46 +000045/// FIXME: This should return an error if the location is invalid.
Marc-Andre Laperle63a10982018-02-21 02:39:08 +000046Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc);
47
Ilya Biryukov71028b82018-03-12 15:28:22 +000048// 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!
50Range halfOpenToRange(const SourceManager &SM, CharSourceRange R);
51
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +000052/// From "a::b::c", return {"a::b::", "c"}. Scope is empty if there's no
53/// qualifier.
54std::pair<llvm::StringRef, llvm::StringRef>
55splitQualifiedName(llvm::StringRef QName);
56
Sam McCallb536a2a2017-12-19 12:23:48 +000057} // namespace clangd
58} // namespace clang
59#endif