Argiris Kirtzidis | 43d4f2e | 2009-07-29 23:38:21 +0000 | [diff] [blame] | 1 | //===--- 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" |
| 19 | using namespace clang; |
| 20 | using namespace idx; |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | class EntityIndexer : public EntityHandler { |
| 25 | TranslationUnit *TU; |
| 26 | Indexer::MapTy ⤅ |
| 27 | |
| 28 | public: |
| 29 | EntityIndexer(TranslationUnit *tu, Indexer::MapTy &map) : TU(tu), Map(map) { } |
| 30 | |
Argiris Kirtzidis | 21f89b9 | 2009-07-29 23:38:35 +0000 | [diff] [blame] | 31 | virtual void Handle(Entity Ent) { |
Argiris Kirtzidis | 43d4f2e | 2009-07-29 23:38:21 +0000 | [diff] [blame] | 32 | if (Ent.isInternalToTU()) |
| 33 | return; |
| 34 | Map[Ent].insert(TU); |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | } // anonymous namespace |
| 39 | |
| 40 | void Indexer::IndexAST(TranslationUnit *TU) { |
Argiris Kirtzidis | 8f2a722 | 2009-07-29 23:38:51 +0000 | [diff] [blame] | 41 | assert(TU && "Passed null TranslationUnit"); |
Argiris Kirtzidis | 43d4f2e | 2009-07-29 23:38:21 +0000 | [diff] [blame] | 42 | EntityIndexer Idx(TU, Map); |
Argiris Kirtzidis | c13d3b6 | 2009-07-29 23:38:45 +0000 | [diff] [blame] | 43 | Prog.FindEntities(TU->getASTContext(), Idx); |
Argiris Kirtzidis | 43d4f2e | 2009-07-29 23:38:21 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void Indexer::GetTranslationUnitsFor(Entity Ent, |
Argiris Kirtzidis | c13d3b6 | 2009-07-29 23:38:45 +0000 | [diff] [blame] | 47 | TranslationUnitHandler &Handler) { |
Argiris Kirtzidis | 43d4f2e | 2009-07-29 23:38:21 +0000 | [diff] [blame] | 48 | assert(Ent.isValid() && "Expected valid Entity"); |
| 49 | assert(!Ent.isInternalToTU() && |
| 50 | "Expected an Entity visible outside of its translation unit"); |
| 51 | |
| 52 | MapTy::iterator I = Map.find(Ent); |
| 53 | if (I == Map.end()) |
| 54 | return; |
| 55 | |
| 56 | TUSetTy &Set = I->second; |
| 57 | for (TUSetTy::iterator I = Set.begin(), E = Set.end(); I != E; ++I) |
Argiris Kirtzidis | c13d3b6 | 2009-07-29 23:38:45 +0000 | [diff] [blame] | 58 | Handler.Handle(*I); |
Argiris Kirtzidis | 43d4f2e | 2009-07-29 23:38:21 +0000 | [diff] [blame] | 59 | } |