[llvm] [RISCV] Error out on incorrectly spelt @plt on call symbols (PR #135324)

Sudharsan Veeravalli via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 11 00:47:23 PDT 2025


https://github.com/svs-quic created https://github.com/llvm/llvm-project/pull/135324

The asm parser currently accepts anything after the `@` eg. `call symbol at rlt` on call symbols and produces an empty object file. It would be better if we error out.

Fixes #135323

>From 41533fa50538fb06e377dc144e4f7825bbce9e31 Mon Sep 17 00:00:00 2001
From: Sudharsan Veeravalli <quic_svs at quicinc.com>
Date: Fri, 11 Apr 2025 13:11:05 +0530
Subject: [PATCH] [RISCV] Error out on incorrectly spelt @plt on call symbols

The asm parser currently accepts call symbol at rlt without any error and
prints out an empty object file. It would be better if we error out.

Fixes #135323
---
 llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp | 7 +++++--
 llvm/test/MC/RISCV/function-call-invalid.s         | 1 +
 llvm/test/MC/RISCV/tail-call-invalid.s             | 1 +
 3 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp b/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
index 55f1a90b2a01a..5355d38fc046d 100644
--- a/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
+++ b/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
@@ -2123,14 +2123,18 @@ ParseStatus RISCVAsmParser::parseCallSymbol(OperandVector &Operands) {
 
   if (getLexer().getKind() != AsmToken::Identifier)
     return ParseStatus::NoMatch;
+
   std::string Identifier(getTok().getIdentifier());
+  SMLoc E = getTok().getEndLoc();
 
   if (getLexer().peekTok().is(AsmToken::At)) {
     Lex();
     Lex();
     StringRef PLT;
+    SMLoc PLTLoc = getLoc();
     if (getParser().parseIdentifier(PLT) || PLT != "plt")
-      return ParseStatus::Failure;
+      return Error(PLTLoc,
+                   "'@plt' is the only valid operand for this instruction");
   } else if (!getLexer().peekTok().is(AsmToken::EndOfStatement)) {
     // Avoid parsing the register in `call rd, foo` as a call symbol.
     return ParseStatus::NoMatch;
@@ -2138,7 +2142,6 @@ ParseStatus RISCVAsmParser::parseCallSymbol(OperandVector &Operands) {
     Lex();
   }
 
-  SMLoc E = SMLoc::getFromPointer(S.getPointer() + Identifier.size());
   RISCVMCExpr::Specifier Kind = RISCVMCExpr::VK_CALL_PLT;
 
   MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
diff --git a/llvm/test/MC/RISCV/function-call-invalid.s b/llvm/test/MC/RISCV/function-call-invalid.s
index 2b7a85245880d..358d7342c6d1f 100644
--- a/llvm/test/MC/RISCV/function-call-invalid.s
+++ b/llvm/test/MC/RISCV/function-call-invalid.s
@@ -10,3 +10,4 @@ call %lo(1234) # CHECK: :[[@LINE]]:6: error: operand must be a bare symbol name
 call %hi(foo) # CHECK: :[[@LINE]]:6: error: operand must be a bare symbol name
 call %lo(foo) # CHECK: :[[@LINE]]:6: error: operand must be a bare symbol name
 call foo, bar # CHECK: :[[@LINE]]:6: error: operand must be a bare symbol name
+call foo at rlt # CHECK: :[[@LINE]]:10: error: '@plt' is the only valid operand for this instruction
diff --git a/llvm/test/MC/RISCV/tail-call-invalid.s b/llvm/test/MC/RISCV/tail-call-invalid.s
index 270d84df58ac4..6d31354ffce1f 100644
--- a/llvm/test/MC/RISCV/tail-call-invalid.s
+++ b/llvm/test/MC/RISCV/tail-call-invalid.s
@@ -10,3 +10,4 @@ tail %hi(1234) # CHECK: :[[@LINE]]:6: error: operand must be a bare symbol name
 tail %lo(1234) # CHECK: :[[@LINE]]:6: error: operand must be a bare symbol name
 tail %hi(foo) # CHECK: :[[@LINE]]:6: error: operand must be a bare symbol name
 tail %lo(foo) # CHECK: :[[@LINE]]:6: error: operand must be a bare symbol name
+tail foo at mlt # CHECK: :[[@LINE]]:10: error: '@plt' is the only valid operand for this instruction



More information about the llvm-commits mailing list