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