Skip to content

Commit 5874fc5

Browse files
committed
Handle constant folding for the shift operators
1 parent e020598 commit 5874fc5

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

simplecpp.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ void simplecpp::TokenList::constFold()
589589
constFoldUnaryNotPosNeg(tok);
590590
constFoldMulDivRem(tok);
591591
constFoldAddSub(tok);
592+
constFoldShift(tok);
592593
constFoldComparison(tok);
593594
constFoldBitwise(tok);
594595
constFoldLogicalOp(tok);
@@ -769,6 +770,29 @@ void simplecpp::TokenList::constFoldAddSub(Token *tok)
769770
}
770771
}
771772

773+
void simplecpp::TokenList::constFoldShift(Token *tok)
774+
{
775+
for (; tok && tok->op != ')'; tok = tok->next) {
776+
if (!tok->previous || !tok->previous->number)
777+
continue;
778+
if (!tok->next || !tok->next->number)
779+
continue;
780+
781+
long long result;
782+
if (tok->str == "<<")
783+
result = stringToLL(tok->previous->str) << stringToLL(tok->next->str);
784+
else if (tok->str == ">>")
785+
result = stringToLL(tok->previous->str) >> stringToLL(tok->next->str);
786+
else
787+
continue;
788+
789+
tok = tok->previous;
790+
tok->setstr(toString(result));
791+
deleteToken(tok->next);
792+
deleteToken(tok->next);
793+
}
794+
}
795+
772796
static const std::string NOTEQ("not_eq");
773797
void simplecpp::TokenList::constFoldComparison(Token *tok)
774798
{

simplecpp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ namespace simplecpp {
249249
void constFoldUnaryNotPosNeg(Token *tok);
250250
void constFoldMulDivRem(Token *tok);
251251
void constFoldAddSub(Token *tok);
252+
void constFoldShift(Token *tok);
252253
void constFoldComparison(Token *tok);
253254
void constFoldBitwise(Token *tok);
254255
void constFoldLogicalOp(Token *tok);

0 commit comments

Comments
 (0)