clang  14.0.0git
FileIndexRecord.cpp
Go to the documentation of this file.
1 //===--- FileIndexRecord.cpp - Index data per file --------------*- 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 "FileIndexRecord.h"
10 #include "clang/AST/ASTContext.h"
11 #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 
21  if (!IsSorted) {
22  llvm::stable_sort(Decls,
23  [](const DeclOccurrence &A, const DeclOccurrence &B) {
24  return A.Offset < B.Offset;
25  });
26  IsSorted = true;
27  }
28  return Decls;
29 }
30 
32  const Decl *D,
33  ArrayRef<SymbolRelation> Relations) {
34  assert(D->isCanonicalDecl() &&
35  "Occurrences should be associated with their canonical decl");
36  IsSorted = false;
37  Decls.emplace_back(Roles, Offset, D, Relations);
38 }
39 
41  const IdentifierInfo *Name,
42  const MacroInfo *MI) {
43  IsSorted = false;
44  Decls.emplace_back(Roles, Offset, Name, MI);
45 }
46 
48  auto It =
49  std::remove_if(Decls.begin(), Decls.end(), [](const DeclOccurrence &D) {
50  if (const auto *MI = D.DeclOrMacro.dyn_cast<const MacroInfo *>())
51  return MI->isUsedForHeaderGuard();
52  return false;
53  });
54  Decls.erase(It, Decls.end());
55 }
56 
57 void FileIndexRecord::print(llvm::raw_ostream &OS, SourceManager &SM) const {
58  OS << "DECLS BEGIN ---\n";
59  for (auto &DclInfo : Decls) {
60  if (const auto *D = DclInfo.DeclOrMacro.dyn_cast<const Decl *>()) {
61  SourceLocation Loc = SM.getFileLoc(D->getLocation());
62  PresumedLoc PLoc = SM.getPresumedLoc(Loc);
63  OS << llvm::sys::path::filename(PLoc.getFilename()) << ':'
64  << PLoc.getLine() << ':' << PLoc.getColumn();
65 
66  if (const auto *ND = dyn_cast<NamedDecl>(D)) {
67  OS << ' ' << ND->getDeclName();
68  }
69  } else {
70  const auto *MI = DclInfo.DeclOrMacro.get<const MacroInfo *>();
71  SourceLocation Loc = SM.getFileLoc(MI->getDefinitionLoc());
72  PresumedLoc PLoc = SM.getPresumedLoc(Loc);
73  OS << llvm::sys::path::filename(PLoc.getFilename()) << ':'
74  << PLoc.getLine() << ':' << PLoc.getColumn();
75  OS << ' ' << DclInfo.MacroName->getName();
76  }
77 
78  OS << '\n';
79  }
80  OS << "DECLS END ---\n";
81 }
clang::index::DeclOccurrence
Definition: DeclOccurrence.h:23
clang::PresumedLoc::getLine
unsigned getLine() const
Return the presumed line number of this location.
Definition: SourceLocation.h:339
clang::index::FileIndexRecord::removeHeaderGuardMacros
void removeHeaderGuardMacros()
Remove any macro occurrences for header guards.
Definition: FileIndexRecord.cpp:47
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::index::FileIndexRecord::addDeclOccurence
void addDeclOccurence(SymbolRoleSet Roles, unsigned Offset, const Decl *D, ArrayRef< SymbolRelation > Relations)
Adds an occurrence of the canonical declaration D at the supplied Offset.
Definition: FileIndexRecord.cpp:31
FileIndexRecord.h
clang::MacroInfo
Encapsulates the data about a macro definition (e.g.
Definition: MacroInfo.h:39
SourceManager.h
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:626
Offset
unsigned Offset
Definition: Format.cpp:2335
DeclTemplate.h
clang::PresumedLoc::getFilename
const char * getFilename() const
Return the presumed filename of this location.
Definition: SourceLocation.h:326
clang::index::FileIndexRecord::print
void print(llvm::raw_ostream &OS, SourceManager &SM) const
Definition: FileIndexRecord.cpp:57
ASTContext.h
clang::PresumedLoc::getColumn
unsigned getColumn() const
Return the presumed column number of this location.
Definition: SourceLocation.h:347
clang::index::FileIndexRecord::addMacroOccurence
void addMacroOccurence(SymbolRoleSet Roles, unsigned Offset, const IdentifierInfo *Name, const MacroInfo *MI)
Adds an occurrence of the given macro at the supplied Offset.
Definition: FileIndexRecord.cpp:40
clang::index
Definition: CommentToXML.h:22
clang::index::DeclOccurrence::Offset
unsigned Offset
Definition: DeclOccurrence.h:25
llvm::ArrayRef
Definition: LLVM.h:34
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:89
clang::IdentifierInfo
One of these records is kept for each identifier that is lexed.
Definition: IdentifierTable.h:84
clang::index::FileIndexRecord::getDeclOccurrencesSortedByOffset
ArrayRef< DeclOccurrence > getDeclOccurrencesSortedByOffset() const
Definition: FileIndexRecord.cpp:20
clang
Definition: CalledOnceCheck.h:17
unsigned
clang::PresumedLoc
Represents an unpacked "presumed" location which can be presented to the user.
Definition: SourceLocation.h:304
SM
#define SM(sm)
Definition: Cuda.cpp:78
clang::Decl::isCanonicalDecl
bool isCanonicalDecl() const
Whether this particular Decl is a canonical one.
Definition: DeclBase.h:907