Skip to content

Commit cb2eafe

Browse files
authored
[TableGen] Use SmallVectors for preprocessor include stack. NFC. (#121571)
This is just a minor cleanup and a small step in the direction of using LLVM containers in preference to STL containers in lib/TableGen.
1 parent 11c6af6 commit cb2eafe

File tree

2 files changed

+17
-29
lines changed

2 files changed

+17
-29
lines changed

llvm/lib/TableGen/TGLexer.cpp

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ TGLexer::TGLexer(SourceMgr &SM, ArrayRef<std::string> Macros) : SrcMgr(SM) {
8181
TokStart = nullptr;
8282

8383
// Pretend that we enter the "top-level" include file.
84-
PrepIncludeStack.push_back(
85-
std::make_unique<std::vector<PreprocessorControlDesc>>());
84+
PrepIncludeStack.emplace_back();
8685

8786
// Add all macros defined on the command line to the DefinedMacros set.
8887
// Check invalid macro names and print fatal error if we find one.
@@ -453,8 +452,7 @@ bool TGLexer::LexInclude() {
453452
CurBuf = SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer();
454453
CurPtr = CurBuf.begin();
455454

456-
PrepIncludeStack.push_back(
457-
std::make_unique<std::vector<PreprocessorControlDesc>>());
455+
PrepIncludeStack.emplace_back();
458456
return false;
459457
}
460458

@@ -656,17 +654,13 @@ tgtok::TokKind TGLexer::LexExclaim() {
656654
bool TGLexer::prepExitInclude(bool IncludeStackMustBeEmpty) {
657655
// Report an error, if preprocessor control stack for the current
658656
// file is not empty.
659-
if (!PrepIncludeStack.back()->empty()) {
657+
if (!PrepIncludeStack.back().empty()) {
660658
prepReportPreprocessorStackError();
661659

662660
return false;
663661
}
664662

665663
// Pop the preprocessing controls from the include stack.
666-
if (PrepIncludeStack.empty()) {
667-
PrintFatalError("preprocessor include stack is empty");
668-
}
669-
670664
PrepIncludeStack.pop_back();
671665

672666
if (IncludeStackMustBeEmpty) {
@@ -761,7 +755,7 @@ tgtok::TokKind TGLexer::lexPreprocessor(tgtok::TokKind Kind,
761755
// Regardless of whether we are processing tokens or not,
762756
// we put the #ifdef control on stack.
763757
// Note that MacroIsDefined has been canonicalized against ifdef.
764-
PrepIncludeStack.back()->push_back(
758+
PrepIncludeStack.back().push_back(
765759
{tgtok::Ifdef, MacroIsDefined, SMLoc::getFromPointer(TokStart)});
766760

767761
if (!prepSkipDirectiveEnd())
@@ -789,10 +783,10 @@ tgtok::TokKind TGLexer::lexPreprocessor(tgtok::TokKind Kind,
789783
} else if (Kind == tgtok::Else) {
790784
// Check if this #else is correct before calling prepSkipDirectiveEnd(),
791785
// which will move CurPtr away from the beginning of #else.
792-
if (PrepIncludeStack.back()->empty())
786+
if (PrepIncludeStack.back().empty())
793787
return ReturnError(TokStart, "#else without #ifdef or #ifndef");
794788

795-
PreprocessorControlDesc IfdefEntry = PrepIncludeStack.back()->back();
789+
PreprocessorControlDesc IfdefEntry = PrepIncludeStack.back().back();
796790

797791
if (IfdefEntry.Kind != tgtok::Ifdef) {
798792
PrintError(TokStart, "double #else");
@@ -801,9 +795,8 @@ tgtok::TokKind TGLexer::lexPreprocessor(tgtok::TokKind Kind,
801795

802796
// Replace the corresponding #ifdef's control with its negation
803797
// on the control stack.
804-
PrepIncludeStack.back()->pop_back();
805-
PrepIncludeStack.back()->push_back(
806-
{Kind, !IfdefEntry.IsDefined, SMLoc::getFromPointer(TokStart)});
798+
PrepIncludeStack.back().back() = {Kind, !IfdefEntry.IsDefined,
799+
SMLoc::getFromPointer(TokStart)};
807800

808801
if (!prepSkipDirectiveEnd())
809802
return ReturnError(CurPtr, "only comments are supported after #else");
@@ -822,10 +815,10 @@ tgtok::TokKind TGLexer::lexPreprocessor(tgtok::TokKind Kind,
822815
} else if (Kind == tgtok::Endif) {
823816
// Check if this #endif is correct before calling prepSkipDirectiveEnd(),
824817
// which will move CurPtr away from the beginning of #endif.
825-
if (PrepIncludeStack.back()->empty())
818+
if (PrepIncludeStack.back().empty())
826819
return ReturnError(TokStart, "#endif without #ifdef");
827820

828-
auto &IfdefOrElseEntry = PrepIncludeStack.back()->back();
821+
auto &IfdefOrElseEntry = PrepIncludeStack.back().back();
829822

830823
if (IfdefOrElseEntry.Kind != tgtok::Ifdef &&
831824
IfdefOrElseEntry.Kind != tgtok::Else) {
@@ -836,7 +829,7 @@ tgtok::TokKind TGLexer::lexPreprocessor(tgtok::TokKind Kind,
836829
if (!prepSkipDirectiveEnd())
837830
return ReturnError(CurPtr, "only comments are supported after #endif");
838831

839-
PrepIncludeStack.back()->pop_back();
832+
PrepIncludeStack.back().pop_back();
840833

841834
// If we were processing tokens before this #endif, then
842835
// we should continue it.
@@ -1055,20 +1048,16 @@ bool TGLexer::prepSkipDirectiveEnd() {
10551048
}
10561049

10571050
bool TGLexer::prepIsProcessingEnabled() {
1058-
for (const PreprocessorControlDesc &I :
1059-
llvm::reverse(*PrepIncludeStack.back()))
1060-
if (!I.IsDefined)
1061-
return false;
1062-
1063-
return true;
1051+
return all_of(PrepIncludeStack.back(),
1052+
[](const PreprocessorControlDesc &I) { return I.IsDefined; });
10641053
}
10651054

10661055
void TGLexer::prepReportPreprocessorStackError() {
1067-
if (PrepIncludeStack.back()->empty())
1056+
if (PrepIncludeStack.back().empty())
10681057
PrintFatalError("prepReportPreprocessorStackError() called with "
10691058
"empty control stack");
10701059

1071-
auto &PrepControl = PrepIncludeStack.back()->back();
1060+
auto &PrepControl = PrepIncludeStack.back().back();
10721061
PrintError(CurBuf.end(), "reached EOF without matching #endif");
10731062
PrintError(PrepControl.SrcPos, "the latest preprocessor control is here");
10741063

llvm/lib/TableGen/TGLexer.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef LLVM_LIB_TABLEGEN_TGLEXER_H
1414
#define LLVM_LIB_TABLEGEN_TGLEXER_H
1515

16+
#include "llvm/ADT/SmallVector.h"
1617
#include "llvm/ADT/StringRef.h"
1718
#include "llvm/ADT/StringSet.h"
1819
#include "llvm/Support/DataTypes.h"
@@ -21,7 +22,6 @@
2122
#include <memory>
2223
#include <set>
2324
#include <string>
24-
#include <vector>
2525

2626
namespace llvm {
2727
template <typename T> class ArrayRef;
@@ -323,8 +323,7 @@ class TGLexer {
323323
// preprocessing control stacks for the current file and all its
324324
// parent files. The back() element is the preprocessing control
325325
// stack for the current file.
326-
std::vector<std::unique_ptr<std::vector<PreprocessorControlDesc>>>
327-
PrepIncludeStack;
326+
SmallVector<SmallVector<PreprocessorControlDesc>> PrepIncludeStack;
328327

329328
// Validate that the current preprocessing control stack is empty,
330329
// since we are about to exit a file, and pop the include stack.

0 commit comments

Comments
 (0)