blob: a3dbc7abe3f9866b8d2bdf7e8031494784315d2a [file] [log] [blame]
Sam McCallb536a2a2017-12-19 12:23:48 +00001//===--- SourceCode.h - Manipulating source code as strings -----*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 McCallb536a2a2017-12-19 12:23:48 +00006//
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 Cetinkaya2f84d912018-08-08 08:59:29 +000016#include "clang/Basic/Diagnostic.h"
Marc-Andre Laperle63a10982018-02-21 02:39:08 +000017#include "clang/Basic/SourceLocation.h"
Kadir Cetinkayad08eab42018-11-27 16:08:53 +000018#include "clang/Basic/SourceManager.h"
Eric Liu9133ecd2018-05-11 12:12:08 +000019#include "clang/Tooling/Core/Replacement.h"
Kadir Cetinkayad08eab42018-11-27 16:08:53 +000020#include "llvm/Support/SHA1.h"
Sam McCallb536a2a2017-12-19 12:23:48 +000021
22namespace clang {
Marc-Andre Laperle63a10982018-02-21 02:39:08 +000023class SourceManager;
24
Sam McCallb536a2a2017-12-19 12:23:48 +000025namespace clangd {
26
Kadir Cetinkayad08eab42018-11-27 16:08:53 +000027// 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.
30using FileDigest = decltype(llvm::SHA1::hash({}));
31FileDigest digest(StringRef Content);
32Optional<FileDigest> digestFile(const SourceManager &SM, FileID FID);
33
Sam McCall71891122018-10-23 11:51:53 +000034// Counts the number of UTF-16 code units needed to represent a string (LSP
35// specifies string lengths in UTF-16 code units).
36size_t lspLength(StringRef Code);
37
Sam McCallb536a2a2017-12-19 12:23:48 +000038/// Turn a [line, column] pair into an offset in Code.
Simon Marchi766338a2018-03-21 14:36:46 +000039///
Sam McCalla4962cc2018-04-27 11:59:28 +000040/// 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 Marchi766338a2018-03-21 14:36:46 +000043///
44/// The returned value is in the range [0, Code.size()].
45llvm::Expected<size_t>
46positionToOffset(llvm::StringRef Code, Position P,
47 bool AllowColumnsBeyondLineLength = true);
Sam McCallb536a2a2017-12-19 12:23:48 +000048
49/// Turn an offset in Code into a [line, column] pair.
Sam McCalla4962cc2018-04-27 11:59:28 +000050/// The offset must be in range [0, Code.size()].
Sam McCallb536a2a2017-12-19 12:23:48 +000051Position offsetToPosition(llvm::StringRef Code, size_t Offset);
52
Marc-Andre Laperle63a10982018-02-21 02:39:08 +000053/// Turn a SourceLocation into a [line, column] pair.
Simon Marchi766338a2018-03-21 14:36:46 +000054/// FIXME: This should return an error if the location is invalid.
Marc-Andre Laperle63a10982018-02-21 02:39:08 +000055Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc);
56
Ilya Biryukov71028b82018-03-12 15:28:22 +000057// 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!
59Range halfOpenToRange(const SourceManager &SM, CharSourceRange R);
60
Sam McCalla4962cc2018-04-27 11:59:28 +000061// 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.
64std::pair<size_t, size_t> offsetToClangLineColumn(llvm::StringRef Code,
65 size_t Offset);
66
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +000067/// From "a::b::c", return {"a::b::", "c"}. Scope is empty if there's no
68/// qualifier.
69std::pair<llvm::StringRef, llvm::StringRef>
70splitQualifiedName(llvm::StringRef QName);
71
Eric Liu9133ecd2018-05-11 12:12:08 +000072TextEdit replacementToEdit(StringRef Code, const tooling::Replacement &R);
73
74std::vector<TextEdit> replacementsToEdits(StringRef Code,
75 const tooling::Replacements &Repls);
76
Kadir Cetinkaya2f84d912018-08-08 08:59:29 +000077TextEdit toTextEdit(const FixItHint &FixIt, const SourceManager &M,
78 const LangOptions &L);
79
Kadir Cetinkayadd677932018-12-19 10:46:21 +000080/// Get the canonical path of \p F. This means:
Simon Marchi25f1f732018-08-10 22:27:53 +000081///
82/// - Absolute path
83/// - Symlinks resolved
84/// - No "." or ".." component
85/// - No duplicate or trailing directory separator
86///
Kadir Cetinkayadd677932018-12-19 10:46:21 +000087/// 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.
90llvm::Optional<std::string> getCanonicalPath(const FileEntry *F,
91 const SourceManager &SourceMgr);
Kadir Cetinkayaa9c9d002018-08-13 08:23:01 +000092
93bool IsRangeConsecutive(const Range &Left, const Range &Right);
Sam McCallb536a2a2017-12-19 12:23:48 +000094} // namespace clangd
95} // namespace clang
96#endif