Skip to content

[TableGen] Use assert instead of PrintFatalError in TGLexer. NFC. #122303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 23 additions & 40 deletions llvm/lib/TableGen/TGLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,7 @@ tgtok::TokKind TGLexer::LexToken(bool FileOrLineStart) {
return tgtok::dot;

case '\r':
PrintFatalError("getNextChar() must never return '\r'");
return tgtok::Error;
llvm_unreachable("getNextChar() must never return '\r'");

case ' ':
case '\t':
Expand Down Expand Up @@ -664,11 +663,10 @@ bool TGLexer::prepExitInclude(bool IncludeStackMustBeEmpty) {
PrepIncludeStack.pop_back();

if (IncludeStackMustBeEmpty) {
if (!PrepIncludeStack.empty())
PrintFatalError("preprocessor include stack is not empty");
assert(PrepIncludeStack.empty() &&
"preprocessor include stack is not empty");
} else {
if (PrepIncludeStack.empty())
PrintFatalError("preprocessor include stack is empty");
assert(!PrepIncludeStack.empty() && "preprocessor include stack is empty");
}

return true;
Expand Down Expand Up @@ -718,27 +716,25 @@ tgtok::TokKind TGLexer::prepIsDirective() const {
return tgtok::Error;
}

bool TGLexer::prepEatPreprocessorDirective(tgtok::TokKind Kind) {
void TGLexer::prepEatPreprocessorDirective(tgtok::TokKind Kind) {
TokStart = CurPtr;

for (const auto [PKind, PWord] : PreprocessorDirs)
for (const auto [PKind, PWord] : PreprocessorDirs) {
if (PKind == Kind) {
// Advance CurPtr to the end of the preprocessing word.
CurPtr += PWord.size();
return true;
return;
}
}

PrintFatalError("unsupported preprocessing token in "
"prepEatPreprocessorDirective()");
return false;
llvm_unreachable(
"unsupported preprocessing token in prepEatPreprocessorDirective()");
}

tgtok::TokKind TGLexer::lexPreprocessor(tgtok::TokKind Kind,
bool ReturnNextLiveToken) {
// We must be looking at a preprocessing directive. Eat it!
if (!prepEatPreprocessorDirective(Kind))
PrintFatalError("lexPreprocessor() called for unknown "
"preprocessor directive");
prepEatPreprocessorDirective(Kind);

if (Kind == tgtok::Ifdef || Kind == tgtok::Ifndef) {
StringRef MacroName = prepLexMacroName();
Expand Down Expand Up @@ -820,11 +816,9 @@ tgtok::TokKind TGLexer::lexPreprocessor(tgtok::TokKind Kind,

auto &IfdefOrElseEntry = PrepIncludeStack.back().back();

if (IfdefOrElseEntry.Kind != tgtok::Ifdef &&
IfdefOrElseEntry.Kind != tgtok::Else) {
PrintFatalError("invalid preprocessor control on the stack");
return tgtok::Error;
}
assert((IfdefOrElseEntry.Kind == tgtok::Ifdef ||
IfdefOrElseEntry.Kind == tgtok::Else) &&
"invalid preprocessor control on the stack");

if (!prepSkipDirectiveEnd())
return ReturnError(CurPtr, "only comments are supported after #endif");
Expand Down Expand Up @@ -852,21 +846,17 @@ tgtok::TokKind TGLexer::lexPreprocessor(tgtok::TokKind Kind,
return ReturnError(CurPtr,
"only comments are supported after #define NAME");

if (!ReturnNextLiveToken) {
PrintFatalError("#define must be ignored during the lines skipping");
return tgtok::Error;
}
assert(ReturnNextLiveToken &&
"#define must be ignored during the lines skipping");

return LexToken();
}

PrintFatalError("preprocessing directive is not supported");
return tgtok::Error;
llvm_unreachable("preprocessing directive is not supported");
}

bool TGLexer::prepSkipRegion(bool MustNeverBeFalse) {
if (!MustNeverBeFalse)
PrintFatalError("invalid recursion.");
assert(MustNeverBeFalse && "invalid recursion.");

do {
// Skip all symbols to the line end.
Expand Down Expand Up @@ -902,20 +892,17 @@ bool TGLexer::prepSkipRegion(bool MustNeverBeFalse) {
if (ProcessedKind == tgtok::Error)
return false;

if (Kind != ProcessedKind)
PrintFatalError("prepIsDirective() and lexPreprocessor() "
"returned different token kinds");
assert(Kind == ProcessedKind && "prepIsDirective() and lexPreprocessor() "
"returned different token kinds");

// If this preprocessing directive enables tokens processing,
// then return to the lexPreprocessor() and get to the next token.
// We can move from line-skipping mode to processing tokens only
// due to #else or #endif.
if (prepIsProcessingEnabled()) {
if (Kind != tgtok::Else && Kind != tgtok::Endif) {
PrintFatalError("tokens processing was enabled by an unexpected "
"preprocessing directive");
return false;
}
assert((Kind == tgtok::Else || Kind == tgtok::Endif) &&
"tokens processing was enabled by an unexpected preprocessing "
"directive");

return true;
}
Expand Down Expand Up @@ -1053,10 +1040,6 @@ bool TGLexer::prepIsProcessingEnabled() {
}

void TGLexer::prepReportPreprocessorStackError() {
if (PrepIncludeStack.back().empty())
PrintFatalError("prepReportPreprocessorStackError() called with "
"empty control stack");

auto &PrepControl = PrepIncludeStack.back().back();
PrintError(CurBuf.end(), "reached EOF without matching #endif");
PrintError(PrepControl.SrcPos, "the latest preprocessor control is here");
Expand Down
5 changes: 2 additions & 3 deletions llvm/lib/TableGen/TGLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,13 @@ class TGLexer {
tgtok::TokKind prepIsDirective() const;

// Given a preprocessing token kind, adjusts CurPtr to the end
// of the preprocessing directive word. Returns true, unless
// an unsupported token kind is passed in.
// of the preprocessing directive word.
//
// We use look-ahead prepIsDirective() and prepEatPreprocessorDirective()
// to avoid adjusting CurPtr before we are sure that '#' is followed
// by a preprocessing directive. If it is not, then we fall back to
// tgtok::paste interpretation of '#'.
bool prepEatPreprocessorDirective(tgtok::TokKind Kind);
void prepEatPreprocessorDirective(tgtok::TokKind Kind);

// The main "exit" point from the token parsing to preprocessor.
//
Expand Down
Loading