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/DeclTemplate.h"
11#include "clang/Basic/SourceManager.h"
12#include "llvm/Support/Path.h"
13
14using namespace clang;
15using namespace clang::index;
16
17ArrayRef<DeclOccurrence>
18FileIndexRecord::getDeclOccurrencesSortedByOffset() const {
19 if (!IsSorted) {
20 llvm::stable_sort(Range&: Decls,
21 C: [](const DeclOccurrence &A, const DeclOccurrence &B) {
22 return A.Offset < B.Offset;
23 });
24 IsSorted = true;
25 }
26 return Decls;
27}
28
29void FileIndexRecord::addDeclOccurence(SymbolRoleSet Roles, unsigned Offset,
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(args&: Roles, args&: Offset, args&: D, args&: Relations);
36}
37
38void FileIndexRecord::addMacroOccurence(SymbolRoleSet Roles, unsigned Offset,
39 const IdentifierInfo *Name,
40 const MacroInfo *MI) {
41 IsSorted = false;
42 Decls.emplace_back(args&: Roles, args&: Offset, args&: Name, args&: MI);
43}
44
45void FileIndexRecord::removeHeaderGuardMacros() {
46 llvm::erase_if(C&: Decls, P: [](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 *>(Val&: DclInfo.DeclOrMacro)) {
57 SourceLocation Loc = SM.getFileLoc(Loc: D->getLocation());
58 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
59 OS << llvm::sys::path::filename(path: PLoc.getFilename()) << ':'
60 << PLoc.getLine() << ':' << PLoc.getColumn();
61
62 if (const auto *ND = dyn_cast<NamedDecl>(Val: D)) {
63 OS << ' ' << ND->getDeclName();
64 }
65 } else {
66 const auto *MI = cast<const MacroInfo *>(Val&: DclInfo.DeclOrMacro);
67 SourceLocation Loc = SM.getFileLoc(Loc: MI->getDefinitionLoc());
68 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
69 OS << llvm::sys::path::filename(path: PLoc.getFilename()) << ':'
70 << PLoc.getLine() << ':' << PLoc.getColumn();
71 OS << ' ' << DclInfo.MacroName->getName();
72 }
73
74 OS << '\n';
75 }
76 OS << "DECLS END ---\n";
77}
78

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of clang/lib/Index/FileIndexRecord.cpp