blob: ed50974ec15bbe94a9f00dc78e38841494f50772 [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"
Kadir Cetinkaya2f84d912018-08-08 08:59:29 +000017#include "clang/Basic/Diagnostic.h"
Marc-Andre Laperle63a10982018-02-21 02:39:08 +000018#include "clang/Basic/SourceLocation.h"
Eric Liu9133ecd2018-05-11 12:12:08 +000019#include "clang/Tooling/Core/Replacement.h"
Sam McCallb536a2a2017-12-19 12:23:48 +000020
21namespace clang {
Marc-Andre Laperle63a10982018-02-21 02:39:08 +000022class SourceManager;
23
Sam McCallb536a2a2017-12-19 12:23:48 +000024namespace clangd {
25
26/// Turn a [line, column] pair into an offset in Code.
Simon Marchi766338a2018-03-21 14:36:46 +000027///
Sam McCalla4962cc2018-04-27 11:59:28 +000028/// If P.character exceeds the line length, returns the offset at end-of-line.
29/// (If !AllowColumnsBeyondLineLength, then returns an error instead).
30/// If the line number is out of range, returns an error.
Simon Marchi766338a2018-03-21 14:36:46 +000031///
32/// The returned value is in the range [0, Code.size()].
33llvm::Expected<size_t>
34positionToOffset(llvm::StringRef Code, Position P,
35 bool AllowColumnsBeyondLineLength = true);
Sam McCallb536a2a2017-12-19 12:23:48 +000036
37/// Turn an offset in Code into a [line, column] pair.
Sam McCalla4962cc2018-04-27 11:59:28 +000038/// The offset must be in range [0, Code.size()].
Sam McCallb536a2a2017-12-19 12:23:48 +000039Position offsetToPosition(llvm::StringRef Code, size_t Offset);
40
Marc-Andre Laperle63a10982018-02-21 02:39:08 +000041/// Turn a SourceLocation into a [line, column] pair.
Simon Marchi766338a2018-03-21 14:36:46 +000042/// FIXME: This should return an error if the location is invalid.
Marc-Andre Laperle63a10982018-02-21 02:39:08 +000043Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc);
44
Ilya Biryukov71028b82018-03-12 15:28:22 +000045// Converts a half-open clang source range to an LSP range.
46// Note that clang also uses closed source ranges, which this can't handle!
47Range halfOpenToRange(const SourceManager &SM, CharSourceRange R);
48
Sam McCalla4962cc2018-04-27 11:59:28 +000049// Converts an offset to a clang line/column (1-based, columns are bytes).
50// The offset must be in range [0, Code.size()].
51// Prefer to use SourceManager if one is available.
52std::pair<size_t, size_t> offsetToClangLineColumn(llvm::StringRef Code,
53 size_t Offset);
54
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +000055/// From "a::b::c", return {"a::b::", "c"}. Scope is empty if there's no
56/// qualifier.
57std::pair<llvm::StringRef, llvm::StringRef>
58splitQualifiedName(llvm::StringRef QName);
59
Eric Liu9133ecd2018-05-11 12:12:08 +000060TextEdit replacementToEdit(StringRef Code, const tooling::Replacement &R);
61
62std::vector<TextEdit> replacementsToEdits(StringRef Code,
63 const tooling::Replacements &Repls);
64
Kadir Cetinkaya2f84d912018-08-08 08:59:29 +000065TextEdit toTextEdit(const FixItHint &FixIt, const SourceManager &M,
66 const LangOptions &L);
67
Simon Marchi25f1f732018-08-10 22:27:53 +000068/// Get the real/canonical path of \p F. This means:
69///
70/// - Absolute path
71/// - Symlinks resolved
72/// - No "." or ".." component
73/// - No duplicate or trailing directory separator
74///
75/// This function should be used when sending paths to clients, so that paths
76/// are normalized as much as possible.
77llvm::Optional<std::string> getRealPath(const FileEntry *F,
78 const SourceManager &SourceMgr);
Kadir Cetinkayaa9c9d002018-08-13 08:23:01 +000079
80bool IsRangeConsecutive(const Range &Left, const Range &Right);
Sam McCallb536a2a2017-12-19 12:23:48 +000081} // namespace clangd
82} // namespace clang
83#endif