150
|
1 //===--- FileIndexRecord.cpp - Index data per file --------------*- C++ -*-===//
|
|
2 //
|
236
|
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
|
150
|
6 //
|
|
7 //===----------------------------------------------------------------------===//
|
|
8
|
|
9 #include "FileIndexRecord.h"
|
|
10 #include "clang/AST/ASTContext.h"
|
|
11 #include "clang/AST/DeclTemplate.h"
|
173
|
12 #include "clang/Basic/SourceManager.h"
|
150
|
13 #include "llvm/ADT/SmallString.h"
|
|
14 #include "llvm/Support/Path.h"
|
|
15
|
|
16 using namespace clang;
|
|
17 using namespace clang::index;
|
|
18
|
223
|
19 ArrayRef<DeclOccurrence>
|
|
20 FileIndexRecord::getDeclOccurrencesSortedByOffset() const {
|
|
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;
|
221
|
27 }
|
223
|
28 return Decls;
|
221
|
29 }
|
|
30
|
150
|
31 void FileIndexRecord::addDeclOccurence(SymbolRoleSet Roles, unsigned Offset,
|
|
32 const Decl *D,
|
|
33 ArrayRef<SymbolRelation> Relations) {
|
|
34 assert(D->isCanonicalDecl() &&
|
|
35 "Occurrences should be associated with their canonical decl");
|
223
|
36 IsSorted = false;
|
|
37 Decls.emplace_back(Roles, Offset, D, Relations);
|
221
|
38 }
|
150
|
39
|
221
|
40 void FileIndexRecord::addMacroOccurence(SymbolRoleSet Roles, unsigned Offset,
|
|
41 const IdentifierInfo *Name,
|
|
42 const MacroInfo *MI) {
|
223
|
43 IsSorted = false;
|
|
44 Decls.emplace_back(Roles, Offset, Name, MI);
|
221
|
45 }
|
150
|
46
|
221
|
47 void FileIndexRecord::removeHeaderGuardMacros() {
|
236
|
48 llvm::erase_if(Decls, [](const DeclOccurrence &D) {
|
|
49 if (const auto *MI = D.DeclOrMacro.dyn_cast<const MacroInfo *>())
|
|
50 return MI->isUsedForHeaderGuard();
|
|
51 return false;
|
|
52 });
|
150
|
53 }
|
|
54
|
221
|
55 void FileIndexRecord::print(llvm::raw_ostream &OS, SourceManager &SM) const {
|
150
|
56 OS << "DECLS BEGIN ---\n";
|
|
57 for (auto &DclInfo : Decls) {
|
221
|
58 if (const auto *D = DclInfo.DeclOrMacro.dyn_cast<const Decl *>()) {
|
|
59 SourceLocation Loc = SM.getFileLoc(D->getLocation());
|
|
60 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
|
|
61 OS << llvm::sys::path::filename(PLoc.getFilename()) << ':'
|
|
62 << PLoc.getLine() << ':' << PLoc.getColumn();
|
150
|
63
|
221
|
64 if (const auto *ND = dyn_cast<NamedDecl>(D)) {
|
|
65 OS << ' ' << ND->getDeclName();
|
|
66 }
|
|
67 } else {
|
|
68 const auto *MI = DclInfo.DeclOrMacro.get<const MacroInfo *>();
|
|
69 SourceLocation Loc = SM.getFileLoc(MI->getDefinitionLoc());
|
|
70 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
|
|
71 OS << llvm::sys::path::filename(PLoc.getFilename()) << ':'
|
|
72 << PLoc.getLine() << ':' << PLoc.getColumn();
|
|
73 OS << ' ' << DclInfo.MacroName->getName();
|
150
|
74 }
|
|
75
|
|
76 OS << '\n';
|
|
77 }
|
|
78 OS << "DECLS END ---\n";
|
|
79 }
|