blob: 848ca31a5e32b04380596db7f52cb5c3894faad4 [file] [log] [blame]
Ted Kremenek16b55a72010-01-26 19:31:51 +00001//===- 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"
Ted Kremenek0a90d322010-11-17 23:24:11 +000016#include "CXTranslationUnit.h"
Ted Kremenek16b55a72010-01-26 19:31:51 +000017#include "CXSourceLocation.h"
18#include "clang/AST/DeclVisitor.h"
Benjamin Kramerb846deb2010-04-12 19:45:50 +000019#include "clang/Frontend/ASTUnit.h"
Ted Kremenek16b55a72010-01-26 19:31:51 +000020#include "llvm/ADT/SmallString.h"
21#include "llvm/Support/raw_ostream.h"
Benjamin Kramerb846deb2010-04-12 19:45:50 +000022using namespace clang;
Ted Kremenek16b55a72010-01-26 19:31:51 +000023
24extern "C" {
25void clang_getInclusions(CXTranslationUnit TU, CXInclusionVisitor CB,
26 CXClientData clientData) {
27
Ted Kremeneka60ed472010-11-16 08:15:36 +000028 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
Ted Kremenek16b55a72010-01-26 19:31:51 +000029 SourceManager &SM = CXXUnit->getSourceManager();
30 ASTContext &Ctx = CXXUnit->getASTContext();
31
Chris Lattner5f9e2722011-07-23 10:55:15 +000032 SmallVector<CXSourceLocation, 10> InclusionStack;
Douglas Gregorf62d43d2011-07-19 16:10:42 +000033 unsigned n = SM.local_sloc_entry_size();
Ted Kremenek16b55a72010-01-26 19:31:51 +000034
35 // In the case where all the SLocEntries are in an external source, traverse
36 // those SLocEntries as well. This is the case where we are looking
37 // at the inclusion stack of an AST/PCH file.
Douglas Gregorf62d43d2011-07-19 16:10:42 +000038 const SrcMgr::SLocEntry &(SourceManager::*Getter)(unsigned, bool*) const;
39 if (n == 1) {
40 Getter = &SourceManager::getLoadedSLocEntry;
41 n = SM.loaded_sloc_entry_size();
42 } else
43 Getter = &SourceManager::getLocalSLocEntry;
44
45 for (unsigned i = 0 ; i < n ; ++i) {
Douglas Gregore23ac652011-04-20 00:21:03 +000046 bool Invalid = false;
Douglas Gregorf62d43d2011-07-19 16:10:42 +000047 const SrcMgr::SLocEntry &SL = (SM.*Getter)(i, &Invalid);
Ted Kremenek16b55a72010-01-26 19:31:51 +000048
Douglas Gregore23ac652011-04-20 00:21:03 +000049 if (!SL.isFile() || Invalid)
Ted Kremenek16b55a72010-01-26 19:31:51 +000050 continue;
51
52 const SrcMgr::FileInfo &FI = SL.getFile();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +000053 if (!FI.getContentCache()->OrigEntry)
Ted Kremenek16b55a72010-01-26 19:31:51 +000054 continue;
55
56 // Build the inclusion stack.
57 SourceLocation L = FI.getIncludeLoc();
58 InclusionStack.clear();
59 while (L.isValid()) {
60 PresumedLoc PLoc = SM.getPresumedLoc(L);
61 InclusionStack.push_back(cxloc::translateSourceLocation(Ctx, L));
Douglas Gregorcb7b1e12010-11-12 07:15:47 +000062 L = PLoc.isValid()? PLoc.getIncludeLoc() : SourceLocation();
Ted Kremenek16b55a72010-01-26 19:31:51 +000063 }
64
65 // Callback to the client.
66 // FIXME: We should have a function to construct CXFiles.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +000067 CB((CXFile) FI.getContentCache()->OrigEntry,
Ted Kremenek16b55a72010-01-26 19:31:51 +000068 InclusionStack.data(), InclusionStack.size(), clientData);
69 }
70}
71} // end extern C