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 "clang/Basic/IdentifierTable.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.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,
|
|
|
|
llvm::StringRef FileName,
|
|
|
|
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;
|
|
|
|
this->FileName = llvm::StringRef(Memory, FileName.size());
|
|
|
|
}
|
|
|
|
|
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::MaybeLoadPreallocatedEntities() const {
|
|
|
|
if (!ExternalSource || LoadedPreallocatedEntities)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LoadedPreallocatedEntities = true;
|
|
|
|
ExternalSource->ReadPreprocessedEntities();
|
|
|
|
}
|
|
|
|
|
2011-05-07 00:33:08 +08:00
|
|
|
PreprocessingRecord::PreprocessingRecord(bool IncludeNestedMacroInstantiations)
|
|
|
|
: IncludeNestedMacroInstantiations(IncludeNestedMacroInstantiations),
|
|
|
|
ExternalSource(0), NumPreallocatedEntities(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
|
|
|
LoadedPreallocatedEntities(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PreprocessingRecord::iterator
|
|
|
|
PreprocessingRecord::begin(bool OnlyLocalEntities) {
|
|
|
|
if (OnlyLocalEntities)
|
|
|
|
return PreprocessedEntities.begin() + NumPreallocatedEntities;
|
|
|
|
|
|
|
|
MaybeLoadPreallocatedEntities();
|
|
|
|
return PreprocessedEntities.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
PreprocessingRecord::iterator PreprocessingRecord::end(bool OnlyLocalEntities) {
|
|
|
|
if (!OnlyLocalEntities)
|
|
|
|
MaybeLoadPreallocatedEntities();
|
|
|
|
|
|
|
|
return PreprocessedEntities.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
PreprocessingRecord::const_iterator
|
|
|
|
PreprocessingRecord::begin(bool OnlyLocalEntities) const {
|
|
|
|
if (OnlyLocalEntities)
|
|
|
|
return PreprocessedEntities.begin() + NumPreallocatedEntities;
|
|
|
|
|
|
|
|
MaybeLoadPreallocatedEntities();
|
|
|
|
return PreprocessedEntities.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
PreprocessingRecord::const_iterator
|
|
|
|
PreprocessingRecord::end(bool OnlyLocalEntities) const {
|
|
|
|
if (!OnlyLocalEntities)
|
|
|
|
MaybeLoadPreallocatedEntities();
|
|
|
|
|
|
|
|
return PreprocessedEntities.end();
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
PreprocessedEntities.push_back(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
|
|
|
void PreprocessingRecord::SetExternalSource(
|
|
|
|
ExternalPreprocessingRecordSource &Source,
|
|
|
|
unsigned NumPreallocatedEntities) {
|
|
|
|
assert(!ExternalSource &&
|
|
|
|
"Preprocessing record already has an external source");
|
|
|
|
ExternalSource = &Source;
|
|
|
|
this->NumPreallocatedEntities = NumPreallocatedEntities;
|
|
|
|
PreprocessedEntities.insert(PreprocessedEntities.begin(),
|
|
|
|
NumPreallocatedEntities, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PreprocessingRecord::SetPreallocatedEntity(unsigned Index,
|
|
|
|
PreprocessedEntity *Entity) {
|
|
|
|
assert(Index < NumPreallocatedEntities &&"Out-of-bounds preallocated entity");
|
|
|
|
PreprocessedEntities[Index] = Entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro,
|
|
|
|
MacroDefinition *MD) {
|
|
|
|
MacroDefinitions[Macro] = MD;
|
|
|
|
}
|
|
|
|
|
2010-03-20 05:58:23 +08:00
|
|
|
MacroDefinition *PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) {
|
2010-03-20 01:12:43 +08:00
|
|
|
llvm::DenseMap<const MacroInfo *, MacroDefinition *>::iterator Pos
|
|
|
|
= MacroDefinitions.find(MI);
|
|
|
|
if (Pos == MacroDefinitions.end())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return Pos->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI) {
|
2011-05-07 00:33:08 +08:00
|
|
|
if (!IncludeNestedMacroInstantiations && Id.getLocation().isMacroID())
|
|
|
|
return;
|
|
|
|
|
2010-03-20 05:58:23 +08:00
|
|
|
if (MacroDefinition *Def = findMacroDefinition(MI))
|
|
|
|
PreprocessedEntities.push_back(
|
2010-03-20 01:12:43 +08:00
|
|
|
new (*this) MacroInstantiation(Id.getIdentifierInfo(),
|
|
|
|
Id.getLocation(),
|
2010-03-20 05:58:23 +08:00
|
|
|
Def));
|
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
|
2010-11-20 05:33:15 +08:00
|
|
|
= new (*this) MacroDefinition(Id.getIdentifierInfo(),
|
|
|
|
MI->getDefinitionLoc(),
|
|
|
|
R);
|
2010-03-20 01:12:43 +08:00
|
|
|
MacroDefinitions[MI] = Def;
|
|
|
|
PreprocessedEntities.push_back(Def);
|
|
|
|
}
|
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) {
|
|
|
|
llvm::DenseMap<const MacroInfo *, MacroDefinition *>::iterator Pos
|
|
|
|
= 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,
|
|
|
|
llvm::StringRef FileName,
|
|
|
|
bool IsAngled,
|
|
|
|
const FileEntry *File,
|
|
|
|
clang::SourceLocation EndLoc,
|
2011-04-27 05:50:03 +08:00
|
|
|
llvm::StringRef SearchPath,
|
|
|
|
llvm::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));
|
2010-10-21 06:00:55 +08:00
|
|
|
PreprocessedEntities.push_back(ID);
|
|
|
|
}
|