1 //===- CIndexInclusionStack.cpp - Clang-C Source Indexing Library ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a callback mechanism for clients to get the inclusion
11 // stack from a translation unit.
13 //===----------------------------------------------------------------------===//
16 #include "CXSourceLocation.h"
17 #include "CXTranslationUnit.h"
18 #include "clang/AST/DeclVisitor.h"
19 #include "clang/Frontend/ASTUnit.h"
20 using namespace clang;
22 static void getInclusions(const SrcMgr::SLocEntry &(SourceManager::*Getter)(unsigned, bool*) const, unsigned n,
23 CXTranslationUnit TU, CXInclusionVisitor CB,
24 CXClientData clientData)
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();
32 for (unsigned i = 0 ; i < n ; ++i) {
34 const SrcMgr::SLocEntry &SL = (SM.*Getter)(i, &Invalid);
36 if (!SL.isFile() || Invalid)
39 const SrcMgr::FileInfo &FI = SL.getFile();
40 if (!FI.getContentCache()->OrigEntry)
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))
49 // Build the inclusion stack.
50 InclusionStack.clear();
52 PresumedLoc PLoc = SM.getPresumedLoc(L);
53 InclusionStack.push_back(cxloc::translateSourceLocation(Ctx, L));
54 L = PLoc.isValid()? PLoc.getIncludeLoc() : SourceLocation();
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();
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);
71 void clang_getInclusions(CXTranslationUnit TU, CXInclusionVisitor CB,
72 CXClientData clientData) {
73 if (cxtu::isNotUsableTU(TU)) {
78 SourceManager &SM = cxtu::getASTUnit(TU)->getSourceManager();
79 const unsigned n = SM.local_sloc_entry_size();
81 // In the case where all the SLocEntries are in an external source, traverse
82 // those SLocEntries as well. This is the case where we are looking
83 // at the inclusion stack of an AST/PCH file. Also, if we are not looking at
84 // a AST/PCH file, but this file has a pre-compiled preamble, we also need
85 // to look in that file.
86 if (n == 1 || SM.getPreambleFileID().isValid()) {
87 getInclusions(&SourceManager::getLoadedSLocEntry,
88 SM.loaded_sloc_entry_size(), TU, CB, clientData);
91 // Not a PCH/AST file. Note, if there is a preamble, it could still be that
92 // there are #includes in this file (e.g. for any include after the first
95 getInclusions(&SourceManager::getLocalSLocEntry, n, TU, CB, clientData);