2017-12-09 06:39:26 +08:00
|
|
|
//===- PreprocessingRecord.cpp - Record of Preprocessing ------------------===//
|
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
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-12-09 06:39:26 +08:00
|
|
|
|
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
|
|
|
#include "clang/Lex/PreprocessingRecord.h"
|
2017-12-09 06:39:26 +08:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
|
|
|
#include "clang/Basic/LLVM.h"
|
|
|
|
#include "clang/Basic/SourceLocation.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Basic/TokenKinds.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
|
|
|
#include "clang/Lex/MacroInfo.h"
|
|
|
|
#include "clang/Lex/Token.h"
|
2017-12-09 06:39:26 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/iterator_range.h"
|
2011-07-28 02:41:20 +08:00
|
|
|
#include "llvm/Support/Capacity.h"
|
2017-12-09 06:39:26 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2017-12-09 06:39:26 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstring>
|
|
|
|
#include <iterator>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
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;
|
|
|
|
|
2017-12-09 06:39:26 +08:00
|
|
|
ExternalPreprocessingRecordSource::~ExternalPreprocessingRecordSource() =
|
|
|
|
default;
|
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-01 23:03:47 +08:00
|
|
|
InclusionDirective::InclusionDirective(PreprocessingRecord &PPRec,
|
2016-10-20 22:27:22 +08:00
|
|
|
InclusionKind Kind, StringRef FileName,
|
2012-10-03 00:10:46 +08:00
|
|
|
bool InQuotes, bool ImportedModule,
|
2016-10-20 22:27:22 +08:00
|
|
|
const FileEntry *File, SourceRange Range)
|
|
|
|
: PreprocessingDirective(InclusionDirectiveKind, Range), InQuotes(InQuotes),
|
|
|
|
Kind(Kind), ImportedModule(ImportedModule), File(File) {
|
|
|
|
char *Memory = (char *)PPRec.Allocate(FileName.size() + 1, alignof(char));
|
2010-11-01 23:03:47 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-12-09 06:39:26 +08:00
|
|
|
PreprocessingRecord::PreprocessingRecord(SourceManager &SM) : SourceMgr(SM) {}
|
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
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns a pair of [Begin, End) iterators of preprocessed entities
|
2012-06-22 13:37:13 +08:00
|
|
|
/// that source range \p Range encompasses.
|
2015-02-07 01:25:10 +08:00
|
|
|
llvm::iterator_range<PreprocessingRecord::iterator>
|
2011-09-20 04:40:25 +08:00
|
|
|
PreprocessingRecord::getPreprocessedEntitiesInRange(SourceRange Range) {
|
|
|
|
if (Range.isInvalid())
|
2015-02-07 01:25:10 +08:00
|
|
|
return llvm::make_range(iterator(), iterator());
|
2011-10-25 08:29:50 +08:00
|
|
|
|
|
|
|
if (CachedRangeQuery.Range == Range) {
|
2015-02-07 01:25:10 +08:00
|
|
|
return llvm::make_range(iterator(this, CachedRangeQuery.Result.first),
|
|
|
|
iterator(this, CachedRangeQuery.Result.second));
|
2011-10-25 08:29:50 +08:00
|
|
|
}
|
|
|
|
|
2012-10-05 08:22:28 +08:00
|
|
|
std::pair<int, int> Res = getPreprocessedEntitiesInRangeSlow(Range);
|
2011-10-25 08:29:50 +08:00
|
|
|
|
|
|
|
CachedRangeQuery.Range = Range;
|
|
|
|
CachedRangeQuery.Result = Res;
|
2015-02-07 01:25:10 +08:00
|
|
|
|
|
|
|
return llvm::make_range(iterator(this, Res.first),
|
|
|
|
iterator(this, Res.second));
|
2011-10-25 08:29:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool isPreprocessedEntityIfInFileID(PreprocessedEntity *PPE, FileID FID,
|
|
|
|
SourceManager &SM) {
|
2015-10-03 18:46:20 +08:00
|
|
|
assert(FID.isValid());
|
2011-10-25 08:29:50 +08:00
|
|
|
if (!PPE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SourceLocation Loc = PPE->getSourceRange().getBegin();
|
|
|
|
if (Loc.isInvalid())
|
|
|
|
return false;
|
2015-03-09 10:02:07 +08:00
|
|
|
|
|
|
|
return SM.isInFileID(SM.getFileLoc(Loc), FID);
|
2011-10-25 08:29:50 +08:00
|
|
|
}
|
2011-09-20 04:40:25 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns true if the preprocessed entity that \arg PPEI iterator
|
2011-10-25 08:29:50 +08:00
|
|
|
/// 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
|
2012-06-22 13:37:13 +08:00
|
|
|
/// and not from files \#included in the range given at
|
2011-10-25 08:29:50 +08:00
|
|
|
/// \see getPreprocessedEntitiesInRange.
|
|
|
|
bool PreprocessingRecord::isEntityInFileID(iterator PPEI, FileID FID) {
|
|
|
|
if (FID.isInvalid())
|
|
|
|
return false;
|
|
|
|
|
2015-03-15 23:27:19 +08:00
|
|
|
int Pos = std::distance(iterator(this, 0), PPEI);
|
2012-10-05 08:22:28 +08:00
|
|
|
if (Pos < 0) {
|
2013-02-13 05:41:23 +08:00
|
|
|
if (unsigned(-Pos-1) >= LoadedPreprocessedEntities.size()) {
|
|
|
|
assert(0 && "Out-of bounds loaded preprocessed entity");
|
|
|
|
return false;
|
|
|
|
}
|
2011-10-25 08:29:50 +08:00
|
|
|
assert(ExternalSource && "No external source to load from");
|
2012-10-05 08:22:28 +08:00
|
|
|
unsigned LoadedIndex = LoadedPreprocessedEntities.size()+Pos;
|
2011-10-25 08:29:50 +08:00
|
|
|
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.
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<bool> IsInFile =
|
|
|
|
ExternalSource->isPreprocessedEntityInFileID(LoadedIndex, FID);
|
2011-10-25 08:29:50 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-02-13 05:41:23 +08:00
|
|
|
if (unsigned(Pos) >= PreprocessedEntities.size()) {
|
|
|
|
assert(0 && "Out-of bounds local preprocessed entity");
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-05 08:22:28 +08:00
|
|
|
return isPreprocessedEntityIfInFileID(PreprocessedEntities[Pos],
|
2011-10-25 08:29:50 +08:00
|
|
|
FID, SourceMgr);
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns a pair of [Begin, End) iterators of preprocessed entities
|
2011-10-25 08:29:50 +08:00
|
|
|
/// that source range \arg R encompasses.
|
2012-10-05 08:22:28 +08:00
|
|
|
std::pair<int, int>
|
2011-10-25 08:29:50 +08:00
|
|
|
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;
|
|
|
|
|
2017-12-09 06:39:26 +08:00
|
|
|
explicit PPEntityComp(const SourceManager &SM) : SM(SM) {}
|
2011-09-20 04:40:25 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-09 06:39:26 +08:00
|
|
|
} // namespace
|
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();
|
|
|
|
}
|
|
|
|
|
2012-03-28 02:47:48 +08:00
|
|
|
PreprocessingRecord::PPEntityID
|
|
|
|
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();
|
2012-03-28 02:47:48 +08:00
|
|
|
|
2015-05-04 10:25:31 +08:00
|
|
|
if (isa<MacroDefinitionRecord>(Entity)) {
|
2012-03-28 02:47:48 +08:00
|
|
|
assert((PreprocessedEntities.empty() ||
|
2015-05-04 10:25:31 +08:00
|
|
|
!SourceMgr.isBeforeInTranslationUnit(
|
|
|
|
BeginLoc,
|
|
|
|
PreprocessedEntities.back()->getSourceRange().getBegin())) &&
|
2013-01-10 07:22:20 +08:00
|
|
|
"a macro definition was encountered out-of-order");
|
2012-03-28 02:47:48 +08:00
|
|
|
PreprocessedEntities.push_back(Entity);
|
|
|
|
return getPPEntityID(PreprocessedEntities.size()-1, /*isLoaded=*/false);
|
|
|
|
}
|
|
|
|
|
2011-10-13 01:36:33 +08:00
|
|
|
// 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);
|
2012-03-28 02:47:48 +08:00
|
|
|
return getPPEntityID(PreprocessedEntities.size()-1, /*isLoaded=*/false);
|
2011-10-13 01:36:33 +08:00
|
|
|
}
|
|
|
|
|
2012-03-28 02:47:48 +08:00
|
|
|
// The entity's location is not after the previous one; this can happen with
|
|
|
|
// include directives that form the filename using macros, e.g:
|
2013-01-10 07:22:20 +08:00
|
|
|
// "#include MACRO(STUFF)"
|
|
|
|
// or with macro expansions inside macro arguments where the arguments are
|
|
|
|
// not expanded in the same order as listed, e.g:
|
|
|
|
// \code
|
|
|
|
// #define M1 1
|
|
|
|
// #define M2 2
|
|
|
|
// #define FM(x,y) y x
|
|
|
|
// FM(M1, M2)
|
|
|
|
// \endcode
|
2012-03-28 02:47:48 +08:00
|
|
|
|
2017-12-09 06:39:26 +08:00
|
|
|
using pp_iter = std::vector<PreprocessedEntity *>::iterator;
|
2012-03-28 02:47:48 +08:00
|
|
|
|
|
|
|
// Usually there are few macro expansions when defining the filename, do a
|
|
|
|
// linear search for a few entities.
|
|
|
|
unsigned count = 0;
|
|
|
|
for (pp_iter RI = PreprocessedEntities.end(),
|
|
|
|
Begin = PreprocessedEntities.begin();
|
|
|
|
RI != Begin && count < 4; --RI, ++count) {
|
|
|
|
pp_iter I = RI;
|
2011-10-13 01:36:33 +08:00
|
|
|
--I;
|
|
|
|
if (!SourceMgr.isBeforeInTranslationUnit(BeginLoc,
|
|
|
|
(*I)->getSourceRange().getBegin())) {
|
2012-03-28 02:47:48 +08:00
|
|
|
pp_iter insertI = PreprocessedEntities.insert(RI, Entity);
|
|
|
|
return getPPEntityID(insertI - PreprocessedEntities.begin(),
|
|
|
|
/*isLoaded=*/false);
|
2011-10-13 01:36:33 +08:00
|
|
|
}
|
|
|
|
}
|
2012-03-28 02:47:48 +08:00
|
|
|
|
|
|
|
// Linear search unsuccessful. Do a binary search.
|
|
|
|
pp_iter I = std::upper_bound(PreprocessedEntities.begin(),
|
|
|
|
PreprocessedEntities.end(),
|
|
|
|
BeginLoc,
|
|
|
|
PPEntityComp<&SourceRange::getBegin>(SourceMgr));
|
|
|
|
pp_iter insertI = PreprocessedEntities.insert(I, Entity);
|
|
|
|
return getPPEntityID(insertI - PreprocessedEntities.begin(),
|
|
|
|
/*isLoaded=*/false);
|
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;
|
|
|
|
}
|
|
|
|
|
2018-01-16 03:14:16 +08:00
|
|
|
unsigned PreprocessingRecord::allocateSkippedRanges(unsigned NumRanges) {
|
|
|
|
unsigned Result = SkippedRanges.size();
|
|
|
|
SkippedRanges.resize(SkippedRanges.size() + NumRanges);
|
|
|
|
SkippedRangesAllLoaded = false;
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PreprocessingRecord::ensureSkippedRangesLoaded() {
|
|
|
|
if (SkippedRangesAllLoaded || !ExternalSource)
|
|
|
|
return;
|
|
|
|
for (unsigned Index = 0; Index != SkippedRanges.size(); ++Index) {
|
|
|
|
if (SkippedRanges[Index].isInvalid())
|
|
|
|
SkippedRanges[Index] = ExternalSource->ReadSkippedRange(Index);
|
|
|
|
}
|
|
|
|
SkippedRangesAllLoaded = true;
|
|
|
|
}
|
|
|
|
|
2011-09-16 02:02:56 +08:00
|
|
|
void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro,
|
2015-05-04 10:25:31 +08:00
|
|
|
MacroDefinitionRecord *Def) {
|
2013-02-23 02:35:59 +08:00
|
|
|
MacroDefinitions[Macro] = Def;
|
2011-09-16 02:02:56 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Retrieve the preprocessed entity at the given ID.
|
2011-09-16 02:02:56 +08:00
|
|
|
PreprocessedEntity *PreprocessingRecord::getPreprocessedEntity(PPEntityID PPID){
|
2012-10-05 08:22:28 +08:00
|
|
|
if (PPID.ID < 0) {
|
|
|
|
unsigned Index = -PPID.ID - 1;
|
|
|
|
assert(Index < LoadedPreprocessedEntities.size() &&
|
2011-09-16 02:02:56 +08:00
|
|
|
"Out-of bounds loaded preprocessed entity");
|
2012-10-05 08:22:28 +08:00
|
|
|
return getLoadedPreprocessedEntity(Index);
|
2011-09-16 02:02:56 +08:00
|
|
|
}
|
2012-10-05 08:22:28 +08:00
|
|
|
|
|
|
|
if (PPID.ID == 0)
|
2014-05-18 07:10:59 +08:00
|
|
|
return nullptr;
|
2012-10-05 08:22:28 +08:00
|
|
|
unsigned Index = PPID.ID - 1;
|
|
|
|
assert(Index < PreprocessedEntities.size() &&
|
2011-09-16 02:02:56 +08:00
|
|
|
"Out-of bounds local preprocessed entity");
|
2012-10-05 08:22:28 +08:00
|
|
|
return PreprocessedEntities[Index];
|
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
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Retrieve the loaded preprocessed entity at the given index.
|
2011-09-16 02:02:56 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-05-04 10:25:31 +08:00
|
|
|
MacroDefinitionRecord *
|
|
|
|
PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) {
|
|
|
|
llvm::DenseMap<const MacroInfo *, MacroDefinitionRecord *>::iterator Pos =
|
|
|
|
MacroDefinitions.find(MI);
|
2010-03-20 01:12:43 +08:00
|
|
|
if (Pos == MacroDefinitions.end())
|
2014-05-18 07:10:59 +08:00
|
|
|
return nullptr;
|
2013-02-23 02:35:59 +08:00
|
|
|
|
|
|
|
return Pos->second;
|
2010-03-20 01:12:43 +08:00
|
|
|
}
|
|
|
|
|
2012-12-08 10:21:17 +08:00
|
|
|
void PreprocessingRecord::addMacroExpansion(const Token &Id,
|
|
|
|
const MacroInfo *MI,
|
|
|
|
SourceRange Range) {
|
2012-02-25 10:41:16 +08:00
|
|
|
// We don't record nested macro expansions.
|
|
|
|
if (Id.getLocation().isMacroID())
|
2011-05-07 00:33:08 +08:00
|
|
|
return;
|
|
|
|
|
2011-09-09 01:18:41 +08:00
|
|
|
if (MI->isBuiltinMacro())
|
2015-05-04 10:25:31 +08:00
|
|
|
addPreprocessedEntity(new (*this)
|
|
|
|
MacroExpansion(Id.getIdentifierInfo(), Range));
|
|
|
|
else if (MacroDefinitionRecord *Def = findMacroDefinition(MI))
|
|
|
|
addPreprocessedEntity(new (*this) MacroExpansion(Def, Range));
|
2010-03-20 01:12:43 +08:00
|
|
|
}
|
|
|
|
|
2012-12-08 10:21:17 +08:00
|
|
|
void PreprocessingRecord::Ifdef(SourceLocation Loc, const Token &MacroNameTok,
|
2015-05-04 11:15:40 +08:00
|
|
|
const MacroDefinition &MD) {
|
2012-12-08 10:21:17 +08:00
|
|
|
// This is not actually a macro expansion but record it as a macro reference.
|
2013-02-24 08:05:14 +08:00
|
|
|
if (MD)
|
2015-05-04 11:15:40 +08:00
|
|
|
addMacroExpansion(MacroNameTok, MD.getMacroInfo(),
|
2013-03-27 01:17:01 +08:00
|
|
|
MacroNameTok.getLocation());
|
2012-12-08 10:21:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PreprocessingRecord::Ifndef(SourceLocation Loc, const Token &MacroNameTok,
|
2015-05-04 11:15:40 +08:00
|
|
|
const MacroDefinition &MD) {
|
2012-12-08 10:21:17 +08:00
|
|
|
// This is not actually a macro expansion but record it as a macro reference.
|
2013-02-24 08:05:14 +08:00
|
|
|
if (MD)
|
2015-05-04 11:15:40 +08:00
|
|
|
addMacroExpansion(MacroNameTok, MD.getMacroInfo(),
|
2013-03-27 01:17:01 +08:00
|
|
|
MacroNameTok.getLocation());
|
2012-12-08 10:21:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PreprocessingRecord::Defined(const Token &MacroNameTok,
|
2015-05-04 11:15:40 +08:00
|
|
|
const MacroDefinition &MD,
|
2013-07-20 02:50:04 +08:00
|
|
|
SourceRange Range) {
|
2012-12-08 10:21:17 +08:00
|
|
|
// This is not actually a macro expansion but record it as a macro reference.
|
2013-02-24 08:05:14 +08:00
|
|
|
if (MD)
|
2015-05-04 11:15:40 +08:00
|
|
|
addMacroExpansion(MacroNameTok, MD.getMacroInfo(),
|
2013-03-27 01:17:01 +08:00
|
|
|
MacroNameTok.getLocation());
|
2012-12-08 10:21:17 +08:00
|
|
|
}
|
|
|
|
|
2017-09-12 04:47:42 +08:00
|
|
|
void PreprocessingRecord::SourceRangeSkipped(SourceRange Range,
|
|
|
|
SourceLocation EndifLoc) {
|
2018-01-16 03:14:16 +08:00
|
|
|
assert(Range.isValid());
|
2017-09-12 04:47:42 +08:00
|
|
|
SkippedRanges.emplace_back(Range.getBegin(), EndifLoc);
|
2013-12-05 16:19:32 +08:00
|
|
|
}
|
|
|
|
|
2015-05-04 11:15:40 +08:00
|
|
|
void PreprocessingRecord::MacroExpands(const Token &Id,
|
|
|
|
const MacroDefinition &MD,
|
2013-05-04 06:31:32 +08:00
|
|
|
SourceRange Range,
|
|
|
|
const MacroArgs *Args) {
|
2015-05-04 11:15:40 +08:00
|
|
|
addMacroExpansion(Id, MD.getMacroInfo(), Range);
|
2012-12-08 10:21:17 +08:00
|
|
|
}
|
|
|
|
|
2010-11-20 05:33:15 +08:00
|
|
|
void PreprocessingRecord::MacroDefined(const Token &Id,
|
2013-02-24 08:05:14 +08:00
|
|
|
const MacroDirective *MD) {
|
2013-03-27 01:17:01 +08:00
|
|
|
const MacroInfo *MI = MD->getMacroInfo();
|
2010-03-20 01:12:43 +08:00
|
|
|
SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
|
2015-05-04 10:25:31 +08:00
|
|
|
MacroDefinitionRecord *Def =
|
|
|
|
new (*this) MacroDefinitionRecord(Id.getIdentifierInfo(), R);
|
2013-02-23 02:35:59 +08:00
|
|
|
addPreprocessedEntity(Def);
|
|
|
|
MacroDefinitions[MI] = Def;
|
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,
|
2017-04-27 05:05:44 +08:00
|
|
|
const MacroDefinition &MD,
|
|
|
|
const MacroDirective *Undef) {
|
2015-05-04 11:15:40 +08:00
|
|
|
MD.forAllDefinitions([&](MacroInfo *MI) { MacroDefinitions.erase(MI); });
|
2010-03-20 05:58:23 +08:00
|
|
|
}
|
|
|
|
|
2011-03-17 02:34:36 +08:00
|
|
|
void PreprocessingRecord::InclusionDirective(
|
|
|
|
SourceLocation HashLoc,
|
2017-12-09 06:39:26 +08:00
|
|
|
const Token &IncludeTok,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef FileName,
|
2011-03-17 02:34:36 +08:00
|
|
|
bool IsAngled,
|
2012-09-27 09:42:07 +08:00
|
|
|
CharSourceRange FilenameRange,
|
2011-03-17 02:34:36 +08:00
|
|
|
const FileEntry *File,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef SearchPath,
|
2012-09-29 09:06:10 +08:00
|
|
|
StringRef RelativePath,
|
2018-05-10 06:25:47 +08:00
|
|
|
const Module *Imported) {
|
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");
|
|
|
|
}
|
2012-09-27 09:42:07 +08:00
|
|
|
|
|
|
|
SourceLocation EndLoc;
|
|
|
|
if (!IsAngled) {
|
|
|
|
EndLoc = FilenameRange.getBegin();
|
|
|
|
} else {
|
|
|
|
EndLoc = FilenameRange.getEnd();
|
|
|
|
if (FilenameRange.isCharRange())
|
|
|
|
EndLoc = EndLoc.getLocWithOffset(-1); // the InclusionDirective expects
|
|
|
|
// a token range.
|
|
|
|
}
|
2017-12-09 06:39:26 +08:00
|
|
|
clang::InclusionDirective *ID =
|
|
|
|
new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled,
|
|
|
|
(bool)Imported, 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)
|
2018-01-16 03:14:16 +08:00
|
|
|
+ llvm::capacity_in_bytes(LoadedPreprocessedEntities)
|
|
|
|
+ llvm::capacity_in_bytes(SkippedRanges);
|
2011-07-27 05:17:24 +08:00
|
|
|
}
|