[llvm] 92c93f5 - [MC] Merge MCAsmLexer and AsmLexer
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 3 22:11:54 PDT 2025
Author: Fangrui Song
Date: 2025-04-03T22:11:49-07:00
New Revision: 92c93f5286b9ff33f27ff694d2dc33da1c07afdd
URL: https://github.com/llvm/llvm-project/commit/92c93f5286b9ff33f27ff694d2dc33da1c07afdd
DIFF: https://github.com/llvm/llvm-project/commit/92c93f5286b9ff33f27ff694d2dc33da1c07afdd.diff
LOG: [MC] Merge MCAsmLexer and AsmLexer
Follow-up to #134207
Both classes define `IsAtStartOfStatement` but the semantics are
confusingly different. Rename the base class one.
Added:
Modified:
llvm/include/llvm/MC/MCParser/AsmLexer.h
llvm/include/llvm/MC/MCParser/MCAsmParser.h
llvm/lib/MC/MCParser/AsmLexer.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/MC/MCParser/AsmLexer.h b/llvm/include/llvm/MC/MCParser/AsmLexer.h
index d1599dc47e76b..bf4aa09df613f 100644
--- a/llvm/include/llvm/MC/MCParser/AsmLexer.h
+++ b/llvm/include/llvm/MC/MCParser/AsmLexer.h
@@ -39,17 +39,24 @@ class AsmCommentConsumer {
virtual void HandleComment(SMLoc Loc, StringRef CommentText) = 0;
};
-/// Generic assembler lexer interface, for use by target specific assembly
-/// lexers.
-class MCAsmLexer {
+class AsmLexer {
/// The current token, stored in the base class for faster access.
SmallVector<AsmToken, 1> CurTok;
+ const char *CurPtr = nullptr;
+ StringRef CurBuf;
+
/// The location and description of the current error
SMLoc ErrLoc;
std::string Err;
-protected: // Can only create subclasses.
+ const MCAsmInfo &MAI;
+
+ bool IsAtStartOfLine = true;
+ bool AtStartOfStatement = true;
+ bool IsPeeking = false;
+ bool EndStatementAtEOF = true;
+
const char *TokStart = nullptr;
bool SkipSpace = true;
bool AllowAtInIdentifier = false;
@@ -65,9 +72,7 @@ class MCAsmLexer {
bool LexHLASMStrings = false;
AsmCommentConsumer *CommentConsumer = nullptr;
- MCAsmLexer();
-
- virtual AsmToken LexToken() = 0;
+ AsmToken LexToken();
void SetError(SMLoc errLoc, const std::string &err) {
ErrLoc = errLoc;
@@ -75,9 +80,9 @@ class MCAsmLexer {
}
public:
- MCAsmLexer(const MCAsmLexer &) = delete;
- MCAsmLexer &operator=(const MCAsmLexer &) = delete;
- virtual ~MCAsmLexer();
+ AsmLexer(const MCAsmInfo &MAI);
+ AsmLexer(const AsmLexer &) = delete;
+ AsmLexer &operator=(const AsmLexer &) = delete;
/// Consume the next token from the input stream and return it.
///
@@ -86,7 +91,7 @@ class MCAsmLexer {
const AsmToken &Lex() {
assert(!CurTok.empty());
// Mark if we parsing out a EndOfStatement.
- IsAtStartOfStatement = CurTok.front().getKind() == AsmToken::EndOfStatement;
+ AtStartOfStatement = CurTok.front().getKind() == AsmToken::EndOfStatement;
CurTok.erase(CurTok.begin());
// LexToken may generate multiple tokens via UnLex but will always return
// the first one. Place returned value at head of CurTok vector.
@@ -98,16 +103,16 @@ class MCAsmLexer {
}
void UnLex(AsmToken const &Token) {
- IsAtStartOfStatement = false;
+ AtStartOfStatement = false;
CurTok.insert(CurTok.begin(), Token);
}
- bool isAtStartOfStatement() { return IsAtStartOfStatement; }
+ bool isAtStartOfStatement() { return AtStartOfStatement; }
- virtual StringRef LexUntilEndOfStatement() = 0;
+ StringRef LexUntilEndOfStatement();
/// Get the current source location.
- SMLoc getLoc() const;
+ SMLoc getLoc() const { return SMLoc::getFromPointer(TokStart); }
/// Get the current (last) lexed token.
const AsmToken &getTok() const { return CurTok[0]; }
@@ -126,8 +131,7 @@ class MCAsmLexer {
}
/// Look ahead an arbitrary number of tokens.
- virtual size_t peekTokens(MutableArrayRef<AsmToken> Buf,
- bool ShouldSkipSpace = true) = 0;
+ size_t peekTokens(MutableArrayRef<AsmToken> Buf, bool ShouldSkipSpace = true);
/// Get the current error location
SMLoc getErrLoc() { return ErrLoc; }
@@ -185,37 +189,10 @@ class MCAsmLexer {
/// setting this option to true, will disable lexing for character and string
/// literals.
void setLexHLASMStrings(bool V) { LexHLASMStrings = V; }
-};
-
-/// AsmLexer - Lexer class for assembly files.
-class AsmLexer final : public MCAsmLexer {
- const MCAsmInfo &MAI;
-
- const char *CurPtr = nullptr;
- StringRef CurBuf;
- bool IsAtStartOfLine = true;
- bool IsAtStartOfStatement = true;
- bool IsPeeking = false;
- bool EndStatementAtEOF = true;
-
-protected:
- /// LexToken - Read the next token and return its code.
- AsmToken LexToken() override;
-
-public:
- AsmLexer(const MCAsmInfo &MAI);
- AsmLexer(const AsmLexer &) = delete;
- AsmLexer &operator=(const AsmLexer &) = delete;
- ~AsmLexer() override;
void setBuffer(StringRef Buf, const char *ptr = nullptr,
bool EndStatementAtEOF = true);
- StringRef LexUntilEndOfStatement() override;
-
- size_t peekTokens(MutableArrayRef<AsmToken> Buf,
- bool ShouldSkipSpace = true) override;
-
const MCAsmInfo &getMAI() const { return MAI; }
private:
@@ -237,6 +214,8 @@ class AsmLexer final : public MCAsmLexer {
StringRef LexUntilEndOfLine();
};
+using MCAsmLexer = AsmLexer;
+
} // end namespace llvm
#endif // LLVM_MC_MCPARSER_ASMLEXER_H
diff --git a/llvm/include/llvm/MC/MCParser/MCAsmParser.h b/llvm/include/llvm/MC/MCParser/MCAsmParser.h
index c65a38c944eea..7cdd99a207468 100644
--- a/llvm/include/llvm/MC/MCParser/MCAsmParser.h
+++ b/llvm/include/llvm/MC/MCParser/MCAsmParser.h
@@ -24,7 +24,6 @@
namespace llvm {
-class MCAsmLexer;
class MCAsmInfo;
class MCAsmParserExtension;
class MCExpr;
diff --git a/llvm/lib/MC/MCParser/AsmLexer.cpp b/llvm/lib/MC/MCParser/AsmLexer.cpp
index 3c911dba8cc26..b91f1f544d29c 100644
--- a/llvm/lib/MC/MCParser/AsmLexer.cpp
+++ b/llvm/lib/MC/MCParser/AsmLexer.cpp
@@ -111,21 +111,15 @@ void AsmToken::dump(raw_ostream &OS) const {
OS << "\")";
}
-MCAsmLexer::MCAsmLexer() { CurTok.emplace_back(AsmToken::Space, StringRef()); }
-
-MCAsmLexer::~MCAsmLexer() = default;
-
-SMLoc MCAsmLexer::getLoc() const { return SMLoc::getFromPointer(TokStart); }
-
AsmLexer::AsmLexer(const MCAsmInfo &MAI) : MAI(MAI) {
// For COFF targets, this is true, while for ELF targets, it should be false.
// Currently, @specifier parsing depends on '@' being included in the token.
AllowAtInIdentifier = !StringRef(MAI.getCommentString()).starts_with("@") &&
MAI.useAtForSpecifier();
LexMotorolaIntegers = MAI.shouldUseMotorolaIntegers();
-}
-AsmLexer::~AsmLexer() = default;
+ CurTok.emplace_back(AsmToken::Space, StringRef());
+}
void AsmLexer::setBuffer(StringRef Buf, const char *ptr,
bool EndStatementAtEOF) {
More information about the llvm-commits
mailing list