xref: /freebsd/contrib/llvm-project/clang/lib/Index/FileIndexRecord.cpp (revision 6be3386466ab79a84b48429ae66244f21526d3df)
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 
20 void FileIndexRecord::addDeclOccurence(SymbolRoleSet Roles, unsigned Offset,
21                                        const Decl *D,
22                                        ArrayRef<SymbolRelation> Relations) {
23   assert(D->isCanonicalDecl() &&
24          "Occurrences should be associated with their canonical decl");
25 
26   auto IsNextOccurence = [&]() -> bool {
27     if (Decls.empty())
28       return true;
29     auto &Last = Decls.back();
30     return Last.Offset < Offset;
31   };
32 
33   if (IsNextOccurence()) {
34     Decls.emplace_back(Roles, Offset, D, Relations);
35     return;
36   }
37 
38   DeclOccurrence NewInfo(Roles, Offset, D, Relations);
39   // We keep Decls in order as we need to access them in this order in all cases.
40   auto It = llvm::upper_bound(Decls, NewInfo);
41   Decls.insert(It, std::move(NewInfo));
42 }
43 
44 void FileIndexRecord::print(llvm::raw_ostream &OS) const {
45   OS << "DECLS BEGIN ---\n";
46   for (auto &DclInfo : Decls) {
47     const Decl *D = DclInfo.Dcl;
48     SourceManager &SM = D->getASTContext().getSourceManager();
49     SourceLocation Loc = SM.getFileLoc(D->getLocation());
50     PresumedLoc PLoc = SM.getPresumedLoc(Loc);
51     OS << llvm::sys::path::filename(PLoc.getFilename()) << ':' << PLoc.getLine()
52        << ':' << PLoc.getColumn();
53 
54     if (auto ND = dyn_cast<NamedDecl>(D)) {
55       OS << ' ' << ND->getNameAsString();
56     }
57 
58     OS << '\n';
59   }
60   OS << "DECLS END ---\n";
61 }
62