[llvm] d1ebd35 - [RISCV][MC] Simplify .option and make error messages more conventional

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 1 17:58:28 PST 2023


Author: Fangrui Song
Date: 2023-02-01T17:58:23-08:00
New Revision: d1ebd352b06c70b4cc0fcb580034f1ae832a35d6

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

LOG: [RISCV][MC] Simplify .option and make error messages more conventional

and add line/column information to tests.

Added: 
    

Modified: 
    llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
    llvm/test/MC/RISCV/option-invalid.s

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp b/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
index c05de2244f0e0..1ade316082922 100644
--- a/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
+++ b/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
@@ -2108,34 +2108,28 @@ bool RISCVAsmParser::parseDirectiveOption() {
   MCAsmParser &Parser = getParser();
   // Get the option token.
   AsmToken Tok = Parser.getTok();
+
   // At the moment only identifiers are supported.
-  if (Tok.isNot(AsmToken::Identifier))
-    return Error(Parser.getTok().getLoc(),
-                 "unexpected token, expected identifier");
+  if (parseToken(AsmToken::Identifier, "expected identifier"))
+    return true;
 
   StringRef Option = Tok.getIdentifier();
 
   if (Option == "push") {
-    getTargetStreamer().emitDirectiveOptionPush();
-
-    Parser.Lex();
-    if (Parser.getTok().isNot(AsmToken::EndOfStatement))
-      return Error(Parser.getTok().getLoc(),
-                   "unexpected token, expected end of statement");
+    if (Parser.parseEOL())
+      return true;
 
+    getTargetStreamer().emitDirectiveOptionPush();
     pushFeatureBits();
     return false;
   }
 
   if (Option == "pop") {
     SMLoc StartLoc = Parser.getTok().getLoc();
-    getTargetStreamer().emitDirectiveOptionPop();
-
-    Parser.Lex();
-    if (Parser.getTok().isNot(AsmToken::EndOfStatement))
-      return Error(Parser.getTok().getLoc(),
-                   "unexpected token, expected end of statement");
+    if (Parser.parseEOL())
+      return true;
 
+    getTargetStreamer().emitDirectiveOptionPop();
     if (popFeatureBits())
       return Error(StartLoc, ".option pop with no .option push");
 
@@ -2143,74 +2137,56 @@ bool RISCVAsmParser::parseDirectiveOption() {
   }
 
   if (Option == "rvc") {
-    getTargetStreamer().emitDirectiveOptionRVC();
-
-    Parser.Lex();
-    if (Parser.getTok().isNot(AsmToken::EndOfStatement))
-      return Error(Parser.getTok().getLoc(),
-                   "unexpected token, expected end of statement");
+    if (Parser.parseEOL())
+      return true;
 
+    getTargetStreamer().emitDirectiveOptionRVC();
     setFeatureBits(RISCV::FeatureStdExtC, "c");
     return false;
   }
 
   if (Option == "norvc") {
-    getTargetStreamer().emitDirectiveOptionNoRVC();
-
-    Parser.Lex();
-    if (Parser.getTok().isNot(AsmToken::EndOfStatement))
-      return Error(Parser.getTok().getLoc(),
-                   "unexpected token, expected end of statement");
+    if (Parser.parseEOL())
+      return true;
 
+    getTargetStreamer().emitDirectiveOptionNoRVC();
     clearFeatureBits(RISCV::FeatureStdExtC, "c");
     clearFeatureBits(RISCV::FeatureExtZca, "+experimental-zca");
     return false;
   }
 
   if (Option == "pic") {
-    getTargetStreamer().emitDirectiveOptionPIC();
-
-    Parser.Lex();
-    if (Parser.getTok().isNot(AsmToken::EndOfStatement))
-      return Error(Parser.getTok().getLoc(),
-                   "unexpected token, expected end of statement");
+    if (Parser.parseEOL())
+      return true;
 
+    getTargetStreamer().emitDirectiveOptionPIC();
     ParserOptions.IsPicEnabled = true;
     return false;
   }
 
   if (Option == "nopic") {
-    getTargetStreamer().emitDirectiveOptionNoPIC();
-
-    Parser.Lex();
-    if (Parser.getTok().isNot(AsmToken::EndOfStatement))
-      return Error(Parser.getTok().getLoc(),
-                   "unexpected token, expected end of statement");
+    if (Parser.parseEOL())
+      return true;
 
+    getTargetStreamer().emitDirectiveOptionNoPIC();
     ParserOptions.IsPicEnabled = false;
     return false;
   }
 
   if (Option == "relax") {
-    getTargetStreamer().emitDirectiveOptionRelax();
-
-    Parser.Lex();
-    if (Parser.getTok().isNot(AsmToken::EndOfStatement))
-      return Error(Parser.getTok().getLoc(),
-                   "unexpected token, expected end of statement");
+    if (Parser.parseEOL())
+      return true;
 
+    getTargetStreamer().emitDirectiveOptionRelax();
     setFeatureBits(RISCV::FeatureRelax, "relax");
     return false;
   }
 
   if (Option == "norelax") {
-    getTargetStreamer().emitDirectiveOptionNoRelax();
-
-    Parser.Lex();
-    if (Parser.getTok().isNot(AsmToken::EndOfStatement))
-      return Error(Parser.getTok().getLoc(),
-                   "unexpected token, expected end of statement");
+    if (Parser.parseEOL())
+      return true;
 
+    getTargetStreamer().emitDirectiveOptionNoRelax();
     clearFeatureBits(RISCV::FeatureRelax, "relax");
     return false;
   }

diff  --git a/llvm/test/MC/RISCV/option-invalid.s b/llvm/test/MC/RISCV/option-invalid.s
index d4509b8f9f1c3..b2a0344493c93 100644
--- a/llvm/test/MC/RISCV/option-invalid.s
+++ b/llvm/test/MC/RISCV/option-invalid.s
@@ -1,26 +1,26 @@
 # RUN: not llvm-mc -triple riscv32 < %s 2>&1 \
 # RUN:   | FileCheck -check-prefixes=CHECK %s
 
-# CHECK: error: unexpected token, expected identifier
+# CHECK: :[[#@LINE+1]]:8: error: expected identifier
 .option
 
-# CHECK: error: unexpected token, expected identifier
+# CHECK: :[[#@LINE+1]]:9: error: expected identifier
 .option 123
 
-# CHECK: error: unexpected token, expected identifier
+# CHECK: :[[#@LINE+1]]:9: error: expected identifier
 .option "str"
 
-# CHECK: error: unexpected token, expected end of statement
+# CHECK: :[[#@LINE+1]]:13: error: expected newline
 .option rvc foo
 
-# CHECK: warning: unknown option, expected 'push', 'pop', 'rvc', 'norvc', 'relax' or 'norelax'
+# CHECK: :[[#@LINE+1]]:12: warning: unknown option, expected 'push', 'pop', 'rvc', 'norvc', 'relax' or 'norelax'
 .option bar
 
-# CHECK: error: .option pop with no .option push
+# CHECK: :[[#@LINE+1]]:12: error: .option pop with no .option push
 .option pop
 
-# CHECK: error: unexpected token, expected end of statement
+# CHECK: :[[#@LINE+1]]:14: error: expected newline
 .option push 123
 
-# CHECK: error: unexpected token, expected end of statement
+# CHECK: :[[#@LINE+1]]:13: error: expected newline
 .option pop 123


        


More information about the llvm-commits mailing list