diff options
author | Jay Foad <jay.foad@amd.com> | 2025-01-13 10:30:55 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-01-13 10:30:55 +0000 |
commit | a3b3c26048e1e9397cf412b07f09f82fe49e351e (patch) | |
tree | 0ce75dc1d610d4fb8755f983e261cbc8ef28c27f /llvm/lib | |
parent | b5987157e86b3ef87b8ed95f737e0a016974c793 (diff) | |
download | llvm-a3b3c26048e1e9397cf412b07f09f82fe49e351e.zip llvm-a3b3c26048e1e9397cf412b07f09f82fe49e351e.tar.gz llvm-a3b3c26048e1e9397cf412b07f09f82fe49e351e.tar.bz2 |
[TableGen] Use assert instead of PrintFatalError in TGLexer. NFC. (#122303)
Do not use the PrintFatalError diagnostic machinery for conditions that
can never happen with any input.
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/TableGen/TGLexer.cpp | 63 | ||||
-rw-r--r-- | llvm/lib/TableGen/TGLexer.h | 5 |
2 files changed, 25 insertions, 43 deletions
diff --git a/llvm/lib/TableGen/TGLexer.cpp b/llvm/lib/TableGen/TGLexer.cpp index e23aec6..c423023 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 f8b32dc..bac583c 100644 --- a/llvm/lib/TableGen/TGLexer.h +++ b/llvm/lib/TableGen/TGLexer.h @@ -347,14 +347,13 @@ private: 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. // |