1e5dd7070Spatrick //===--- FileIndexRecord.cpp - Index data per file --------------*- C++ -*-===//
2e5dd7070Spatrick //
3*12c85518Srobert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*12c85518Srobert // See https://llvm.org/LICENSE.txt for license information.
5*12c85518Srobert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick 
9e5dd7070Spatrick #include "FileIndexRecord.h"
10e5dd7070Spatrick #include "clang/AST/ASTContext.h"
11e5dd7070Spatrick #include "clang/AST/DeclTemplate.h"
12ec727ea7Spatrick #include "clang/Basic/SourceManager.h"
13e5dd7070Spatrick #include "llvm/ADT/SmallString.h"
14e5dd7070Spatrick #include "llvm/Support/Path.h"
15e5dd7070Spatrick 
16e5dd7070Spatrick using namespace clang;
17e5dd7070Spatrick using namespace clang::index;
18e5dd7070Spatrick 
19a9ac8606Spatrick ArrayRef<DeclOccurrence>
getDeclOccurrencesSortedByOffset() const20a9ac8606Spatrick FileIndexRecord::getDeclOccurrencesSortedByOffset() const {
21a9ac8606Spatrick   if (!IsSorted) {
22a9ac8606Spatrick     llvm::stable_sort(Decls,
23a9ac8606Spatrick                       [](const DeclOccurrence &A, const DeclOccurrence &B) {
24a9ac8606Spatrick                         return A.Offset < B.Offset;
25a9ac8606Spatrick                       });
26a9ac8606Spatrick     IsSorted = true;
27a9ac8606Spatrick   }
28a9ac8606Spatrick   return Decls;
29a9ac8606Spatrick }
30a9ac8606Spatrick 
addDeclOccurence(SymbolRoleSet Roles,unsigned Offset,const Decl * D,ArrayRef<SymbolRelation> Relations)31e5dd7070Spatrick void FileIndexRecord::addDeclOccurence(SymbolRoleSet Roles, unsigned Offset,
32e5dd7070Spatrick                                        const Decl *D,
33e5dd7070Spatrick                                        ArrayRef<SymbolRelation> Relations) {
34e5dd7070Spatrick   assert(D->isCanonicalDecl() &&
35e5dd7070Spatrick          "Occurrences should be associated with their canonical decl");
36a9ac8606Spatrick   IsSorted = false;
37e5dd7070Spatrick   Decls.emplace_back(Roles, Offset, D, Relations);
38e5dd7070Spatrick }
39e5dd7070Spatrick 
addMacroOccurence(SymbolRoleSet Roles,unsigned Offset,const IdentifierInfo * Name,const MacroInfo * MI)40a9ac8606Spatrick void FileIndexRecord::addMacroOccurence(SymbolRoleSet Roles, unsigned Offset,
41a9ac8606Spatrick                                         const IdentifierInfo *Name,
42a9ac8606Spatrick                                         const MacroInfo *MI) {
43a9ac8606Spatrick   IsSorted = false;
44a9ac8606Spatrick   Decls.emplace_back(Roles, Offset, Name, MI);
45e5dd7070Spatrick }
46e5dd7070Spatrick 
removeHeaderGuardMacros()47a9ac8606Spatrick void FileIndexRecord::removeHeaderGuardMacros() {
48*12c85518Srobert   llvm::erase_if(Decls, [](const DeclOccurrence &D) {
49a9ac8606Spatrick     if (const auto *MI = D.DeclOrMacro.dyn_cast<const MacroInfo *>())
50a9ac8606Spatrick       return MI->isUsedForHeaderGuard();
51a9ac8606Spatrick     return false;
52a9ac8606Spatrick   });
53a9ac8606Spatrick }
54a9ac8606Spatrick 
print(llvm::raw_ostream & OS,SourceManager & SM) const55a9ac8606Spatrick void FileIndexRecord::print(llvm::raw_ostream &OS, SourceManager &SM) const {
56e5dd7070Spatrick   OS << "DECLS BEGIN ---\n";
57e5dd7070Spatrick   for (auto &DclInfo : Decls) {
58a9ac8606Spatrick     if (const auto *D = DclInfo.DeclOrMacro.dyn_cast<const Decl *>()) {
59e5dd7070Spatrick       SourceLocation Loc = SM.getFileLoc(D->getLocation());
60e5dd7070Spatrick       PresumedLoc PLoc = SM.getPresumedLoc(Loc);
61a9ac8606Spatrick       OS << llvm::sys::path::filename(PLoc.getFilename()) << ':'
62a9ac8606Spatrick          << PLoc.getLine() << ':' << PLoc.getColumn();
63e5dd7070Spatrick 
64a9ac8606Spatrick       if (const auto *ND = dyn_cast<NamedDecl>(D)) {
65a9ac8606Spatrick         OS << ' ' << ND->getDeclName();
66a9ac8606Spatrick       }
67a9ac8606Spatrick     } else {
68a9ac8606Spatrick       const auto *MI = DclInfo.DeclOrMacro.get<const MacroInfo *>();
69a9ac8606Spatrick       SourceLocation Loc = SM.getFileLoc(MI->getDefinitionLoc());
70a9ac8606Spatrick       PresumedLoc PLoc = SM.getPresumedLoc(Loc);
71a9ac8606Spatrick       OS << llvm::sys::path::filename(PLoc.getFilename()) << ':'
72a9ac8606Spatrick          << PLoc.getLine() << ':' << PLoc.getColumn();
73a9ac8606Spatrick       OS << ' ' << DclInfo.MacroName->getName();
74e5dd7070Spatrick     }
75e5dd7070Spatrick 
76e5dd7070Spatrick     OS << '\n';
77e5dd7070Spatrick   }
78e5dd7070Spatrick   OS << "DECLS END ---\n";
79e5dd7070Spatrick }
80