@@ -206,11 +206,23 @@ static bool isAlternativeBinaryOp(const simplecpp::Token *tok, const std::string
206206 (isValue (tok->next ) || tok->next ->name || tok->next ->op == ' (' ));
207207}
208208
209+ static bool isAlternativeOpName (const simplecpp::Token *tok)
210+ {
211+ static const std::set<std::string> altop{" and" ," or" ," bitand" ," bitor" ," compl" ," not" ," not_eq" ," xor" };
212+ return tok->name && altop.find (tok->str ()) != altop.end ();
213+ }
214+
209215static bool isAlternativeUnaryOp (const simplecpp::Token *tok, const std::string &alt)
210216{
211- return ((tok->name && tok->str () == alt) &&
212- (!tok->previous || tok->previous ->op == ' (' ) &&
213- (tok->next && (tok->next ->name || tok->next ->number )));
217+ if (!tok->name || tok->str () != alt)
218+ return false ;
219+ // a unary operator comes first or after another operator, but not after an operand
220+ const simplecpp::Token *prev = tok->previous ;
221+ if (prev && (isValue (prev) || prev->op == ' )' || (prev->name && !isAlternativeOpName (prev))))
222+ return false ;
223+ // it is followed by the start of an operand
224+ const simplecpp::Token *next = tok->next ;
225+ return next && (next->name || next->number || next->isOneOf (" (!~+-" ));
214226}
215227
216228static std::string replaceAll (std::string s, const std::string& from, const std::string& to)
@@ -1318,7 +1330,7 @@ void simplecpp::TokenList::constFoldUnaryNotPosNeg(simplecpp::Token *tok1)
13181330 tok->setstr (toString (~stringToLL (tok->next->str ())));
13191331 deleteToken (tok->next);
13201332 } else {
1321- if (tok->previous && (isValue (tok->previous ) || tok->previous ->name ))
1333+ if (tok->previous && (isValue (tok->previous ) || ( tok->previous ->name && ! isAlternativeOpName (tok-> previous )) ))
13221334 continue ;
13231335 if (!isValue (tok->next ))
13241336 continue ;
@@ -2992,8 +3004,7 @@ static void simplifyName(simplecpp::TokenList &expr, const simplecpp::DUI &dui)
29923004 tok->setstr (tok->str () == " true" ? " 1" : " 0" );
29933005 continue ;
29943006 }
2995- static const std::set<std::string> altop{" and" ," or" ," bitand" ," bitor" ," compl" ," not" ," not_eq" ," xor" };
2996- if (altop.find (tok->str ()) != altop.end ()) {
3007+ if (isAlternativeOpName (tok)) {
29973008 bool alt;
29983009 if (tok->str () == " not" || tok->str () == " compl" ) {
29993010 alt = isAlternativeUnaryOp (tok,tok->str ());
0 commit comments