FileIndexRecord.cpp revision 1.1.1.3
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
17using namespace clang;
18using namespace clang::index;
19
20ArrayRef<DeclOccurrence>
21FileIndexRecord::getDeclOccurrencesSortedByOffset() const {
22  if (!IsSorted) {
23    llvm::stable_sort(Decls,
24                      [](const DeclOccurrence &A, const DeclOccurrence &B) {
25                        return A.Offset < B.Offset;
26                      });
27    IsSorted = true;
28  }
29  return Decls;
30}
31
32void FileIndexRecord::addDeclOccurence(SymbolRoleSet Roles, unsigned Offset,
33                                       const Decl *D,
34                                       ArrayRef<SymbolRelation> Relations) {
35  assert(D->isCanonicalDecl() &&
36         "Occurrences should be associated with their canonical decl");
37  IsSorted = false;
38  Decls.emplace_back(Roles, Offset, D, Relations);
39}
40
41void FileIndexRecord::addMacroOccurence(SymbolRoleSet Roles, unsigned Offset,
42                                        const IdentifierInfo *Name,
43                                        const MacroInfo *MI) {
44  IsSorted = false;
45  Decls.emplace_back(Roles, Offset, Name, MI);
46}
47
48void FileIndexRecord::removeHeaderGuardMacros() {
49  auto It =
50      std::remove_if(Decls.begin(), Decls.end(), [](const DeclOccurrence &D) {
51        if (const auto *MI = D.DeclOrMacro.dyn_cast<const MacroInfo *>())
52          return MI->isUsedForHeaderGuard();
53        return false;
54      });
55  Decls.erase(It, Decls.end());
56}
57
58void FileIndexRecord::print(llvm::raw_ostream &OS, SourceManager &SM) const {
59  OS << "DECLS BEGIN ---\n";
60  for (auto &DclInfo : Decls) {
61    if (const auto *D = DclInfo.DeclOrMacro.dyn_cast<const Decl *>()) {
62      SourceLocation Loc = SM.getFileLoc(D->getLocation());
63      PresumedLoc PLoc = SM.getPresumedLoc(Loc);
64      OS << llvm::sys::path::filename(PLoc.getFilename()) << ':'
65         << PLoc.getLine() << ':' << PLoc.getColumn();
66
67      if (const auto *ND = dyn_cast<NamedDecl>(D)) {
68        OS << ' ' << ND->getDeclName();
69      }
70    } else {
71      const auto *MI = DclInfo.DeclOrMacro.get<const MacroInfo *>();
72      SourceLocation Loc = SM.getFileLoc(MI->getDefinitionLoc());
73      PresumedLoc PLoc = SM.getPresumedLoc(Loc);
74      OS << llvm::sys::path::filename(PLoc.getFilename()) << ':'
75         << PLoc.getLine() << ':' << PLoc.getColumn();
76      OS << ' ' << DclInfo.MacroName->getName();
77    }
78
79    OS << '\n';
80  }
81  OS << "DECLS END ---\n";
82}
83