clang-tools 10.0.0

SymbolLocation.h
Go to the documentation of this file.
1 //===--- SymbolLocation.h ----------------------------------------*- C++-*-===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOL_LOCATION_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOL_LOCATION_H
11
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/Support/raw_ostream.h"
14 #include <cstdint>
15
16 namespace clang {
17 namespace clangd {
18
19 struct SymbolLocation {
20 // Specify a position (Line, Column) of symbol. Using Line/Column allows us to
21 // build LSP responses without reading the file content.
22 //
23 // clangd uses the following definitions, which differ slightly from LSP:
24 // - Line is the number of newline characters (\n) before the point.
25 // - Column is (by default) the number of UTF-16 code between the last \n
26 // (or start of file) and the point.
27 // If the `offsetEncoding` protocol extension is used to negotiate UTF-8,
28 // then it is instead the number of *bytes* since the last \n.
29 //
30 // Position is encoded into 32 bits to save space.
31 // If Line/Column overflow, the value will be their maximum value.
32 struct Position {
33 Position() : Line(0), Column(0) {}
34 void setLine(uint32_t Line);
35 uint32_t line() const { return Line; }
36 void setColumn(uint32_t Column);
37 uint32_t column() const { return Column; }
38
39 bool hasOverflow() const {
40 return Line >= MaxLine || Column >= MaxColumn;
41 }
42
43 static constexpr uint32_t MaxLine = (1 << 20) - 1;
44 static constexpr uint32_t MaxColumn = (1 << 12) - 1;
45
46 private:
47 uint32_t Line : 20; // 0-based
48 uint32_t Column : 12; // 0-based
49 };
50
51 /// The symbol range, using half-open range [Start, End).
52 Position Start;
53 Position End;
54
55 explicit operator bool() const { return !llvm::StringRef(FileURI).empty(); }
56
57 // The URI of the source file where a symbol occurs.
58 // The string must be null-terminated.
59 //
60 // We avoid using llvm::StringRef here to save memory.
61 // WARNING: unless you know what you are doing, it is recommended to use it
62 // via llvm::StringRef.
63 const char *FileURI = "";
64 };
65
66 inline bool operator==(const SymbolLocation::Position &L,
67 const SymbolLocation::Position &R) {
68 return std::make_tuple(L.line(), L.column()) ==
69 std::make_tuple(R.line(), R.column());
70 }
71 inline bool operator<(const SymbolLocation::Position &L,
72 const SymbolLocation::Position &R) {
73 return std::make_tuple(L.line(), L.column()) <
74 std::make_tuple(R.line(), R.column());
75 }
76 inline bool operator==(const SymbolLocation &L, const SymbolLocation &R) {
77 assert(L.FileURI && R.FileURI);
78 return !std::strcmp(L.FileURI, R.FileURI) &&
79 std::tie(L.Start, L.End) == std::tie(R.Start, R.End);
80 }
81 inline bool operator<(const SymbolLocation &L, const SymbolLocation &R) {
82 assert(L.FileURI && R.FileURI);
83 int Cmp = std::strcmp(L.FileURI, R.FileURI);
84 if (Cmp != 0)
85 return Cmp < 0;
86 return std::tie(L.Start, L.End) < std::tie(R.Start, R.End);
87 }
88
89 llvm::raw_ostream &operator<<(llvm::raw_ostream &, const SymbolLocation &);
90
91 } // namespace clangd
92 } // namespace clang
93
94 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOL_LOCATION_H
clang::clangd::SymbolLocation::Position::setColumn
void setColumn(uint32_t Column)
Definition: SymbolLocation.cpp:24
clang::clangd::SymbolLocation
Definition: SymbolLocation.h:19
clang::clangd::SymbolLocation::Position::column
uint32_t column() const
Definition: SymbolLocation.h:37
clang::clangd::operator==
bool operator==(const HoverInfo::Param &LHS, const HoverInfo::Param &RHS)
Definition: Hover.h:78
clang::clangd::SymbolLocation::Position::setLine
void setLine(uint32_t Line)
Definition: SymbolLocation.cpp:17
clang::clangd::operator<
bool operator<(const Ref &L, const Ref &R)
Definition: Ref.h:58
clang::clangd::SymbolLocation::Position
Definition: SymbolLocation.h:32
clang::clangd::SymbolLocation::Position::MaxColumn
static constexpr uint32_t MaxColumn
Definition: SymbolLocation.h:44
clang::clangd::SymbolLocation::Position::Position
Position()
Definition: SymbolLocation.h:33
clang::clangd::SymbolLocation::End
Position End
Definition: SymbolLocation.h:53
clang::clangd::SymbolLocation::Position::MaxLine
static constexpr uint32_t MaxLine
Definition: SymbolLocation.h:43
clang::clangd::SymbolLocation::Start
Position Start
The symbol range, using half-open range [Start, End).
Definition: SymbolLocation.h:52
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::SymbolLocation::Position::line
uint32_t line() const
Definition: SymbolLocation.h:35
clang::clangd::operator<<
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
Definition: CodeComplete.cpp:1850
clang::clangd::SymbolLocation::FileURI
const char * FileURI
Definition: SymbolLocation.h:63
clang::clangd::SymbolLocation::Position::hasOverflow
bool hasOverflow() const
Definition: SymbolLocation.h:39