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 "CXTranslationUnit.h"
17 #include "CXSourceLocation.h"
18 #include "clang/AST/DeclVisitor.h"
19 #include "clang/Frontend/ASTUnit.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/Support/raw_ostream.h"
22 using namespace clang
;
25 void clang_getInclusions(CXTranslationUnit TU
, CXInclusionVisitor CB
,
26 CXClientData clientData
) {
28 ASTUnit
*CXXUnit
= static_cast<ASTUnit
*>(TU
->TUData
);
29 SourceManager
&SM
= CXXUnit
->getSourceManager();
30 ASTContext
&Ctx
= CXXUnit
->getASTContext();
32 llvm::SmallVector
<CXSourceLocation
, 10> InclusionStack
;
33 unsigned i
= SM
.sloc_loaded_entry_size();
34 unsigned n
= SM
.sloc_entry_size();
36 // In the case where all the SLocEntries are in an external source, traverse
37 // those SLocEntries as well. This is the case where we are looking
38 // at the inclusion stack of an AST/PCH file.
42 for ( ; i
< n
; ++i
) {
44 const SrcMgr::SLocEntry
&SL
= SM
.getSLocEntry(i
);
49 const SrcMgr::FileInfo
&FI
= SL
.getFile();
50 if (!FI
.getContentCache()->Entry
)
53 // Build the inclusion stack.
54 SourceLocation L
= FI
.getIncludeLoc();
55 InclusionStack
.clear();
57 PresumedLoc PLoc
= SM
.getPresumedLoc(L
);
58 InclusionStack
.push_back(cxloc::translateSourceLocation(Ctx
, L
));
59 L
= PLoc
.isValid()? PLoc
.getIncludeLoc() : SourceLocation();
62 // Callback to the client.
63 // FIXME: We should have a function to construct CXFiles.
64 CB((CXFile
) FI
.getContentCache()->Entry
,
65 InclusionStack
.data(), InclusionStack
.size(), clientData
);