@@ -1918,6 +1918,34 @@ namespace simplecpp {
19181918 return valueDefinedInCode_;
19191919 }
19201920
1921+ /* *
1922+ * Copy a macro call "NAME( ... )" to a tokenlist with no line breaks:
1923+ * all tokens get the location of the name.
1924+ * @param dest destination tokenlist
1925+ * @param nameTok the macro name, followed by '('
1926+ * @return the token after the ')', or nullptr if the call is not terminated
1927+ * @throws invalidDirectiveAsMacroParameter if there is a preprocessor directive in the arguments
1928+ */
1929+ static const Token *copyMacroCall (TokenList &dest, const Token *nameTok) {
1930+ const Location &loc = nameTok->location ;
1931+ dest.push_back (new Token (nameTok->str (), loc, nameTok->whitespaceahead ));
1932+ const Token *tok = nameTok->next ;
1933+ dest.push_back (new Token (tok->str (), loc, tok->whitespaceahead ));
1934+ tok = tok->next ;
1935+ int par = 1 ;
1936+ while (tok && par > 0 ) {
1937+ if (tok->op == ' (' )
1938+ ++par;
1939+ else if (tok->op == ' )' )
1940+ --par;
1941+ else if (tok->op == ' #' && !sameline (tok->previous , tok))
1942+ throw invalidDirectiveAsMacroParameter (tok->location );
1943+ dest.push_back (new Token (tok->str (), loc, tok->whitespaceahead ));
1944+ tok = tok->next ;
1945+ }
1946+ return tok;
1947+ }
1948+
19211949 /* *
19221950 * Expand macro. This will recursively expand inner macros.
19231951 * @param output destination tokenlist
@@ -1944,24 +1972,9 @@ namespace simplecpp {
19441972 TokenList output2 (inputFiles);
19451973
19461974 if (functionLike () && rawtok->next && rawtok->next ->op == ' (' ) {
1947- // Copy macro call to a new tokenlist with no linebreaks
19481975 const Token * const rawtok1 = rawtok;
19491976 TokenList rawtokens2 (inputFiles);
1950- rawtokens2.push_back (new Token (rawtok->str (), rawtok1->location , rawtok->whitespaceahead ));
1951- rawtok = rawtok->next ;
1952- rawtokens2.push_back (new Token (rawtok->str (), rawtok1->location , rawtok->whitespaceahead ));
1953- rawtok = rawtok->next ;
1954- int par = 1 ;
1955- while (rawtok && par > 0 ) {
1956- if (rawtok->op == ' (' )
1957- ++par;
1958- else if (rawtok->op == ' )' )
1959- --par;
1960- else if (rawtok->op == ' #' && !sameline (rawtok->previous , rawtok))
1961- throw invalidDirectiveAsMacroParameter (rawtok->location );
1962- rawtokens2.push_back (new Token (rawtok->str (), rawtok1->location , rawtok->whitespaceahead ));
1963- rawtok = rawtok->next ;
1964- }
1977+ rawtok = copyMacroCall (rawtokens2, rawtok);
19651978 if (expand (output2, rawtok1->location , rawtokens2.cfront (), macros, expandedmacros, outputList))
19661979 rawtok = rawtok1->next ;
19671980 } else {
@@ -2293,6 +2306,29 @@ namespace simplecpp {
22932306 return sameline (lpar,tok) ? tok : nullptr ;
22942307 }
22952308
2309+ /* *
2310+ * Expand the built-in macros __FILE__, __LINE__ and __COUNTER__.
2311+ * The usage must already be added to usageList, __COUNTER__ is the number of earlier usages.
2312+ * @param output destination tokenlist
2313+ * @param loc location of the usage
2314+ * @param nameTokInst the macro name
2315+ * @return true if the macro is built-in and its value is added to output
2316+ */
2317+ bool expandBuiltin (TokenList &output, const Location &loc, const Token *nameTokInst) const {
2318+ std::string value;
2319+ if (nameTokInst->str () == " __FILE__" )
2320+ value = ' \" ' + output.file (loc) + ' \" ' ;
2321+ else if (nameTokInst->str () == " __LINE__" )
2322+ value = toString (loc.line );
2323+ else if (nameTokInst->str () == " __COUNTER__" )
2324+ value = toString (usageList.size () - 1U );
2325+ else
2326+ return false ;
2327+ output.push_back (new Token (value, loc));
2328+ output.back ()->macro = nameTokInst->str ();
2329+ return true ;
2330+ }
2331+
22962332 const Token * expand (TokenList & output, const Location &loc, const Token * const nameTokInst, const MacroMap ¯os, std::set<TokenString> expandedmacros, OutputList *outputList) const {
22972333 expandedmacros.insert (nameTokInst->str ());
22982334
@@ -2302,21 +2338,8 @@ namespace simplecpp {
23022338
23032339 usageList.emplace_back (loc);
23042340
2305- if (nameTokInst->str () == " __FILE__" ) {
2306- output.push_back (new Token (' \" ' +output.file (loc)+' \" ' , loc));
2307- output.back ()->macro = " __FILE__" ;
2308- return nameTokInst->next ;
2309- }
2310- if (nameTokInst->str () == " __LINE__" ) {
2311- output.push_back (new Token (toString (loc.line ), loc));
2312- output.back ()->macro = " __LINE__" ;
2341+ if (expandBuiltin (output, loc, nameTokInst))
23132342 return nameTokInst->next ;
2314- }
2315- if (nameTokInst->str () == " __COUNTER__" ) {
2316- output.push_back (new Token (toString (usageList.size ()-1U ), loc));
2317- output.back ()->macro = " __COUNTER__" ;
2318- return nameTokInst->next ;
2319- }
23202343
23212344 const bool calledInDefine = (loc.fileIndex != nameTokInst->location .fileIndex ||
23222345 loc.line < nameTokInst->location .line );
@@ -2567,42 +2590,95 @@ namespace simplecpp {
25672590 return recursiveExpandToken (output, temp, loc, tok2, macros, expandedmacros, parametertokens, outputList);
25682591 }
25692592
2570- if (tok->str () == DEFINED ) {
2571- const Token * const tok2 = tok->next ;
2572- const Token * const tok3 = tok2 ? tok2->next : nullptr ;
2573- const Token * const tok4 = tok3 ? tok3->next : nullptr ;
2574- const Token *defToken = nullptr ;
2575- const Token *lastToken = nullptr ;
2576- if (sameline (tok, tok4) && tok2->op == ' (' && tok3->name && tok4->op == ' )' ) {
2577- defToken = tok3;
2578- lastToken = tok4;
2579- } else if (sameline (tok,tok2) && tok2->name ) {
2580- defToken = lastToken = tok2;
2581- }
2582- if (defToken) {
2583- std::string macroName = defToken->str ();
2584- if (defToken->next && defToken->next ->op == ' #' && defToken->next ->next && defToken->next ->next ->op == ' #' && defToken->next ->next ->next && defToken->next ->next ->next ->name && sameline (defToken,defToken->next ->next ->next )) {
2585- TokenList temp (files);
2586- if (expandArg (temp, defToken, parametertokens))
2587- macroName = temp.cback ()->str ();
2588- if (expandArg (temp, defToken->next ->next ->next , parametertokens))
2589- macroName += temp.cback () ? temp.cback ()->str () : " " ;
2590- else
2591- macroName += defToken->next ->next ->next ->str ();
2592- lastToken = defToken->next ->next ->next ;
2593- }
2594- const bool def = (macros.find (macroName) != macros.end ());
2595- output.push_back (newMacroToken (def ? " 1" : " 0" , loc, true ));
2596- return lastToken->next ;
2597- }
2598- }
2593+ const Token *next = nullptr ;
2594+ if (tok->str () == DEFINED && expandDefined (output, loc, tok, macros, parametertokens, next))
2595+ return next;
25992596
26002597 output.push_back (newMacroToken (tok->str (), loc, true , tok));
26012598 if (it != macros.end ())
26022599 output.back ()->markExpandedFrom (&it->second );
26032600 return tok->next ;
26042601 }
26052602
2603+ /* *
2604+ * Find the operand of "defined" in a replacement list: "defined X", "defined(X)" or "defined X ## Y".
2605+ * @param tok the "defined" token
2606+ * @param first output: X
2607+ * @param second output: Y for "X ## Y", otherwise nullptr
2608+ * @param last output: the last token of the operand
2609+ * @return false if there is no operand
2610+ */
2611+ static bool findDefinedOperand (const Token *tok, const Token *&first, const Token *&second, const Token *&last) {
2612+ const Token * const tok2 = tok->next ;
2613+ const Token * const tok3 = tok2 ? tok2->next : nullptr ;
2614+ const Token * const tok4 = tok3 ? tok3->next : nullptr ;
2615+ first = last = second = nullptr ;
2616+ if (sameline (tok, tok4) && tok2->op == ' (' && tok3->name && tok4->op == ' )' ) {
2617+ first = tok3;
2618+ last = tok4;
2619+ } else if (sameline (tok,tok2) && tok2->name ) {
2620+ first = last = tok2;
2621+ }
2622+ if (!first)
2623+ return false ;
2624+ if (first->next && first->next ->op == ' #' && first->next ->next && first->next ->next ->op == ' #' && first->next ->next ->next && first->next ->next ->next ->name && sameline (first,first->next ->next ->next )) {
2625+ second = first->next ->next ->next ;
2626+ last = second;
2627+ }
2628+ return true ;
2629+ }
2630+
2631+ /* *
2632+ * The name of the macro that "defined" checks.
2633+ * @param first X in "defined X"
2634+ * @param second Y in "defined X ## Y", or nullptr
2635+ * @param argText for "X ## Y": gives the text of a parameter, and returns false if the token is not a parameter
2636+ */
2637+ template <class ArgText >
2638+ static std::string definedMacroName (const Token *first, const Token *second, ArgText argText) {
2639+ std::string macroName = first->str ();
2640+ if (second) {
2641+ std::string text;
2642+ if (argText (first, text))
2643+ macroName = text;
2644+ if (argText (second, text))
2645+ macroName += text;
2646+ else
2647+ macroName += second->str ();
2648+ }
2649+ return macroName;
2650+ }
2651+
2652+ /* *
2653+ * Expand "defined X" or "defined(X)" in the replacement list to 1 or 0.
2654+ * The operand can be a parameter, or two names pasted with ##.
2655+ * @param output destination tokenlist
2656+ * @param loc location for the result
2657+ * @param tok the "defined" token
2658+ * @param macros all macros
2659+ * @param parametertokens arguments of this macro call
2660+ * @param next output: the token after the operand
2661+ * @return false if there is no operand, then nothing is added to output
2662+ */
2663+ bool expandDefined (TokenList &output, const Location &loc, const Token *tok, const MacroMap ¯os, const std::vector<const Token*> ¶metertokens, const Token *&next) const {
2664+ const Token *first;
2665+ const Token *second;
2666+ const Token *last;
2667+ if (!findDefinedOperand (tok, first, second, last))
2668+ return false ;
2669+ TokenList temp (files);
2670+ const std::string macroName = definedMacroName (first, second, [&](const Token *argtok, std::string &text) {
2671+ if (!expandArg (temp, argtok, parametertokens))
2672+ return false ;
2673+ text = temp.cback () ? temp.cback ()->str () : " " ;
2674+ return true ;
2675+ });
2676+ const bool def = (macros.find (macroName) != macros.end ());
2677+ output.push_back (newMacroToken (def ? " 1" : " 0" , loc, true ));
2678+ next = last->next ;
2679+ return true ;
2680+ }
2681+
26062682 bool expandArg (TokenList &output, const Token *tok, const std::vector<const Token*> ¶metertokens) const {
26072683 if (!tok->name )
26082684 return false ;
0 commit comments