Bug 87351 - misleading error message: missing binary operator before token "("
Summary: misleading error message: missing binary operator before token "("
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: preprocessor (show other bugs)
Version: 9.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2018-09-18 11:09 UTC by Vincent Lefèvre
Modified: 2021-12-19 10:18 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-09-18 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vincent Lefèvre 2018-09-18 11:09:21 UTC
The error message

  error: missing binary operator before token "("

from the preprocessor is misleading in general, as in most cases, it is not a binary operator that is missing, but the error is due to the use of sizeof, a cast, or a function-like macro that is not defined. The preprocessor could either output a fixed error message that would reflect the most common misusages, or try to guess what is wrong (like the use of sizeof or something that looks like a cast).

For instance:

$ cat tst.c
#if sizeof(int) > 4
#endif
$ gcc-snapshot -E tst.c
# 1 "tst.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "tst.c"
tst.c:1:11: error: missing binary operator before token "("
1 | #if sizeof(int) > 4
  |           ^

Some users can get confused. For instance, see:
* https://stackoverflow.com/questions/21338385/what-does-the-compiler-error-missing-binary-operator-before-token-mean
* https://cboard.cprogramming.com/c-programming/158452-error-missing-binary-operator-before-token.html
* https://www.linuxquestions.org/questions/programming-9/missing-binary-operator-before-token-4175547706/
* https://forum.kde.org/viewtopic.php?f=269&t=128141
Comment 1 Andrew Pinski 2021-12-19 10:18:43 UTC
clang does a reasonable job at their error message here:
<source>:2:5: error: function-like macro 'sizeof' is not defined
#if sizeof(int) > 4
    ^

ICC is almost as bad as GCC:
<source>(2): error: function call is not allowed in a constant expression
  #if sizeof(int) > 4
      ^

MSVC is worse than GCC:
<source>(2): fatal error C1017: invalid integer constant expression