2010-01-27 03:31:51 +08:00
|
|
|
//===- CIndexInclusionStack.cpp - Clang-C Source Indexing Library ---------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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"
|
2010-01-27 03:31:51 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-04-13 03:45:50 +08:00
|
|
|
using namespace clang;
|
2010-01-27 03:31:51 +08:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
void clang_getInclusions(CXTranslationUnit TU, CXInclusionVisitor CB,
|
|
|
|
CXClientData clientData) {
|
2014-02-11 23:02:48 +08:00
|
|
|
if (cxtu::isNotUsableTU(TU)) {
|
2014-02-11 22:34:14 +08:00
|
|
|
LOG_BAD_TU(TU);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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;
|
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
|
|
|
unsigned n = SM.local_sloc_entry_size();
|
2010-01-27 03:31:51 +08:00
|
|
|
|
|
|
|
// 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.
|
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 &(SourceManager::*Getter)(unsigned, bool*) const;
|
|
|
|
if (n == 1) {
|
|
|
|
Getter = &SourceManager::getLoadedSLocEntry;
|
|
|
|
n = SM.loaded_sloc_entry_size();
|
|
|
|
} else
|
|
|
|
Getter = &SourceManager::getLocalSLocEntry;
|
|
|
|
|
|
|
|
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);
|
2010-01-27 03:31: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;
|
|
|
|
|
|
|
|
// Build the inclusion stack.
|
|
|
|
SourceLocation L = FI.getIncludeLoc();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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>(
|
|
|
|
const_cast<FileEntry *>(FI.getContentCache()->OrigEntry)),
|
2010-01-27 03:31:51 +08:00
|
|
|
InclusionStack.data(), InclusionStack.size(), clientData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // end extern C
|