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