| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | #include "CIndexer.h" |
| 15 | #include "CXSourceLocation.h" |
| 16 | #include "CXTranslationUnit.h" |
| 17 | #include "clang/AST/DeclVisitor.h" |
| 18 | #include "clang/Frontend/ASTUnit.h" |
| 19 | using namespace clang; |
| 20 | |
| 21 | static void getInclusions(const SrcMgr::SLocEntry &(SourceManager::*Getter)(unsigned, bool*) const, unsigned n, |
| 22 | CXTranslationUnit TU, CXInclusionVisitor CB, |
| 23 | CXClientData clientData) |
| 24 | { |
| 25 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
| 26 | SourceManager &SM = CXXUnit->getSourceManager(); |
| 27 | ASTContext &Ctx = CXXUnit->getASTContext(); |
| 28 | SmallVector<CXSourceLocation, 10> InclusionStack; |
| 29 | const bool HasPreamble = SM.getPreambleFileID().isValid(); |
| 30 | |
| 31 | for (unsigned i = 0 ; i < n ; ++i) { |
| 32 | bool Invalid = false; |
| 33 | const SrcMgr::SLocEntry &SL = (SM.*Getter)(i, &Invalid); |
| 34 | |
| 35 | if (!SL.isFile() || Invalid) |
| 36 | continue; |
| 37 | |
| 38 | const SrcMgr::FileInfo &FI = SL.getFile(); |
| 39 | if (!FI.getContentCache()->OrigEntry) |
| 40 | continue; |
| 41 | |
| 42 | |
| 43 | |
| 44 | SourceLocation L = FI.getIncludeLoc(); |
| 45 | if (HasPreamble && CXXUnit->isInMainFileID(L)) |
| 46 | continue; |
| 47 | |
| 48 | |
| 49 | InclusionStack.clear(); |
| 50 | while (L.isValid()) { |
| 51 | PresumedLoc PLoc = SM.getPresumedLoc(L); |
| 52 | InclusionStack.push_back(cxloc::translateSourceLocation(Ctx, L)); |
| 53 | L = PLoc.isValid()? PLoc.getIncludeLoc() : SourceLocation(); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | |
| 58 | if (HasPreamble && !InclusionStack.empty()) |
| 59 | InclusionStack.pop_back(); |
| 60 | |
| 61 | |
| 62 | |
| 63 | CB(static_cast<CXFile>( |
| 64 | const_cast<FileEntry *>(FI.getContentCache()->OrigEntry)), |
| 65 | InclusionStack.data(), InclusionStack.size(), clientData); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | |
| 70 | void clang_getInclusions(CXTranslationUnit TU, CXInclusionVisitor CB, |
| 71 | CXClientData clientData) { |
| 72 | if (cxtu::isNotUsableTU(TU)) { |
| 73 | LOG_BAD_TU(TU); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | SourceManager &SM = cxtu::getASTUnit(TU)->getSourceManager(); |
| 78 | const unsigned n = SM.local_sloc_entry_size(); |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | if (n == 1 || SM.getPreambleFileID().isValid()) { |
| 86 | getInclusions(&SourceManager::getLoadedSLocEntry, |
| 87 | SM.loaded_sloc_entry_size(), TU, CB, clientData); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | if (n != 1) |
| 94 | getInclusions(&SourceManager::getLocalSLocEntry, n, TU, CB, clientData); |
| 95 | |
| 96 | } |
| 97 | |