brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.5 KiB · ea7d605 Raw
96 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOLLOCATION_H10#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOLLOCATION_H11 12#include "llvm/ADT/StringRef.h"13#include "llvm/Support/raw_ostream.h"14#include <cstdint>15 16namespace clang {17namespace clangd {18 19struct SymbolLocation {20  // Specify a position (Line, Column) of symbol. Using Line/Column allows us to21  // 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 \n26  //    (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() = default;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; }39 40    bool hasOverflow() const {41      return line() == MaxLine || column() == MaxColumn;42    }43 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;47 48  private:49    uint32_t LineColumnPacked = 0; // Top 20 bit line, bottom 12 bits column.50  };51 52  /// The symbol range, using half-open range [Start, End).53  Position Start;54  Position End;55 56  explicit operator bool() const { return !llvm::StringRef(FileURI).empty(); }57 58  // The URI of the source file where a symbol occurs.59  // The string must be null-terminated.60  //61  // We avoid using llvm::StringRef here to save memory.62  // WARNING: unless you know what you are doing, it is recommended to use it63  // via llvm::StringRef.64  const char *FileURI = "";65};66 67inline 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());71}72inline 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());76}77inline 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);81}82inline bool operator<(const SymbolLocation &L, const SymbolLocation &R) {83  assert(L.FileURI && R.FileURI);84  int Cmp = std::strcmp(L.FileURI, R.FileURI);85  if (Cmp != 0)86    return Cmp < 0;87  return std::tie(L.Start, L.End) < std::tie(R.Start, R.End);88}89 90llvm::raw_ostream &operator<<(llvm::raw_ostream &, const SymbolLocation &);91 92} // namespace clangd93} // namespace clang94 95#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOLLOCATION_H96