xref: /netbsd-src/external/apache2/llvm/dist/clang/lib/Index/FileIndexRecord.cpp (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
1 //===--- FileIndexRecord.cpp - Index data per file --------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "FileIndexRecord.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/AST/DeclTemplate.h"
13 #include "clang/Basic/SourceManager.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/Support/Path.h"
16 
17 using namespace clang;
18 using namespace clang::index;
19 
addOccurrence(std::vector<DeclOccurrence> & Decls,DeclOccurrence Info)20 static void addOccurrence(std::vector<DeclOccurrence> &Decls,
21                           DeclOccurrence Info) {
22   auto IsNextOccurence = [&]() -> bool {
23     if (Decls.empty())
24       return true;
25     auto &Last = Decls.back();
26     return Last.Offset < Info.Offset;
27   };
28 
29   if (IsNextOccurence()) {
30     Decls.push_back(std::move(Info));
31     return;
32   }
33 
34   // We keep Decls in order as we need to access them in this order in all cases.
35   auto It = llvm::upper_bound(Decls, Info);
36   Decls.insert(It, std::move(Info));
37 }
38 
addDeclOccurence(SymbolRoleSet Roles,unsigned Offset,const Decl * D,ArrayRef<SymbolRelation> Relations)39 void FileIndexRecord::addDeclOccurence(SymbolRoleSet Roles, unsigned Offset,
40                                        const Decl *D,
41                                        ArrayRef<SymbolRelation> Relations) {
42   assert(D->isCanonicalDecl() &&
43          "Occurrences should be associated with their canonical decl");
44   addOccurrence(Decls, DeclOccurrence(Roles, Offset, D, Relations));
45 }
46 
addMacroOccurence(SymbolRoleSet Roles,unsigned Offset,const IdentifierInfo * Name,const MacroInfo * MI)47 void FileIndexRecord::addMacroOccurence(SymbolRoleSet Roles, unsigned Offset,
48                                         const IdentifierInfo *Name,
49                                         const MacroInfo *MI) {
50   addOccurrence(Decls, DeclOccurrence(Roles, Offset, Name, MI));
51 }
52 
removeHeaderGuardMacros()53 void FileIndexRecord::removeHeaderGuardMacros() {
54   auto It =
55       std::remove_if(Decls.begin(), Decls.end(), [](const DeclOccurrence &D) {
56         if (const auto *MI = D.DeclOrMacro.dyn_cast<const MacroInfo *>())
57           return MI->isUsedForHeaderGuard();
58         return false;
59       });
60   Decls.erase(It, Decls.end());
61 }
62 
print(llvm::raw_ostream & OS,SourceManager & SM) const63 void FileIndexRecord::print(llvm::raw_ostream &OS, SourceManager &SM) const {
64   OS << "DECLS BEGIN ---\n";
65   for (auto &DclInfo : Decls) {
66     if (const auto *D = DclInfo.DeclOrMacro.dyn_cast<const Decl *>()) {
67       SourceLocation Loc = SM.getFileLoc(D->getLocation());
68       PresumedLoc PLoc = SM.getPresumedLoc(Loc);
69       OS << llvm::sys::path::filename(PLoc.getFilename()) << ':'
70          << PLoc.getLine() << ':' << PLoc.getColumn();
71 
72       if (const auto *ND = dyn_cast<NamedDecl>(D)) {
73         OS << ' ' << ND->getDeclName();
74       }
75     } else {
76       const auto *MI = DclInfo.DeclOrMacro.get<const MacroInfo *>();
77       SourceLocation Loc = SM.getFileLoc(MI->getDefinitionLoc());
78       PresumedLoc PLoc = SM.getPresumedLoc(Loc);
79       OS << llvm::sys::path::filename(PLoc.getFilename()) << ':'
80          << PLoc.getLine() << ':' << PLoc.getColumn();
81       OS << ' ' << DclInfo.MacroName->getName();
82     }
83 
84     OS << '\n';
85   }
86   OS << "DECLS END ---\n";
87 }
88