blob: e6ce8c3be5da8d24e3bec9c1c22a262ac1833b34 [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"
Ilya Biryukov43998782019-01-31 21:30:05 +000017#include "clang/Basic/LangOptions.h"
Marc-Andre Laperle63a10982018-02-21 02:39:08 +000018#include "clang/Basic/SourceLocation.h"
Kadir Cetinkayad08eab42018-11-27 16:08:53 +000019#include "clang/Basic/SourceManager.h"
Eric Liudd662772019-01-28 14:01:55 +000020#include "clang/Format/Format.h"
Eric Liu9133ecd2018-05-11 12:12:08 +000021#include "clang/Tooling/Core/Replacement.h"
Eric Liudd662772019-01-28 14:01:55 +000022#include "llvm/ADT/StringRef.h"
Kadir Cetinkayad08eab42018-11-27 16:08:53 +000023#include "llvm/Support/SHA1.h"
Sam McCallb536a2a2017-12-19 12:23:48 +000024
25namespace clang {
Marc-Andre Laperle63a10982018-02-21 02:39:08 +000026class SourceManager;
27
Sam McCallb536a2a2017-12-19 12:23:48 +000028namespace clangd {
29
Kadir Cetinkayad08eab42018-11-27 16:08:53 +000030// 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.
33using FileDigest = decltype(llvm::SHA1::hash({}));
34FileDigest digest(StringRef Content);
35Optional<FileDigest> digestFile(const SourceManager &SM, FileID FID);
36
Sam McCall71891122018-10-23 11:51:53 +000037// Counts the number of UTF-16 code units needed to represent a string (LSP
38// specifies string lengths in UTF-16 code units).
39size_t lspLength(StringRef Code);
40
Sam McCallb536a2a2017-12-19 12:23:48 +000041/// Turn a [line, column] pair into an offset in Code.
Simon Marchi766338a2018-03-21 14:36:46 +000042///
Sam McCalla4962cc2018-04-27 11:59:28 +000043/// 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 Marchi766338a2018-03-21 14:36:46 +000046///
47/// The returned value is in the range [0, Code.size()].
48llvm::Expected<size_t>
49positionToOffset(llvm::StringRef Code, Position P,
Fangrui Song8ebb8542019-02-07 15:38:14 +000050 bool AllowColumnsBeyondLineLength = true);
Sam McCallb536a2a2017-12-19 12:23:48 +000051
52/// Turn an offset in Code into a [line, column] pair.
Sam McCalla4962cc2018-04-27 11:59:28 +000053/// The offset must be in range [0, Code.size()].
Sam McCallb536a2a2017-12-19 12:23:48 +000054Position offsetToPosition(llvm::StringRef Code, size_t Offset);
55
Marc-Andre Laperle63a10982018-02-21 02:39:08 +000056/// Turn a SourceLocation into a [line, column] pair.
Simon Marchi766338a2018-03-21 14:36:46 +000057/// FIXME: This should return an error if the location is invalid.
Marc-Andre Laperle63a10982018-02-21 02:39:08 +000058Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc);
59
Ilya Biryukovcce67a32019-01-29 14:17:36 +000060/// Return the file location, corresponding to \p P. Note that one should take
61/// care to avoid comparing the result with expansion locations.
62llvm::Expected<SourceLocation> sourceLocationInMainFile(const SourceManager &SM,
63 Position P);
64
Ilya Biryukov43998782019-01-31 21:30:05 +000065/// 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.
78llvm::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.
88bool 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.
92bool 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.
98bool 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.
103llvm::StringRef toSourceCode(const SourceManager &SM, SourceRange R);
104
Ilya Biryukov71028b82018-03-12 15:28:22 +0000105// 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!
107Range halfOpenToRange(const SourceManager &SM, CharSourceRange R);
108
Sam McCalla4962cc2018-04-27 11:59:28 +0000109// 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.
112std::pair<size_t, size_t> offsetToClangLineColumn(llvm::StringRef Code,
Fangrui Song8ebb8542019-02-07 15:38:14 +0000113 size_t Offset);
Sam McCalla4962cc2018-04-27 11:59:28 +0000114
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000115/// From "a::b::c", return {"a::b::", "c"}. Scope is empty if there's no
116/// qualifier.
117std::pair<llvm::StringRef, llvm::StringRef>
118splitQualifiedName(llvm::StringRef QName);
119
Eric Liu9133ecd2018-05-11 12:12:08 +0000120TextEdit replacementToEdit(StringRef Code, const tooling::Replacement &R);
121
122std::vector<TextEdit> replacementsToEdits(StringRef Code,
Fangrui Song8ebb8542019-02-07 15:38:14 +0000123 const tooling::Replacements &Repls);
Eric Liu9133ecd2018-05-11 12:12:08 +0000124
Kadir Cetinkaya2f84d912018-08-08 08:59:29 +0000125TextEdit toTextEdit(const FixItHint &FixIt, const SourceManager &M,
Fangrui Song8ebb8542019-02-07 15:38:14 +0000126 const LangOptions &L);
Kadir Cetinkaya2f84d912018-08-08 08:59:29 +0000127
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000128/// Get the canonical path of \p F. This means:
Simon Marchi25f1f732018-08-10 22:27:53 +0000129///
130/// - Absolute path
131/// - Symlinks resolved
132/// - No "." or ".." component
133/// - No duplicate or trailing directory separator
134///
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000135/// 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.
138llvm::Optional<std::string> getCanonicalPath(const FileEntry *F,
Fangrui Song8ebb8542019-02-07 15:38:14 +0000139 const SourceManager &SourceMgr);
Kadir Cetinkayaa9c9d002018-08-13 08:23:01 +0000140
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000141bool isRangeConsecutive(const Range &Left, const Range &Right);
Eric Liudd662772019-01-28 14:01:55 +0000142
143format::FormatStyle getFormatStyleForFile(llvm::StringRef File,
144 llvm::StringRef Content,
145 llvm::vfs::FileSystem *FS);
146
Haojian Wu12e194c2019-02-06 15:24:50 +0000147// Cleanup and format the given replacements.
148llvm::Expected<tooling::Replacements>
149cleanupAndFormat(StringRef Code, const tooling::Replacements &Replaces,
150 const format::FormatStyle &Style);
151
Sam McCallb536a2a2017-12-19 12:23:48 +0000152} // namespace clangd
153} // namespace clang
154#endif