1 //===- CIndexInclusionStack.cpp - Clang-C Source Indexing Library ---------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines a callback mechanism for clients to get the inclusion
10 // stack from a translation unit.
12 //===----------------------------------------------------------------------===//
16 #include "CXSourceLocation.h"
17 #include "CXTranslationUnit.h"
18 #include "clang/AST/DeclVisitor.h"
19 #include "clang/Frontend/ASTUnit.h"
20 using namespace clang
;
23 void getInclusions(bool IsLocal
, unsigned n
, CXTranslationUnit TU
,
24 CXInclusionVisitor CB
, CXClientData clientData
) {
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();
31 for (unsigned i
= 0 ; i
< n
; ++i
) {
33 const SrcMgr::SLocEntry
&SL
=
34 IsLocal
? SM
.getLocalSLocEntry(i
) : SM
.getLoadedSLocEntry(i
, &Invalid
);
35 if (!SL
.isFile() || Invalid
)
38 const SrcMgr::FileInfo
&FI
= SL
.getFile();
39 if (!FI
.getContentCache().OrigEntry
)
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
))
48 // Build the inclusion stack.
49 InclusionStack
.clear();
51 PresumedLoc PLoc
= SM
.getPresumedLoc(L
);
52 InclusionStack
.push_back(cxloc::translateSourceLocation(Ctx
, L
));
53 L
= PLoc
.isValid()? PLoc
.getIncludeLoc() : SourceLocation();
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();
61 // Callback to the client.
62 CB(cxfile::makeCXFile(*FI
.getContentCache().OrigEntry
),
63 InclusionStack
.data(), InclusionStack
.size(), clientData
);
68 void clang_getInclusions(CXTranslationUnit TU
, CXInclusionVisitor CB
,
69 CXClientData clientData
) {
70 if (cxtu::isNotUsableTU(TU
)) {
75 SourceManager
&SM
= cxtu::getASTUnit(TU
)->getSourceManager();
76 const unsigned n
= SM
.local_sloc_entry_size();
78 // In the case where all the SLocEntries are in an external source, traverse
79 // those SLocEntries as well. This is the case where we are looking
80 // at the inclusion stack of an AST/PCH file. Also, if we are not looking at
81 // a AST/PCH file, but this file has a pre-compiled preamble, we also need
82 // to look in that file.
83 if (n
== 1 || SM
.getPreambleFileID().isValid()) {
84 getInclusions(/*IsLocal=*/false, SM
.loaded_sloc_entry_size(), TU
, CB
,
88 // Not a PCH/AST file. Note, if there is a preamble, it could still be that
89 // there are #includes in this file (e.g. for any include after the first
92 getInclusions(/*IsLocal=*/true, n
, TU
, CB
, clientData
);