blob: 4a884e58843209cfec2920a64cab47f89426be4a [file] [log] [blame]
Shih-wei Liaof8fd82b2010-02-10 11:10:31 -08001//===- 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 "clang/AST/DeclVisitor.h"
18#include "llvm/ADT/SmallString.h"
19#include "llvm/Support/raw_ostream.h"
20
21extern "C" {
22void clang_getInclusions(CXTranslationUnit TU, CXInclusionVisitor CB,
23 CXClientData clientData) {
24
25 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
26 SourceManager &SM = CXXUnit->getSourceManager();
27 ASTContext &Ctx = CXXUnit->getASTContext();
28
29 llvm::SmallVector<CXSourceLocation, 10> InclusionStack;
30 unsigned i = SM.sloc_loaded_entry_size();
31 unsigned n = SM.sloc_entry_size();
32
33 // In the case where all the SLocEntries are in an external source, traverse
34 // those SLocEntries as well. This is the case where we are looking
35 // at the inclusion stack of an AST/PCH file.
36 if (i >= n)
37 i = 0;
38
39 for ( ; i < n ; ++i) {
40
41 const SrcMgr::SLocEntry &SL = SM.getSLocEntry(i);
42
43 if (!SL.isFile())
44 continue;
45
46 const SrcMgr::FileInfo &FI = SL.getFile();
47 if (!FI.getContentCache()->Entry)
48 continue;
49
50 // Build the inclusion stack.
51 SourceLocation L = FI.getIncludeLoc();
52 InclusionStack.clear();
53 while (L.isValid()) {
54 PresumedLoc PLoc = SM.getPresumedLoc(L);
55 InclusionStack.push_back(cxloc::translateSourceLocation(Ctx, L));
56 L = PLoc.getIncludeLoc();
57 }
58
59 // Callback to the client.
60 // FIXME: We should have a function to construct CXFiles.
61 CB((CXFile) FI.getContentCache()->Entry,
62 InclusionStack.data(), InclusionStack.size(), clientData);
63 }
64}
65} // end extern C