From aec674999947cc9e9e8d191960118e005ed3c99d Mon Sep 17 00:00:00 2001 From: "Duncan P. N. Exon Smith" Date: Mon, 19 Jan 2015 23:44:41 +0000 Subject: [PATCH] AsmParser: Abstract more of MDLocation parser, NFC git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@226527 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/AsmParser/LLParser.cpp | 43 ++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/lib/AsmParser/LLParser.cpp b/lib/AsmParser/LLParser.cpp index 467ed484f92..78818b288fa 100644 --- a/lib/AsmParser/LLParser.cpp +++ b/lib/AsmParser/LLParser.cpp @@ -2992,7 +2992,12 @@ bool LLParser::ParseSpecializedMDNode(MDNode *&N, bool IsDistinct) { return TokError("expected metadata type"); } -#define PARSE_MD_FIELD(NAME) \ +#define DECLARE_FIELD(NAME, TYPE, INIT) TYPE NAME INIT +#define NOP_FIELD(NAME, TYPE, INIT) +#define REQUIRE_FIELD(NAME, TYPE, INIT) \ + if (!NAME.Seen) \ + return Error(ClosingLoc, "missing required field '" #NAME "'"); +#define PARSE_MD_FIELD(NAME, TYPE, DEFAULT) \ do { \ if (Lex.getStrVal() == #NAME) { \ LocTy Loc = Lex.getLoc(); \ @@ -3002,31 +3007,37 @@ bool LLParser::ParseSpecializedMDNode(MDNode *&N, bool IsDistinct) { return false; \ } \ } while (0) +#define PARSE_MD_FIELDS() \ + VISIT_MD_FIELDS(DECLARE_FIELD, DECLARE_FIELD) \ + do { \ + LocTy ClosingLoc; \ + if (ParseMDFieldsImpl([&]() -> bool { \ + VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD) \ + return TokError(Twine("invalid field '") + Lex.getStrVal() + "'"); \ + }, ClosingLoc)) \ + return true; \ + VISIT_MD_FIELDS(NOP_FIELD, REQUIRE_FIELD) \ + } while (false) /// ParseMDLocationFields: /// ::= !MDLocation(line: 43, column: 8, scope: !5, inlinedAt: !6) bool LLParser::ParseMDLocation(MDNode *&Result, bool IsDistinct) { - MDUnsignedField line(0, ~0u >> 8); - MDUnsignedField column(0, ~0u >> 16); - MDField scope; - MDField inlinedAt; - LocTy Loc; - if (ParseMDFieldsImpl([&]() -> bool { - PARSE_MD_FIELD(line); - PARSE_MD_FIELD(column); - PARSE_MD_FIELD(scope); - PARSE_MD_FIELD(inlinedAt); - return TokError(Twine("invalid field '") + Lex.getStrVal() + "'"); - }, Loc)) - return true; +#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ + OPTIONAL(line, MDUnsignedField, (0, ~0u >> 8)); \ + OPTIONAL(column, MDUnsignedField, (0, ~0u >> 16)); \ + REQUIRED(scope, MDField, ); \ + OPTIONAL(inlinedAt, MDField, ); + PARSE_MD_FIELDS(); +#undef VISIT_MD_FIELDS - if (!scope.Seen) - return Error(Loc, "missing required field 'scope'"); auto get = (IsDistinct ? MDLocation::getDistinct : MDLocation::get); Result = get(Context, line.Val, column.Val, scope.Val, inlinedAt.Val); return false; } #undef PARSE_MD_FIELD +#undef NOP_FIELD +#undef REQUIRE_FIELD +#undef DECLARE_FIELD /// ParseMetadataAsValue /// ::= metadata i32 %local