1 //===--- FileIndexRecord.cpp - Index data per file --------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "FileIndexRecord.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/AST/DeclTemplate.h"
12 #include "clang/Basic/SourceManager.h"
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/Support/Path.h"
16 using namespace clang
;
17 using namespace clang::index
;
19 ArrayRef
<DeclOccurrence
>
20 FileIndexRecord::getDeclOccurrencesSortedByOffset() const {
22 llvm::stable_sort(Decls
,
23 [](const DeclOccurrence
&A
, const DeclOccurrence
&B
) {
24 return A
.Offset
< B
.Offset
;
31 void FileIndexRecord::addDeclOccurence(SymbolRoleSet Roles
, unsigned Offset
,
33 ArrayRef
<SymbolRelation
> Relations
) {
34 assert(D
->isCanonicalDecl() &&
35 "Occurrences should be associated with their canonical decl");
37 Decls
.emplace_back(Roles
, Offset
, D
, Relations
);
40 void FileIndexRecord::addMacroOccurence(SymbolRoleSet Roles
, unsigned Offset
,
41 const IdentifierInfo
*Name
,
42 const MacroInfo
*MI
) {
44 Decls
.emplace_back(Roles
, Offset
, Name
, MI
);
47 void FileIndexRecord::removeHeaderGuardMacros() {
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();
54 Decls
.erase(It
, Decls
.end());
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();
66 if (const auto *ND
= dyn_cast
<NamedDecl
>(D
)) {
67 OS
<< ' ' << ND
->getDeclName();
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();
80 OS
<< "DECLS END ---\n";