clang-tools 10.0.0

Serialization.h
Go to the documentation of this file.
1 //===--- Serialization.h - Binary serialization of index data ----*- 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 // This file provides serialization of indexed symbols and other data.
10 //
11 // It writes sections:
12 // - metadata such as version info
13 // - a string table (which is compressed)
14 // - lists of encoded symbols
15 //
16 // The format has a simple versioning scheme: the format version number is
17 // written in the file and non-current versions are rejected when reading.
18 //
19 // Human-readable YAML serialization is also supported, and recommended for
20 // debugging and experiments only.
21 //
22 //===----------------------------------------------------------------------===//
23
24 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_RIFF_H
25 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_RIFF_H
26
27 #include "Headers.h"
28 #include "Index.h"
29 #include "index/Symbol.h"
30 #include "clang/Tooling/CompilationDatabase.h"
31 #include "llvm/Support/Error.h"
32
33 namespace clang {
34 namespace clangd {
35
36 enum class IndexFileFormat {
37 RIFF, // Versioned binary format, suitable for production use.
38 YAML, // Human-readable format, suitable for experiments and debugging.
39 };
40
41 // Holds the contents of an index file that was read.
42 struct IndexFileIn {
43 llvm::Optional<SymbolSlab> Symbols;
44 llvm::Optional<RefSlab> Refs;
45 llvm::Optional<RelationSlab> Relations;
46 // Keys are URIs of the source files.
47 llvm::Optional<IncludeGraph> Sources;
48 // This contains only the Directory and CommandLine.
49 llvm::Optional<tooling::CompileCommand> Cmd;
50 };
51 // Parse an index file. The input must be a RIFF or YAML file.
52 llvm::Expected<IndexFileIn> readIndexFile(llvm::StringRef);
53
54 // Specifies the contents of an index file to be written.
55 struct IndexFileOut {
56 const SymbolSlab *Symbols = nullptr;
57 const RefSlab *Refs = nullptr;
58 const RelationSlab *Relations = nullptr;
59 // Keys are URIs of the source files.
60 const IncludeGraph *Sources = nullptr;
61 // TODO: Support serializing Dex posting lists.
62 IndexFileFormat Format = IndexFileFormat::RIFF;
63 const tooling::CompileCommand *Cmd = nullptr;
64
65 IndexFileOut() = default;
66 IndexFileOut(const IndexFileIn &I)
67 : Symbols(I.Symbols ? I.Symbols.getPointer() : nullptr),
68 Refs(I.Refs ? I.Refs.getPointer() : nullptr),
69 Relations(I.Relations ? I.Relations.getPointer() : nullptr),
70 Sources(I.Sources ? I.Sources.getPointer() : nullptr),
71 Cmd(I.Cmd ? I.Cmd.getPointer() : nullptr) {}
72 };
73 // Serializes an index file.
74 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const IndexFileOut &O);
75
76 // Convert a single symbol to YAML, a nice debug representation.
77 std::string toYAML(const Symbol &);
78 std::string toYAML(const std::pair<SymbolID, ArrayRef<Ref>> &);
79 std::string toYAML(const Relation &);
80
81 // Build an in-memory static index from an index file.
82 // The size should be relatively small, so data can be managed in memory.
83 std::unique_ptr<SymbolIndex> loadIndex(llvm::StringRef Filename,
84 bool UseDex = true);
85
86 } // namespace clangd
87 } // namespace clang
88
89 #endif
clang::clangd::IndexFileIn::Symbols
llvm::Optional< SymbolSlab > Symbols
Definition: Serialization.h:43
clang::clangd::SymbolSlab
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:177
clang::clangd::IndexFileIn::Cmd
llvm::Optional< tooling::CompileCommand > Cmd
Definition: Serialization.h:49
clang::clangd::Relation
Represents a relation between two symbols.
Definition: Relation.h:29
clang::clangd::loadIndex
std::unique_ptr< SymbolIndex > loadIndex(llvm::StringRef SymbolFilename, bool UseDex)
Definition: Serialization.cpp:663
clang::clangd::RefSlab
An efficient structure of storing large set of symbol references in memory.
Definition: Ref.h:69
Index.h
clang::clangd::RelationSlab
Definition: Relation.h:45
Symbol.h
clang::clangd::IndexFileFormat::YAML
clang::clangd::IndexFileFormat
IndexFileFormat
Definition: Serialization.h:36
clang::clangd::IndexFileFormat::RIFF
clang::clangd::readIndexFile
llvm::Expected< IndexFileIn > readIndexFile(llvm::StringRef Data)
Definition: Serialization.cpp:652
clang::clangd::toYAML
std::string toYAML(const Symbol &)
Definition: YAMLSerialization.cpp:388
clang::clangd::IndexFileIn::Relations
llvm::Optional< RelationSlab > Relations
Definition: Serialization.h:45
Headers.h
clang::clangd::IndexFileIn::Sources
llvm::Optional< IncludeGraph > Sources
Definition: Serialization.h:47
clang::clangd::IndexFileIn
Definition: Serialization.h:42
clang::clangd::IncludeGraph
llvm::StringMap< IncludeGraphNode > IncludeGraph
Definition: Headers.h:82
Filename
std::string Filename
Filename as a string.
Definition: IncludeOrderCheck.cpp:39
Symbols
SymbolSlab Symbols
Definition: SymbolCollectorTests.cpp:295
clang::clangd::IndexFileOut::IndexFileOut
IndexFileOut(const IndexFileIn &I)
Definition: Serialization.h:66
clang::clangd::IndexFileIn::Refs
llvm::Optional< RefSlab > Refs
Definition: Serialization.h:44
clang::clangd::Symbol
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::IndexFileOut
Definition: Serialization.h:55
Refs
RefSlab Refs
Definition: SymbolCollectorTests.cpp:296
clang::clangd::operator<<
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
Definition: CodeComplete.cpp:1850
clang::clangd::SymbolID
Definition: SymbolID.h:31