2323#include " simplecpp.h"
2424
2525#include < algorithm>
26+ #include < cassert>
2627#include < climits>
2728#include < cstddef>
2829#include < cstdlib>
4344#ifdef SIMPLECPP_WINDOWS
4445#include < windows.h>
4546#undef ERROR
46- #undef TRUE
4747#endif
4848
4949#if (__cplusplus < 201103L) && !defined(__APPLE__)
@@ -351,6 +351,7 @@ class StdIStream : public simplecpp::TokenList::Stream {
351351 StdIStream (std::istream &istr)
352352 : istr(istr)
353353 {
354+ assert (istr.good ());
354355 init ();
355356 }
356357
@@ -378,6 +379,7 @@ class FileStream : public simplecpp::TokenList::Stream {
378379 , lastCh(0 )
379380 , lastStatus(0 )
380381 {
382+ assert (file != nullptr );
381383 init ();
382384 }
383385
@@ -3209,12 +3211,12 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
32093211 }
32103212 }
32113213
3212- // TRUE => code in current #if block should be kept
3213- // ELSE_IS_TRUE => code in current #if block should be dropped. the code in the #else should be kept.
3214- // ALWAYS_FALSE => drop all code in #if and #else
3215- enum IfState { TRUE , ELSE_IS_TRUE, ALWAYS_FALSE };
3214+ // True => code in current #if block should be kept
3215+ // ElseIsTrue => code in current #if block should be dropped. the code in the #else should be kept.
3216+ // AlwaysFalse => drop all code in #if and #else
3217+ enum IfState { True, ElseIsTrue, AlwaysFalse };
32163218 std::stack<int > ifstates;
3217- ifstates.push (TRUE );
3219+ ifstates.push (True );
32183220
32193221 std::stack<const Token *> includetokenstack;
32203222
@@ -3259,7 +3261,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
32593261 return ;
32603262 }
32613263
3262- if (ifstates.top () == TRUE && (rawtok->str () == ERROR || rawtok->str () == WARNING)) {
3264+ if (ifstates.top () == True && (rawtok->str () == ERROR || rawtok->str () == WARNING)) {
32633265 if (outputList) {
32643266 simplecpp::Output err (rawtok->location .files );
32653267 err.type = rawtok->str () == ERROR ? Output::ERROR : Output::WARNING;
@@ -3279,7 +3281,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
32793281 }
32803282
32813283 if (rawtok->str () == DEFINE) {
3282- if (ifstates.top () != TRUE )
3284+ if (ifstates.top () != True )
32833285 continue ;
32843286 try {
32853287 const Macro ¯o = Macro (rawtok->previous , files);
@@ -3301,7 +3303,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
33013303 output.clear ();
33023304 return ;
33033305 }
3304- } else if (ifstates.top () == TRUE && rawtok->str () == INCLUDE) {
3306+ } else if (ifstates.top () == True && rawtok->str () == INCLUDE) {
33053307 TokenList inc1 (files);
33063308 for (const Token *inctok = rawtok->next ; sameline (rawtok,inctok); inctok = inctok->next ) {
33073309 if (!inctok->comment )
@@ -3392,7 +3394,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
33923394 }
33933395
33943396 bool conditionIsTrue;
3395- if (ifstates.top () == ALWAYS_FALSE || (ifstates.top () == ELSE_IS_TRUE && rawtok->str () != ELIF))
3397+ if (ifstates.top () == AlwaysFalse || (ifstates.top () == ElseIsTrue && rawtok->str () != ELIF))
33963398 conditionIsTrue = false ;
33973399 else if (rawtok->str () == IFDEF) {
33983400 conditionIsTrue = (macros.find (rawtok->next ->str ()) != macros.end () || (hasInclude && rawtok->next ->str () == HAS_INCLUDE));
@@ -3520,35 +3522,35 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
35203522
35213523 if (rawtok->str () != ELIF) {
35223524 // push a new ifstate..
3523- if (ifstates.top () != TRUE )
3524- ifstates.push (ALWAYS_FALSE );
3525+ if (ifstates.top () != True )
3526+ ifstates.push (AlwaysFalse );
35253527 else
3526- ifstates.push (conditionIsTrue ? TRUE : ELSE_IS_TRUE );
3527- } else if (ifstates.top () == TRUE ) {
3528- ifstates.top () = ALWAYS_FALSE ;
3529- } else if (ifstates.top () == ELSE_IS_TRUE && conditionIsTrue) {
3530- ifstates.top () = TRUE ;
3528+ ifstates.push (conditionIsTrue ? True : ElseIsTrue );
3529+ } else if (ifstates.top () == True ) {
3530+ ifstates.top () = AlwaysFalse ;
3531+ } else if (ifstates.top () == ElseIsTrue && conditionIsTrue) {
3532+ ifstates.top () = True ;
35313533 }
35323534 } else if (rawtok->str () == ELSE) {
3533- ifstates.top () = (ifstates.top () == ELSE_IS_TRUE ) ? TRUE : ALWAYS_FALSE ;
3535+ ifstates.top () = (ifstates.top () == ElseIsTrue ) ? True : AlwaysFalse ;
35343536 } else if (rawtok->str () == ENDIF) {
35353537 ifstates.pop ();
35363538 } else if (rawtok->str () == UNDEF) {
3537- if (ifstates.top () == TRUE ) {
3539+ if (ifstates.top () == True ) {
35383540 const Token *tok = rawtok->next ;
35393541 while (sameline (rawtok,tok) && tok->comment )
35403542 tok = tok->next ;
35413543 if (sameline (rawtok, tok))
35423544 macros.erase (tok->str ());
35433545 }
3544- } else if (ifstates.top () == TRUE && rawtok->str () == PRAGMA && rawtok->next && rawtok->next ->str () == ONCE && sameline (rawtok,rawtok->next )) {
3546+ } else if (ifstates.top () == True && rawtok->str () == PRAGMA && rawtok->next && rawtok->next ->str () == ONCE && sameline (rawtok,rawtok->next )) {
35453547 pragmaOnce.insert (rawtok->location .file ());
35463548 }
35473549 rawtok = gotoNextLine (rawtok);
35483550 continue ;
35493551 }
35503552
3551- if (ifstates.top () != TRUE ) {
3553+ if (ifstates.top () != True ) {
35523554 // drop code
35533555 rawtok = gotoNextLine (rawtok);
35543556 continue ;
0 commit comments