@@ -3127,7 +3127,12 @@ namespace simplecpp {
31273127 continue ;
31283128 }
31293129
3130- const MacroCall call = readMacroCall (macro, tok, input);
3130+ MacroCall call;
3131+ if (!readMacroCall (call, macro, tok, input)) {
3132+ // unterminated macro call: the name is not expanded
3133+ output.push_back (tok);
3134+ continue ;
3135+ }
31313136 TokenList result (files);
31323137 substitute (result, macro, call);
31333138 input.pushFront (result);
@@ -3136,21 +3141,28 @@ namespace simplecpp {
31363141
31373142 /* *
31383143 * Read a macro call: the name, and "( ... )" for a function-like macro.
3144+ * @param call the macro call
31393145 * @param macro the macro
3140- * @param nameTok the macro name, it is deleted
3146+ * @param nameTok the macro name, it is deleted unless the call is unterminated
31413147 * @param input the input after the name
3148+ * @return false if the call is not terminated; the tokens after the name are put back in input
31423149 * @throws wrongNumberOfParameters if a function-like macro has the wrong number of arguments
31433150 */
3144- MacroCall readMacroCall (const Macro ¯o, Token *nameTok, Input &input) {
3145- MacroCall call;
3151+ bool readMacroCall (MacroCall &call, const Macro ¯o, Token *nameTok, Input &input) {
31463152 if (!macro.functionLike ()) {
31473153 call.hideSet = nameTok->hideSet ();
31483154 call.whitespaceAfter = nameTok->whitespaceahead ;
31493155 delete nameTok;
31503156 } else {
31513157 TokenList tokens (files);
3152- tokens.push_back (nameTok);
3153- readCall (tokens, input);
3158+ tokens.push_back (new Token (*nameTok));
3159+ if (!readCall (tokens, input)) {
3160+ // give the tokens after the name back
3161+ tokens.deleteToken (tokens.front ());
3162+ input.pushFront (tokens);
3163+ return false ;
3164+ }
3165+ delete nameTok;
31543166 const std::vector<const Token *> delimiters = macro.getMacroParameters (tokens.cfront (), false );
31553167 // the name and the ) have the hide sets of the macro calls that this call
31563168 // was produced by, the arguments are not affected by them
@@ -3162,19 +3174,20 @@ namespace simplecpp {
31623174 call.whitespaceAfter = delimiters.back ()->whitespaceahead ;
31633175 }
31643176 call.hideSet .insert (macro.name ());
3165- return call ;
3177+ return true ;
31663178 }
31673179
3168- /* * Read "( ... )" after the macro name in call */
3169- static void readCall (TokenList &call, Input &input) {
3180+ /* * Read "( ... )" after the macro name in call; false if there is no ) */
3181+ static bool readCall (TokenList &call, Input &input) {
31703182 int par = 0 ;
31713183 while (Token * const tok = input.read ()) {
31723184 call.push_back (tok);
31733185 if (tok->op == ' (' )
31743186 ++par;
31753187 else if (tok->op == ' )' && --par == 0 )
3176- return ;
3188+ return true ;
31773189 }
3190+ return false ;
31783191 }
31793192
31803193 /* * Each argument of the call, as written in the call */
0 commit comments