Skip to content

[clang-format] Bug OneLineFormatOffRegex broken if regex matches on first token #162402

@sebastianmueller01

Description

@sebastianmueller01

@owenca

.clang_format:

OneLineFormatOffRegex: MACRO_TEST

test.cpp:

int main() {
        MACRO_TEST();
        MACRO_TEST();
        MACRO_TEST();
        MACRO_TEST();
        MACRO_TEST();
}

Running
clang-format-21 -i --style=file:.clang_format test.cpp

Results in the following incorrectly formatted output:

int main() {
        MACRO_TEST();
  MACRO_TEST();
        MACRO_TEST();
  MACRO_TEST();
        MACRO_TEST();
}

It seems like if we previously skipped formatting a line, then the first token of the next line is skipped from the regex match check. In the example, since the only matching token is the first in the line, we get this behavior. This doesn't occur if the token matching is at the EOL.

When transitioning from FO_CurrentLine (or FO_NextLine) to FO_None, the code breaks immediately, so the current token never gets evaluated against the regex in the default case.
https://github.com/llvm/llvm-project/pull/137577/files#diff-8e6ad02b15a65cc2ff3b2e622d25b2b45e8e900f54ce9ee79026d76115076a5eR93-R98

I think a possible solution to this would be
Image

Also instead of evaluateCurrentToken you might also be able to look for !Tok.Finalized but I haven't read through enough of the code to confirm.

Metadata

Metadata

Assignees

Type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions