Menu

#587 AStyle 3.6.7: wrong indentation of multi-line string concat

open-accepted
None
2025-03-11
2025-02-28
Zenju
No

AStyle 3.6.7 produces: (full repro when inside .cpp file)

std::string test()
{
    return "--" + boundaryString +                         "\r\n"
           "Content-Type: application/json; charset=UTF-8" "\r\n"
           /**/                                            "\r\n" +
           metaDataBuf +                                   "\r\n"
                                                    "--" + boundaryString +                         "\r\n"
                                                    "Content-Type: application/octet-stream"        "\r\n"
           /**/                                            "\r\n";
}

The expected indentation before "return" should be:

std::string test()
{
    return "--" + boundaryString +                         "\r\n"
           "Content-Type: application/json; charset=UTF-8" "\r\n"
           /**/                                            "\r\n" +
           metaDataBuf +                                   "\r\n"
           "--" + boundaryString +                         "\r\n"
           "Content-Type: application/octet-stream"        "\r\n"
           /**/                                            "\r\n";
}

using

--style=allman
--suffix=none
--keep-one-line-blocks
--align-pointer=type
--min-conditional-indent=0
--max-continuation-indent=80
--indent-switches
--indent-col1-comments
--indent-preproc-block
--indent-preproc-define
--pad-header
--pad-comma
--formatted
--lineend=linux
--indent-preproc-block
--convert-tabs
--preserve-ws

Discussion

  • André Simon

    André Simon - 2025-02-28
    • status: open --> open-accepted
    • assigned_to: André Simon
     
  • André Simon

    André Simon - 2025-03-10

    Hi, until a fix is available you can avoid this by adding string concat operator (+) like this:

    std::string test()
    {
        return "--" + boundaryString +                         "\r\n" +
               "Content-Type: application/json; charset=UTF-8" "\r\n" +
               /**/                                            "\r\n" +
               metaDataBuf +                                   "\r\n" +
               "--" + boundaryString +                         "\r\n"
               "Content-Type: application/octet-stream"        "\r\n"
               /**/                                            "\r\n";
    }
    
     
    • w-peuker

      w-peuker - 2025-03-11

      I don't think it's a good idea to change the actual code just to avoid incorrect formatting of the sources. Some compilers will optimize away the string concatenations, others will not, resulting in significant runtime degradation. A better way, also obvious for future reader, is to prevent formatting on portions of the code:

      // *INDENT-OFF*
      std::string test()
      {
          return "--" + boundaryString +                         "\r\n"
                 "Content-Type: application/json; charset=UTF-8" "\r\n"
                 /**/                                            "\r\n" +
                 metaDataBuf +                                   "\r\n"
                 "--" + boundaryString +                         "\r\n"
                 "Content-Type: application/octet-stream"        "\r\n"
                 /**/                                            "\r\n";
      }
      // *INDENT-ON*
      

      For details, see Disable Formatting in the Artistic Style documentation.

       

      Last edit: w-peuker 2025-03-12

Log in to post a comment.

MongoDB Logo MongoDB