Skip to content

parse_format_replacements_xml mishandles multi-line replacements #168

Description

@nclark-nasuni

parse_format_replacements_xml uses get_line_cnt_from_cols to determine which line is being updated, which indicates on which line the replacement starts but the replacement can span multiple lines into the lines which changed when lines_changed_only is set.

Example code

#include <string>

class Test {
public:
    const std::string getString() const { return str_member_; }

    std::string toString() const {
            return std::string("Test[string:") + str_member_ + ", int: " + int_member + "]";
    }

private:
    std::string str_member_{};
    int         int_member_{};
}

If formatter is run only on lines 8:9

$ /usr/bin/clang-format -style=file --output-replacements-xml --lines=8:9 test.cpp 
<?xml version='1.0'?>
<replacements xml:space='preserve' incomplete_format='false'>
<replacement offset='139' length='13'>&#10;        </replacement>
</replacements>

The offset is 139 which when run through get_line_cnt_from_cols returns line 7 which is not in the range 8:9 so the replacement will be ignored. The parse_format_replacements_xml needs to take into consideration the entire range of the replacement to see if it ends in the line range or trust that clang will only format the specified lines so that all replacements pertain to the lines specified and the line check can be removed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions