1 //===--- SymbolLocation.h ----------------------------------------*- C++-*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOLLOCATION_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOLLOCATION_H
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/Support/raw_ostream.h"
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.
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.
30 // Position is encoded into 32 bits to save space.
31 // If Line/Column overflow, the value will be their maximum value.
34 void setLine(uint32_t Line
);
35 uint32_t line() const { return LineColumnPacked
>> ColumnBits
; }
36 void setColumn(uint32_t Column
);
37 uint32_t column() const { return LineColumnPacked
& MaxColumn
; }
38 uint32_t rep() const { return LineColumnPacked
; }
40 bool hasOverflow() const {
41 return line() == MaxLine
|| column() == MaxColumn
;
44 static constexpr unsigned ColumnBits
= 12;
45 static constexpr uint32_t MaxLine
= (1 << (32 - ColumnBits
)) - 1;
46 static constexpr uint32_t MaxColumn
= (1 << ColumnBits
) - 1;
49 uint32_t LineColumnPacked
= 0; // Top 20 bit line, bottom 12 bits column.
52 /// The symbol range, using half-open range [Start, End).
56 explicit operator bool() const { return !llvm::StringRef(FileURI
).empty(); }
58 // The URI of the source file where a symbol occurs.
59 // The string must be null-terminated.
61 // We avoid using llvm::StringRef here to save memory.
62 // WARNING: unless you know what you are doing, it is recommended to use it
63 // via llvm::StringRef.
64 const char *FileURI
= "";
67 inline bool operator==(const SymbolLocation::Position
&L
,
68 const SymbolLocation::Position
&R
) {
69 return std::make_tuple(L
.line(), L
.column()) ==
70 std::make_tuple(R
.line(), R
.column());
72 inline bool operator<(const SymbolLocation::Position
&L
,
73 const SymbolLocation::Position
&R
) {
74 return std::make_tuple(L
.line(), L
.column()) <
75 std::make_tuple(R
.line(), R
.column());
77 inline bool operator==(const SymbolLocation
&L
, const SymbolLocation
&R
) {
78 assert(L
.FileURI
&& R
.FileURI
);
79 return !std::strcmp(L
.FileURI
, R
.FileURI
) &&
80 std::tie(L
.Start
, L
.End
) == std::tie(R
.Start
, R
.End
);
82 inline bool operator<(const SymbolLocation
&L
, const SymbolLocation
&R
) {
83 assert(L
.FileURI
&& R
.FileURI
);
84 int Cmp
= std::strcmp(L
.FileURI
, R
.FileURI
);
87 return std::tie(L
.Start
, L
.End
) < std::tie(R
.Start
, R
.End
);
90 llvm::raw_ostream
&operator<<(llvm::raw_ostream
&, const SymbolLocation
&);
95 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOLLOCATION_H