@@ -161,7 +161,7 @@ void simplecpp::TokenList::push_back(Token *tok) {
161161}
162162
163163void simplecpp::TokenList::dump () const {
164- std::cout << stringify ();
164+ std::cout << stringify () << std::endl ;
165165}
166166
167167std::string simplecpp::TokenList::stringify () const {
@@ -579,19 +579,26 @@ void simplecpp::TokenList::constFoldBitwise(Token *tok)
579579{
580580 Token * const tok1 = tok;
581581 for (const char *op = " &^|" ; *op; op++) {
582+ std::string altop;
583+ if (*op == ' &' )
584+ altop = " bitand" ;
585+ else if (*op == ' |' )
586+ altop = " bitor" ;
587+ else
588+ altop = " xor" ;
582589 for (tok = tok1; tok && tok->op != ' )' ; tok = tok->next ) {
583- if (tok->op != *op)
590+ if (tok->op != *op && tok-> str != altop )
584591 continue ;
585592 if (!tok->previous || !tok->previous ->number )
586593 continue ;
587594 if (!tok->next || !tok->next ->number )
588595 continue ;
589596 long long result;
590- if (tok-> op == ' &' )
597+ if (* op == ' &' )
591598 result = (stringToLL (tok->previous ->str ) & stringToLL (tok->next ->str ));
592- else if (tok-> op == ' ^' )
599+ else if (* op == ' ^' )
593600 result = (stringToLL (tok->previous ->str ) ^ stringToLL (tok->next ->str ));
594- else /* if (tok-> op == '|')*/
601+ else /* if (* op == '|')*/
595602 result = (stringToLL (tok->previous ->str ) | stringToLL (tok->next ->str ));
596603 tok = tok->previous ;
597604 tok->setstr (toString (result));
@@ -603,15 +610,15 @@ void simplecpp::TokenList::constFoldBitwise(Token *tok)
603610
604611void simplecpp::TokenList::constFoldLogicalOp (Token *tok) {
605612 for (; tok && tok->op != ' )' ; tok = tok->next ) {
606- if (tok->str != " &&" && tok->str != " ||" )
613+ if (tok->str != " &&" && tok->str != " ||" && tok-> str != " and " && tok-> str != " or " )
607614 continue ;
608615 if (!tok->previous || !tok->previous ->number )
609616 continue ;
610617 if (!tok->next || !tok->next ->number )
611618 continue ;
612619
613620 int result;
614- if (tok->str == " ||" )
621+ if (tok->str == " ||" || tok-> str == " or " )
615622 result = (stringToLL (tok->previous ->str ) || stringToLL (tok->next ->str ));
616623 else /* if (tok->str == "&&")*/
617624 result = (stringToLL (tok->previous ->str ) && stringToLL (tok->next ->str ));
@@ -983,6 +990,32 @@ class Macro {
983990 return parametertokens;
984991 }
985992
993+ const Token *appendTokens (TokenList *tokens,
994+ const Token *lpar,
995+ const std::map<TokenString,Macro> ¯os,
996+ const std::set<TokenString> &expandedmacros1,
997+ const std::set<TokenString> &expandedmacros,
998+ const std::vector<const Token*> ¶metertokens) const {
999+ if (!lpar || lpar->op != ' (' )
1000+ return NULL ;
1001+ unsigned int par = 0 ;
1002+ const Token *tok = lpar;
1003+ while (sameline (lpar, tok)) {
1004+ if (!expandArg (tokens, tok, tok->location , macros, expandedmacros1, expandedmacros, parametertokens))
1005+ tokens->push_back (new Token (*tok));
1006+ if (tok->op == ' (' )
1007+ ++par;
1008+ else if (tok->op == ' )' ) {
1009+ --par;
1010+ if (par == 0U )
1011+ break ;
1012+ }
1013+ tok = tok->next ;
1014+ }
1015+ return sameline (lpar,tok) ? tok : NULL ;
1016+ }
1017+
1018+
9861019 const Token *expandToken (TokenList *output, const Location &loc, const Token *tok, const std::map<TokenString,Macro> ¯os, std::set<TokenString> expandedmacros1, std::set<TokenString> expandedmacros, const std::vector<const Token*> ¶metertokens) const {
9871020 // Not name..
9881021 if (!tok->name ) {
@@ -991,8 +1024,40 @@ class Macro {
9911024 }
9921025
9931026 // Macro parameter..
994- if (expandArg (output, tok, loc, macros, expandedmacros1, expandedmacros, parametertokens))
995- return tok->next ;
1027+ {
1028+ TokenList temp (files);
1029+ if (expandArg (&temp, tok, loc, macros, expandedmacros1, expandedmacros, parametertokens)) {
1030+ if (!(temp.cend () && temp.cend ()->name && tok->next && tok->next ->op == ' (' )) {
1031+ output->takeTokens (temp);
1032+ return tok->next ;
1033+ }
1034+
1035+ const std::map<TokenString, Macro>::const_iterator it = macros.find (temp.cend ()->str );
1036+ if (it == macros.end () || expandedmacros1.find (temp.cend ()->str ) != expandedmacros1.end ()) {
1037+ output->takeTokens (temp);
1038+ return tok->next ;
1039+ }
1040+
1041+ const Macro &calledMacro = it->second ;
1042+ if (!calledMacro.functionLike ()) {
1043+ output->takeTokens (temp);
1044+ return tok->next ;
1045+ }
1046+
1047+ TokenList temp2 (files);
1048+ temp2.push_back (new Token (temp.cend ()->str , tok->location ));
1049+
1050+ const Token *tok2 = appendTokens (&temp2, tok->next , macros, expandedmacros1, expandedmacros, parametertokens);
1051+ if (!tok2)
1052+ return tok->next ;
1053+
1054+ output->takeTokens (temp);
1055+ output->deleteToken (output->end ());
1056+ calledMacro.expand (output, loc, temp2.cbegin (), macros, expandedmacros);
1057+
1058+ return tok2->next ;
1059+ }
1060+ }
9961061
9971062 // Macro..
9981063 const std::map<TokenString, Macro>::const_iterator it = macros.find (tok->str );
@@ -1006,19 +1071,10 @@ class Macro {
10061071 }
10071072 TokenList tokens (files);
10081073 tokens.push_back (new Token (*tok));
1009- unsigned int par = 0 ;
1010- const Token *tok2 = tok->next ;
1011- while (sameline (tok,tok2)) {
1012- if (!expandArg (&tokens, tok2, tok2->location , macros, expandedmacros1, expandedmacros, parametertokens))
1013- tokens.push_back (new Token (*tok2));
1014- if (tok2->op == ' (' )
1015- ++par;
1016- else if (tok2->op == ' )' ) {
1017- --par;
1018- if (par == 0U )
1019- break ;
1020- }
1021- tok2 = tok2->next ;
1074+ const Token *tok2 = appendTokens (&tokens, tok->next , macros, expandedmacros1, expandedmacros, parametertokens);
1075+ if (!tok2) {
1076+ // FIXME: handle this
1077+ throw wrongNumberOfParameters (tok->location , tok->str );
10221078 }
10231079 calledMacro.expand (output, loc, tokens.cbegin (), macros, expandedmacros);
10241080 return tok2->next ;
@@ -1135,9 +1191,25 @@ void simplifySizeof(simplecpp::TokenList &expr, const std::map<std::string, std:
11351191}
11361192
11371193void simplifyName (simplecpp::TokenList &expr) {
1194+ std::set<std::string> altop;
1195+ altop.insert (" and" );
1196+ altop.insert (" or" );
1197+ altop.insert (" bitand" );
1198+ altop.insert (" bitor" );
1199+ altop.insert (" xor" );
11381200 for (simplecpp::Token *tok = expr.begin (); tok; tok = tok->next ) {
1139- if (tok->name )
1201+ if (tok->name ) {
1202+ if (altop.find (tok->str ) != altop.end ()) {
1203+ bool alt = true ;
1204+ if (!tok->previous || !tok->next )
1205+ alt = false ;
1206+ if (!(tok->previous ->number || tok->previous ->op == ' )' ))
1207+ alt = false ;
1208+ if (alt)
1209+ continue ;
1210+ }
11401211 tok->setstr (" 0" );
1212+ }
11411213 }
11421214}
11431215
0 commit comments