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