1 //===- CIndexInclusionStack.cpp - Clang-C Source Indexing Library ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines a callback mechanism for clients to get the inclusion
10 // stack from a translation unit.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CIndexer.h"
15 #include "CXSourceLocation.h"
16 #include "CXTranslationUnit.h"
17 #include "clang/AST/DeclVisitor.h"
18 #include "clang/Frontend/ASTUnit.h"
19 using namespace clang;
20 
21 static void getInclusions(const SrcMgr::SLocEntry &(SourceManager::*Getter)(unsigned, bool*) const, unsigned n,
22                           CXTranslationUnit TU, CXInclusionVisitor CB,
23                           CXClientData clientData)
24 {
25   ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
26   SourceManager &SM = CXXUnit->getSourceManager();
27   ASTContext &Ctx = CXXUnit->getASTContext();
28   SmallVector<CXSourceLocation, 10> InclusionStack;
29   const bool HasPreamble = SM.getPreambleFileID().isValid();
30 
31   for (unsigned i = 0 ; i < n ; ++i) {
32     bool Invalid = false;
33     const SrcMgr::SLocEntry &SL = (SM.*Getter)(i, &Invalid);
34 
35     if (!SL.isFile() || Invalid)
36       continue;
37 
38     const SrcMgr::FileInfo &FI = SL.getFile();
39     if (!FI.getContentCache()->OrigEntry)
40       continue;
41 
42     // If this is the main file, and there is a preamble, skip this SLoc. The
43     // inclusions of the preamble already showed it.
44     SourceLocation L = FI.getIncludeLoc();
45     if (HasPreamble && CXXUnit->isInMainFileID(L))
46       continue;
47 
48     // Build the inclusion stack.
49     InclusionStack.clear();
50     while (L.isValid()) {
51       PresumedLoc PLoc = SM.getPresumedLoc(L);
52       InclusionStack.push_back(cxloc::translateSourceLocation(Ctx, L));
53       L = PLoc.isValid()? PLoc.getIncludeLoc() : SourceLocation();
54     }
55 
56     // If there is a preamble, the last entry is the "inclusion" of that
57     // preamble into the main file, which has the bogus entry of main.c:1:1
58     if (HasPreamble && !InclusionStack.empty())
59       InclusionStack.pop_back();
60 
61     // Callback to the client.
62     // FIXME: We should have a function to construct CXFiles.
63     CB(static_cast<CXFile>(
64          const_cast<FileEntry *>(FI.getContentCache()->OrigEntry)),
65        InclusionStack.data(), InclusionStack.size(), clientData);
66   }
67 }
68 
69 
70 void clang_getInclusions(CXTranslationUnit TU, CXInclusionVisitor CB,
71                          CXClientData clientData) {
72   if (cxtu::isNotUsableTU(TU)) {
73     LOG_BAD_TU(TU);
74     return;
75   }
76 
77   SourceManager &SM = cxtu::getASTUnit(TU)->getSourceManager();
78   const unsigned n =  SM.local_sloc_entry_size();
79 
80   // In the case where all the SLocEntries are in an external source, traverse
81   // those SLocEntries as well.  This is the case where we are looking
82   // at the inclusion stack of an AST/PCH file. Also, if we are not looking at
83   // a AST/PCH file, but this file has a pre-compiled preamble, we also need
84   // to look in that file.
85   if (n == 1 || SM.getPreambleFileID().isValid()) {
86     getInclusions(&SourceManager::getLoadedSLocEntry,
87                   SM.loaded_sloc_entry_size(), TU, CB, clientData);
88   }
89 
90   // Not a PCH/AST file. Note, if there is a preamble, it could still be that
91   // there are #includes in this file (e.g. for any include after the first
92   // declaration).
93   if (n != 1)
94     getInclusions(&SourceManager::getLocalSLocEntry, n, TU, CB, clientData);
95 
96 }
97