-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[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
Conversation
Do not use the PrintFatalError diagnostic machinery for conditions that can never happen with any input.
@llvm/pr-subscribers-tablegen Author: Jay Foad (jayfoad) ChangesDo not use the PrintFatalError diagnostic machinery for conditions that Full diff: https://github.com/llvm/llvm-project/pull/122303.diff 2 Files Affected:
diff --git a/llvm/lib/TableGen/TGLexer.cpp b/llvm/lib/TableGen/TGLexer.cpp
index e23aec6efba59d..c423023077cd8b 100644
--- a/llvm/lib/TableGen/TGLexer.cpp
+++ b/llvm/lib/TableGen/TGLexer.cpp
@@ -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':
@@ -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;
@@ -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();
@@ -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");
@@ -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.
@@ -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;
}
@@ -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");
diff --git a/llvm/lib/TableGen/TGLexer.h b/llvm/lib/TableGen/TGLexer.h
index f8b32dc5377f58..bac583c4e33a18 100644
--- a/llvm/lib/TableGen/TGLexer.h
+++ b/llvm/lib/TableGen/TGLexer.h
@@ -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.
//
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Happy when s-barannikov is happy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/16/builds/11868 Here is the relevant piece of the build log for the reference
|
…vm#122303) Do not use the PrintFatalError diagnostic machinery for conditions that can never happen with any input.
Do not use the PrintFatalError diagnostic machinery for conditions that
can never happen with any input.