Skip to content

Commit f566848

Browse files
authored
Add caching of conditional directive chains (#468)
1 parent 4bbd1bf commit f566848

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

simplecpp.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3532,6 +3532,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
35323532
// AlwaysFalse => drop all code in #if and #else
35333533
enum IfState { True, ElseIsTrue, AlwaysFalse };
35343534
std::stack<int> ifstates;
3535+
std::stack<const Token *> iftokens;
35353536
ifstates.push(True);
35363537

35373538
std::stack<const Token *> includetokenstack;
@@ -3855,15 +3856,24 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
38553856
ifstates.push(AlwaysFalse);
38563857
else
38573858
ifstates.push(conditionIsTrue ? True : ElseIsTrue);
3859+
iftokens.push(rawtok);
38583860
} else if (ifstates.top() == True) {
38593861
ifstates.top() = AlwaysFalse;
3862+
iftokens.top()->nextcond = rawtok;
3863+
iftokens.top() = rawtok;
38603864
} else if (ifstates.top() == ElseIsTrue && conditionIsTrue) {
38613865
ifstates.top() = True;
3866+
iftokens.top()->nextcond = rawtok;
3867+
iftokens.top() = rawtok;
38623868
}
38633869
} else if (rawtok->str() == ELSE) {
38643870
ifstates.top() = (ifstates.top() == ElseIsTrue) ? True : AlwaysFalse;
3871+
iftokens.top()->nextcond = rawtok;
3872+
iftokens.top() = rawtok;
38653873
} else if (rawtok->str() == ENDIF) {
38663874
ifstates.pop();
3875+
iftokens.top()->nextcond = rawtok;
3876+
iftokens.pop();
38673877
} else if (rawtok->str() == UNDEF) {
38683878
if (ifstates.top() == True) {
38693879
const Token *tok = rawtok->next;
@@ -3875,7 +3885,10 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
38753885
} else if (ifstates.top() == True && rawtok->str() == PRAGMA && rawtok->next && rawtok->next->str() == ONCE && sameline(rawtok,rawtok->next)) {
38763886
pragmaOnce.insert(rawtok->location.file());
38773887
}
3878-
rawtok = gotoNextLine(rawtok);
3888+
if (ifstates.top() != True && rawtok->nextcond)
3889+
rawtok = rawtok->nextcond->previous;
3890+
else
3891+
rawtok = gotoNextLine(rawtok);
38793892
continue;
38803893
}
38813894

simplecpp.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ namespace simplecpp {
9696
class SIMPLECPP_LIB Token {
9797
public:
9898
Token(const TokenString &s, const Location &loc, bool wsahead = false) :
99-
whitespaceahead(wsahead), location(loc), previous(nullptr), next(nullptr), string(s) {
99+
whitespaceahead(wsahead), location(loc), previous(nullptr), next(nullptr), nextcond(nullptr), string(s) {
100100
flags();
101101
}
102102

103103
Token(const Token &tok) :
104-
macro(tok.macro), op(tok.op), comment(tok.comment), name(tok.name), number(tok.number), whitespaceahead(tok.whitespaceahead), location(tok.location), previous(nullptr), next(nullptr), string(tok.string), mExpandedFrom(tok.mExpandedFrom) {
104+
macro(tok.macro), op(tok.op), comment(tok.comment), name(tok.name), number(tok.number), whitespaceahead(tok.whitespaceahead), location(tok.location), previous(nullptr), next(nullptr), nextcond(nullptr), string(tok.string), mExpandedFrom(tok.mExpandedFrom) {
105105
}
106106

107107
void flags() {
@@ -137,6 +137,7 @@ namespace simplecpp {
137137
Location location;
138138
Token *previous;
139139
Token *next;
140+
mutable const Token *nextcond;
140141

141142
const Token *previousSkipComments() const {
142143
const Token *tok = this->previous;

0 commit comments

Comments
 (0)