[llvm] 0f20d5a - [AsmParser] Deduplicate argument list parsing code (NFC)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 19 06:03:22 PST 2024


Author: Nikita Popov
Date: 2024-01-19T15:03:12+01:00
New Revision: 0f20d5a8b10b98c83c73b6b7808b7bf88aaaaa2e

URL: https://github.com/llvm/llvm-project/commit/0f20d5a8b10b98c83c73b6b7808b7bf88aaaaa2e
DIFF: https://github.com/llvm/llvm-project/commit/0f20d5a8b10b98c83c73b6b7808b7bf88aaaaa2e.diff

LOG: [AsmParser] Deduplicate argument list parsing code (NFC)

The handling for parsing the first argument and all later arguments
was duplicated. Consolidate them into a single loop.

Added: 
    

Modified: 
    llvm/lib/AsmParser/LLParser.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index 2e859c42d55759a..ea7f9a00d719fd5 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -2982,49 +2982,8 @@ bool LLParser::parseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
   assert(Lex.getKind() == lltok::lparen);
   Lex.Lex(); // eat the (.
 
-  if (Lex.getKind() == lltok::rparen) {
-    // empty
-  } else if (Lex.getKind() == lltok::dotdotdot) {
-    IsVarArg = true;
-    Lex.Lex();
-  } else {
-    LocTy TypeLoc = Lex.getLoc();
-    Type *ArgTy = nullptr;
-    AttrBuilder Attrs(M->getContext());
-    std::string Name;
-
-    if (parseType(ArgTy) || parseOptionalParamAttrs(Attrs))
-      return true;
-
-    if (ArgTy->isVoidTy())
-      return error(TypeLoc, "argument can not have void type");
-
-    if (Lex.getKind() == lltok::LocalVar) {
-      Name = Lex.getStrVal();
-      Lex.Lex();
-    } else {
-      unsigned ArgID;
-      if (Lex.getKind() == lltok::LocalVarID) {
-        ArgID = Lex.getUIntVal();
-        if (checkValueID(TypeLoc, "argument", "%", CurValID, ArgID))
-          return true;
-        Lex.Lex();
-      } else {
-        ArgID = CurValID;
-      }
-
-      UnnamedArgNums.push_back(ArgID);
-      CurValID = ArgID + 1;
-    }
-
-    if (!FunctionType::isValidArgumentType(ArgTy))
-      return error(TypeLoc, "invalid type for function argument");
-
-    ArgList.emplace_back(TypeLoc, ArgTy,
-                         AttributeSet::get(ArgTy->getContext(), Attrs),
-                         std::move(Name));
-
-    while (EatIfPresent(lltok::comma)) {
+  if (Lex.getKind() != lltok::rparen) {
+    do {
       // Handle ... at end of arg list.
       if (EatIfPresent(lltok::dotdotdot)) {
         IsVarArg = true;
@@ -3032,13 +2991,16 @@ bool LLParser::parseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
       }
 
       // Otherwise must be an argument type.
-      TypeLoc = Lex.getLoc();
+      LocTy TypeLoc = Lex.getLoc();
+      Type *ArgTy = nullptr;
+      AttrBuilder Attrs(M->getContext());
       if (parseType(ArgTy) || parseOptionalParamAttrs(Attrs))
         return true;
 
       if (ArgTy->isVoidTy())
         return error(TypeLoc, "argument can not have void type");
 
+      std::string Name;
       if (Lex.getKind() == lltok::LocalVar) {
         Name = Lex.getStrVal();
         Lex.Lex();
@@ -3054,7 +3016,6 @@ bool LLParser::parseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
         }
         UnnamedArgNums.push_back(ArgID);
         CurValID = ArgID + 1;
-        Name = "";
       }
 
       if (!ArgTy->isFirstClassType())
@@ -3063,7 +3024,7 @@ bool LLParser::parseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
       ArgList.emplace_back(TypeLoc, ArgTy,
                            AttributeSet::get(ArgTy->getContext(), Attrs),
                            std::move(Name));
-    }
+    } while (EatIfPresent(lltok::comma));
   }
 
   return parseToken(lltok::rparen, "expected ')' at end of argument list");


        


More information about the llvm-commits mailing list