xref: /freebsd/contrib/llvm-project/clang/lib/Index/FileIndexRecord.cpp (revision e64bea71c21eb42e97aa615188ba91f6cce0d36d)
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 
14 using namespace clang;
15 using namespace clang::index;
16 
17 ArrayRef<DeclOccurrence>
18 FileIndexRecord::getDeclOccurrencesSortedByOffset() const {
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 
29 void 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(Roles, Offset, D, Relations);
36 }
37 
38 void FileIndexRecord::addMacroOccurence(SymbolRoleSet Roles, unsigned Offset,
39                                         const IdentifierInfo *Name,
40                                         const MacroInfo *MI) {
41   IsSorted = false;
42   Decls.emplace_back(Roles, Offset, Name, MI);
43 }
44 
45 void FileIndexRecord::removeHeaderGuardMacros() {
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 
53 void 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 }
78