2010-03-21 10:50:08 +00:00
|
|
|
//===--- PreprocessingRecord.h - 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 defines the PreprocessingRecord class, which maintains a record
|
|
|
|
// of what occurred during preprocessing.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef LLVM_CLANG_LEX_PREPROCESSINGRECORD_H
|
|
|
|
#define LLVM_CLANG_LEX_PREPROCESSINGRECORD_H
|
|
|
|
|
2011-10-20 21:14:49 +00:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
2013-04-08 18:45:10 +00:00
|
|
|
#include "clang/Basic/SourceLocation.h"
|
|
|
|
#include "clang/Lex/PPCallbacks.h"
|
2010-03-21 10:50:08 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2012-04-14 14:01:31 +00:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2013-04-08 18:45:10 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2010-03-21 10:50:08 +00:00
|
|
|
#include "llvm/Support/Allocator.h"
|
2012-04-14 14:01:31 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2010-03-21 10:50:08 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
class IdentifierInfo;
|
2013-04-08 18:45:10 +00:00
|
|
|
class MacroInfo;
|
2010-03-21 10:50:08 +00:00
|
|
|
class PreprocessingRecord;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Allocates memory within a Clang preprocessing record.
|
|
|
|
void* operator new(size_t bytes, clang::PreprocessingRecord& PR,
|
|
|
|
unsigned alignment = 8) throw();
|
|
|
|
|
|
|
|
/// \brief Frees memory allocated in a Clang preprocessing record.
|
|
|
|
void operator delete(void* ptr, clang::PreprocessingRecord& PR,
|
|
|
|
unsigned) throw();
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
class MacroDefinition;
|
2011-02-20 13:06:31 +00:00
|
|
|
class FileEntry;
|
2010-03-21 10:50:08 +00:00
|
|
|
|
|
|
|
/// \brief Base class that describes a preprocessed entity, which may be a
|
2011-07-17 15:40:56 +00:00
|
|
|
/// preprocessor directive or macro expansion.
|
2010-03-21 10:50:08 +00:00
|
|
|
class PreprocessedEntity {
|
|
|
|
public:
|
|
|
|
/// \brief The kind of preprocessed entity an object describes.
|
|
|
|
enum EntityKind {
|
2011-10-20 21:14:49 +00:00
|
|
|
/// \brief Indicates a problem trying to load the preprocessed entity.
|
|
|
|
InvalidKind,
|
|
|
|
|
2011-07-17 15:40:56 +00:00
|
|
|
/// \brief A macro expansion.
|
|
|
|
MacroExpansionKind,
|
2010-03-21 10:50:08 +00:00
|
|
|
|
2011-10-20 21:14:49 +00:00
|
|
|
/// \defgroup Preprocessing directives
|
|
|
|
/// @{
|
2010-03-21 10:50:08 +00:00
|
|
|
|
|
|
|
/// \brief A macro definition.
|
|
|
|
MacroDefinitionKind,
|
|
|
|
|
2012-08-15 20:02:54 +00:00
|
|
|
/// \brief An inclusion directive, such as \c \#include, \c
|
|
|
|
/// \#import, or \c \#include_next.
|
2011-02-20 13:06:31 +00:00
|
|
|
InclusionDirectiveKind,
|
|
|
|
|
2011-10-20 21:14:49 +00:00
|
|
|
/// @}
|
|
|
|
|
|
|
|
FirstPreprocessingDirective = MacroDefinitionKind,
|
2011-02-20 13:06:31 +00:00
|
|
|
LastPreprocessingDirective = InclusionDirectiveKind
|
2010-03-21 10:50:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// \brief The kind of preprocessed entity that this object describes.
|
|
|
|
EntityKind Kind;
|
|
|
|
|
|
|
|
/// \brief The source range that covers this preprocessed entity.
|
|
|
|
SourceRange Range;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
PreprocessedEntity(EntityKind Kind, SourceRange Range)
|
|
|
|
: Kind(Kind), Range(Range) { }
|
2011-10-20 21:14:49 +00:00
|
|
|
|
|
|
|
friend class PreprocessingRecord;
|
|
|
|
|
2010-03-21 10:50:08 +00:00
|
|
|
public:
|
|
|
|
/// \brief Retrieve the kind of preprocessed entity stored in this object.
|
|
|
|
EntityKind getKind() const { return Kind; }
|
|
|
|
|
|
|
|
/// \brief Retrieve the source range that covers this entire preprocessed
|
|
|
|
/// entity.
|
2012-04-14 14:01:31 +00:00
|
|
|
SourceRange getSourceRange() const LLVM_READONLY { return Range; }
|
2011-10-20 21:14:49 +00:00
|
|
|
|
|
|
|
/// \brief Returns true if there was a problem loading the preprocessed
|
|
|
|
/// entity.
|
|
|
|
bool isInvalid() const { return Kind == InvalidKind; }
|
|
|
|
|
2010-03-21 10:50:08 +00:00
|
|
|
// Only allow allocation of preprocessed entities using the allocator
|
|
|
|
// in PreprocessingRecord or by doing a placement new.
|
|
|
|
void* operator new(size_t bytes, PreprocessingRecord& PR,
|
|
|
|
unsigned alignment = 8) throw() {
|
|
|
|
return ::operator new(bytes, PR, alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* operator new(size_t bytes, void* mem) throw() {
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator delete(void* ptr, PreprocessingRecord& PR,
|
|
|
|
unsigned alignment) throw() {
|
|
|
|
return ::operator delete(ptr, PR, alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator delete(void*, std::size_t) throw() { }
|
|
|
|
void operator delete(void*, void*) throw() { }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Make vanilla 'new' and 'delete' illegal for preprocessed entities.
|
|
|
|
void* operator new(size_t bytes) throw();
|
|
|
|
void operator delete(void* data) throw();
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Records the presence of a preprocessor directive.
|
|
|
|
class PreprocessingDirective : public PreprocessedEntity {
|
|
|
|
public:
|
|
|
|
PreprocessingDirective(EntityKind Kind, SourceRange Range)
|
|
|
|
: PreprocessedEntity(Kind, Range) { }
|
|
|
|
|
|
|
|
// Implement isa/cast/dyncast/etc.
|
|
|
|
static bool classof(const PreprocessedEntity *PD) {
|
|
|
|
return PD->getKind() >= FirstPreprocessingDirective &&
|
|
|
|
PD->getKind() <= LastPreprocessingDirective;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Record the location of a macro definition.
|
|
|
|
class MacroDefinition : public PreprocessingDirective {
|
|
|
|
/// \brief The name of the macro being defined.
|
|
|
|
const IdentifierInfo *Name;
|
|
|
|
|
|
|
|
public:
|
2011-10-20 21:14:49 +00:00
|
|
|
explicit MacroDefinition(const IdentifierInfo *Name, SourceRange Range)
|
|
|
|
: PreprocessingDirective(MacroDefinitionKind, Range), Name(Name) { }
|
2010-03-21 10:50:08 +00:00
|
|
|
|
|
|
|
/// \brief Retrieve the name of the macro being defined.
|
|
|
|
const IdentifierInfo *getName() const { return Name; }
|
|
|
|
|
|
|
|
/// \brief Retrieve the location of the macro name in the definition.
|
2011-10-20 21:14:49 +00:00
|
|
|
SourceLocation getLocation() const { return getSourceRange().getBegin(); }
|
2010-03-21 10:50:08 +00:00
|
|
|
|
|
|
|
// Implement isa/cast/dyncast/etc.
|
|
|
|
static bool classof(const PreprocessedEntity *PE) {
|
|
|
|
return PE->getKind() == MacroDefinitionKind;
|
|
|
|
}
|
|
|
|
};
|
2011-10-20 21:14:49 +00:00
|
|
|
|
|
|
|
/// \brief Records the location of a macro expansion.
|
|
|
|
class MacroExpansion : public PreprocessedEntity {
|
|
|
|
/// \brief The definition of this macro or the name of the macro if it is
|
|
|
|
/// a builtin macro.
|
|
|
|
llvm::PointerUnion<IdentifierInfo *, MacroDefinition *> NameOrDef;
|
|
|
|
|
|
|
|
public:
|
|
|
|
MacroExpansion(IdentifierInfo *BuiltinName, SourceRange Range)
|
|
|
|
: PreprocessedEntity(MacroExpansionKind, Range),
|
|
|
|
NameOrDef(BuiltinName) { }
|
|
|
|
|
|
|
|
MacroExpansion(MacroDefinition *Definition, SourceRange Range)
|
|
|
|
: PreprocessedEntity(MacroExpansionKind, Range),
|
|
|
|
NameOrDef(Definition) { }
|
|
|
|
|
|
|
|
/// \brief True if it is a builtin macro.
|
|
|
|
bool isBuiltinMacro() const { return NameOrDef.is<IdentifierInfo *>(); }
|
|
|
|
|
|
|
|
/// \brief The name of the macro being expanded.
|
|
|
|
const IdentifierInfo *getName() const {
|
|
|
|
if (MacroDefinition *Def = getDefinition())
|
|
|
|
return Def->getName();
|
|
|
|
return NameOrDef.get<IdentifierInfo*>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief The definition of the macro being expanded. May return null if
|
|
|
|
/// this is a builtin macro.
|
|
|
|
MacroDefinition *getDefinition() const {
|
|
|
|
return NameOrDef.dyn_cast<MacroDefinition *>();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement isa/cast/dyncast/etc.
|
|
|
|
static bool classof(const PreprocessedEntity *PE) {
|
|
|
|
return PE->getKind() == MacroExpansionKind;
|
|
|
|
}
|
|
|
|
};
|
2011-02-20 13:06:31 +00:00
|
|
|
|
|
|
|
/// \brief Record the location of an inclusion directive, such as an
|
2012-08-15 20:02:54 +00:00
|
|
|
/// \c \#include or \c \#import statement.
|
2011-02-20 13:06:31 +00:00
|
|
|
class InclusionDirective : public PreprocessingDirective {
|
|
|
|
public:
|
|
|
|
/// \brief The kind of inclusion directives known to the
|
|
|
|
/// preprocessor.
|
|
|
|
enum InclusionKind {
|
2012-08-15 20:02:54 +00:00
|
|
|
/// \brief An \c \#include directive.
|
2011-02-20 13:06:31 +00:00
|
|
|
Include,
|
2012-08-15 20:02:54 +00:00
|
|
|
/// \brief An Objective-C \c \#import directive.
|
2011-02-20 13:06:31 +00:00
|
|
|
Import,
|
2012-08-15 20:02:54 +00:00
|
|
|
/// \brief A GNU \c \#include_next directive.
|
2011-02-20 13:06:31 +00:00
|
|
|
IncludeNext,
|
2012-08-15 20:02:54 +00:00
|
|
|
/// \brief A Clang \c \#__include_macros directive.
|
2011-02-20 13:06:31 +00:00
|
|
|
IncludeMacros
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// \brief The name of the file that was included, as written in
|
|
|
|
/// the source.
|
2011-10-20 21:14:49 +00:00
|
|
|
StringRef FileName;
|
2011-02-20 13:06:31 +00:00
|
|
|
|
|
|
|
/// \brief Whether the file name was in quotation marks; otherwise, it was
|
|
|
|
/// in angle brackets.
|
|
|
|
unsigned InQuotes : 1;
|
|
|
|
|
|
|
|
/// \brief The kind of inclusion directive we have.
|
|
|
|
///
|
|
|
|
/// This is a value of type InclusionKind.
|
|
|
|
unsigned Kind : 2;
|
|
|
|
|
2012-12-02 13:20:44 +00:00
|
|
|
/// \brief Whether the inclusion directive was automatically turned into
|
|
|
|
/// a module import.
|
|
|
|
unsigned ImportedModule : 1;
|
|
|
|
|
2011-02-20 13:06:31 +00:00
|
|
|
/// \brief The file that was included.
|
|
|
|
const FileEntry *File;
|
|
|
|
|
|
|
|
public:
|
|
|
|
InclusionDirective(PreprocessingRecord &PPRec,
|
2011-10-20 21:14:49 +00:00
|
|
|
InclusionKind Kind, StringRef FileName,
|
2012-12-02 13:20:44 +00:00
|
|
|
bool InQuotes, bool ImportedModule,
|
|
|
|
const FileEntry *File, SourceRange Range);
|
2011-02-20 13:06:31 +00:00
|
|
|
|
|
|
|
/// \brief Determine what kind of inclusion directive this is.
|
|
|
|
InclusionKind getKind() const { return static_cast<InclusionKind>(Kind); }
|
|
|
|
|
|
|
|
/// \brief Retrieve the included file name as it was written in the source.
|
2011-10-20 21:14:49 +00:00
|
|
|
StringRef getFileName() const { return FileName; }
|
2011-02-20 13:06:31 +00:00
|
|
|
|
|
|
|
/// \brief Determine whether the included file name was written in quotes;
|
|
|
|
/// otherwise, it was written in angle brackets.
|
|
|
|
bool wasInQuotes() const { return InQuotes; }
|
2012-12-02 13:20:44 +00:00
|
|
|
|
|
|
|
/// \brief Determine whether the inclusion directive was automatically
|
|
|
|
/// turned into a module import.
|
|
|
|
bool importedModule() const { return ImportedModule; }
|
2011-02-20 13:06:31 +00:00
|
|
|
|
|
|
|
/// \brief Retrieve the file entry for the actual file that was included
|
|
|
|
/// by this directive.
|
|
|
|
const FileEntry *getFile() const { return File; }
|
|
|
|
|
|
|
|
// Implement isa/cast/dyncast/etc.
|
|
|
|
static bool classof(const PreprocessedEntity *PE) {
|
|
|
|
return PE->getKind() == InclusionDirectiveKind;
|
|
|
|
}
|
|
|
|
};
|
2010-03-21 10:50:08 +00:00
|
|
|
|
|
|
|
/// \brief An abstract class that should be subclassed by any external source
|
|
|
|
/// of preprocessing record entries.
|
|
|
|
class ExternalPreprocessingRecordSource {
|
|
|
|
public:
|
|
|
|
virtual ~ExternalPreprocessingRecordSource();
|
|
|
|
|
2011-10-20 21:14:49 +00:00
|
|
|
/// \brief Read a preallocated preprocessed entity from the external source.
|
|
|
|
///
|
|
|
|
/// \returns null if an error occurred that prevented the preprocessed
|
|
|
|
/// entity from being loaded.
|
|
|
|
virtual PreprocessedEntity *ReadPreprocessedEntity(unsigned Index) = 0;
|
|
|
|
|
|
|
|
/// \brief Returns a pair of [Begin, End) indices of preallocated
|
2012-12-02 13:20:44 +00:00
|
|
|
/// preprocessed entities that \p Range encompasses.
|
2011-10-20 21:14:49 +00:00
|
|
|
virtual std::pair<unsigned, unsigned>
|
|
|
|
findPreprocessedEntitiesInRange(SourceRange Range) = 0;
|
2012-04-14 14:01:31 +00:00
|
|
|
|
|
|
|
/// \brief Optionally returns true or false if the preallocated preprocessed
|
2012-12-02 13:20:44 +00:00
|
|
|
/// entity with index \p Index came from file \p FID.
|
2013-04-08 18:45:10 +00:00
|
|
|
virtual Optional<bool> isPreprocessedEntityInFileID(unsigned Index,
|
|
|
|
FileID FID) {
|
|
|
|
return None;
|
2012-04-14 14:01:31 +00:00
|
|
|
}
|
2010-03-21 10:50:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief A record of the steps taken while preprocessing a source file,
|
|
|
|
/// including the various preprocessing directives processed, macros
|
2011-07-17 15:40:56 +00:00
|
|
|
/// expanded, etc.
|
2010-03-21 10:50:08 +00:00
|
|
|
class PreprocessingRecord : public PPCallbacks {
|
2011-10-20 21:14:49 +00:00
|
|
|
SourceManager &SourceMgr;
|
2011-06-12 15:46:16 +00:00
|
|
|
|
2010-03-21 10:50:08 +00:00
|
|
|
/// \brief Allocator used to store preprocessing objects.
|
|
|
|
llvm::BumpPtrAllocator BumpAlloc;
|
|
|
|
|
|
|
|
/// \brief The set of preprocessed entities in this record, in order they
|
|
|
|
/// were seen.
|
|
|
|
std::vector<PreprocessedEntity *> PreprocessedEntities;
|
|
|
|
|
2011-10-20 21:14:49 +00:00
|
|
|
/// \brief The set of preprocessed entities in this record that have been
|
|
|
|
/// loaded from external sources.
|
|
|
|
///
|
|
|
|
/// The entries in this vector are loaded lazily from the external source,
|
|
|
|
/// and are referenced by the iterator using negative indices.
|
|
|
|
std::vector<PreprocessedEntity *> LoadedPreprocessedEntities;
|
|
|
|
|
|
|
|
/// \brief Global (loaded or local) ID for a preprocessed entity.
|
|
|
|
/// Negative values are used to indicate preprocessed entities
|
|
|
|
/// loaded from the external source while non-negative values are used to
|
|
|
|
/// indicate preprocessed entities introduced by the current preprocessor.
|
2012-12-02 13:20:44 +00:00
|
|
|
/// Value -1 corresponds to element 0 in the loaded entities vector,
|
|
|
|
/// value -2 corresponds to element 1 in the loaded entities vector, etc.
|
|
|
|
/// Value 0 is an invalid value, the index to local entities is 1-based,
|
|
|
|
/// value 1 corresponds to element 0 in the local entities vector,
|
|
|
|
/// value 2 corresponds to element 1 in the local entities vector, etc.
|
|
|
|
class PPEntityID {
|
|
|
|
int ID;
|
|
|
|
explicit PPEntityID(int ID) : ID(ID) {}
|
|
|
|
friend class PreprocessingRecord;
|
|
|
|
public:
|
|
|
|
PPEntityID() : ID(0) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
static PPEntityID getPPEntityID(unsigned Index, bool isLoaded) {
|
|
|
|
return isLoaded ? PPEntityID(-int(Index)-1) : PPEntityID(Index+1);
|
2011-10-20 21:14:49 +00:00
|
|
|
}
|
|
|
|
|
2010-03-21 10:50:08 +00:00
|
|
|
/// \brief Mapping from MacroInfo structures to their definitions.
|
2013-04-08 18:45:10 +00:00
|
|
|
llvm::DenseMap<const MacroInfo *, MacroDefinition *> MacroDefinitions;
|
2010-03-21 10:50:08 +00:00
|
|
|
|
|
|
|
/// \brief External source of preprocessed entities.
|
|
|
|
ExternalPreprocessingRecordSource *ExternalSource;
|
2011-10-20 21:14:49 +00:00
|
|
|
|
|
|
|
/// \brief Retrieve the preprocessed entity at the given ID.
|
|
|
|
PreprocessedEntity *getPreprocessedEntity(PPEntityID PPID);
|
|
|
|
|
|
|
|
/// \brief Retrieve the loaded preprocessed entity at the given index.
|
|
|
|
PreprocessedEntity *getLoadedPreprocessedEntity(unsigned Index);
|
2010-03-21 10:50:08 +00:00
|
|
|
|
2011-10-20 21:14:49 +00:00
|
|
|
/// \brief Determine the number of preprocessed entities that were
|
|
|
|
/// loaded (or can be loaded) from an external source.
|
|
|
|
unsigned getNumLoadedPreprocessedEntities() const {
|
|
|
|
return LoadedPreprocessedEntities.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Returns a pair of [Begin, End) indices of local preprocessed
|
2012-12-02 13:20:44 +00:00
|
|
|
/// entities that \p Range encompasses.
|
2011-10-20 21:14:49 +00:00
|
|
|
std::pair<unsigned, unsigned>
|
|
|
|
findLocalPreprocessedEntitiesInRange(SourceRange Range) const;
|
|
|
|
unsigned findBeginLocalPreprocessedEntity(SourceLocation Loc) const;
|
|
|
|
unsigned findEndLocalPreprocessedEntity(SourceLocation Loc) const;
|
|
|
|
|
|
|
|
/// \brief Allocate space for a new set of loaded preprocessed entities.
|
|
|
|
///
|
|
|
|
/// \returns The index into the set of loaded preprocessed entities, which
|
|
|
|
/// corresponds to the first newly-allocated entity.
|
|
|
|
unsigned allocateLoadedEntities(unsigned NumEntities);
|
|
|
|
|
|
|
|
/// \brief Register a new macro definition.
|
2013-04-08 18:45:10 +00:00
|
|
|
void RegisterMacroDefinition(MacroInfo *Macro, MacroDefinition *Def);
|
2010-03-21 10:50:08 +00:00
|
|
|
|
|
|
|
public:
|
2011-10-20 21:14:49 +00:00
|
|
|
/// \brief Construct a new preprocessing record.
|
2013-04-08 18:45:10 +00:00
|
|
|
explicit PreprocessingRecord(SourceManager &SM);
|
2010-03-21 10:50:08 +00:00
|
|
|
|
|
|
|
/// \brief Allocate memory in the preprocessing record.
|
|
|
|
void *Allocate(unsigned Size, unsigned Align = 8) {
|
|
|
|
return BumpAlloc.Allocate(Size, Align);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Deallocate memory in the preprocessing record.
|
|
|
|
void Deallocate(void *Ptr) { }
|
2011-10-20 21:14:49 +00:00
|
|
|
|
|
|
|
size_t getTotalMemory() const;
|
|
|
|
|
|
|
|
SourceManager &getSourceManager() const { return SourceMgr; }
|
|
|
|
|
2010-03-21 10:50:08 +00:00
|
|
|
// Iteration over the preprocessed entities.
|
2011-10-20 21:14:49 +00:00
|
|
|
class iterator {
|
|
|
|
PreprocessingRecord *Self;
|
|
|
|
|
|
|
|
/// \brief Position within the preprocessed entity sequence.
|
|
|
|
///
|
|
|
|
/// In a complete iteration, the Position field walks the range [-M, N),
|
|
|
|
/// where negative values are used to indicate preprocessed entities
|
|
|
|
/// loaded from the external source while non-negative values are used to
|
|
|
|
/// indicate preprocessed entities introduced by the current preprocessor.
|
|
|
|
/// However, to provide iteration in source order (for, e.g., chained
|
|
|
|
/// precompiled headers), dereferencing the iterator flips the negative
|
|
|
|
/// values (corresponding to loaded entities), so that position -M
|
|
|
|
/// corresponds to element 0 in the loaded entities vector, position -M+1
|
|
|
|
/// corresponds to element 1 in the loaded entities vector, etc. This
|
|
|
|
/// gives us a reasonably efficient, source-order walk.
|
2012-12-02 13:20:44 +00:00
|
|
|
int Position;
|
2011-10-20 21:14:49 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
typedef PreprocessedEntity *value_type;
|
|
|
|
typedef value_type& reference;
|
|
|
|
typedef value_type* pointer;
|
|
|
|
typedef std::random_access_iterator_tag iterator_category;
|
|
|
|
typedef int difference_type;
|
|
|
|
|
|
|
|
iterator() : Self(0), Position(0) { }
|
|
|
|
|
2012-12-02 13:20:44 +00:00
|
|
|
iterator(PreprocessingRecord *Self, int Position)
|
2011-10-20 21:14:49 +00:00
|
|
|
: Self(Self), Position(Position) { }
|
|
|
|
|
|
|
|
value_type operator*() const {
|
2012-12-02 13:20:44 +00:00
|
|
|
bool isLoaded = Position < 0;
|
|
|
|
unsigned Index = isLoaded ?
|
|
|
|
Self->LoadedPreprocessedEntities.size() + Position : Position;
|
|
|
|
PPEntityID ID = Self->getPPEntityID(Index, isLoaded);
|
|
|
|
return Self->getPreprocessedEntity(ID);
|
2011-10-20 21:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
value_type operator[](difference_type D) {
|
|
|
|
return *(*this + D);
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator &operator++() {
|
|
|
|
++Position;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator operator++(int) {
|
|
|
|
iterator Prev(*this);
|
|
|
|
++Position;
|
|
|
|
return Prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator &operator--() {
|
|
|
|
--Position;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator operator--(int) {
|
|
|
|
iterator Prev(*this);
|
|
|
|
--Position;
|
|
|
|
return Prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator==(const iterator &X, const iterator &Y) {
|
|
|
|
return X.Position == Y.Position;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator!=(const iterator &X, const iterator &Y) {
|
|
|
|
return X.Position != Y.Position;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator<(const iterator &X, const iterator &Y) {
|
|
|
|
return X.Position < Y.Position;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator>(const iterator &X, const iterator &Y) {
|
|
|
|
return X.Position > Y.Position;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator<=(const iterator &X, const iterator &Y) {
|
|
|
|
return X.Position < Y.Position;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator>=(const iterator &X, const iterator &Y) {
|
|
|
|
return X.Position > Y.Position;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend iterator& operator+=(iterator &X, difference_type D) {
|
|
|
|
X.Position += D;
|
|
|
|
return X;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend iterator& operator-=(iterator &X, difference_type D) {
|
|
|
|
X.Position -= D;
|
|
|
|
return X;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend iterator operator+(iterator X, difference_type D) {
|
|
|
|
X.Position += D;
|
|
|
|
return X;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend iterator operator+(difference_type D, iterator X) {
|
|
|
|
X.Position += D;
|
|
|
|
return X;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend difference_type operator-(const iterator &X, const iterator &Y) {
|
|
|
|
return X.Position - Y.Position;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend iterator operator-(iterator X, difference_type D) {
|
|
|
|
X.Position -= D;
|
|
|
|
return X;
|
|
|
|
}
|
2012-04-14 14:01:31 +00:00
|
|
|
friend class PreprocessingRecord;
|
2011-10-20 21:14:49 +00:00
|
|
|
};
|
|
|
|
friend class iterator;
|
|
|
|
|
|
|
|
/// \brief Begin iterator for all preprocessed entities.
|
|
|
|
iterator begin() {
|
|
|
|
return iterator(this, -(int)LoadedPreprocessedEntities.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief End iterator for all preprocessed entities.
|
|
|
|
iterator end() {
|
|
|
|
return iterator(this, PreprocessedEntities.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Begin iterator for local, non-loaded, preprocessed entities.
|
|
|
|
iterator local_begin() {
|
|
|
|
return iterator(this, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief End iterator for local, non-loaded, preprocessed entities.
|
|
|
|
iterator local_end() {
|
|
|
|
return iterator(this, PreprocessedEntities.size());
|
|
|
|
}
|
|
|
|
|
2012-12-02 13:20:44 +00:00
|
|
|
/// \brief begin/end iterator pair for the given range of loaded
|
|
|
|
/// preprocessed entities.
|
|
|
|
std::pair<iterator, iterator>
|
|
|
|
getIteratorsForLoadedRange(unsigned start, unsigned count) {
|
|
|
|
unsigned end = start + count;
|
|
|
|
assert(end <= LoadedPreprocessedEntities.size());
|
|
|
|
return std::make_pair(
|
|
|
|
iterator(this, int(start)-LoadedPreprocessedEntities.size()),
|
|
|
|
iterator(this, int(end)-LoadedPreprocessedEntities.size()));
|
|
|
|
}
|
|
|
|
|
2011-10-20 21:14:49 +00:00
|
|
|
/// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
|
2012-12-02 13:20:44 +00:00
|
|
|
/// that source range \p R encompasses.
|
2012-04-14 14:01:31 +00:00
|
|
|
///
|
|
|
|
/// \param R the range to look for preprocessed entities.
|
|
|
|
///
|
2011-10-20 21:14:49 +00:00
|
|
|
std::pair<iterator, iterator> getPreprocessedEntitiesInRange(SourceRange R);
|
2011-02-20 13:06:31 +00:00
|
|
|
|
2012-12-02 13:20:44 +00:00
|
|
|
/// \brief Returns true if the preprocessed entity that \p PPEI iterator
|
|
|
|
/// points to is coming from the file \p FID.
|
2012-04-14 14:01:31 +00:00
|
|
|
///
|
|
|
|
/// Can be used to avoid implicit deserializations of preallocated
|
|
|
|
/// preprocessed entities if we only care about entities of a specific file
|
2012-08-15 20:02:54 +00:00
|
|
|
/// and not from files \#included in the range given at
|
2012-04-14 14:01:31 +00:00
|
|
|
/// \see getPreprocessedEntitiesInRange.
|
|
|
|
bool isEntityInFileID(iterator PPEI, FileID FID);
|
|
|
|
|
2010-03-21 10:50:08 +00:00
|
|
|
/// \brief Add a new preprocessed entity to this record.
|
2012-04-14 14:01:31 +00:00
|
|
|
PPEntityID addPreprocessedEntity(PreprocessedEntity *Entity);
|
|
|
|
|
2010-03-21 10:50:08 +00:00
|
|
|
/// \brief Set the external source for preprocessed entities.
|
2011-10-20 21:14:49 +00:00
|
|
|
void SetExternalSource(ExternalPreprocessingRecordSource &Source);
|
2011-02-20 13:06:31 +00:00
|
|
|
|
|
|
|
/// \brief Retrieve the external source for preprocessed entities.
|
|
|
|
ExternalPreprocessingRecordSource *getExternalSource() const {
|
|
|
|
return ExternalSource;
|
|
|
|
}
|
|
|
|
|
2010-03-21 10:50:08 +00:00
|
|
|
/// \brief Retrieve the macro definition that corresponds to the given
|
|
|
|
/// \c MacroInfo.
|
|
|
|
MacroDefinition *findMacroDefinition(const MacroInfo *MI);
|
2011-10-20 21:14:49 +00:00
|
|
|
|
2012-04-14 14:01:31 +00:00
|
|
|
private:
|
2013-04-08 18:45:10 +00:00
|
|
|
virtual void MacroExpands(const Token &Id, const MacroDirective *MD,
|
2013-06-10 20:45:12 +00:00
|
|
|
SourceRange Range, const MacroArgs *Args);
|
2013-04-08 18:45:10 +00:00
|
|
|
virtual void MacroDefined(const Token &Id, const MacroDirective *MD);
|
|
|
|
virtual void MacroUndefined(const Token &Id, const MacroDirective *MD);
|
2011-02-20 13:06:31 +00:00
|
|
|
virtual void InclusionDirective(SourceLocation HashLoc,
|
|
|
|
const Token &IncludeTok,
|
2011-10-20 21:14:49 +00:00
|
|
|
StringRef FileName,
|
2011-02-20 13:06:31 +00:00
|
|
|
bool IsAngled,
|
2012-12-02 13:20:44 +00:00
|
|
|
CharSourceRange FilenameRange,
|
2011-02-20 13:06:31 +00:00
|
|
|
const FileEntry *File,
|
2011-10-20 21:14:49 +00:00
|
|
|
StringRef SearchPath,
|
2012-12-02 13:20:44 +00:00
|
|
|
StringRef RelativePath,
|
|
|
|
const Module *Imported);
|
2013-04-08 18:45:10 +00:00
|
|
|
virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
|
|
|
|
const MacroDirective *MD);
|
|
|
|
virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
|
|
|
|
const MacroDirective *MD);
|
|
|
|
/// \brief Hook called whenever the 'defined' operator is seen.
|
2013-12-22 00:07:40 +00:00
|
|
|
virtual void Defined(const Token &MacroNameTok, const MacroDirective *MD,
|
|
|
|
SourceRange Range);
|
2013-04-08 18:45:10 +00:00
|
|
|
|
|
|
|
void addMacroExpansion(const Token &Id, const MacroInfo *MI,
|
|
|
|
SourceRange Range);
|
2012-04-14 14:01:31 +00:00
|
|
|
|
|
|
|
/// \brief Cached result of the last \see getPreprocessedEntitiesInRange
|
|
|
|
/// query.
|
|
|
|
struct {
|
|
|
|
SourceRange Range;
|
2012-12-02 13:20:44 +00:00
|
|
|
std::pair<int, int> Result;
|
2012-04-14 14:01:31 +00:00
|
|
|
} CachedRangeQuery;
|
|
|
|
|
2012-12-02 13:20:44 +00:00
|
|
|
std::pair<int, int> getPreprocessedEntitiesInRangeSlow(SourceRange R);
|
2011-10-20 21:14:49 +00:00
|
|
|
|
|
|
|
friend class ASTReader;
|
|
|
|
friend class ASTWriter;
|
2010-03-21 10:50:08 +00:00
|
|
|
};
|
|
|
|
} // end namespace clang
|
|
|
|
|
|
|
|
inline void* operator new(size_t bytes, clang::PreprocessingRecord& PR,
|
|
|
|
unsigned alignment) throw() {
|
|
|
|
return PR.Allocate(bytes, alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void operator delete(void* ptr, clang::PreprocessingRecord& PR,
|
|
|
|
unsigned) throw() {
|
|
|
|
PR.Deallocate(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // LLVM_CLANG_LEX_PREPROCESSINGRECORD_H
|