
-Use an array of offsets for all preprocessed entities -Get rid of the separate array of offsets for just macro definitions; for references to macro definitions use an index inside the preprocessed entities array. -Deserialize each preprocessed entity lazily, at first request; not in bulk. Paves the way for binary searching of preprocessed entities that will offer efficiency and will simplify things on the libclang side a lot. llvm-svn: 139809
199 lines
6.9 KiB
C++
199 lines
6.9 KiB
C++
//===--- 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"
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
#include "llvm/Support/Capacity.h"
|
|
|
|
using namespace clang;
|
|
|
|
ExternalPreprocessingRecordSource::~ExternalPreprocessingRecordSource() { }
|
|
|
|
|
|
InclusionDirective::InclusionDirective(PreprocessingRecord &PPRec,
|
|
InclusionKind Kind,
|
|
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 = StringRef(Memory, FileName.size());
|
|
}
|
|
|
|
PreprocessingRecord::PreprocessingRecord(bool IncludeNestedMacroExpansions)
|
|
: IncludeNestedMacroExpansions(IncludeNestedMacroExpansions),
|
|
ExternalSource(0)
|
|
{
|
|
}
|
|
|
|
PreprocessingRecord::iterator
|
|
PreprocessingRecord::begin(bool OnlyLocalEntities) {
|
|
if (OnlyLocalEntities)
|
|
return iterator(this, 0);
|
|
|
|
return iterator(this, -(int)LoadedPreprocessedEntities.size());
|
|
}
|
|
|
|
PreprocessingRecord::iterator PreprocessingRecord::end(bool OnlyLocalEntities) {
|
|
return iterator(this, PreprocessedEntities.size());
|
|
}
|
|
|
|
void PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) {
|
|
PreprocessedEntities.push_back(Entity);
|
|
}
|
|
|
|
void PreprocessingRecord::SetExternalSource(
|
|
ExternalPreprocessingRecordSource &Source) {
|
|
assert(!ExternalSource &&
|
|
"Preprocessing record already has an external source");
|
|
ExternalSource = &Source;
|
|
}
|
|
|
|
unsigned PreprocessingRecord::allocateLoadedEntities(unsigned NumEntities) {
|
|
unsigned Result = LoadedPreprocessedEntities.size();
|
|
LoadedPreprocessedEntities.resize(LoadedPreprocessedEntities.size()
|
|
+ NumEntities);
|
|
return Result;
|
|
}
|
|
|
|
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];
|
|
}
|
|
|
|
/// \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;
|
|
}
|
|
|
|
MacroDefinition *PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) {
|
|
llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos
|
|
= MacroDefinitions.find(MI);
|
|
if (Pos == MacroDefinitions.end())
|
|
return 0;
|
|
|
|
PreprocessedEntity *Entity = getPreprocessedEntity(Pos->second);
|
|
if (Entity->isInvalid())
|
|
return 0;
|
|
return cast<MacroDefinition>(Entity);
|
|
}
|
|
|
|
void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI,
|
|
SourceRange Range) {
|
|
if (!IncludeNestedMacroExpansions && Id.getLocation().isMacroID())
|
|
return;
|
|
|
|
if (MI->isBuiltinMacro())
|
|
PreprocessedEntities.push_back(
|
|
new (*this) MacroExpansion(Id.getIdentifierInfo(),Range));
|
|
else if (MacroDefinition *Def = findMacroDefinition(MI))
|
|
PreprocessedEntities.push_back(
|
|
new (*this) MacroExpansion(Def, Range));
|
|
}
|
|
|
|
void PreprocessingRecord::MacroDefined(const Token &Id,
|
|
const MacroInfo *MI) {
|
|
SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
|
|
MacroDefinition *Def
|
|
= new (*this) MacroDefinition(Id.getIdentifierInfo(),
|
|
MI->getDefinitionLoc(),
|
|
R);
|
|
PreprocessedEntities.push_back(Def);
|
|
MacroDefinitions[MI] = getPPEntityID(PreprocessedEntities.size()-1,
|
|
/*isLoaded=*/false);
|
|
}
|
|
|
|
void PreprocessingRecord::MacroUndefined(const Token &Id,
|
|
const MacroInfo *MI) {
|
|
llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos
|
|
= MacroDefinitions.find(MI);
|
|
if (Pos != MacroDefinitions.end())
|
|
MacroDefinitions.erase(Pos);
|
|
}
|
|
|
|
void PreprocessingRecord::InclusionDirective(
|
|
SourceLocation HashLoc,
|
|
const clang::Token &IncludeTok,
|
|
StringRef FileName,
|
|
bool IsAngled,
|
|
const FileEntry *File,
|
|
clang::SourceLocation EndLoc,
|
|
StringRef SearchPath,
|
|
StringRef RelativePath) {
|
|
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
|
|
= new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled,
|
|
File, SourceRange(HashLoc, EndLoc));
|
|
PreprocessedEntities.push_back(ID);
|
|
}
|
|
|
|
size_t PreprocessingRecord::getTotalMemory() const {
|
|
return BumpAlloc.getTotalMemory()
|
|
+ llvm::capacity_in_bytes(MacroDefinitions)
|
|
+ llvm::capacity_in_bytes(PreprocessedEntities)
|
|
+ llvm::capacity_in_bytes(LoadedPreprocessedEntities);
|
|
}
|