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