llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

clang-format 14 WhitespaceSensitiveMacros regression

Open JohnC32 opened this issue 3 years ago • 2 comments

Given:

#include <string.h>

#include <iostream>

#define CHECK(a) \
    { std::cout << "CHECK = " << strcmp("one:two:three", #a) << '\n'; }

int main() {
    CHECK(one:two:three)
    // unnecessary semicolon added:
    CHECK(one:two:three);
    return 0;
}

and _clang-format:

---
BasedOnStyle: Google
IndentWidth: 4
AllowShortFunctionsOnASingleLine: Empty
WhitespaceSensitiveMacros: ['CHECK']
...

Using this example, clang-format 13 does the correct thing (leaves both CHECK macro usages untouched) and clang-format 14 alters the first one causing the program answers to diff:

#include <string.h>

#include <iostream>

#define CHECK(a) \
    { std::cout << "CHECK = " << strcmp("one:two:three", #a) << '\n'; }

int main() {
    CHECK(one:two : three)
    // unnecessary semicolon added:
    CHECK(one:two:three);
    return 0;
}

Notice that spaces have been added around the second colon in the first CHECK macro usage, i.e. clang-format 14 is not honoring WhitespaceSensitiveMacros specification. The second CHECK macro usage behaves correctly (WhitespaceSensitiveMacros is honored). Adding the unnecessary semicolon is not desired as compiler warnings flag it as unnecessary. For example, clang will produce warning warning: empty expression statement has no effect; remove unnecessary ';' to silence this warning [-Wextra-semi-stmt]. It's also not intuitive that you get different indent behavior because of an unnecessary semicolon.

A patch would be helpful for us because we happen to define macros where trailing semicolons will be flagged as unnecessary, so the workaround presents problems.

JohnC32 avatar Aug 15 '22 15:08 JohnC32

@llvm/issue-subscribers-clang-format

llvmbot avatar Aug 15 '22 15:08 llvmbot

https://reviews.llvm.org/D132001

owenca avatar Aug 17 '22 00:08 owenca