diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp index 009465d11d781..33287c6529caa 100644 --- a/llvm/lib/MC/MCParser/AsmParser.cpp +++ b/llvm/lib/MC/MCParser/AsmParser.cpp @@ -295,10 +295,9 @@ class AsmParser : public MCAsmParser { void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body, ArrayRef Parameters); - bool expandMacro(raw_svector_ostream &OS, StringRef Body, + bool expandMacro(raw_svector_ostream &OS, const MCAsmMacro &Macro, ArrayRef Parameters, - ArrayRef A, bool EnableAtPseudoVariable, - SMLoc L); + ArrayRef A, bool EnableAtPseudoVariable); /// Are macros enabled in the parser? bool areMacrosEnabled() {return MacrosEnabledFlag;} @@ -2496,17 +2495,16 @@ static bool isIdentifierChar(char c) { c == '.'; } -bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body, +bool AsmParser::expandMacro(raw_svector_ostream &OS, const MCAsmMacro &Macro, ArrayRef Parameters, ArrayRef A, - bool EnableAtPseudoVariable, SMLoc L) { + bool EnableAtPseudoVariable) { unsigned NParameters = Parameters.size(); bool HasVararg = NParameters ? Parameters.back().Vararg : false; - if ((!IsDarwin || NParameters != 0) && NParameters != A.size()) - return Error(L, "Wrong number of arguments"); // A macro without parameters is handled differently on Darwin: // gas accepts no arguments and does no substitutions + StringRef Body = Macro.Body; while (!Body.empty()) { // Scan for the next substitution. std::size_t End = Body.size(), Pos = 0; @@ -2882,10 +2880,11 @@ bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) { // Macro instantiation is lexical, unfortunately. We construct a new buffer // to hold the macro body with substitutions. SmallString<256> Buf; - StringRef Body = M->Body; raw_svector_ostream OS(Buf); - if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc())) + if ((!IsDarwin || M->Parameters.size()) && M->Parameters.size() != A.size()) + return Error(getTok().getLoc(), "Wrong number of arguments"); + if (expandMacro(OS, *M, M->Parameters, A, true)) return true; // We include the .endmacro in the buffer as our cue to exit the macro @@ -5694,8 +5693,7 @@ bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) { raw_svector_ostream OS(Buf); while (Count--) { // Note that the AtPseudoVariable is disabled for instantiations of .rep(t). - if (expandMacro(OS, M->Body, std::nullopt, std::nullopt, false, - getTok().getLoc())) + if (expandMacro(OS, *M, std::nullopt, std::nullopt, false)) return true; } instantiateMacroLikeBody(M, DirectiveLoc, OS); @@ -5726,7 +5724,7 @@ bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) { for (const MCAsmMacroArgument &Arg : A) { // Note that the AtPseudoVariable is enabled for instantiations of .irp. // This is undocumented, but GAS seems to support it. - if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc())) + if (expandMacro(OS, *M, Parameter, Arg, true)) return true; } @@ -5768,7 +5766,7 @@ bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) { // Note that the AtPseudoVariable is enabled for instantiations of .irpc. // This is undocumented, but GAS seems to support it. - if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc())) + if (expandMacro(OS, *M, Parameter, Arg, true)) return true; }