MCParser: Modernize memory handling.

NFC.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@218998 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Benjamin Kramer 2014-10-03 18:32:55 +00:00
parent 83e571e7ba
commit 3c5eaffc07

View File

@ -69,8 +69,8 @@ struct MCAsmMacro {
MCAsmMacroParameters Parameters; MCAsmMacroParameters Parameters;
public: public:
MCAsmMacro(StringRef N, StringRef B, ArrayRef<MCAsmMacroParameter> P) : MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P)
Name(N), Body(B), Parameters(P) {} : Name(N), Body(B), Parameters(std::move(P)) {}
}; };
/// \brief Helper class for storing information about an active macro /// \brief Helper class for storing information about an active macro
@ -121,7 +121,7 @@ private:
SourceMgr &SrcMgr; SourceMgr &SrcMgr;
SourceMgr::DiagHandlerTy SavedDiagHandler; SourceMgr::DiagHandlerTy SavedDiagHandler;
void *SavedDiagContext; void *SavedDiagContext;
MCAsmParserExtension *PlatformParser; std::unique_ptr<MCAsmParserExtension> PlatformParser;
/// This is the current buffer index we're lexing from as managed by the /// This is the current buffer index we're lexing from as managed by the
/// SourceMgr object. /// SourceMgr object.
@ -136,7 +136,7 @@ private:
StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap; StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
/// \brief Map of currently defined macros. /// \brief Map of currently defined macros.
StringMap<MCAsmMacro*> MacroMap; StringMap<MCAsmMacro> MacroMap;
/// \brief Stack of active macro instantiations. /// \brief Stack of active macro instantiations.
std::vector<MacroInstantiation*> ActiveMacros; std::vector<MacroInstantiation*> ActiveMacros;
@ -262,7 +262,7 @@ private:
const MCAsmMacro* lookupMacro(StringRef Name); const MCAsmMacro* lookupMacro(StringRef Name);
/// \brief Define a new macro with the given name and information. /// \brief Define a new macro with the given name and information.
void defineMacro(StringRef Name, const MCAsmMacro& Macro); void defineMacro(StringRef Name, MCAsmMacro Macro);
/// \brief Undefine a macro. If no such macro was defined, it's a no-op. /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
void undefineMacro(StringRef Name); void undefineMacro(StringRef Name);
@ -502,34 +502,24 @@ AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
// Initialize the platform / file format parser. // Initialize the platform / file format parser.
switch (_Ctx.getObjectFileInfo()->getObjectFileType()) { switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
case MCObjectFileInfo::IsCOFF: case MCObjectFileInfo::IsCOFF:
PlatformParser = createCOFFAsmParser(); PlatformParser.reset(createCOFFAsmParser());
PlatformParser->Initialize(*this);
break; break;
case MCObjectFileInfo::IsMachO: case MCObjectFileInfo::IsMachO:
PlatformParser = createDarwinAsmParser(); PlatformParser.reset(createDarwinAsmParser());
PlatformParser->Initialize(*this);
IsDarwin = true; IsDarwin = true;
break; break;
case MCObjectFileInfo::IsELF: case MCObjectFileInfo::IsELF:
PlatformParser = createELFAsmParser(); PlatformParser.reset(createELFAsmParser());
PlatformParser->Initialize(*this);
break; break;
} }
PlatformParser->Initialize(*this);
initializeDirectiveKindMap(); initializeDirectiveKindMap();
} }
AsmParser::~AsmParser() { AsmParser::~AsmParser() {
assert((HadError || ActiveMacros.empty()) && assert((HadError || ActiveMacros.empty()) &&
"Unexpected active macro instantiation!"); "Unexpected active macro instantiation!");
// Destroy any macros.
for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
ie = MacroMap.end();
it != ie; ++it)
delete it->getValue();
delete PlatformParser;
} }
void AsmParser::printMacroInstantiations() { void AsmParser::printMacroInstantiations() {
@ -2088,21 +2078,15 @@ bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
} }
const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) { const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name); StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
return (I == MacroMap.end()) ? nullptr : I->getValue(); return (I == MacroMap.end()) ? nullptr : &I->getValue();
} }
void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) { void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) {
MacroMap[Name] = new MCAsmMacro(Macro); MacroMap.insert(std::make_pair(Name, std::move(Macro)));
} }
void AsmParser::undefineMacro(StringRef Name) { void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); }
StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
if (I != MacroMap.end()) {
delete I->getValue();
MacroMap.erase(I);
}
}
bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) { bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
// Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
@ -3325,7 +3309,7 @@ bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
"'" + Parameter.Name + "' in macro '" + Name + "'"); "'" + Parameter.Name + "' in macro '" + Name + "'");
} }
Parameters.push_back(Parameter); Parameters.push_back(std::move(Parameter));
if (getLexer().is(AsmToken::Comma)) if (getLexer().is(AsmToken::Comma))
Lex(); Lex();
@ -3377,7 +3361,7 @@ bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
const char *BodyEnd = EndToken.getLoc().getPointer(); const char *BodyEnd = EndToken.getLoc().getPointer();
StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
checkForBadMacro(DirectiveLoc, Name, Body, Parameters); checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
defineMacro(Name, MCAsmMacro(Name, Body, Parameters)); defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters)));
return false; return false;
} }
@ -4306,7 +4290,8 @@ MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
// We Are Anonymous. // We Are Anonymous.
MacroLikeBodies.push_back(MCAsmMacro(StringRef(), Body, None)); MacroLikeBodies.push_back(
MCAsmMacro(StringRef(), Body, MCAsmMacroParameters()));
return &MacroLikeBodies.back(); return &MacroLikeBodies.back();
} }