Introduce the notion of a "preprocessing record", which keeps track of
the macro definitions and macro instantiations that are found
during preprocessing. Preprocessing records are *not* generated by
default; rather, we provide a PPCallbacks subclass that hooks into the
existing callback mechanism to record this activity.
The only client of preprocessing records is CIndex, which keeps track
of macro definitions and instantations so that they can be exposed via
cursors. At present, only token annotation uses these facilities, and
only for macro instantiations; both will change in the near
future. However, with this change, token annotation properly annotates
macro instantiations that do not produce any tokens and instantiations
of macros that are later undef'd, improving our consistency.
Preprocessing directives that are not macro definitions are still
handled by clang_annotateTokens() via re-lexing, so that we don't have
to track every preprocessing directive in the preprocessing record.
Performance impact of preprocessing records is still TBD, although it
is limited to CIndex and therefore out of the path of the main compiler.
llvm-svn: 98836
2010-03-19 01:52:52 +08:00
|
|
|
//===--- PreprocessingRecord.cpp - Record of Preprocessing ------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the PreprocessingRecord class, which maintains a record
|
|
|
|
// of what occurred during preprocessing, and its helpers.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "clang/Lex/PreprocessingRecord.h"
|
|
|
|
#include "clang/Lex/MacroInfo.h"
|
|
|
|
#include "clang/Lex/Token.h"
|
2010-10-21 06:00:55 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2011-07-28 02:41:20 +08:00
|
|
|
#include "llvm/Support/Capacity.h"
|
Introduce the notion of a "preprocessing record", which keeps track of
the macro definitions and macro instantiations that are found
during preprocessing. Preprocessing records are *not* generated by
default; rather, we provide a PPCallbacks subclass that hooks into the
existing callback mechanism to record this activity.
The only client of preprocessing records is CIndex, which keeps track
of macro definitions and instantations so that they can be exposed via
cursors. At present, only token annotation uses these facilities, and
only for macro instantiations; both will change in the near
future. However, with this change, token annotation properly annotates
macro instantiations that do not produce any tokens and instantiations
of macros that are later undef'd, improving our consistency.
Preprocessing directives that are not macro definitions are still
handled by clang_annotateTokens() via re-lexing, so that we don't have
to track every preprocessing directive in the preprocessing record.
Performance impact of preprocessing records is still TBD, although it
is limited to CIndex and therefore out of the path of the main compiler.
llvm-svn: 98836
2010-03-19 01:52:52 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
Implement serialization and lazy deserialization of the preprocessing
record (which includes all macro instantiations and definitions). As
with all lay deserialization, this introduces a new external source
(here, an external preprocessing record source) that loads all of the
preprocessed entities prior to iterating over the entities.
The preprocessing record is an optional part of the precompiled header
that is disabled by default (enabled with
-detailed-preprocessing-record). When the preprocessor given to the
PCH writer has a preprocessing record, that record is written into the
PCH file. When the PCH reader is given a PCH file that contains a
preprocessing record, it will be lazily loaded (which, effectively,
implicitly adds -detailed-preprocessing-record). This is the first
case where we have sections of the precompiled header that are
added/removed based on a compilation flag, which is
unfortunate. However, this data consumes ~550k in the PCH file for
Cocoa.h (out of ~9.9MB), and there is a non-trivial cost to gathering
this detailed preprocessing information, so it's too expensive to turn
on by default. In the future, we should investigate a better encoding
of this information.
llvm-svn: 99002
2010-03-20 05:51:54 +08:00
|
|
|
ExternalPreprocessingRecordSource::~ExternalPreprocessingRecordSource() { }
|
|
|
|
|
2010-11-01 23:03:47 +08:00
|
|
|
|
|
|
|
InclusionDirective::InclusionDirective(PreprocessingRecord &PPRec,
|
|
|
|
InclusionKind Kind,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef FileName,
|
2010-11-01 23:03:47 +08:00
|
|
|
bool InQuotes, const FileEntry *File,
|
|
|
|
SourceRange Range)
|
|
|
|
: PreprocessingDirective(InclusionDirectiveKind, Range),
|
|
|
|
InQuotes(InQuotes), Kind(Kind), File(File)
|
|
|
|
{
|
|
|
|
char *Memory
|
|
|
|
= (char*)PPRec.Allocate(FileName.size() + 1, llvm::alignOf<char>());
|
|
|
|
memcpy(Memory, FileName.data(), FileName.size());
|
|
|
|
Memory[FileName.size()] = 0;
|
2011-07-23 18:55:15 +08:00
|
|
|
this->FileName = StringRef(Memory, FileName.size());
|
2010-11-01 23:03:47 +08:00
|
|
|
}
|
|
|
|
|
2011-09-20 04:40:25 +08:00
|
|
|
PreprocessingRecord::PreprocessingRecord(SourceManager &SM,
|
|
|
|
bool IncludeNestedMacroExpansions)
|
|
|
|
: SourceMgr(SM), IncludeNestedMacroExpansions(IncludeNestedMacroExpansions),
|
2011-09-16 02:02:56 +08:00
|
|
|
ExternalSource(0)
|
Implement serialization and lazy deserialization of the preprocessing
record (which includes all macro instantiations and definitions). As
with all lay deserialization, this introduces a new external source
(here, an external preprocessing record source) that loads all of the
preprocessed entities prior to iterating over the entities.
The preprocessing record is an optional part of the precompiled header
that is disabled by default (enabled with
-detailed-preprocessing-record). When the preprocessor given to the
PCH writer has a preprocessing record, that record is written into the
PCH file. When the PCH reader is given a PCH file that contains a
preprocessing record, it will be lazily loaded (which, effectively,
implicitly adds -detailed-preprocessing-record). This is the first
case where we have sections of the precompiled header that are
added/removed based on a compilation flag, which is
unfortunate. However, this data consumes ~550k in the PCH file for
Cocoa.h (out of ~9.9MB), and there is a non-trivial cost to gathering
this detailed preprocessing information, so it's too expensive to turn
on by default. In the future, we should investigate a better encoding
of this information.
llvm-svn: 99002
2010-03-20 05:51:54 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-09-20 04:40:25 +08:00
|
|
|
/// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
|
|
|
|
/// that source range \arg R encompasses.
|
|
|
|
std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
|
|
|
|
PreprocessingRecord::getPreprocessedEntitiesInRange(SourceRange Range) {
|
|
|
|
if (Range.isInvalid())
|
2011-10-25 08:29:50 +08:00
|
|
|
return std::make_pair(iterator(), iterator());
|
|
|
|
|
|
|
|
if (CachedRangeQuery.Range == Range) {
|
|
|
|
return std::make_pair(iterator(this, CachedRangeQuery.Result.first),
|
|
|
|
iterator(this, CachedRangeQuery.Result.second));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<PPEntityID, PPEntityID>
|
|
|
|
Res = getPreprocessedEntitiesInRangeSlow(Range);
|
|
|
|
|
|
|
|
CachedRangeQuery.Range = Range;
|
|
|
|
CachedRangeQuery.Result = Res;
|
|
|
|
|
|
|
|
return std::make_pair(iterator(this, Res.first), iterator(this, Res.second));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isPreprocessedEntityIfInFileID(PreprocessedEntity *PPE, FileID FID,
|
|
|
|
SourceManager &SM) {
|
|
|
|
assert(!FID.isInvalid());
|
|
|
|
if (!PPE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SourceLocation Loc = PPE->getSourceRange().getBegin();
|
|
|
|
if (Loc.isInvalid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (SM.isInFileID(SM.getFileLoc(Loc), FID))
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2011-09-20 04:40:25 +08:00
|
|
|
|
2011-10-25 08:29:50 +08:00
|
|
|
/// \brief Returns true if the preprocessed entity that \arg PPEI iterator
|
|
|
|
/// points to is coming from the file \arg FID.
|
|
|
|
///
|
|
|
|
/// Can be used to avoid implicit deserializations of preallocated
|
|
|
|
/// preprocessed entities if we only care about entities of a specific file
|
|
|
|
/// and not from files #included in the range given at
|
|
|
|
/// \see getPreprocessedEntitiesInRange.
|
|
|
|
bool PreprocessingRecord::isEntityInFileID(iterator PPEI, FileID FID) {
|
|
|
|
if (FID.isInvalid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
PPEntityID PPID = PPEI.Position;
|
|
|
|
if (PPID < 0) {
|
|
|
|
assert(unsigned(-PPID-1) < LoadedPreprocessedEntities.size() &&
|
|
|
|
"Out-of bounds loaded preprocessed entity");
|
|
|
|
assert(ExternalSource && "No external source to load from");
|
|
|
|
unsigned LoadedIndex = LoadedPreprocessedEntities.size()+PPID;
|
|
|
|
if (PreprocessedEntity *PPE = LoadedPreprocessedEntities[LoadedIndex])
|
|
|
|
return isPreprocessedEntityIfInFileID(PPE, FID, SourceMgr);
|
|
|
|
|
|
|
|
// See if the external source can see if the entity is in the file without
|
|
|
|
// deserializing it.
|
|
|
|
llvm::Optional<bool>
|
|
|
|
IsInFile = ExternalSource->isPreprocessedEntityInFileID(LoadedIndex, FID);
|
|
|
|
if (IsInFile.hasValue())
|
|
|
|
return IsInFile.getValue();
|
|
|
|
|
|
|
|
// The external source did not provide a definite answer, go and deserialize
|
|
|
|
// the entity to check it.
|
|
|
|
return isPreprocessedEntityIfInFileID(
|
|
|
|
getLoadedPreprocessedEntity(LoadedIndex),
|
|
|
|
FID, SourceMgr);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(unsigned(PPID) < PreprocessedEntities.size() &&
|
|
|
|
"Out-of bounds local preprocessed entity");
|
|
|
|
return isPreprocessedEntityIfInFileID(PreprocessedEntities[PPID],
|
|
|
|
FID, SourceMgr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
|
|
|
|
/// that source range \arg R encompasses.
|
|
|
|
std::pair<PreprocessingRecord::PPEntityID, PreprocessingRecord::PPEntityID>
|
|
|
|
PreprocessingRecord::getPreprocessedEntitiesInRangeSlow(SourceRange Range) {
|
|
|
|
assert(Range.isValid());
|
|
|
|
assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
|
|
|
|
|
2011-09-20 04:40:25 +08:00
|
|
|
std::pair<unsigned, unsigned>
|
|
|
|
Local = findLocalPreprocessedEntitiesInRange(Range);
|
2011-10-25 08:29:50 +08:00
|
|
|
|
2011-09-20 04:40:25 +08:00
|
|
|
// Check if range spans local entities.
|
|
|
|
if (!ExternalSource || SourceMgr.isLocalSourceLocation(Range.getBegin()))
|
2011-10-25 08:29:50 +08:00
|
|
|
return std::make_pair(Local.first, Local.second);
|
|
|
|
|
2011-09-20 04:40:25 +08:00
|
|
|
std::pair<unsigned, unsigned>
|
|
|
|
Loaded = ExternalSource->findPreprocessedEntitiesInRange(Range);
|
2011-10-25 08:29:50 +08:00
|
|
|
|
2011-09-20 04:40:25 +08:00
|
|
|
// Check if range spans local entities.
|
|
|
|
if (Loaded.first == Loaded.second)
|
2011-10-25 08:29:50 +08:00
|
|
|
return std::make_pair(Local.first, Local.second);
|
|
|
|
|
2011-09-20 04:40:25 +08:00
|
|
|
unsigned TotalLoaded = LoadedPreprocessedEntities.size();
|
2011-10-25 08:29:50 +08:00
|
|
|
|
2011-09-20 04:40:25 +08:00
|
|
|
// Check if range spans loaded entities.
|
|
|
|
if (Local.first == Local.second)
|
2011-10-25 08:29:50 +08:00
|
|
|
return std::make_pair(int(Loaded.first)-TotalLoaded,
|
|
|
|
int(Loaded.second)-TotalLoaded);
|
|
|
|
|
2011-09-20 04:40:25 +08:00
|
|
|
// Range spands loaded and local entities.
|
2011-10-25 08:29:50 +08:00
|
|
|
return std::make_pair(int(Loaded.first)-TotalLoaded, Local.second);
|
2011-09-20 04:40:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<unsigned, unsigned>
|
|
|
|
PreprocessingRecord::findLocalPreprocessedEntitiesInRange(
|
|
|
|
SourceRange Range) const {
|
|
|
|
if (Range.isInvalid())
|
|
|
|
return std::make_pair(0,0);
|
|
|
|
assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
|
|
|
|
|
|
|
|
unsigned Begin = findBeginLocalPreprocessedEntity(Range.getBegin());
|
|
|
|
unsigned End = findEndLocalPreprocessedEntity(Range.getEnd());
|
|
|
|
return std::make_pair(Begin, End);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
template <SourceLocation (SourceRange::*getRangeLoc)() const>
|
|
|
|
struct PPEntityComp {
|
|
|
|
const SourceManager &SM;
|
|
|
|
|
|
|
|
explicit PPEntityComp(const SourceManager &SM) : SM(SM) { }
|
|
|
|
|
2011-09-22 00:58:20 +08:00
|
|
|
bool operator()(PreprocessedEntity *L, PreprocessedEntity *R) const {
|
|
|
|
SourceLocation LHS = getLoc(L);
|
|
|
|
SourceLocation RHS = getLoc(R);
|
|
|
|
return SM.isBeforeInTranslationUnit(LHS, RHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(PreprocessedEntity *L, SourceLocation RHS) const {
|
2011-09-20 04:40:25 +08:00
|
|
|
SourceLocation LHS = getLoc(L);
|
|
|
|
return SM.isBeforeInTranslationUnit(LHS, RHS);
|
|
|
|
}
|
|
|
|
|
2011-09-22 00:58:20 +08:00
|
|
|
bool operator()(SourceLocation LHS, PreprocessedEntity *R) const {
|
2011-09-20 04:40:25 +08:00
|
|
|
SourceLocation RHS = getLoc(R);
|
|
|
|
return SM.isBeforeInTranslationUnit(LHS, RHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceLocation getLoc(PreprocessedEntity *PPE) const {
|
2011-09-20 06:02:08 +08:00
|
|
|
SourceRange Range = PPE->getSourceRange();
|
|
|
|
return (Range.*getRangeLoc)();
|
2011-09-20 04:40:25 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned PreprocessingRecord::findBeginLocalPreprocessedEntity(
|
|
|
|
SourceLocation Loc) const {
|
|
|
|
if (SourceMgr.isLoadedSourceLocation(Loc))
|
|
|
|
return 0;
|
|
|
|
|
2011-09-23 05:17:02 +08:00
|
|
|
size_t Count = PreprocessedEntities.size();
|
|
|
|
size_t Half;
|
2011-09-20 04:40:25 +08:00
|
|
|
std::vector<PreprocessedEntity *>::const_iterator
|
2011-09-23 05:17:02 +08:00
|
|
|
First = PreprocessedEntities.begin();
|
|
|
|
std::vector<PreprocessedEntity *>::const_iterator I;
|
|
|
|
|
|
|
|
// Do a binary search manually instead of using std::lower_bound because
|
|
|
|
// The end locations of entities may be unordered (when a macro expansion
|
|
|
|
// is inside another macro argument), but for this case it is not important
|
|
|
|
// whether we get the first macro expansion or its containing macro.
|
|
|
|
while (Count > 0) {
|
|
|
|
Half = Count/2;
|
|
|
|
I = First;
|
|
|
|
std::advance(I, Half);
|
|
|
|
if (SourceMgr.isBeforeInTranslationUnit((*I)->getSourceRange().getEnd(),
|
|
|
|
Loc)){
|
|
|
|
First = I;
|
|
|
|
++First;
|
|
|
|
Count = Count - Half - 1;
|
|
|
|
} else
|
|
|
|
Count = Half;
|
|
|
|
}
|
|
|
|
|
|
|
|
return First - PreprocessedEntities.begin();
|
2011-09-20 04:40:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned PreprocessingRecord::findEndLocalPreprocessedEntity(
|
|
|
|
SourceLocation Loc) const {
|
|
|
|
if (SourceMgr.isLoadedSourceLocation(Loc))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
std::vector<PreprocessedEntity *>::const_iterator
|
|
|
|
I = std::upper_bound(PreprocessedEntities.begin(),
|
|
|
|
PreprocessedEntities.end(),
|
|
|
|
Loc,
|
|
|
|
PPEntityComp<&SourceRange::getBegin>(SourceMgr));
|
|
|
|
return I - PreprocessedEntities.begin();
|
|
|
|
}
|
|
|
|
|
Introduce the notion of a "preprocessing record", which keeps track of
the macro definitions and macro instantiations that are found
during preprocessing. Preprocessing records are *not* generated by
default; rather, we provide a PPCallbacks subclass that hooks into the
existing callback mechanism to record this activity.
The only client of preprocessing records is CIndex, which keeps track
of macro definitions and instantations so that they can be exposed via
cursors. At present, only token annotation uses these facilities, and
only for macro instantiations; both will change in the near
future. However, with this change, token annotation properly annotates
macro instantiations that do not produce any tokens and instantiations
of macros that are later undef'd, improving our consistency.
Preprocessing directives that are not macro definitions are still
handled by clang_annotateTokens() via re-lexing, so that we don't have
to track every preprocessing directive in the preprocessing record.
Performance impact of preprocessing records is still TBD, although it
is limited to CIndex and therefore out of the path of the main compiler.
llvm-svn: 98836
2010-03-19 01:52:52 +08:00
|
|
|
void PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) {
|
2011-09-21 07:27:33 +08:00
|
|
|
assert(Entity);
|
2011-10-13 01:36:33 +08:00
|
|
|
SourceLocation BeginLoc = Entity->getSourceRange().getBegin();
|
|
|
|
|
|
|
|
// Check normal case, this entity begin location is after the previous one.
|
|
|
|
if (PreprocessedEntities.empty() ||
|
|
|
|
!SourceMgr.isBeforeInTranslationUnit(BeginLoc,
|
|
|
|
PreprocessedEntities.back()->getSourceRange().getBegin())) {
|
|
|
|
PreprocessedEntities.push_back(Entity);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The entity's location is not after the previous one; this can happen rarely
|
|
|
|
// e.g. with "#include MACRO".
|
|
|
|
// Iterate the entities vector in reverse until we find the right place to
|
|
|
|
// insert the new entity.
|
|
|
|
for (std::vector<PreprocessedEntity *>::iterator
|
|
|
|
RI = PreprocessedEntities.end(), Begin = PreprocessedEntities.begin();
|
|
|
|
RI != Begin; --RI) {
|
|
|
|
std::vector<PreprocessedEntity *>::iterator I = RI;
|
|
|
|
--I;
|
|
|
|
if (!SourceMgr.isBeforeInTranslationUnit(BeginLoc,
|
|
|
|
(*I)->getSourceRange().getBegin())) {
|
|
|
|
PreprocessedEntities.insert(RI, Entity);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
Introduce the notion of a "preprocessing record", which keeps track of
the macro definitions and macro instantiations that are found
during preprocessing. Preprocessing records are *not* generated by
default; rather, we provide a PPCallbacks subclass that hooks into the
existing callback mechanism to record this activity.
The only client of preprocessing records is CIndex, which keeps track
of macro definitions and instantations so that they can be exposed via
cursors. At present, only token annotation uses these facilities, and
only for macro instantiations; both will change in the near
future. However, with this change, token annotation properly annotates
macro instantiations that do not produce any tokens and instantiations
of macros that are later undef'd, improving our consistency.
Preprocessing directives that are not macro definitions are still
handled by clang_annotateTokens() via re-lexing, so that we don't have
to track every preprocessing directive in the preprocessing record.
Performance impact of preprocessing records is still TBD, although it
is limited to CIndex and therefore out of the path of the main compiler.
llvm-svn: 98836
2010-03-19 01:52:52 +08:00
|
|
|
}
|
|
|
|
|
Implement serialization and lazy deserialization of the preprocessing
record (which includes all macro instantiations and definitions). As
with all lay deserialization, this introduces a new external source
(here, an external preprocessing record source) that loads all of the
preprocessed entities prior to iterating over the entities.
The preprocessing record is an optional part of the precompiled header
that is disabled by default (enabled with
-detailed-preprocessing-record). When the preprocessor given to the
PCH writer has a preprocessing record, that record is written into the
PCH file. When the PCH reader is given a PCH file that contains a
preprocessing record, it will be lazily loaded (which, effectively,
implicitly adds -detailed-preprocessing-record). This is the first
case where we have sections of the precompiled header that are
added/removed based on a compilation flag, which is
unfortunate. However, this data consumes ~550k in the PCH file for
Cocoa.h (out of ~9.9MB), and there is a non-trivial cost to gathering
this detailed preprocessing information, so it's too expensive to turn
on by default. In the future, we should investigate a better encoding
of this information.
llvm-svn: 99002
2010-03-20 05:51:54 +08:00
|
|
|
void PreprocessingRecord::SetExternalSource(
|
2011-07-21 08:47:40 +08:00
|
|
|
ExternalPreprocessingRecordSource &Source) {
|
Implement serialization and lazy deserialization of the preprocessing
record (which includes all macro instantiations and definitions). As
with all lay deserialization, this introduces a new external source
(here, an external preprocessing record source) that loads all of the
preprocessed entities prior to iterating over the entities.
The preprocessing record is an optional part of the precompiled header
that is disabled by default (enabled with
-detailed-preprocessing-record). When the preprocessor given to the
PCH writer has a preprocessing record, that record is written into the
PCH file. When the PCH reader is given a PCH file that contains a
preprocessing record, it will be lazily loaded (which, effectively,
implicitly adds -detailed-preprocessing-record). This is the first
case where we have sections of the precompiled header that are
added/removed based on a compilation flag, which is
unfortunate. However, this data consumes ~550k in the PCH file for
Cocoa.h (out of ~9.9MB), and there is a non-trivial cost to gathering
this detailed preprocessing information, so it's too expensive to turn
on by default. In the future, we should investigate a better encoding
of this information.
llvm-svn: 99002
2010-03-20 05:51:54 +08:00
|
|
|
assert(!ExternalSource &&
|
|
|
|
"Preprocessing record already has an external source");
|
|
|
|
ExternalSource = &Source;
|
|
|
|
}
|
|
|
|
|
2011-07-21 08:47:40 +08:00
|
|
|
unsigned PreprocessingRecord::allocateLoadedEntities(unsigned NumEntities) {
|
|
|
|
unsigned Result = LoadedPreprocessedEntities.size();
|
|
|
|
LoadedPreprocessedEntities.resize(LoadedPreprocessedEntities.size()
|
|
|
|
+ NumEntities);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2011-09-16 02:02:56 +08:00
|
|
|
void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro,
|
|
|
|
PPEntityID PPID) {
|
|
|
|
MacroDefinitions[Macro] = PPID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Retrieve the preprocessed entity at the given ID.
|
|
|
|
PreprocessedEntity *PreprocessingRecord::getPreprocessedEntity(PPEntityID PPID){
|
|
|
|
if (PPID < 0) {
|
|
|
|
assert(unsigned(-PPID-1) < LoadedPreprocessedEntities.size() &&
|
|
|
|
"Out-of bounds loaded preprocessed entity");
|
|
|
|
return getLoadedPreprocessedEntity(LoadedPreprocessedEntities.size()+PPID);
|
|
|
|
}
|
|
|
|
assert(unsigned(PPID) < PreprocessedEntities.size() &&
|
|
|
|
"Out-of bounds local preprocessed entity");
|
|
|
|
return PreprocessedEntities[PPID];
|
Implement serialization and lazy deserialization of the preprocessing
record (which includes all macro instantiations and definitions). As
with all lay deserialization, this introduces a new external source
(here, an external preprocessing record source) that loads all of the
preprocessed entities prior to iterating over the entities.
The preprocessing record is an optional part of the precompiled header
that is disabled by default (enabled with
-detailed-preprocessing-record). When the preprocessor given to the
PCH writer has a preprocessing record, that record is written into the
PCH file. When the PCH reader is given a PCH file that contains a
preprocessing record, it will be lazily loaded (which, effectively,
implicitly adds -detailed-preprocessing-record). This is the first
case where we have sections of the precompiled header that are
added/removed based on a compilation flag, which is
unfortunate. However, this data consumes ~550k in the PCH file for
Cocoa.h (out of ~9.9MB), and there is a non-trivial cost to gathering
this detailed preprocessing information, so it's too expensive to turn
on by default. In the future, we should investigate a better encoding
of this information.
llvm-svn: 99002
2010-03-20 05:51:54 +08:00
|
|
|
}
|
|
|
|
|
2011-09-16 02:02:56 +08:00
|
|
|
/// \brief Retrieve the loaded preprocessed entity at the given index.
|
|
|
|
PreprocessedEntity *
|
|
|
|
PreprocessingRecord::getLoadedPreprocessedEntity(unsigned Index) {
|
|
|
|
assert(Index < LoadedPreprocessedEntities.size() &&
|
|
|
|
"Out-of bounds loaded preprocessed entity");
|
|
|
|
assert(ExternalSource && "No external source to load from");
|
|
|
|
PreprocessedEntity *&Entity = LoadedPreprocessedEntities[Index];
|
|
|
|
if (!Entity) {
|
|
|
|
Entity = ExternalSource->ReadPreprocessedEntity(Index);
|
|
|
|
if (!Entity) // Failed to load.
|
|
|
|
Entity = new (*this)
|
|
|
|
PreprocessedEntity(PreprocessedEntity::InvalidKind, SourceRange());
|
|
|
|
}
|
|
|
|
return Entity;
|
Implement serialization and lazy deserialization of the preprocessing
record (which includes all macro instantiations and definitions). As
with all lay deserialization, this introduces a new external source
(here, an external preprocessing record source) that loads all of the
preprocessed entities prior to iterating over the entities.
The preprocessing record is an optional part of the precompiled header
that is disabled by default (enabled with
-detailed-preprocessing-record). When the preprocessor given to the
PCH writer has a preprocessing record, that record is written into the
PCH file. When the PCH reader is given a PCH file that contains a
preprocessing record, it will be lazily loaded (which, effectively,
implicitly adds -detailed-preprocessing-record). This is the first
case where we have sections of the precompiled header that are
added/removed based on a compilation flag, which is
unfortunate. However, this data consumes ~550k in the PCH file for
Cocoa.h (out of ~9.9MB), and there is a non-trivial cost to gathering
this detailed preprocessing information, so it's too expensive to turn
on by default. In the future, we should investigate a better encoding
of this information.
llvm-svn: 99002
2010-03-20 05:51:54 +08:00
|
|
|
}
|
|
|
|
|
2010-03-20 05:58:23 +08:00
|
|
|
MacroDefinition *PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) {
|
2011-09-16 02:02:56 +08:00
|
|
|
llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos
|
2010-03-20 01:12:43 +08:00
|
|
|
= MacroDefinitions.find(MI);
|
|
|
|
if (Pos == MacroDefinitions.end())
|
|
|
|
return 0;
|
|
|
|
|
2011-09-16 02:02:56 +08:00
|
|
|
PreprocessedEntity *Entity = getPreprocessedEntity(Pos->second);
|
|
|
|
if (Entity->isInvalid())
|
|
|
|
return 0;
|
|
|
|
return cast<MacroDefinition>(Entity);
|
2010-03-20 01:12:43 +08:00
|
|
|
}
|
|
|
|
|
2011-08-18 09:05:45 +08:00
|
|
|
void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI,
|
|
|
|
SourceRange Range) {
|
2011-07-14 16:20:46 +08:00
|
|
|
if (!IncludeNestedMacroExpansions && Id.getLocation().isMacroID())
|
2011-05-07 00:33:08 +08:00
|
|
|
return;
|
|
|
|
|
2011-09-09 01:18:41 +08:00
|
|
|
if (MI->isBuiltinMacro())
|
2011-09-20 04:40:25 +08:00
|
|
|
addPreprocessedEntity(
|
2011-09-09 01:18:41 +08:00
|
|
|
new (*this) MacroExpansion(Id.getIdentifierInfo(),Range));
|
|
|
|
else if (MacroDefinition *Def = findMacroDefinition(MI))
|
2011-09-20 04:40:25 +08:00
|
|
|
addPreprocessedEntity(
|
2011-09-09 01:18:41 +08:00
|
|
|
new (*this) MacroExpansion(Def, Range));
|
2010-03-20 01:12:43 +08:00
|
|
|
}
|
|
|
|
|
2010-11-20 05:33:15 +08:00
|
|
|
void PreprocessingRecord::MacroDefined(const Token &Id,
|
2010-03-20 01:12:43 +08:00
|
|
|
const MacroInfo *MI) {
|
|
|
|
SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
|
|
|
|
MacroDefinition *Def
|
2011-09-21 06:14:48 +08:00
|
|
|
= new (*this) MacroDefinition(Id.getIdentifierInfo(), R);
|
2011-09-20 04:40:25 +08:00
|
|
|
addPreprocessedEntity(Def);
|
2011-09-16 02:02:56 +08:00
|
|
|
MacroDefinitions[MI] = getPPEntityID(PreprocessedEntities.size()-1,
|
|
|
|
/*isLoaded=*/false);
|
2010-03-20 01:12:43 +08:00
|
|
|
}
|
Implement serialization and lazy deserialization of the preprocessing
record (which includes all macro instantiations and definitions). As
with all lay deserialization, this introduces a new external source
(here, an external preprocessing record source) that loads all of the
preprocessed entities prior to iterating over the entities.
The preprocessing record is an optional part of the precompiled header
that is disabled by default (enabled with
-detailed-preprocessing-record). When the preprocessor given to the
PCH writer has a preprocessing record, that record is written into the
PCH file. When the PCH reader is given a PCH file that contains a
preprocessing record, it will be lazily loaded (which, effectively,
implicitly adds -detailed-preprocessing-record). This is the first
case where we have sections of the precompiled header that are
added/removed based on a compilation flag, which is
unfortunate. However, this data consumes ~550k in the PCH file for
Cocoa.h (out of ~9.9MB), and there is a non-trivial cost to gathering
this detailed preprocessing information, so it's too expensive to turn
on by default. In the future, we should investigate a better encoding
of this information.
llvm-svn: 99002
2010-03-20 05:51:54 +08:00
|
|
|
|
2010-11-20 05:33:15 +08:00
|
|
|
void PreprocessingRecord::MacroUndefined(const Token &Id,
|
2010-03-20 05:58:23 +08:00
|
|
|
const MacroInfo *MI) {
|
2011-09-16 02:02:56 +08:00
|
|
|
llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos
|
2010-03-20 05:58:23 +08:00
|
|
|
= MacroDefinitions.find(MI);
|
|
|
|
if (Pos != MacroDefinitions.end())
|
|
|
|
MacroDefinitions.erase(Pos);
|
|
|
|
}
|
|
|
|
|
2011-03-17 02:34:36 +08:00
|
|
|
void PreprocessingRecord::InclusionDirective(
|
|
|
|
SourceLocation HashLoc,
|
|
|
|
const clang::Token &IncludeTok,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef FileName,
|
2011-03-17 02:34:36 +08:00
|
|
|
bool IsAngled,
|
|
|
|
const FileEntry *File,
|
|
|
|
clang::SourceLocation EndLoc,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef SearchPath,
|
|
|
|
StringRef RelativePath) {
|
2010-10-21 06:00:55 +08:00
|
|
|
InclusionDirective::InclusionKind Kind = InclusionDirective::Include;
|
|
|
|
|
|
|
|
switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
|
|
|
|
case tok::pp_include:
|
|
|
|
Kind = InclusionDirective::Include;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case tok::pp_import:
|
|
|
|
Kind = InclusionDirective::Import;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case tok::pp_include_next:
|
|
|
|
Kind = InclusionDirective::IncludeNext;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case tok::pp___include_macros:
|
|
|
|
Kind = InclusionDirective::IncludeMacros;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown include directive kind");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
clang::InclusionDirective *ID
|
2010-11-01 23:03:47 +08:00
|
|
|
= new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled,
|
|
|
|
File, SourceRange(HashLoc, EndLoc));
|
2011-09-20 04:40:25 +08:00
|
|
|
addPreprocessedEntity(ID);
|
2010-10-21 06:00:55 +08:00
|
|
|
}
|
2011-07-27 05:17:24 +08:00
|
|
|
|
|
|
|
size_t PreprocessingRecord::getTotalMemory() const {
|
|
|
|
return BumpAlloc.getTotalMemory()
|
2011-07-28 02:41:20 +08:00
|
|
|
+ llvm::capacity_in_bytes(MacroDefinitions)
|
|
|
|
+ llvm::capacity_in_bytes(PreprocessedEntities)
|
|
|
|
+ llvm::capacity_in_bytes(LoadedPreprocessedEntities);
|
2011-07-27 05:17:24 +08:00
|
|
|
}
|