Skip to content

Add caching of conditional directive chains #468

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

Merged
merged 1 commit into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3532,6 +3532,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
// AlwaysFalse => drop all code in #if and #else
enum IfState { True, ElseIsTrue, AlwaysFalse };
std::stack<int> ifstates;
std::stack<const Token *> iftokens;
ifstates.push(True);

std::stack<const Token *> includetokenstack;
Expand Down Expand Up @@ -3855,15 +3856,24 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
ifstates.push(AlwaysFalse);
else
ifstates.push(conditionIsTrue ? True : ElseIsTrue);
iftokens.push(rawtok);
} else if (ifstates.top() == True) {
ifstates.top() = AlwaysFalse;
iftokens.top()->nextcond = rawtok;
iftokens.top() = rawtok;
} else if (ifstates.top() == ElseIsTrue && conditionIsTrue) {
ifstates.top() = True;
iftokens.top()->nextcond = rawtok;
iftokens.top() = rawtok;
}
} else if (rawtok->str() == ELSE) {
ifstates.top() = (ifstates.top() == ElseIsTrue) ? True : AlwaysFalse;
iftokens.top()->nextcond = rawtok;
iftokens.top() = rawtok;
} else if (rawtok->str() == ENDIF) {
ifstates.pop();
iftokens.top()->nextcond = rawtok;
iftokens.pop();
} else if (rawtok->str() == UNDEF) {
if (ifstates.top() == True) {
const Token *tok = rawtok->next;
Expand All @@ -3875,7 +3885,10 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
} else if (ifstates.top() == True && rawtok->str() == PRAGMA && rawtok->next && rawtok->next->str() == ONCE && sameline(rawtok,rawtok->next)) {
pragmaOnce.insert(rawtok->location.file());
}
rawtok = gotoNextLine(rawtok);
if (ifstates.top() != True && rawtok->nextcond)
rawtok = rawtok->nextcond->previous;
else
rawtok = gotoNextLine(rawtok);
continue;
}

Expand Down
5 changes: 3 additions & 2 deletions simplecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ namespace simplecpp {
class SIMPLECPP_LIB Token {
public:
Token(const TokenString &s, const Location &loc, bool wsahead = false) :
whitespaceahead(wsahead), location(loc), previous(nullptr), next(nullptr), string(s) {
whitespaceahead(wsahead), location(loc), previous(nullptr), next(nullptr), nextcond(nullptr), string(s) {
flags();
}

Token(const Token &tok) :
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) {
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) {
}

void flags() {
Expand Down Expand Up @@ -137,6 +137,7 @@ namespace simplecpp {
Location location;
Token *previous;
Token *next;
mutable const Token *nextcond;

const Token *previousSkipComments() const {
const Token *tok = this->previous;
Expand Down
Loading