Bug 99720 - Feature Request: Better errors for C++ features when compiling C (pass by reference)
Summary: Feature Request: Better errors for C++ features when compiling C (pass by ref...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 9.3.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2021-03-23 02:04 UTC by Cameron Tacklind
Modified: 2021-08-12 22:20 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-08-12 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Cameron Tacklind 2021-03-23 02:04:11 UTC
I'm embarrassed that I spent so long on this issue, to the point that I thought it was worth writing this issue in the hope that it might save someone the same trouble. If this is inappropriate (or already address), my apologies.

I normally work in the C++ world for simple embedded programming, mostly just with a subset of the language features. I love the sugar. The old "C" way of doing things has always bugged me. I'm thinking of basic features like overloading function.

Now I'm working on a project that has a lot of legacy C that's too much work to convert to C++.

In implementing a simple function, without thinking, I used a reference (`void foo(int&i);`) to pass a variable to a function (not a plain pointer). The astute among you will remember that passing by reference is a C++ feature that simply does not exist in C.

Error message:
> test.c:1:13: error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token

It took me most of a day to realize I was trying to use a feature that simply doesn't exist in C (lots of other #define macros that contributed to my confusion). It feels like this error message should be easy enough to improve.

With how often C and C++ get conflated, it seems prudent to me to ensure that accidentally using C++ features in a C compiler would generate warnings or errors that point towards these possible mistake explicitly.
Comment 1 Richard Biener 2021-03-23 07:31:07 UTC
Confirmed.  The caret diagnostic will probably ring a bell for the user but a fixit hint would be nice indeed:

t.c:1:15: error: expected ';', ',' or ')' before '&' token
    1 | void foo (int &i);
      |               ^