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" |
| 16 | #include "CXSourceLocation.h" |
Chandler Carruth | f59edb9 | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 17 | #include "CXTranslationUnit.h" |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 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) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 27 | if (cxtu::isNotUsableTU(TU)) { |
| 28 | LOG_BAD_TU(TU); |
| 29 | return; |
| 30 | } |
| 31 | |
Dmitri Gribenko | 5694feb | 2013-01-26 18:53:38 +0000 | [diff] [blame] | 32 | ASTUnit *CXXUnit = cxtu::getASTUnit(TU); |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 33 | SourceManager &SM = CXXUnit->getSourceManager(); |
| 34 | ASTContext &Ctx = CXXUnit->getASTContext(); |
| 35 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 36 | SmallVector<CXSourceLocation, 10> InclusionStack; |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 37 | unsigned n = SM.local_sloc_entry_size(); |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 38 | |
| 39 | // In the case where all the SLocEntries are in an external source, traverse |
| 40 | // those SLocEntries as well. This is the case where we are looking |
| 41 | // at the inclusion stack of an AST/PCH file. |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 42 | const SrcMgr::SLocEntry &(SourceManager::*Getter)(unsigned, bool*) const; |
| 43 | if (n == 1) { |
| 44 | Getter = &SourceManager::getLoadedSLocEntry; |
| 45 | n = SM.loaded_sloc_entry_size(); |
| 46 | } else |
| 47 | Getter = &SourceManager::getLocalSLocEntry; |
| 48 | |
| 49 | for (unsigned i = 0 ; i < n ; ++i) { |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 50 | bool Invalid = false; |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 51 | const SrcMgr::SLocEntry &SL = (SM.*Getter)(i, &Invalid); |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 52 | |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 53 | if (!SL.isFile() || Invalid) |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 54 | continue; |
| 55 | |
| 56 | const SrcMgr::FileInfo &FI = SL.getFile(); |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 57 | if (!FI.getContentCache()->OrigEntry) |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 58 | continue; |
| 59 | |
| 60 | // Build the inclusion stack. |
| 61 | SourceLocation L = FI.getIncludeLoc(); |
| 62 | InclusionStack.clear(); |
| 63 | while (L.isValid()) { |
| 64 | PresumedLoc PLoc = SM.getPresumedLoc(L); |
| 65 | InclusionStack.push_back(cxloc::translateSourceLocation(Ctx, L)); |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 66 | L = PLoc.isValid()? PLoc.getIncludeLoc() : SourceLocation(); |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Callback to the client. |
| 70 | // FIXME: We should have a function to construct CXFiles. |
David Greene | 61eacf0 | 2013-01-15 22:09:46 +0000 | [diff] [blame] | 71 | CB(static_cast<CXFile>( |
| 72 | const_cast<FileEntry *>(FI.getContentCache()->OrigEntry)), |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 73 | InclusionStack.data(), InclusionStack.size(), clientData); |
| 74 | } |
| 75 | } |
| 76 | } // end extern C |