blob: 455937680230f6b1456a8687e05fdc393c7b51c3 [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
Sam McCalla69698f2019-03-27 17:47:49 +000015#include "Context.h"
Sam McCallb536a2a2017-12-19 12:23:48 +000016#include "Protocol.h"
Kadir Cetinkaya2f84d912018-08-08 08:59:29 +000017#include "clang/Basic/Diagnostic.h"
Ilya Biryukov43998782019-01-31 21:30:05 +000018#include "clang/Basic/LangOptions.h"
Marc-Andre Laperle63a10982018-02-21 02:39:08 +000019#include "clang/Basic/SourceLocation.h"
Kadir Cetinkayad08eab42018-11-27 16:08:53 +000020#include "clang/Basic/SourceManager.h"
Eric Liudd662772019-01-28 14:01:55 +000021#include "clang/Format/Format.h"
Eric Liu9133ecd2018-05-11 12:12:08 +000022#include "clang/Tooling/Core/Replacement.h"
Eric Liudd662772019-01-28 14:01:55 +000023#include "llvm/ADT/StringRef.h"
Kadir Cetinkayad08eab42018-11-27 16:08:53 +000024#include "llvm/Support/SHA1.h"
Sam McCallb536a2a2017-12-19 12:23:48 +000025
26namespace clang {
Marc-Andre Laperle63a10982018-02-21 02:39:08 +000027class SourceManager;
28
Sam McCallb536a2a2017-12-19 12:23:48 +000029namespace clangd {
30
Kadir Cetinkayad08eab42018-11-27 16:08:53 +000031// We tend to generate digests for source codes in a lot of different places.
32// This represents the type for those digests to prevent us hard coding details
33// of hashing function at every place that needs to store this information.
34using FileDigest = decltype(llvm::SHA1::hash({}));
35FileDigest digest(StringRef Content);
36Optional<FileDigest> digestFile(const SourceManager &SM, FileID FID);
37
Sam McCalla69698f2019-03-27 17:47:49 +000038// This context variable controls the behavior of functions in this file
39// that convert between LSP offsets and native clang byte offsets.
40// If not set, defaults to UTF-16 for backwards-compatibility.
41extern Key<OffsetEncoding> kCurrentOffsetEncoding;
42
Sam McCall71891122018-10-23 11:51:53 +000043// Counts the number of UTF-16 code units needed to represent a string (LSP
44// specifies string lengths in UTF-16 code units).
Sam McCalla69698f2019-03-27 17:47:49 +000045// Use of UTF-16 may be overridden by kCurrentOffsetEncoding.
Sam McCall71891122018-10-23 11:51:53 +000046size_t lspLength(StringRef Code);
47
Sam McCallb536a2a2017-12-19 12:23:48 +000048/// Turn a [line, column] pair into an offset in Code.
Simon Marchi766338a2018-03-21 14:36:46 +000049///
Sam McCalla4962cc2018-04-27 11:59:28 +000050/// If P.character exceeds the line length, returns the offset at end-of-line.
51/// (If !AllowColumnsBeyondLineLength, then returns an error instead).
52/// If the line number is out of range, returns an error.
Simon Marchi766338a2018-03-21 14:36:46 +000053///
54/// The returned value is in the range [0, Code.size()].
55llvm::Expected<size_t>
56positionToOffset(llvm::StringRef Code, Position P,
Fangrui Song8ebb8542019-02-07 15:38:14 +000057 bool AllowColumnsBeyondLineLength = true);
Sam McCallb536a2a2017-12-19 12:23:48 +000058
59/// Turn an offset in Code into a [line, column] pair.
Sam McCalla4962cc2018-04-27 11:59:28 +000060/// The offset must be in range [0, Code.size()].
Sam McCallb536a2a2017-12-19 12:23:48 +000061Position offsetToPosition(llvm::StringRef Code, size_t Offset);
62
Marc-Andre Laperle63a10982018-02-21 02:39:08 +000063/// Turn a SourceLocation into a [line, column] pair.
Simon Marchi766338a2018-03-21 14:36:46 +000064/// FIXME: This should return an error if the location is invalid.
Marc-Andre Laperle63a10982018-02-21 02:39:08 +000065Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc);
66
Ilya Biryukovcce67a32019-01-29 14:17:36 +000067/// Return the file location, corresponding to \p P. Note that one should take
68/// care to avoid comparing the result with expansion locations.
69llvm::Expected<SourceLocation> sourceLocationInMainFile(const SourceManager &SM,
70 Position P);
71
Ilya Biryukov43998782019-01-31 21:30:05 +000072/// Turns a token range into a half-open range and checks its correctness.
73/// The resulting range will have only valid source location on both sides, both
74/// of which are file locations.
75///
76/// File locations always point to a particular offset in a file, i.e. they
77/// never refer to a location inside a macro expansion. Turning locations from
78/// macro expansions into file locations is ambiguous - one can use
79/// SourceManager::{getExpansion|getFile|getSpelling}Loc. This function
80/// calls SourceManager::getFileLoc on both ends of \p R to do the conversion.
81///
82/// User input (e.g. cursor position) is expressed as a file location, so this
83/// function can be viewed as a way to normalize the ranges used in the clang
84/// AST so that they are comparable with ranges coming from the user input.
85llvm::Optional<SourceRange> toHalfOpenFileRange(const SourceManager &Mgr,
86 const LangOptions &LangOpts,
87 SourceRange R);
88
89/// Returns true iff all of the following conditions hold:
90/// - start and end locations are valid,
91/// - start and end locations are file locations from the same file
92/// (i.e. expansion locations are not taken into account).
93/// - start offset <= end offset.
94/// FIXME: introduce a type for source range with this invariant.
95bool isValidFileRange(const SourceManager &Mgr, SourceRange R);
96
97/// Returns true iff \p L is contained in \p R.
98/// EXPECTS: isValidFileRange(R) == true, L is a file location.
99bool halfOpenRangeContains(const SourceManager &Mgr, SourceRange R,
100 SourceLocation L);
101
102/// Returns true iff \p L is contained in \p R or \p L is equal to the end point
103/// of \p R.
104/// EXPECTS: isValidFileRange(R) == true, L is a file location.
105bool halfOpenRangeTouches(const SourceManager &Mgr, SourceRange R,
106 SourceLocation L);
107
108/// Returns the source code covered by the source range.
109/// EXPECTS: isValidFileRange(R) == true.
110llvm::StringRef toSourceCode(const SourceManager &SM, SourceRange R);
111
Ilya Biryukov71028b82018-03-12 15:28:22 +0000112// Converts a half-open clang source range to an LSP range.
113// Note that clang also uses closed source ranges, which this can't handle!
114Range halfOpenToRange(const SourceManager &SM, CharSourceRange R);
115
Sam McCalla4962cc2018-04-27 11:59:28 +0000116// Converts an offset to a clang line/column (1-based, columns are bytes).
117// The offset must be in range [0, Code.size()].
118// Prefer to use SourceManager if one is available.
119std::pair<size_t, size_t> offsetToClangLineColumn(llvm::StringRef Code,
Fangrui Song8ebb8542019-02-07 15:38:14 +0000120 size_t Offset);
Sam McCalla4962cc2018-04-27 11:59:28 +0000121
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000122/// From "a::b::c", return {"a::b::", "c"}. Scope is empty if there's no
123/// qualifier.
124std::pair<llvm::StringRef, llvm::StringRef>
125splitQualifiedName(llvm::StringRef QName);
126
Eric Liu9133ecd2018-05-11 12:12:08 +0000127TextEdit replacementToEdit(StringRef Code, const tooling::Replacement &R);
128
129std::vector<TextEdit> replacementsToEdits(StringRef Code,
Fangrui Song8ebb8542019-02-07 15:38:14 +0000130 const tooling::Replacements &Repls);
Eric Liu9133ecd2018-05-11 12:12:08 +0000131
Kadir Cetinkaya2f84d912018-08-08 08:59:29 +0000132TextEdit toTextEdit(const FixItHint &FixIt, const SourceManager &M,
Fangrui Song8ebb8542019-02-07 15:38:14 +0000133 const LangOptions &L);
Kadir Cetinkaya2f84d912018-08-08 08:59:29 +0000134
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000135/// Get the canonical path of \p F. This means:
Simon Marchi25f1f732018-08-10 22:27:53 +0000136///
137/// - Absolute path
138/// - Symlinks resolved
139/// - No "." or ".." component
140/// - No duplicate or trailing directory separator
141///
Kadir Cetinkayadd677932018-12-19 10:46:21 +0000142/// This function should be used when paths needs to be used outside the
143/// component that generate it, so that paths are normalized as much as
144/// possible.
145llvm::Optional<std::string> getCanonicalPath(const FileEntry *F,
Fangrui Song8ebb8542019-02-07 15:38:14 +0000146 const SourceManager &SourceMgr);
Kadir Cetinkayaa9c9d002018-08-13 08:23:01 +0000147
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000148bool isRangeConsecutive(const Range &Left, const Range &Right);
Eric Liudd662772019-01-28 14:01:55 +0000149
150format::FormatStyle getFormatStyleForFile(llvm::StringRef File,
151 llvm::StringRef Content,
152 llvm::vfs::FileSystem *FS);
153
Haojian Wu12e194c2019-02-06 15:24:50 +0000154// Cleanup and format the given replacements.
155llvm::Expected<tooling::Replacements>
156cleanupAndFormat(StringRef Code, const tooling::Replacements &Replaces,
157 const format::FormatStyle &Style);
158
Eric Liu00d99bd2019-04-11 09:36:36 +0000159/// Collects identifiers with counts in the source code.
160llvm::StringMap<unsigned> collectIdentifiers(llvm::StringRef Content,
161 const format::FormatStyle &Style);
162
Sam McCallb536a2a2017-12-19 12:23:48 +0000163} // namespace clangd
164} // namespace clang
165#endif