clang-tools  14.0.0git
Ref.cpp
Go to the documentation of this file.
1 //===--- Ref.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 "Ref.h"
10 #include "llvm/ADT/STLExtras.h"
11 
12 namespace clang {
13 namespace clangd {
14 
15 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, RefKind K) {
16  if (K == RefKind::Unknown)
17  return OS << "Unknown";
18  static constexpr std::array<const char *, 4> Messages = {"Decl", "Def", "Ref",
19  "Spelled"};
20  bool VisitedOnce = false;
21  for (unsigned I = 0; I < Messages.size(); ++I) {
22  if (static_cast<uint8_t>(K) & 1u << I) {
23  if (VisitedOnce)
24  OS << ", ";
25  OS << Messages[I];
26  VisitedOnce = true;
27  }
28  }
29  return OS;
30 }
31 
32 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Ref &R) {
33  return OS << R.Location << ":" << R.Kind;
34 }
35 
36 void RefSlab::Builder::insert(const SymbolID &ID, const Ref &S) {
37  Entry E = {ID, S};
38  E.Reference.Location.FileURI = UniqueStrings.save(S.Location.FileURI).data();
39  Entries.insert(std::move(E));
40 }
41 
43  std::vector<std::pair<SymbolID, llvm::ArrayRef<Ref>>> Result;
44  // We'll reuse the arena, as it only has unique strings and we need them all.
45  // We need to group refs by symbol and form contiguous arrays on the arena.
46  std::vector<std::pair<SymbolID, const Ref *>> Flat;
47  Flat.reserve(Entries.size());
48  for (const Entry &E : Entries)
49  Flat.emplace_back(E.Symbol, &E.Reference);
50  // Group by SymbolID.
51  llvm::sort(Flat, llvm::less_first());
52  std::vector<Ref> Refs;
53  // Loop over symbols, copying refs for each onto the arena.
54  for (auto I = Flat.begin(), End = Flat.end(); I != End;) {
55  SymbolID Sym = I->first;
56  Refs.clear();
57  do {
58  Refs.push_back(*I->second);
59  ++I;
60  } while (I != End && I->first == Sym);
61  llvm::sort(Refs); // By file, affects xrefs display order.
62  Result.emplace_back(Sym, llvm::ArrayRef<Ref>(Refs).copy(Arena));
63  }
64  return RefSlab(std::move(Result), std::move(Arena), Entries.size());
65 }
66 
67 } // namespace clangd
68 } // namespace clang
clang::clangd::TraceLevel::Messages
@ Messages
clang::clangd::Ref::Kind
RefKind Kind
Definition: Ref.h:90
E
const Expr * E
Definition: AvoidBindCheck.cpp:88
clang::clangd::RefKind::Unknown
@ Unknown
clang::clangd::RefSlab::RefSlab
RefSlab()=default
clang::clangd::RefKind
RefKind
Describes the kind of a cross-reference.
Definition: Ref.h:30
clang::clangd::RefSlab
An efficient structure of storing large set of symbol references in memory.
Definition: Ref.h:110
clang::clangd::Ref::Location
SymbolLocation Location
The source location where the symbol is named.
Definition: Ref.h:89
clang::clangd::SymbolLocation::FileURI
const char * FileURI
Definition: SymbolLocation.h:64
clang::clangd::RefSlab::Builder::build
RefSlab build() &&
Consumes the builder to finalize the slab.
Definition: Ref.cpp:42
clang::clangd::RefSlab::Builder::insert
void insert(const SymbolID &ID, const Ref &S)
Adds a ref to the slab. Deep copy: Strings will be owned by the slab.
Definition: Ref.cpp:36
clang::clangd::operator<<
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
Definition: CodeComplete.cpp:2016
ID
static char ID
Definition: Logger.cpp:74
clang::clangd::Ref
Represents a symbol occurrence in the source file.
Definition: Ref.h:87
Ref.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
OS
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:163
clang::clangd::SymbolID
Definition: SymbolID.h:32
K
Kind K
Definition: Rename.cpp:442