2010-01-27 03:31:51 +08:00
|
|
|
//===- CIndexInclusionStack.cpp - Clang-C Source Indexing Library ---------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-01-27 03:31:51 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines a callback mechanism for clients to get the inclusion
|
|
|
|
// stack from a translation unit.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CIndexer.h"
|
|
|
|
#include "CXSourceLocation.h"
|
2012-12-04 17:25:21 +08:00
|
|
|
#include "CXTranslationUnit.h"
|
2010-01-27 03:31:51 +08:00
|
|
|
#include "clang/AST/DeclVisitor.h"
|
2010-04-13 03:45:50 +08:00
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
|
|
|
using namespace clang;
|
2010-01-27 03:31:51 +08:00
|
|
|
|
2016-01-06 23:12:51 +08:00
|
|
|
static void getInclusions(const SrcMgr::SLocEntry &(SourceManager::*Getter)(unsigned, bool*) const, unsigned n,
|
|
|
|
CXTranslationUnit TU, CXInclusionVisitor CB,
|
|
|
|
CXClientData clientData)
|
|
|
|
{
|
2013-01-27 02:53:38 +08:00
|
|
|
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
|
2010-01-27 03:31:51 +08:00
|
|
|
SourceManager &SM = CXXUnit->getSourceManager();
|
|
|
|
ASTContext &Ctx = CXXUnit->getASTContext();
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<CXSourceLocation, 10> InclusionStack;
|
2016-01-06 23:12:51 +08:00
|
|
|
const bool HasPreamble = SM.getPreambleFileID().isValid();
|
Revamp the SourceManager to separate the representation of parsed
source locations from source locations loaded from an AST/PCH file.
Previously, loading an AST/PCH file involved carefully pre-allocating
space at the beginning of the source manager for the source locations
and FileIDs that correspond to the prefix, and then appending the
source locations/FileIDs used for parsing the remaining translation
unit. This design forced us into loading PCH files early, as a prefix,
whic has become a rather significant limitation.
This patch splits the SourceManager space into two parts: for source
location "addresses", the lower values (growing upward) are used to
describe parsed code, while upper values (growing downward) are used
for source locations loaded from AST/PCH files. Similarly, positive
FileIDs are used to describe parsed code while negative FileIDs are
used to file/macro locations loaded from AST/PCH files. As a result,
we can load PCH/AST files even during parsing, making various
improvemnts in the future possible, e.g., teaching #include <foo.h> to
look for and load <foo.h.gch> if it happens to be already available.
This patch was originally written by Sebastian Redl, then brought
forward to the modern age by Jonathan Turner, and finally
polished/finished by me to be committed.
llvm-svn: 135484
2011-07-20 00:10:42 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0 ; i < n ; ++i) {
|
2011-04-20 08:21:03 +08:00
|
|
|
bool Invalid = false;
|
Revamp the SourceManager to separate the representation of parsed
source locations from source locations loaded from an AST/PCH file.
Previously, loading an AST/PCH file involved carefully pre-allocating
space at the beginning of the source manager for the source locations
and FileIDs that correspond to the prefix, and then appending the
source locations/FileIDs used for parsing the remaining translation
unit. This design forced us into loading PCH files early, as a prefix,
whic has become a rather significant limitation.
This patch splits the SourceManager space into two parts: for source
location "addresses", the lower values (growing upward) are used to
describe parsed code, while upper values (growing downward) are used
for source locations loaded from AST/PCH files. Similarly, positive
FileIDs are used to describe parsed code while negative FileIDs are
used to file/macro locations loaded from AST/PCH files. As a result,
we can load PCH/AST files even during parsing, making various
improvemnts in the future possible, e.g., teaching #include <foo.h> to
look for and load <foo.h.gch> if it happens to be already available.
This patch was originally written by Sebastian Redl, then brought
forward to the modern age by Jonathan Turner, and finally
polished/finished by me to be committed.
llvm-svn: 135484
2011-07-20 00:10:42 +08:00
|
|
|
const SrcMgr::SLocEntry &SL = (SM.*Getter)(i, &Invalid);
|
2016-01-06 23:12:51 +08:00
|
|
|
|
2011-04-20 08:21:03 +08:00
|
|
|
if (!SL.isFile() || Invalid)
|
2010-01-27 03:31:51 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
const SrcMgr::FileInfo &FI = SL.getFile();
|
2011-03-05 09:03:53 +08:00
|
|
|
if (!FI.getContentCache()->OrigEntry)
|
2010-01-27 03:31:51 +08:00
|
|
|
continue;
|
2016-01-06 23:12:51 +08:00
|
|
|
|
|
|
|
// If this is the main file, and there is a preamble, skip this SLoc. The
|
|
|
|
// inclusions of the preamble already showed it.
|
2010-01-27 03:31:51 +08:00
|
|
|
SourceLocation L = FI.getIncludeLoc();
|
2016-01-06 23:12:51 +08:00
|
|
|
if (HasPreamble && CXXUnit->isInMainFileID(L))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Build the inclusion stack.
|
2010-01-27 03:31:51 +08:00
|
|
|
InclusionStack.clear();
|
|
|
|
while (L.isValid()) {
|
|
|
|
PresumedLoc PLoc = SM.getPresumedLoc(L);
|
|
|
|
InclusionStack.push_back(cxloc::translateSourceLocation(Ctx, L));
|
2010-11-12 15:15:47 +08:00
|
|
|
L = PLoc.isValid()? PLoc.getIncludeLoc() : SourceLocation();
|
2010-01-27 03:31:51 +08:00
|
|
|
}
|
2016-01-06 23:12:51 +08:00
|
|
|
|
|
|
|
// If there is a preamble, the last entry is the "inclusion" of that
|
|
|
|
// preamble into the main file, which has the bogus entry of main.c:1:1
|
|
|
|
if (HasPreamble && !InclusionStack.empty())
|
|
|
|
InclusionStack.pop_back();
|
|
|
|
|
2010-01-27 03:31:51 +08:00
|
|
|
// Callback to the client.
|
|
|
|
// FIXME: We should have a function to construct CXFiles.
|
2013-01-16 06:09:46 +08:00
|
|
|
CB(static_cast<CXFile>(
|
2016-01-06 23:12:51 +08:00
|
|
|
const_cast<FileEntry *>(FI.getContentCache()->OrigEntry)),
|
2010-01-27 03:31:51 +08:00
|
|
|
InclusionStack.data(), InclusionStack.size(), clientData);
|
2016-01-06 23:12:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void clang_getInclusions(CXTranslationUnit TU, CXInclusionVisitor CB,
|
|
|
|
CXClientData clientData) {
|
|
|
|
if (cxtu::isNotUsableTU(TU)) {
|
|
|
|
LOG_BAD_TU(TU);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceManager &SM = cxtu::getASTUnit(TU)->getSourceManager();
|
|
|
|
const unsigned n = SM.local_sloc_entry_size();
|
|
|
|
|
|
|
|
// In the case where all the SLocEntries are in an external source, traverse
|
|
|
|
// those SLocEntries as well. This is the case where we are looking
|
|
|
|
// at the inclusion stack of an AST/PCH file. Also, if we are not looking at
|
|
|
|
// a AST/PCH file, but this file has a pre-compiled preamble, we also need
|
|
|
|
// to look in that file.
|
|
|
|
if (n == 1 || SM.getPreambleFileID().isValid()) {
|
|
|
|
getInclusions(&SourceManager::getLoadedSLocEntry,
|
|
|
|
SM.loaded_sloc_entry_size(), TU, CB, clientData);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not a PCH/AST file. Note, if there is a preamble, it could still be that
|
|
|
|
// there are #includes in this file (e.g. for any include after the first
|
|
|
|
// declaration).
|
|
|
|
if (n != 1)
|
|
|
|
getInclusions(&SourceManager::getLocalSLocEntry, n, TU, CB, clientData);
|
|
|
|
|
2010-01-27 03:31:51 +08:00
|
|
|
}
|