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"
17 #include "CXTranslationUnit.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;
23
getInclusions(const SrcMgr::SLocEntry & (SourceManager::* Getter)(unsigned,bool *)const,unsigned n,CXTranslationUnit TU,CXInclusionVisitor CB,CXClientData clientData)24 static void getInclusions(const SrcMgr::SLocEntry &(SourceManager::*Getter)(unsigned, bool*) const, unsigned n,
25 CXTranslationUnit TU, CXInclusionVisitor CB,
26 CXClientData clientData)
27 {
28 ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
29 SourceManager &SM = CXXUnit->getSourceManager();
30 ASTContext &Ctx = CXXUnit->getASTContext();
31 SmallVector<CXSourceLocation, 10> InclusionStack;
32 const bool HasPreamble = SM.getPreambleFileID().isValid();
33
34 for (unsigned i = 0 ; i < n ; ++i) {
35 bool Invalid = false;
36 const SrcMgr::SLocEntry &SL = (SM.*Getter)(i, &Invalid);
37
38 if (!SL.isFile() || Invalid)
39 continue;
40
41 const SrcMgr::FileInfo &FI = SL.getFile();
42 if (!FI.getContentCache()->OrigEntry)
43 continue;
44
45 // If this is the main file, and there is a preamble, skip this SLoc. The
46 // inclusions of the preamble already showed it.
47 SourceLocation L = FI.getIncludeLoc();
48 if (HasPreamble && CXXUnit->isInMainFileID(L))
49 continue;
50
51 // Build the inclusion stack.
52 InclusionStack.clear();
53 while (L.isValid()) {
54 PresumedLoc PLoc = SM.getPresumedLoc(L);
55 InclusionStack.push_back(cxloc::translateSourceLocation(Ctx, L));
56 L = PLoc.isValid()? PLoc.getIncludeLoc() : SourceLocation();
57 }
58
59 // If there is a preamble, the last entry is the "inclusion" of that
60 // preamble into the main file, which has the bogus entry of main.c:1:1
61 if (HasPreamble && !InclusionStack.empty())
62 InclusionStack.pop_back();
63
64 // Callback to the client.
65 // FIXME: We should have a function to construct CXFiles.
66 CB(static_cast<CXFile>(
67 const_cast<FileEntry *>(FI.getContentCache()->OrigEntry)),
68 InclusionStack.data(), InclusionStack.size(), clientData);
69 }
70 }
71
72
73 extern "C" {
clang_getInclusions(CXTranslationUnit TU,CXInclusionVisitor CB,CXClientData clientData)74 void clang_getInclusions(CXTranslationUnit TU, CXInclusionVisitor CB,
75 CXClientData clientData) {
76 if (cxtu::isNotUsableTU(TU)) {
77 LOG_BAD_TU(TU);
78 return;
79 }
80
81 SourceManager &SM = cxtu::getASTUnit(TU)->getSourceManager();
82 const unsigned n = SM.local_sloc_entry_size();
83
84 // In the case where all the SLocEntries are in an external source, traverse
85 // those SLocEntries as well. This is the case where we are looking
86 // at the inclusion stack of an AST/PCH file. Also, if we are not looking at
87 // a AST/PCH file, but this file has a pre-compiled preamble, we also need
88 // to look in that file.
89 if (n == 1 || SM.getPreambleFileID().isValid()) {
90 getInclusions(&SourceManager::getLoadedSLocEntry,
91 SM.loaded_sloc_entry_size(), TU, CB, clientData);
92 }
93
94 // Not a PCH/AST file. Note, if there is a preamble, it could still be that
95 // there are #includes in this file (e.g. for any include after the first
96 // declaration).
97 if (n != 1)
98 getInclusions(&SourceManager::getLocalSLocEntry, n, TU, CB, clientData);
99
100 }
101 } // end extern C
102