blob: 58787d14858f6f03c84a17700ca3842575ed7c7a [file] [log] [blame]
Argiris Kirtzidis43d4f2e2009-07-29 23:38:21 +00001//===--- Indexer.cpp - IndexProvider implementation -------------*- 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// IndexProvider implementation.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Index/Indexer.h"
15#include "clang/Index/Program.h"
16#include "clang/Index/Entity.h"
17#include "clang/Index/Handlers.h"
18#include "clang/Index/TranslationUnit.h"
19using namespace clang;
20using namespace idx;
21
22namespace {
23
24class EntityIndexer : public EntityHandler {
25 TranslationUnit *TU;
26 Indexer::MapTy ⤅
27
28public:
29 EntityIndexer(TranslationUnit *tu, Indexer::MapTy &map) : TU(tu), Map(map) { }
30
31 virtual void HandleEntity(Entity Ent) {
32 if (Ent.isInternalToTU())
33 return;
34 Map[Ent].insert(TU);
35 }
36};
37
38} // anonymous namespace
39
40void Indexer::IndexAST(TranslationUnit *TU) {
41 EntityIndexer Idx(TU, Map);
42 Prog.FindEntities(TU->getASTContext(), &Idx);
43}
44
45void Indexer::GetTranslationUnitsFor(Entity Ent,
46 TranslationUnitHandler *Handler) {
47 assert(Ent.isValid() && "Expected valid Entity");
48 assert(!Ent.isInternalToTU() &&
49 "Expected an Entity visible outside of its translation unit");
50
51 MapTy::iterator I = Map.find(Ent);
52 if (I == Map.end())
53 return;
54
55 TUSetTy &Set = I->second;
56 for (TUSetTy::iterator I = Set.begin(), E = Set.end(); I != E; ++I)
57 Handler->Handle(*I);
58}
59
60static Indexer::TUSetTy EmptySet;
61
62Indexer::translation_unit_iterator
63Indexer::translation_units_begin(Entity Ent) const {
64 MapTy::iterator I = Map.find(Ent);
65 if (I == Map.end())
66 return EmptySet.begin();
67
68 return I->second.begin();
69}
70
71Indexer::translation_unit_iterator
72Indexer::translation_units_end(Entity Ent) const {
73 MapTy::iterator I = Map.find(Ent);
74 if (I == Map.end())
75 return EmptySet.end();
76
77 return I->second.end();
78}
79
80bool Indexer::translation_units_empty(Entity Ent) const {
81 MapTy::iterator I = Map.find(Ent);
82 if (I == Map.end())
83 return true;
84
85 return I->second.begin() == I->second.end();
86}