blob: 9a49496d74af43bbb5a198d2f149df78dd084e5e [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
Argiris Kirtzidis21f89b92009-07-29 23:38:35 +000031 virtual void Handle(Entity Ent) {
Argiris Kirtzidis43d4f2e2009-07-29 23:38:21 +000032 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);
Argiris Kirtzidisc13d3b62009-07-29 23:38:45 +000042 Prog.FindEntities(TU->getASTContext(), Idx);
Argiris Kirtzidis43d4f2e2009-07-29 23:38:21 +000043}
44
45void Indexer::GetTranslationUnitsFor(Entity Ent,
Argiris Kirtzidisc13d3b62009-07-29 23:38:45 +000046 TranslationUnitHandler &Handler) {
Argiris Kirtzidis43d4f2e2009-07-29 23:38:21 +000047 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)
Argiris Kirtzidisc13d3b62009-07-29 23:38:45 +000057 Handler.Handle(*I);
Argiris Kirtzidis43d4f2e2009-07-29 23:38:21 +000058}
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}