clang 22.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"
12#include "llvm/Support/Path.h"
13
14using namespace clang;
15using namespace clang::index;
16
19 if (!IsSorted) {
20 llvm::stable_sort(Decls,
21 [](const DeclOccurrence &A, const DeclOccurrence &B) {
22 return A.Offset < B.Offset;
23 });
24 IsSorted = true;
25 }
26 return Decls;
27}
28
30 const Decl *D,
31 ArrayRef<SymbolRelation> Relations) {
32 assert(D->isCanonicalDecl() &&
33 "Occurrences should be associated with their canonical decl");
34 IsSorted = false;
35 Decls.emplace_back(Roles, Offset, D, Relations);
36}
37
39 const IdentifierInfo *Name,
40 const MacroInfo *MI) {
41 IsSorted = false;
42 Decls.emplace_back(Roles, Offset, Name, MI);
43}
44
46 llvm::erase_if(Decls, [](const DeclOccurrence &D) {
47 if (const auto *MI = D.DeclOrMacro.dyn_cast<const MacroInfo *>())
48 return MI->isUsedForHeaderGuard();
49 return false;
50 });
51}
52
53void FileIndexRecord::print(llvm::raw_ostream &OS, SourceManager &SM) const {
54 OS << "DECLS BEGIN ---\n";
55 for (auto &DclInfo : Decls) {
56 if (const auto *D = dyn_cast<const Decl *>(DclInfo.DeclOrMacro)) {
57 SourceLocation Loc = SM.getFileLoc(D->getLocation());
58 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
59 OS << llvm::sys::path::filename(PLoc.getFilename()) << ':'
60 << PLoc.getLine() << ':' << PLoc.getColumn();
61
62 if (const auto *ND = dyn_cast<NamedDecl>(D)) {
63 OS << ' ' << ND->getDeclName();
64 }
65 } else {
66 const auto *MI = cast<const MacroInfo *>(DclInfo.DeclOrMacro);
67 SourceLocation Loc = SM.getFileLoc(MI->getDefinitionLoc());
68 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
69 OS << llvm::sys::path::filename(PLoc.getFilename()) << ':'
70 << PLoc.getLine() << ':' << PLoc.getColumn();
71 OS << ' ' << DclInfo.MacroName->getName();
72 }
73
74 OS << '\n';
75 }
76 OS << "DECLS END ---\n";
77}
Defines the C++ template declaration subclasses.
#define SM(sm)
Defines the SourceManager interface.
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
bool isCanonicalDecl() const
Whether this particular Decl is a canonical one.
Definition DeclBase.h:984
One of these records is kept for each identifier that is lexed.
Encapsulates the data about a macro definition (e.g.
Definition MacroInfo.h:39
Represents an unpacked "presumed" location which can be presented to the user.
unsigned getColumn() const
Return the presumed column number of this location.
const char * getFilename() const
Return the presumed filename of this location.
unsigned getLine() const
Return the presumed line number of this location.
Encodes a location in the source.
This class handles loading and caching of source files into memory.
void print(llvm::raw_ostream &OS, SourceManager &SM) const
ArrayRef< DeclOccurrence > getDeclOccurrencesSortedByOffset() const
void addMacroOccurence(SymbolRoleSet Roles, unsigned Offset, const IdentifierInfo *Name, const MacroInfo *MI)
Adds an occurrence of the given macro at the supplied Offset.
void addDeclOccurence(SymbolRoleSet Roles, unsigned Offset, const Decl *D, ArrayRef< SymbolRelation > Relations)
Adds an occurrence of the canonical declaration D at the supplied Offset.
void removeHeaderGuardMacros()
Remove any macro occurrences for header guards.
unsigned SymbolRoleSet
The JSON file list parser is used to communicate input to InstallAPI.
U cast(CodeGen::Address addr)
Definition Address.h:327
llvm::PointerUnion< const Decl *, const MacroInfo * > DeclOrMacro