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