Index: clang-tools-extra/trunk/clangd/CMakeLists.txt =================================================================== --- clang-tools-extra/trunk/clangd/CMakeLists.txt +++ clang-tools-extra/trunk/clangd/CMakeLists.txt @@ -63,6 +63,7 @@ index/MemIndex.cpp index/Merge.cpp index/SymbolID.cpp + index/SymbolLocation.cpp index/Serialization.cpp index/SymbolCollector.cpp index/YAMLSerialization.cpp Index: clang-tools-extra/trunk/clangd/XRefs.cpp =================================================================== --- clang-tools-extra/trunk/clangd/XRefs.cpp +++ clang-tools-extra/trunk/clangd/XRefs.cpp @@ -10,8 +10,8 @@ #include "Logger.h" #include "SourceCode.h" #include "URI.h" -#include "index/Index.h" #include "index/Merge.h" +#include "index/SymbolLocation.h" #include "clang/AST/DeclTemplate.h" #include "clang/AST/RecursiveASTVisitor.h" #include "clang/Index/IndexDataConsumer.h" Index: clang-tools-extra/trunk/clangd/index/Index.h =================================================================== --- clang-tools-extra/trunk/clangd/index/Index.h +++ clang-tools-extra/trunk/clangd/index/Index.h @@ -11,6 +11,7 @@ #include "ExpectedTypes.h" #include "SymbolID.h" +#include "SymbolLocation.h" #include "clang/Index/IndexSymbol.h" #include "clang/Lex/Lexer.h" #include "llvm/ADT/DenseMap.h" @@ -30,70 +31,6 @@ namespace clang { namespace clangd { -struct SymbolLocation { - // Specify a position (Line, Column) of symbol. Using Line/Column allows us to - // build LSP responses without reading the file content. - // - // Position is encoded into 32 bits to save space. - // If Line/Column overflow, the value will be their maximum value. - struct Position { - Position() : Line(0), Column(0) {} - void setLine(uint32_t Line); - uint32_t line() const { return Line; } - void setColumn(uint32_t Column); - uint32_t column() const { return Column; } - - bool hasOverflow() const { - return Line >= MaxLine || Column >= MaxColumn; - } - - static constexpr uint32_t MaxLine = (1 << 20) - 1; - static constexpr uint32_t MaxColumn = (1 << 12) - 1; - - private: - uint32_t Line : 20; // 0-based - // Using UTF-16 code units. - uint32_t Column : 12; // 0-based - }; - - /// The symbol range, using half-open range [Start, End). - Position Start; - Position End; - - explicit operator bool() const { return !StringRef(FileURI).empty(); } - - // The URI of the source file where a symbol occurs. - // The string must be null-terminated. - // - // We avoid using llvm::StringRef here to save memory. - // WARNING: unless you know what you are doing, it is recommended to use it - // via llvm::StringRef. - const char *FileURI = ""; -}; -inline bool operator==(const SymbolLocation::Position &L, - const SymbolLocation::Position &R) { - return std::make_tuple(L.line(), L.column()) == - std::make_tuple(R.line(), R.column()); -} -inline bool operator<(const SymbolLocation::Position &L, - const SymbolLocation::Position &R) { - return std::make_tuple(L.line(), L.column()) < - std::make_tuple(R.line(), R.column()); -} -inline bool operator==(const SymbolLocation &L, const SymbolLocation &R) { - assert(L.FileURI && R.FileURI); - return !std::strcmp(L.FileURI, R.FileURI) && - std::tie(L.Start, L.End) == std::tie(R.Start, R.End); -} -inline bool operator<(const SymbolLocation &L, const SymbolLocation &R) { - assert(L.FileURI && R.FileURI); - int Cmp = std::strcmp(L.FileURI, R.FileURI); - if (Cmp != 0) - return Cmp < 0; - return std::tie(L.Start, L.End) < std::tie(R.Start, R.End); -} -llvm::raw_ostream &operator<<(llvm::raw_ostream &, const SymbolLocation &); - // Describes the source of information about a symbol. // Mainly useful for debugging, e.g. understanding code completion reuslts. // This is a bitfield as information can be combined from several sources. Index: clang-tools-extra/trunk/clangd/index/Index.cpp =================================================================== --- clang-tools-extra/trunk/clangd/index/Index.cpp +++ clang-tools-extra/trunk/clangd/index/Index.cpp @@ -16,30 +16,6 @@ namespace clang { namespace clangd { -constexpr uint32_t SymbolLocation::Position::MaxLine; -constexpr uint32_t SymbolLocation::Position::MaxColumn; -void SymbolLocation::Position::setLine(uint32_t L) { - if (L > MaxLine) { - Line = MaxLine; - return; - } - Line = L; -} -void SymbolLocation::Position::setColumn(uint32_t Col) { - if (Col > MaxColumn) { - Column = MaxColumn; - return; - } - Column = Col; -} - -llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SymbolLocation &L) { - if (!L) - return OS << "(none)"; - return OS << L.FileURI << "[" << L.Start.line() << ":" << L.Start.column() - << "-" << L.End.line() << ":" << L.End.column() << ")"; -} - llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, SymbolOrigin O) { if (O == SymbolOrigin::Unknown) return OS << "unknown"; Index: clang-tools-extra/trunk/clangd/index/Merge.cpp =================================================================== --- clang-tools-extra/trunk/clangd/index/Merge.cpp +++ clang-tools-extra/trunk/clangd/index/Merge.cpp @@ -9,7 +9,7 @@ #include "Merge.h" #include "Logger.h" #include "Trace.h" -#include "index/Index.h" +#include "index/SymbolLocation.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringSet.h" Index: clang-tools-extra/trunk/clangd/index/Serialization.h =================================================================== --- clang-tools-extra/trunk/clangd/index/Serialization.h +++ clang-tools-extra/trunk/clangd/index/Serialization.h @@ -23,6 +23,7 @@ #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_RIFF_H #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_RIFF_H + #include "Headers.h" #include "Index.h" #include "llvm/Support/Error.h" Index: clang-tools-extra/trunk/clangd/index/Serialization.cpp =================================================================== --- clang-tools-extra/trunk/clangd/index/Serialization.cpp +++ clang-tools-extra/trunk/clangd/index/Serialization.cpp @@ -7,9 +7,9 @@ //===----------------------------------------------------------------------===// #include "Serialization.h" -#include "Index.h" #include "Logger.h" #include "RIFF.h" +#include "SymbolLocation.h" #include "Trace.h" #include "dex/Dex.h" #include "llvm/Support/Compression.h" Index: clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp =================================================================== --- clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp +++ clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp @@ -13,6 +13,7 @@ #include "CodeCompletionStrings.h" #include "Logger.h" #include "SourceCode.h" +#include "SymbolLocation.h" #include "URI.h" #include "clang/AST/Decl.h" #include "clang/AST/DeclBase.h" Index: clang-tools-extra/trunk/clangd/index/SymbolLocation.h =================================================================== --- clang-tools-extra/trunk/clangd/index/SymbolLocation.h +++ clang-tools-extra/trunk/clangd/index/SymbolLocation.h @@ -0,0 +1,88 @@ +//===--- SymbolLocation.h ----------------------------------------*- C++-*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOL_LOCATION_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOL_LOCATION_H + +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/raw_ostream.h" +#include + +namespace clang { +namespace clangd { + +struct SymbolLocation { + // Specify a position (Line, Column) of symbol. Using Line/Column allows us to + // build LSP responses without reading the file content. + // + // Position is encoded into 32 bits to save space. + // If Line/Column overflow, the value will be their maximum value. + struct Position { + Position() : Line(0), Column(0) {} + void setLine(uint32_t Line); + uint32_t line() const { return Line; } + void setColumn(uint32_t Column); + uint32_t column() const { return Column; } + + bool hasOverflow() const { + return Line >= MaxLine || Column >= MaxColumn; + } + + static constexpr uint32_t MaxLine = (1 << 20) - 1; + static constexpr uint32_t MaxColumn = (1 << 12) - 1; + + private: + uint32_t Line : 20; // 0-based + // Using UTF-16 code units. + uint32_t Column : 12; // 0-based + }; + + /// The symbol range, using half-open range [Start, End). + Position Start; + Position End; + + explicit operator bool() const { return !llvm::StringRef(FileURI).empty(); } + + // The URI of the source file where a symbol occurs. + // The string must be null-terminated. + // + // We avoid using llvm::StringRef here to save memory. + // WARNING: unless you know what you are doing, it is recommended to use it + // via llvm::StringRef. + const char *FileURI = ""; +}; + +inline bool operator==(const SymbolLocation::Position &L, + const SymbolLocation::Position &R) { + return std::make_tuple(L.line(), L.column()) == + std::make_tuple(R.line(), R.column()); +} +inline bool operator<(const SymbolLocation::Position &L, + const SymbolLocation::Position &R) { + return std::make_tuple(L.line(), L.column()) < + std::make_tuple(R.line(), R.column()); +} +inline bool operator==(const SymbolLocation &L, const SymbolLocation &R) { + assert(L.FileURI && R.FileURI); + return !std::strcmp(L.FileURI, R.FileURI) && + std::tie(L.Start, L.End) == std::tie(R.Start, R.End); +} +inline bool operator<(const SymbolLocation &L, const SymbolLocation &R) { + assert(L.FileURI && R.FileURI); + int Cmp = std::strcmp(L.FileURI, R.FileURI); + if (Cmp != 0) + return Cmp < 0; + return std::tie(L.Start, L.End) < std::tie(R.Start, R.End); +} + +llvm::raw_ostream &operator<<(llvm::raw_ostream &, const SymbolLocation &); + +} // namespace clangd +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOL_LOCATION_H Index: clang-tools-extra/trunk/clangd/index/SymbolLocation.cpp =================================================================== --- clang-tools-extra/trunk/clangd/index/SymbolLocation.cpp +++ clang-tools-extra/trunk/clangd/index/SymbolLocation.cpp @@ -0,0 +1,40 @@ +//===--- SymbolLocation.cpp --------------------------------------*- C++-*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "SymbolLocation.h" + +namespace clang { +namespace clangd { + +constexpr uint32_t SymbolLocation::Position::MaxLine; +constexpr uint32_t SymbolLocation::Position::MaxColumn; + +void SymbolLocation::Position::setLine(uint32_t L) { + if (L > MaxLine) { + Line = MaxLine; + return; + } + Line = L; +} +void SymbolLocation::Position::setColumn(uint32_t Col) { + if (Col > MaxColumn) { + Column = MaxColumn; + return; + } + Column = Col; +} + +llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SymbolLocation &L) { + if (!L) + return OS << "(none)"; + return OS << L.FileURI << "[" << L.Start.line() << ":" << L.Start.column() + << "-" << L.End.line() << ":" << L.End.column() << ")"; +} + +} // namespace clangd +} // namespace clang Index: clang-tools-extra/trunk/clangd/index/YAMLSerialization.cpp =================================================================== --- clang-tools-extra/trunk/clangd/index/YAMLSerialization.cpp +++ clang-tools-extra/trunk/clangd/index/YAMLSerialization.cpp @@ -14,6 +14,7 @@ #include "Index.h" #include "Serialization.h" +#include "SymbolLocation.h" #include "Trace.h" #include "dex/Dex.h" #include "llvm/ADT/Optional.h"