Argiris Kirtzidis | 9928abb | 2009-07-29 23:40:14 +0000 | [diff] [blame^] | 1 | //===--- Analyzer.cpp - Analysis for indexing information -------*- 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 | // This file implements the Analyzer interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Index/Analyzer.h" |
| 15 | #include "clang/Index/Entity.h" |
| 16 | #include "clang/Index/TranslationUnit.h" |
| 17 | #include "clang/Index/Handlers.h" |
| 18 | #include "clang/Index/ASTLocation.h" |
| 19 | #include "clang/Index/DeclReferenceMap.h" |
| 20 | #include "clang/Index/IndexProvider.h" |
| 21 | #include "clang/AST/Decl.h" |
| 22 | #include "llvm/Support/Compiler.h" |
| 23 | using namespace clang; |
| 24 | using namespace idx; |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | // DeclEntityAnalyzer Implementation |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
| 32 | class VISIBILITY_HIDDEN DeclEntityAnalyzer : public TranslationUnitHandler { |
| 33 | Entity Ent; |
| 34 | TULocationHandler &TULocHandler; |
| 35 | |
| 36 | public: |
| 37 | DeclEntityAnalyzer(Entity ent, TULocationHandler &handler) |
| 38 | : Ent(ent), TULocHandler(handler) { } |
| 39 | |
| 40 | virtual void Handle(TranslationUnit *TU) { |
| 41 | assert(TU && "Passed null translation unit"); |
| 42 | |
| 43 | Decl *D = Ent.getDecl(TU->getASTContext()); |
| 44 | assert(D && "Couldn't resolve Entity"); |
| 45 | |
| 46 | for (Decl::redecl_iterator I = D->redecls_begin(), |
| 47 | E = D->redecls_end(); I != E; ++I) |
| 48 | TULocHandler.Handle(TULocation(TU, ASTLocation(*I))); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | //===----------------------------------------------------------------------===// |
| 53 | // RefEntityAnalyzer Implementation |
| 54 | //===----------------------------------------------------------------------===// |
| 55 | |
| 56 | class VISIBILITY_HIDDEN RefEntityAnalyzer : public TranslationUnitHandler { |
| 57 | Entity Ent; |
| 58 | TULocationHandler &TULocHandler; |
| 59 | |
| 60 | public: |
| 61 | RefEntityAnalyzer(Entity ent, TULocationHandler &handler) |
| 62 | : Ent(ent), TULocHandler(handler) { } |
| 63 | |
| 64 | virtual void Handle(TranslationUnit *TU) { |
| 65 | assert(TU && "Passed null translation unit"); |
| 66 | |
| 67 | Decl *D = Ent.getDecl(TU->getASTContext()); |
| 68 | assert(D && "Couldn't resolve Entity"); |
| 69 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 70 | if (!ND) |
| 71 | return; |
| 72 | |
| 73 | DeclReferenceMap &RefMap = TU->getDeclReferenceMap(); |
| 74 | for (DeclReferenceMap::astlocation_iterator |
| 75 | I = RefMap.refs_begin(ND), E = RefMap.refs_end(ND); I != E; ++I) |
| 76 | TULocHandler.Handle(TULocation(TU, ASTLocation(*I))); |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | } // end anonymous namespace |
| 81 | |
| 82 | //===----------------------------------------------------------------------===// |
| 83 | // Analyzer Implementation |
| 84 | //===----------------------------------------------------------------------===// |
| 85 | |
| 86 | void Analyzer::FindDeclarations(Decl *D, TULocationHandler &Handler) { |
| 87 | assert(D && "Passed null declaration"); |
| 88 | Entity Ent = Entity::get(D, Prog); |
| 89 | if (Ent.isInvalid()) |
| 90 | return; |
| 91 | |
| 92 | DeclEntityAnalyzer DEA(Ent, Handler); |
| 93 | Idxer.GetTranslationUnitsFor(Ent, DEA); |
| 94 | } |
| 95 | |
| 96 | void Analyzer::FindReferences(Decl *D, TULocationHandler &Handler) { |
| 97 | assert(D && "Passed null declaration"); |
| 98 | Entity Ent = Entity::get(D, Prog); |
| 99 | if (Ent.isInvalid()) |
| 100 | return; |
| 101 | |
| 102 | RefEntityAnalyzer REA(Ent, Handler); |
| 103 | Idxer.GetTranslationUnitsFor(Ent, REA); |
| 104 | } |