Skip to content

Commit a3b3c26

Browse files
authored
[TableGen] Use assert instead of PrintFatalError in TGLexer. NFC. (llvm#122303)
Do not use the PrintFatalError diagnostic machinery for conditions that can never happen with any input.
1 parent b598715 commit a3b3c26

File tree

2 files changed

+25
-43
lines changed

2 files changed

+25
-43
lines changed

llvm/lib/TableGen/TGLexer.cpp

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,7 @@ tgtok::TokKind TGLexer::LexToken(bool FileOrLineStart) {
235235
return tgtok::dot;
236236

237237
case '\r':
238-
PrintFatalError("getNextChar() must never return '\r'");
239-
return tgtok::Error;
238+
llvm_unreachable("getNextChar() must never return '\r'");
240239

241240
case ' ':
242241
case '\t':
@@ -664,11 +663,10 @@ bool TGLexer::prepExitInclude(bool IncludeStackMustBeEmpty) {
664663
PrepIncludeStack.pop_back();
665664

666665
if (IncludeStackMustBeEmpty) {
667-
if (!PrepIncludeStack.empty())
668-
PrintFatalError("preprocessor include stack is not empty");
666+
assert(PrepIncludeStack.empty() &&
667+
"preprocessor include stack is not empty");
669668
} else {
670-
if (PrepIncludeStack.empty())
671-
PrintFatalError("preprocessor include stack is empty");
669+
assert(!PrepIncludeStack.empty() && "preprocessor include stack is empty");
672670
}
673671

674672
return true;
@@ -718,27 +716,25 @@ tgtok::TokKind TGLexer::prepIsDirective() const {
718716
return tgtok::Error;
719717
}
720718

721-
bool TGLexer::prepEatPreprocessorDirective(tgtok::TokKind Kind) {
719+
void TGLexer::prepEatPreprocessorDirective(tgtok::TokKind Kind) {
722720
TokStart = CurPtr;
723721

724-
for (const auto [PKind, PWord] : PreprocessorDirs)
722+
for (const auto [PKind, PWord] : PreprocessorDirs) {
725723
if (PKind == Kind) {
726724
// Advance CurPtr to the end of the preprocessing word.
727725
CurPtr += PWord.size();
728-
return true;
726+
return;
729727
}
728+
}
730729

731-
PrintFatalError("unsupported preprocessing token in "
732-
"prepEatPreprocessorDirective()");
733-
return false;
730+
llvm_unreachable(
731+
"unsupported preprocessing token in prepEatPreprocessorDirective()");
734732
}
735733

736734
tgtok::TokKind TGLexer::lexPreprocessor(tgtok::TokKind Kind,
737735
bool ReturnNextLiveToken) {
738736
// We must be looking at a preprocessing directive. Eat it!
739-
if (!prepEatPreprocessorDirective(Kind))
740-
PrintFatalError("lexPreprocessor() called for unknown "
741-
"preprocessor directive");
737+
prepEatPreprocessorDirective(Kind);
742738

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

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

823-
if (IfdefOrElseEntry.Kind != tgtok::Ifdef &&
824-
IfdefOrElseEntry.Kind != tgtok::Else) {
825-
PrintFatalError("invalid preprocessor control on the stack");
826-
return tgtok::Error;
827-
}
819+
assert((IfdefOrElseEntry.Kind == tgtok::Ifdef ||
820+
IfdefOrElseEntry.Kind == tgtok::Else) &&
821+
"invalid preprocessor control on the stack");
828822

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

855-
if (!ReturnNextLiveToken) {
856-
PrintFatalError("#define must be ignored during the lines skipping");
857-
return tgtok::Error;
858-
}
849+
assert(ReturnNextLiveToken &&
850+
"#define must be ignored during the lines skipping");
859851

860852
return LexToken();
861853
}
862854

863-
PrintFatalError("preprocessing directive is not supported");
864-
return tgtok::Error;
855+
llvm_unreachable("preprocessing directive is not supported");
865856
}
866857

867858
bool TGLexer::prepSkipRegion(bool MustNeverBeFalse) {
868-
if (!MustNeverBeFalse)
869-
PrintFatalError("invalid recursion.");
859+
assert(MustNeverBeFalse && "invalid recursion.");
870860

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

905-
if (Kind != ProcessedKind)
906-
PrintFatalError("prepIsDirective() and lexPreprocessor() "
907-
"returned different token kinds");
895+
assert(Kind == ProcessedKind && "prepIsDirective() and lexPreprocessor() "
896+
"returned different token kinds");
908897

909898
// If this preprocessing directive enables tokens processing,
910899
// then return to the lexPreprocessor() and get to the next token.
911900
// We can move from line-skipping mode to processing tokens only
912901
// due to #else or #endif.
913902
if (prepIsProcessingEnabled()) {
914-
if (Kind != tgtok::Else && Kind != tgtok::Endif) {
915-
PrintFatalError("tokens processing was enabled by an unexpected "
916-
"preprocessing directive");
917-
return false;
918-
}
903+
assert((Kind == tgtok::Else || Kind == tgtok::Endif) &&
904+
"tokens processing was enabled by an unexpected preprocessing "
905+
"directive");
919906

920907
return true;
921908
}
@@ -1053,10 +1040,6 @@ bool TGLexer::prepIsProcessingEnabled() {
10531040
}
10541041

10551042
void TGLexer::prepReportPreprocessorStackError() {
1056-
if (PrepIncludeStack.back().empty())
1057-
PrintFatalError("prepReportPreprocessorStackError() called with "
1058-
"empty control stack");
1059-
10601043
auto &PrepControl = PrepIncludeStack.back().back();
10611044
PrintError(CurBuf.end(), "reached EOF without matching #endif");
10621045
PrintError(PrepControl.SrcPos, "the latest preprocessor control is here");

llvm/lib/TableGen/TGLexer.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,14 +347,13 @@ class TGLexer {
347347
tgtok::TokKind prepIsDirective() const;
348348

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

359358
// The main "exit" point from the token parsing to preprocessor.
360359
//

0 commit comments

Comments
 (0)