Open llvmbot opened 3 years ago
When we turn assertions on, we can get:
$ ./clang -o /tmp/a.out -x c -std=c11 -pedantic -Wall test.c clang-13: /home/xxx/data/git/llvm-project/clang/lib/Parse/Parser.cpp:2068: bool clang::Parser::TryAnnotateCXXScopeToken(bool): Assertion `getLangOpts().CPlusPlus && "Call sites of this function should be guarded by checking for C++"' failed. PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace, preprocessed source, and associated run script. Stack dump:
clang-13: error: unable to execute command: Aborted (core dumped) clang-13: error: clang frontend command failed due to signal (use -v to see invocation) clang version 13.0.0 (https://github.com/llvm/llvm-project.git b006902b2dfac792e8ade73798ca1b216654faf7) Target: x86_64-unknown-linux-gnu Thread model: posix InstalledDir: /data/xxx/git/llvm-project/build_trunk/bin clang-13: note: diagnostic msg:
PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT: Preprocessed source(s) and associated run script(s) are located at: clang-13: note: diagnostic msg: /tmp/test-e239b5.c clang-13: note: diagnostic msg: /tmp/test-e239b5.sh clang-13: note: diagnostic msg:
$ cat /tmp/test-e239b5.sh
"/data/xxx/git/llvm-project/build_trunk/bin/clang-13" "-cc1" "-triple" "x86_64-unknown-linux-gnu" "-emit-obj" "-mrelax-all" "--mrelax-relocations" "-disable-free" "-main-file-name" "test.c" "-mrelocation-model" "static" "-mframe-pointer=all" "-fmath-errno" "-fno-rounding-math" "-mconstructor-aliases" "-munwind-tables" "-target-cpu" "x86-64" "-tune-cpu" "generic" "-fno-split-dwarf-inlining" "-debugger-tuning=gdb" "-Wall" "-pedantic" "-std=c11" "-fdebug-compilation-dir=/data/xxx/docker_share/clang" "-ferror-limit" "19" "-fgnuc-version=4.2.1" "-fcolor-diagnostics" "-faddrsig" "-x" "c" "test-e239b5.c"
I can no longer reproduce the assertion, but we still fail to reject this invalid code. The test case can be reduced to:
struct huh {
int (*func_ptr)(int = <);
};
@llvm/issue-subscribers-clang-frontend
The issue is that we're relying on late parsing the class to diagnose this, but late parsing of classes doesn't happen in C as it does in C++.
@zygoloid -- I can approach this two different ways: either add support for late parsing of classes in C, or add && getLangOpts().CPlusPlus
to https://github.com/llvm/llvm-project/blob/38dfcf96dfd5d88e641e8054ea2f3a008ff8bcfd/clang/lib/Parse/ParseDecl.cpp#L7434 The latter feels like a bit of a hack, but it 1) diagnoses reasonably, and 2) breaks zero tests. The former worries me because of how likely it is that we've baked C++ assumptions into late parsing. You've done more with the late parsing functionality than I have; do you have a feeling for which is the right approach?
Extended Description
Clang accepts this presumably-invalid code, which GCC rejects. It affects 11.0, 12.0, and trunk.
clang: https://godbolt.org/z/b7oaxq
GCC: https://godbolt.org/z/1194qP
$ cat test.c
$ ./clang -o /tmp/a.out -x c -std=c11 -pedantic -Wall test.c
no any warning or error
I try to make the compilation as strict as possible, so I set "-std=c11 -pedantic -Wall". Actually, the standard does not matter.