llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.15k stars 12.03k forks source link

[clang-format] Macro `A=;` causes -> to be incorrectly inferred as trailing return type #90227

Open yous opened 6 months ago

yous commented 6 months ago

With EMPTY_MACRO=; in Macros, clang-format 18.1.4 adds spaces around ->.

$ clang-format --version
clang-format version 18.1.4
$ clang-format --dump-config
...
Macros:
  - EMPTY_MACRO=;
...
$ cat foo.c
int main() {
  EMPTY_MACRO
  (void)memcpy(obj->dest, obj->src, 1);
  return 0;
}
$ clang-format foo.c
int main() {
  EMPTY_MACRO
  (void)memcpy(obj -> dest, obj->src, 1);
  return 0;
}

clang-format 17.0.6 doesn't have this issue:

$ clang-format --version
clang-format version 17.0.6 (https://github.com/ssciwr/clang-format-wheel 00700f257e3601a6ef0e714031c1dcef8077633f)
$ clang-format foo.c
int main() {
  EMPTY_MACRO
  (void)memcpy(obj->dest, obj->src, 1);
  return 0;
}
rymiel commented 6 months ago

Seems to be regressed by c83d64f17a6e38f39fde611f06479e6f2a62b70f

llvmbot commented 6 months ago

@llvm/issue-subscribers-clang-format

Author: Chayoung You (yous)

With `EMPTY_MACRO=;` in `Macros`, clang-format 18.1.4 adds spaces around `->`. ``` console $ clang-format --version clang-format version 18.1.4 $ clang-format --dump-config ... Macros: - EMPTY_MACRO=; ... $ cat foo.c int main() { EMPTY_MACRO (void)memcpy(obj->dest, obj->src, 1); return 0; } $ clang-format foo.c int main() { EMPTY_MACRO (void)memcpy(obj -> dest, obj->src, 1); return 0; } ``` clang-format 17.0.6 doesn't have this issue: ``` console $ clang-format --version clang-format version 17.0.6 (https://github.com/ssciwr/clang-format-wheel 00700f257e3601a6ef0e714031c1dcef8077633f) $ clang-format foo.c int main() { EMPTY_MACRO (void)memcpy(obj->dest, obj->src, 1); return 0; } ```
owenca commented 6 months ago

Actually, it's a bug unrelated to c83d64f17a6e, which just exposed the bug:

$ clang-format -version
clang-format version 17.0.6 (https://github.com/llvm/llvm-project 6009708b4367171ccdbf4b5905cb6a803753fe18)
$ cat foo.c
int main() {
  EMPTY_MACRO
  (void)memcpy(obj->dest, obj->src, 1);
  return 0;
}
$ clang-format -style='{Macros: [EMPTY_MACRO=;]}' -debug foo.c
...
Assertion failed: (Tok == Line.Last), function printDebugInfo, file TokenAnnotator.cpp, line 5615.
...
$ 

cc @r4nt

yous commented 1 month ago

clang-format 18.1.8 and 19.1.0 still have this issue.