@@ -733,6 +733,41 @@ static std::string stringifyToken(const std::string &str)
733733 return ret;
734734}
735735
736+ /* *
737+ * The result of the # operator: the tokens as a string literal (C 6.10.3.2).
738+ * The tokens are separated by a space where there was whitespace between them.
739+ * @param tokens the argument of #, not macro expanded
740+ * @param loc location for the warning
741+ * @param macroName the macro that is expanded, for the warning
742+ * @param outputList if not null, a warning is added when a final backslash is dropped
743+ * @return the string literal, including the quotes
744+ */
745+ static std::string stringify (const simplecpp::TokenList &tokens, const simplecpp::Location &loc, const std::string ¯oName, simplecpp::OutputList *outputList)
746+ {
747+ std::string str;
748+ for (const simplecpp::Token *tok = tokens.cfront (), *next; tok; tok = next) {
749+ next = tok->next ;
750+ str += stringifyToken (tok->str ());
751+ if (next && tok->whitespaceahead )
752+ str += ' ' ;
753+ }
754+ // An odd number of trailing backslashes would escape the closing quote and
755+ // yield an unterminated string literal (undefined behavior, C 6.10.3.2p2).
756+ // Drop the final backslash, as gcc and clang do.
757+ std::size_t trailing = 0 ;
758+ while (trailing < str.size () && str[str.size () - 1U - trailing] == ' \\ ' )
759+ ++trailing;
760+ if (trailing % 2U == 1U ) {
761+ str.pop_back ();
762+ if (outputList) {
763+ outputList->emplace_back (simplecpp::Output::PORTABILITY_STRINGIFY , loc,
764+ " Invalid string literal when expanding \' " + macroName + " \' , ignoring final \'\\\' . "
765+ " Undefined behavior according to C standard chapter 6.10.3.2, paragraph 2." );
766+ }
767+ }
768+ return ' \" ' + str + ' \" ' ;
769+ }
770+
736771void simplecpp::TokenList::lineDirective (unsigned int fileIndex_, unsigned int line, Location &location)
737772{
738773 if (fileIndex_ != location.fileIndex || line >= location.line ) {
@@ -2634,28 +2669,7 @@ namespace simplecpp {
26342669 TokenList tokenListHash (files);
26352670 const MacroMap macros2; // temporarily bypass macro expansion
26362671 tok = expandToken (tokenListHash, loc, tok->next , macros2, expandedmacros, parametertokens, outputList);
2637- std::string str;
2638- for (const Token *hashtok = tokenListHash.cfront (), *next; hashtok; hashtok = next) {
2639- next = hashtok->next ;
2640- str += stringifyToken (hashtok->str ());
2641- if (next && hashtok->whitespaceahead )
2642- str += ' ' ;
2643- }
2644- // An odd number of trailing backslashes would escape the closing quote and
2645- // yield an unterminated string literal (undefined behavior, C 6.10.3.2p2).
2646- // Drop the final backslash, as gcc and clang do.
2647- std::size_t trailing = 0 ;
2648- while (trailing < str.size () && str[str.size () - 1U - trailing] == ' \\ ' )
2649- ++trailing;
2650- if (trailing % 2U == 1U ) {
2651- str.pop_back ();
2652- if (outputList) {
2653- outputList->emplace_back (Output::PORTABILITY_STRINGIFY , loc,
2654- " Invalid string literal when expanding \' " + name () + " \' , ignoring final \'\\\' . "
2655- " Undefined behavior according to C standard chapter 6.10.3.2, paragraph 2." );
2656- }
2657- }
2658- output.push_back (newMacroToken (' \" ' + str + ' \" ' , loc, isReplaced (expandedmacros)));
2672+ output.push_back (newMacroToken (stringify (tokenListHash, loc, name (), outputList), loc, isReplaced (expandedmacros)));
26592673 return tok;
26602674 }
26612675
0 commit comments