diff options
author | dim <dim@FreeBSD.org> | 2013-04-08 18:45:10 +0000 |
---|---|---|
committer | dim <dim@FreeBSD.org> | 2013-04-08 18:45:10 +0000 |
commit | c72c57c9e9b69944e3e009cd5e209634839581d3 (patch) | |
tree | 4fc2f184c499d106f29a386c452b49e5197bf63d /lib/Lex/PreprocessingRecord.cpp | |
parent | 5b20025c30d23d521e12c1f33ec8fa6b821952cd (diff) | |
download | FreeBSD-src-c72c57c9e9b69944e3e009cd5e209634839581d3.zip FreeBSD-src-c72c57c9e9b69944e3e009cd5e209634839581d3.tar.gz |
Vendor import of clang trunk r178860:
http://llvm.org/svn/llvm-project/cfe/trunk@178860
Diffstat (limited to 'lib/Lex/PreprocessingRecord.cpp')
-rw-r--r-- | lib/Lex/PreprocessingRecord.cpp | 189 |
1 files changed, 69 insertions, 120 deletions
diff --git a/lib/Lex/PreprocessingRecord.cpp b/lib/Lex/PreprocessingRecord.cpp index 01f3665e..b10e7f7 100644 --- a/lib/Lex/PreprocessingRecord.cpp +++ b/lib/Lex/PreprocessingRecord.cpp @@ -14,8 +14,8 @@ #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" +#include "llvm/Support/ErrorHandling.h" using namespace clang; @@ -38,14 +38,9 @@ InclusionDirective::InclusionDirective(PreprocessingRecord &PPRec, this->FileName = StringRef(Memory, FileName.size()); } -PreprocessingRecord::PreprocessingRecord(SourceManager &SM, - bool RecordConditionalDirectives) +PreprocessingRecord::PreprocessingRecord(SourceManager &SM) : SourceMgr(SM), - RecordCondDirectives(RecordConditionalDirectives), CondDirectiveNextIdx(0), - ExternalSource(0) -{ - if (RecordCondDirectives) - CondDirectiveStack.push_back(CondDirectiveNextIdx++); + ExternalSource(0) { } /// \brief Returns a pair of [Begin, End) iterators of preprocessed entities @@ -97,8 +92,10 @@ bool PreprocessingRecord::isEntityInFileID(iterator PPEI, FileID FID) { int Pos = PPEI.Position; if (Pos < 0) { - assert(unsigned(-Pos-1) < LoadedPreprocessedEntities.size() && - "Out-of bounds loaded preprocessed entity"); + if (unsigned(-Pos-1) >= LoadedPreprocessedEntities.size()) { + assert(0 && "Out-of bounds loaded preprocessed entity"); + return false; + } assert(ExternalSource && "No external source to load from"); unsigned LoadedIndex = LoadedPreprocessedEntities.size()+Pos; if (PreprocessedEntity *PPE = LoadedPreprocessedEntities[LoadedIndex]) @@ -106,8 +103,8 @@ bool PreprocessingRecord::isEntityInFileID(iterator PPEI, FileID FID) { // See if the external source can see if the entity is in the file without // deserializing it. - llvm::Optional<bool> - IsInFile = ExternalSource->isPreprocessedEntityInFileID(LoadedIndex, FID); + Optional<bool> IsInFile = + ExternalSource->isPreprocessedEntityInFileID(LoadedIndex, FID); if (IsInFile.hasValue()) return IsInFile.getValue(); @@ -118,8 +115,10 @@ bool PreprocessingRecord::isEntityInFileID(iterator PPEI, FileID FID) { FID, SourceMgr); } - assert(unsigned(Pos) < PreprocessedEntities.size() && - "Out-of bounds local preprocessed entity"); + if (unsigned(Pos) >= PreprocessedEntities.size()) { + assert(0 && "Out-of bounds local preprocessed entity"); + return false; + } return isPreprocessedEntityIfInFileID(PreprocessedEntities[Pos], FID, SourceMgr); } @@ -249,11 +248,11 @@ PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) { assert(Entity); SourceLocation BeginLoc = Entity->getSourceRange().getBegin(); - if (!isa<class InclusionDirective>(Entity)) { + if (isa<MacroDefinition>(Entity)) { assert((PreprocessedEntities.empty() || !SourceMgr.isBeforeInTranslationUnit(BeginLoc, PreprocessedEntities.back()->getSourceRange().getBegin())) && - "a macro directive was encountered out-of-order"); + "a macro definition was encountered out-of-order"); PreprocessedEntities.push_back(Entity); return getPPEntityID(PreprocessedEntities.size()-1, /*isLoaded=*/false); } @@ -268,7 +267,15 @@ PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) { // The entity's location is not after the previous one; this can happen with // include directives that form the filename using macros, e.g: - // "#include MACRO(STUFF)". + // "#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 typedef std::vector<PreprocessedEntity *>::iterator pp_iter; @@ -313,8 +320,8 @@ unsigned PreprocessingRecord::allocateLoadedEntities(unsigned NumEntities) { } void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro, - PPEntityID PPID) { - MacroDefinitions[Macro] = PPID; + MacroDefinition *Def) { + MacroDefinitions[Macro] = Def; } /// \brief Retrieve the preprocessed entity at the given ID. @@ -351,19 +358,17 @@ PreprocessingRecord::getLoadedPreprocessedEntity(unsigned Index) { } MacroDefinition *PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) { - llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos + llvm::DenseMap<const MacroInfo *, MacroDefinition *>::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); + + return Pos->second; } -void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI, - SourceRange Range) { +void PreprocessingRecord::addMacroExpansion(const Token &Id, + const MacroInfo *MI, + SourceRange Range) { // We don't record nested macro expansions. if (Id.getLocation().isMacroID()) return; @@ -376,17 +381,50 @@ void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI, new (*this) MacroExpansion(Def, Range)); } +void PreprocessingRecord::Ifdef(SourceLocation Loc, const Token &MacroNameTok, + const MacroDirective *MD) { + // This is not actually a macro expansion but record it as a macro reference. + if (MD) + addMacroExpansion(MacroNameTok, MD->getMacroInfo(), + MacroNameTok.getLocation()); +} + +void PreprocessingRecord::Ifndef(SourceLocation Loc, const Token &MacroNameTok, + const MacroDirective *MD) { + // This is not actually a macro expansion but record it as a macro reference. + if (MD) + addMacroExpansion(MacroNameTok, MD->getMacroInfo(), + MacroNameTok.getLocation()); +} + +void PreprocessingRecord::Defined(const Token &MacroNameTok, + const MacroDirective *MD) { + // This is not actually a macro expansion but record it as a macro reference. + if (MD) + addMacroExpansion(MacroNameTok, MD->getMacroInfo(), + MacroNameTok.getLocation()); +} + +void PreprocessingRecord::MacroExpands(const Token &Id,const MacroDirective *MD, + SourceRange Range) { + addMacroExpansion(Id, MD->getMacroInfo(), Range); +} + void PreprocessingRecord::MacroDefined(const Token &Id, - const MacroInfo *MI) { + const MacroDirective *MD) { + const MacroInfo *MI = MD->getMacroInfo(); SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc()); MacroDefinition *Def = new (*this) MacroDefinition(Id.getIdentifierInfo(), R); - MacroDefinitions[MI] = addPreprocessedEntity(Def); + addPreprocessedEntity(Def); + MacroDefinitions[MI] = Def; } void PreprocessingRecord::MacroUndefined(const Token &Id, - const MacroInfo *MI) { - MacroDefinitions.erase(MI); + const MacroDirective *MD) { + // Note: MI may be null (when #undef'ining an undefined macro). + if (MD) + MacroDefinitions.erase(MD->getMacroInfo()); } void PreprocessingRecord::InclusionDirective( @@ -438,95 +476,6 @@ void PreprocessingRecord::InclusionDirective( addPreprocessedEntity(ID); } -bool PreprocessingRecord::rangeIntersectsConditionalDirective( - SourceRange Range) const { - if (Range.isInvalid()) - return false; - - CondDirectiveLocsTy::const_iterator - low = std::lower_bound(CondDirectiveLocs.begin(), CondDirectiveLocs.end(), - Range.getBegin(), CondDirectiveLoc::Comp(SourceMgr)); - if (low == CondDirectiveLocs.end()) - return false; - - if (SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), low->getLoc())) - return false; - - CondDirectiveLocsTy::const_iterator - upp = std::upper_bound(low, CondDirectiveLocs.end(), - Range.getEnd(), CondDirectiveLoc::Comp(SourceMgr)); - unsigned uppIdx; - if (upp != CondDirectiveLocs.end()) - uppIdx = upp->getIdx(); - else - uppIdx = 0; - - return low->getIdx() != uppIdx; -} - -unsigned PreprocessingRecord::findCondDirectiveIdx(SourceLocation Loc) const { - if (Loc.isInvalid()) - return 0; - - CondDirectiveLocsTy::const_iterator - low = std::lower_bound(CondDirectiveLocs.begin(), CondDirectiveLocs.end(), - Loc, CondDirectiveLoc::Comp(SourceMgr)); - if (low == CondDirectiveLocs.end()) - return 0; - return low->getIdx(); -} - -void PreprocessingRecord::addCondDirectiveLoc(CondDirectiveLoc DirLoc) { - // Ignore directives in system headers. - if (SourceMgr.isInSystemHeader(DirLoc.getLoc())) - return; - - assert(CondDirectiveLocs.empty() || - SourceMgr.isBeforeInTranslationUnit(CondDirectiveLocs.back().getLoc(), - DirLoc.getLoc())); - CondDirectiveLocs.push_back(DirLoc); -} - -void PreprocessingRecord::If(SourceLocation Loc, SourceRange ConditionRange) { - if (RecordCondDirectives) { - addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); - CondDirectiveStack.push_back(CondDirectiveNextIdx++); - } -} - -void PreprocessingRecord::Ifdef(SourceLocation Loc, const Token &MacroNameTok) { - if (RecordCondDirectives) { - addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); - CondDirectiveStack.push_back(CondDirectiveNextIdx++); - } -} - -void PreprocessingRecord::Ifndef(SourceLocation Loc,const Token &MacroNameTok) { - if (RecordCondDirectives) { - addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); - CondDirectiveStack.push_back(CondDirectiveNextIdx++); - } -} - -void PreprocessingRecord::Elif(SourceLocation Loc, SourceRange ConditionRange, - SourceLocation IfLoc) { - if (RecordCondDirectives) - addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); -} - -void PreprocessingRecord::Else(SourceLocation Loc, SourceLocation IfLoc) { - if (RecordCondDirectives) - addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); -} - -void PreprocessingRecord::Endif(SourceLocation Loc, SourceLocation IfLoc) { - if (RecordCondDirectives) { - addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); - assert(!CondDirectiveStack.empty()); - CondDirectiveStack.pop_back(); - } -} - size_t PreprocessingRecord::getTotalMemory() const { return BumpAlloc.getTotalMemory() + llvm::capacity_in_bytes(MacroDefinitions) |