Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 1 | //===- 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 Kremenek | 0a90d32 | 2010-11-17 23:24:11 +0000 | [diff] [blame] | 16 | #include "CXTranslationUnit.h" |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 17 | #include "CXSourceLocation.h" |
| 18 | #include "clang/AST/DeclVisitor.h" |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 19 | #include "clang/Frontend/ASTUnit.h" |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallString.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 22 | using namespace clang; |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 23 | |
| 24 | extern "C" { |
| 25 | void clang_getInclusions(CXTranslationUnit TU, CXInclusionVisitor CB, |
| 26 | CXClientData clientData) { |
| 27 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 28 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData); |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 29 | SourceManager &SM = CXXUnit->getSourceManager(); |
| 30 | ASTContext &Ctx = CXXUnit->getASTContext(); |
| 31 | |
| 32 | llvm::SmallVector<CXSourceLocation, 10> InclusionStack; |
| 33 | unsigned i = SM.sloc_loaded_entry_size(); |
| 34 | unsigned n = SM.sloc_entry_size(); |
| 35 | |
| 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. |
| 39 | if (i >= n) |
| 40 | i = 0; |
| 41 | |
| 42 | for ( ; i < n ; ++i) { |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame^] | 43 | bool Invalid = false; |
| 44 | const SrcMgr::SLocEntry &SL = SM.getSLocEntry(i, &Invalid); |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 45 | |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame^] | 46 | if (!SL.isFile() || Invalid) |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 47 | continue; |
| 48 | |
| 49 | const SrcMgr::FileInfo &FI = SL.getFile(); |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 50 | if (!FI.getContentCache()->OrigEntry) |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 51 | continue; |
| 52 | |
| 53 | // Build the inclusion stack. |
| 54 | SourceLocation L = FI.getIncludeLoc(); |
| 55 | InclusionStack.clear(); |
| 56 | while (L.isValid()) { |
| 57 | PresumedLoc PLoc = SM.getPresumedLoc(L); |
| 58 | InclusionStack.push_back(cxloc::translateSourceLocation(Ctx, L)); |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 59 | L = PLoc.isValid()? PLoc.getIncludeLoc() : SourceLocation(); |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // Callback to the client. |
| 63 | // FIXME: We should have a function to construct CXFiles. |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 64 | CB((CXFile) FI.getContentCache()->OrigEntry, |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 65 | InclusionStack.data(), InclusionStack.size(), clientData); |
| 66 | } |
| 67 | } |
| 68 | } // end extern C |