1 //===- CIndexInclusionStack.cpp - Clang-C Source Indexing Library ---------===// 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 defines a callback mechanism for clients to get the inclusion 11 // stack from a translation unit. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "CIndexer.h" 16 #include "CXSourceLocation.h" 17 #include "CXTranslationUnit.h" 18 #include "clang/AST/DeclVisitor.h" 19 #include "clang/Frontend/ASTUnit.h" 20 using namespace clang; 21 22 static void getInclusions(const SrcMgr::SLocEntry &(SourceManager::*Getter)(unsigned, bool*) const, unsigned n, 23 CXTranslationUnit TU, CXInclusionVisitor CB, 24 CXClientData clientData) 25 { 26 ASTUnit *CXXUnit = cxtu::getASTUnit(TU); 27 SourceManager &SM = CXXUnit->getSourceManager(); 28 ASTContext &Ctx = CXXUnit->getASTContext(); 29 SmallVector<CXSourceLocation, 10> InclusionStack; 30 const bool HasPreamble = SM.getPreambleFileID().isValid(); 31 32 for (unsigned i = 0 ; i < n ; ++i) { 33 bool Invalid = false; 34 const SrcMgr::SLocEntry &SL = (SM.*Getter)(i, &Invalid); 35 36 if (!SL.isFile() || Invalid) 37 continue; 38 39 const SrcMgr::FileInfo &FI = SL.getFile(); 40 if (!FI.getContentCache()->OrigEntry) 41 continue; 42 43 // If this is the main file, and there is a preamble, skip this SLoc. The 44 // inclusions of the preamble already showed it. 45 SourceLocation L = FI.getIncludeLoc(); 46 if (HasPreamble && CXXUnit->isInMainFileID(L)) 47 continue; 48 49 // Build the inclusion stack. 50 InclusionStack.clear(); 51 while (L.isValid()) { 52 PresumedLoc PLoc = SM.getPresumedLoc(L); 53 InclusionStack.push_back(cxloc::translateSourceLocation(Ctx, L)); 54 L = PLoc.isValid()? PLoc.getIncludeLoc() : SourceLocation(); 55 } 56 57 // If there is a preamble, the last entry is the "inclusion" of that 58 // preamble into the main file, which has the bogus entry of main.c:1:1 59 if (HasPreamble && !InclusionStack.empty()) 60 InclusionStack.pop_back(); 61 62 // Callback to the client. 63 // FIXME: We should have a function to construct CXFiles. 64 CB(static_cast<CXFile>( 65 const_cast<FileEntry *>(FI.getContentCache()->OrigEntry)), 66 InclusionStack.data(), InclusionStack.size(), clientData); 67 } 68 } 69 70 71 extern "C" { 72 void clang_getInclusions(CXTranslationUnit TU, CXInclusionVisitor CB, 73 CXClientData clientData) { 74 if (cxtu::isNotUsableTU(TU)) { 75 LOG_BAD_TU(TU); 76 return; 77 } 78 79 SourceManager &SM = cxtu::getASTUnit(TU)->getSourceManager(); 80 const unsigned n = SM.local_sloc_entry_size(); 81 82 // In the case where all the SLocEntries are in an external source, traverse 83 // those SLocEntries as well. This is the case where we are looking 84 // at the inclusion stack of an AST/PCH file. Also, if we are not looking at 85 // a AST/PCH file, but this file has a pre-compiled preamble, we also need 86 // to look in that file. 87 if (n == 1 || SM.getPreambleFileID().isValid()) { 88 getInclusions(&SourceManager::getLoadedSLocEntry, 89 SM.loaded_sloc_entry_size(), TU, CB, clientData); 90 } 91 92 // Not a PCH/AST file. Note, if there is a preamble, it could still be that 93 // there are #includes in this file (e.g. for any include after the first 94 // declaration). 95 if (n != 1) 96 getInclusions(&SourceManager::getLocalSLocEntry, n, TU, CB, clientData); 97 98 } 99 } // end extern C 100