clang-format 14 WhitespaceSensitiveMacros regression
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.
@llvm/issue-subscribers-clang-format
https://reviews.llvm.org/D132001