[llvm] 3f7bde7 - [MCParser] Share identifier character set with isMacroArgChar (#219084)

via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 29 16:42:40 PDT 2026


Author: Fangrui Song
Date: 2026-08-29T16:42:36-07:00
New Revision: 3f7bde7a7771df40aca3dfc3ceae53e835b21a78

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

LOG: [MCParser] Share identifier character set with isMacroArgChar (#219084)

Move the predicate to the header so that it can be used by the macro
argument name checker. Replace the incorrect local-sensitive isalnum()
with ASCII-only llvm::isAlnum(). Identifier character set accepting '?'
is a MC extension: https://reviews.llvm.org/D1978

While here, fix some minor bound checking related minor issues.

LLM-aided

Added: 
    

Modified: 
    llvm/include/llvm/MC/MCParser/AsmLexer.h
    llvm/lib/MC/MCParser/AsmLexer.cpp
    llvm/lib/MC/MCParser/AsmParser.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/MC/MCParser/AsmLexer.h b/llvm/include/llvm/MC/MCParser/AsmLexer.h
index 7848fc706d5eb..6ab484565c0d4 100644
--- a/llvm/include/llvm/MC/MCParser/AsmLexer.h
+++ b/llvm/include/llvm/MC/MCParser/AsmLexer.h
@@ -15,6 +15,7 @@
 
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/MC/MCAsmMacro.h"
 #include "llvm/Support/Compiler.h"
@@ -202,6 +203,11 @@ class AsmLexer {
 
   const MCAsmInfo &getMAI() const { return MAI; }
 
+  static bool isIdentifierChar(char C, bool AllowAt, bool AllowHash) {
+    return isAlnum(C) || C == '_' || C == '$' || C == '.' || C == '?' ||
+           (AllowAt && C == '@') || (AllowHash && C == '#');
+  }
+
 private:
   bool isAtStartOfComment(const char *Ptr);
   bool isAtStatementSeparator(const char *Ptr);

diff  --git a/llvm/lib/MC/MCParser/AsmLexer.cpp b/llvm/lib/MC/MCParser/AsmLexer.cpp
index 8e4b7be98bdb6..cb7bb903276d4 100644
--- a/llvm/lib/MC/MCParser/AsmLexer.cpp
+++ b/llvm/lib/MC/MCParser/AsmLexer.cpp
@@ -228,12 +228,6 @@ AsmToken AsmLexer::LexHexFloatLiteral(bool NoIntDigits) {
   return AsmToken(AsmToken::Real, StringRef(TokStart, CurPtr - TokStart));
 }
 
-/// LexIdentifier: [a-zA-Z_$.@?][a-zA-Z0-9_$.@#?]*
-static bool isIdentifierChar(char C, bool AllowAt, bool AllowHash) {
-  return isAlnum(C) || C == '_' || C == '$' || C == '.' || C == '?' ||
-         (AllowAt && C == '@') || (AllowHash && C == '#');
-}
-
 AsmToken AsmLexer::LexIdentifier() {
   // Check for floating point literals.
   if (CurPtr[-1] == '.' && isDigit(*CurPtr)) {

diff  --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index 839dbca990578..588cc8e3ffde3 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -2459,13 +2459,12 @@ void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
     Parser->getContext().diagnose(NewDiag);
 }
 
-// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
-// 
diff erence being that that function accepts '@' as part of identifiers and
-// we can't do that. AsmLexer.cpp should probably be changed to handle
-// '@' as a special case when needed.
-static bool isIdentifierChar(char c) {
-  return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
-         c == '.';
+// A macro argument name ends at '@', '#' and '?', which may be identifier
+// characters.
+static bool isMacroArgChar(char C) {
+  return AsmLexer::isIdentifierChar(C, /*AllowAt=*/false,
+                                    /*AllowHash=*/false) &&
+         C != '?';
 }
 
 bool AsmParser::expandMacro(raw_svector_ostream &OS, MCAsmMacro &Macro,
@@ -2525,7 +2524,7 @@ bool AsmParser::expandMacro(raw_svector_ostream &OS, MCAsmMacro &Macro,
       }
 
       size_t Pos = ++I;
-      while (I != End && isIdentifierChar(Body[I]))
+      while (I != End && isMacroArgChar(Body[I]))
         ++I;
       StringRef Argument(Body.data() + Pos, I - Pos);
       if (AltMacroMode && I != End && Body[I] == '&')
@@ -2571,13 +2570,13 @@ bool AsmParser::expandMacro(raw_svector_ostream &OS, MCAsmMacro &Macro,
       }
     }
 
-    if (!isIdentifierChar(Body[I]) || IsDarwin) {
+    if (!isMacroArgChar(Body[I]) || IsDarwin) {
       OS << Body[I++];
       continue;
     }
 
     const size_t Start = I;
-    while (++I != End && isIdentifierChar(Body[I])) {
+    while (++I != End && isMacroArgChar(Body[I])) {
     }
     StringRef Token(Body.data() + Start, I - Start);
     if (AltMacroMode) {
@@ -4903,7 +4902,7 @@ void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
       Pos += 2;
     } else {
       size_t I = Pos + 1;
-      while (I != End && isIdentifierChar(Body[I]))
+      while (I != End && isMacroArgChar(Body[I]))
         ++I;
 
       const char *Begin = Body.data() + Pos + 1;


        


More information about the llvm-commits mailing list