clang  9.0.0svn
FileIndexRecord.cpp
Go to the documentation of this file.
1 //===--- FileIndexRecord.cpp - Index data per file --------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "FileIndexRecord.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/AST/DeclTemplate.h"
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/Support/Path.h"
15 
16 using namespace clang;
17 using namespace clang::index;
18 
20  const Decl *D,
21  ArrayRef<SymbolRelation> Relations) {
22  assert(D->isCanonicalDecl() &&
23  "Occurrences should be associated with their canonical decl");
24 
25  auto IsNextOccurence = [&]() -> bool {
26  if (Decls.empty())
27  return true;
28  auto &Last = Decls.back();
29  return Last.Offset < Offset;
30  };
31 
32  if (IsNextOccurence()) {
33  Decls.emplace_back(Roles, Offset, D, Relations);
34  return;
35  }
36 
37  DeclOccurrence NewInfo(Roles, Offset, D, Relations);
38  // We keep Decls in order as we need to access them in this order in all cases.
39  auto It = llvm::upper_bound(Decls, NewInfo);
40  Decls.insert(It, std::move(NewInfo));
41 }
42 
43 void FileIndexRecord::print(llvm::raw_ostream &OS) const {
44  OS << "DECLS BEGIN ---\n";
45  for (auto &DclInfo : Decls) {
46  const Decl *D = DclInfo.Dcl;
48  SourceLocation Loc = SM.getFileLoc(D->getLocation());
49  PresumedLoc PLoc = SM.getPresumedLoc(Loc);
50  OS << llvm::sys::path::filename(PLoc.getFilename()) << ':' << PLoc.getLine()
51  << ':' << PLoc.getColumn();
52 
53  if (auto ND = dyn_cast<NamedDecl>(D)) {
54  OS << ' ' << ND->getNameAsString();
55  }
56 
57  OS << '\n';
58  }
59  OS << "DECLS END ---\n";
60 }
Defines the clang::ASTContext interface.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:88
Defines the C++ template declaration subclasses.
void print(llvm::raw_ostream &OS) const
unsigned SymbolRoleSet
Definition: IndexSymbol.h:127
unsigned Offset
Definition: Format.cpp:1713
bool isCanonicalDecl() const
Whether this particular Decl is a canonical one.
Definition: DeclBase.h:883
unsigned getLine() const
Return the presumed line number of this location.
Represents an unpacked "presumed" location which can be presented to the user.
const SourceManager & SM
Definition: Format.cpp:1572
const char * getFilename() const
Return the presumed filename of this location.
unsigned getColumn() const
Return the presumed column number of this location.
Encodes a location in the source.
ASTContext & getASTContext() const LLVM_READONLY
Definition: DeclBase.cpp:376
SourceLocation getFileLoc(SourceLocation Loc) const
Given Loc, if it is a macro location return the expansion location or the spelling location...
void addDeclOccurence(SymbolRoleSet Roles, unsigned Offset, const Decl *D, ArrayRef< SymbolRelation > Relations)
Adds an occurrence of the canonical declaration D at the supplied Offset.
Dataflow Directional Tag Classes.
PresumedLoc getPresumedLoc(SourceLocation Loc, bool UseLineDirectives=true) const
Returns the "presumed" location of a SourceLocation specifies.
SourceManager & getSourceManager()
Definition: ASTContext.h:665
This class handles loading and caching of source files into memory.
SourceLocation getLocation() const
Definition: DeclBase.h:429