clang-tools  13.0.0git
Relation.cpp
Go to the documentation of this file.
1 //===--- Relation.cpp --------------------------------------------*- 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 #include "Relation.h"
10 
11 #include <algorithm>
12 
13 namespace clang {
14 namespace clangd {
15 
16 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const RelationKind R) {
17  switch (R) {
19  return OS << "BaseOf";
21  return OS << "OverriddenBy";
22  }
23  llvm_unreachable("Unhandled RelationKind enum.");
24 }
25 
26 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Relation &R) {
27  return OS << R.Subject << " " << R.Predicate << " " << R.Object;
28 }
29 
30 llvm::iterator_range<RelationSlab::iterator>
31 RelationSlab::lookup(const SymbolID &Subject, RelationKind Predicate) const {
32  auto IterPair = std::equal_range(Relations.begin(), Relations.end(),
33  Relation{Subject, Predicate, SymbolID{}},
34  [](const Relation &A, const Relation &B) {
35  return std::tie(A.Subject, A.Predicate) <
36  std::tie(B.Subject, B.Predicate);
37  });
38  return {IterPair.first, IterPair.second};
39 }
40 
41 RelationSlab RelationSlab::Builder::build() && {
42  // Sort in SPO order.
43  llvm::sort(Relations);
44 
45  // Remove duplicates.
46  Relations.erase(std::unique(Relations.begin(), Relations.end()),
47  Relations.end());
48 
49  return RelationSlab{std::move(Relations)};
50 }
51 
52 } // namespace clangd
53 } // namespace clang
clang::clangd::RelationKind::BaseOf
@ BaseOf
clang::clangd::Relation
Represents a relation between two symbols.
Definition: Relation.h:34
Relation.h
ns1::ns2::A
@ A
Definition: CategoricalFeature.h:3
clang::clangd::RelationSlab
Definition: Relation.h:52
clang::clangd::Relation::Object
SymbolID Object
Definition: Relation.h:37
clang::clangd::operator<<
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
Definition: CodeComplete.cpp:1995
clang::clangd::Relation::Subject
SymbolID Subject
Definition: Relation.h:35
clang::clangd::Relation::Predicate
RelationKind Predicate
Definition: Relation.h:36
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
OS
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:163
ns1::ns2::B
@ B
Definition: CategoricalFeature.h:3
clang::clangd::SymbolID
Definition: SymbolID.h:32
clang::clangd::RelationKind::OverriddenBy
@ OverriddenBy
clang::clangd::RelationSlab::lookup
llvm::iterator_range< iterator > lookup(const SymbolID &Subject, RelationKind Predicate) const
Lookup all relations matching the given subject and predicate.
Definition: Relation.cpp:31
clang::clangd::RelationKind
RelationKind
Definition: Relation.h:22