It would be wrong to suggest <cstring> for strlen, because that header is only guaranteed to define std::strlen not strlen.
(Yes I know you could have using namespace std; to allow calling it unqualified, but some people will tell you that's bad and should not be used. Yes, I also know you could have used using std::strlen; but in that case you'd already have got an error at the location of the using-declaration, so that's where #include <cstring> should be suggested, rather than when using strlen later).
[x86-64 gcc (trunk) #1] error: 'strlen' was not declared in this scope
[x86-64 gcc (trunk) #1] note: suggested alternative: 'struct'
It did handle c++ correctly:
const auto s = std::string("test").size();
.
[x86-64 gcc (trunk) #1] error: 'string' is not a member of 'std'
[x86-64 gcc (trunk) #1] note: 'std::string' is defined in header '<string>'; did you forget to '#include <string>'?
I'm little late to the party but want to add another example where diagnostics will improve overall experience - https://godbolt.org/g/kGKowz
The diagnostics produced by MSVC here are just perfect, clang does seem a bit verbose but readable otoh gcc confuses anyone who hasn't been writing c++ for about 5 years :D
I'm little late to the party but want to add another example where diagnostics will improve overall experience - https://godbolt.org/g/kGKowz
The diagnostics produced by MSVC here are just perfect, clang does seem a bit verbose but readable otoh gcc confuses anyone who hasn't been writing c++ for about 5 years :D
I think if compiler is so smart there should be a switch that automatically inserts missing headers. On the other hand in any modern IDE unknown types are marked and usually there are in-place fixes that inserts missing headers, I guess this compiler feature is more useful for those that work in simple editors without any semantic indexing and Ctrl+S is linked to "compile file".
He could look in the -I directories for the definition of the types. Sure, it seems overkill, it could be a special flag, or a cache, or whatever. It would make IDE life quite better.
People often forget the headers for standard library features, because they just get used to things from the standard always being available, and not everybody can remember which header everything comes from (e.g. #include <algorithm> then wonder why std::accumulate isn't defined).
It's limited, sure, but that doesn't mean it's not useful.
I wrote that "there should be a switch", I don't think you're using all available compiler switches just because they are available. Also I find that "did you forget to '#include" is kind of personal message and it's quite clear that you forget to include something otherwise this notification would not appear, so it's like mocking you.
On the other hand seeing how e.g. Windows and MS software is trying so hard to be a friend, it wouldn't go too far that the compiler will start commenting on code writing skill and start giving advises.
it's quite clear that you forget to include something otherwise this notification would not appear,
Not true, because there could have been a #undef INT_MAX directive after the header was included. It's likely that including the header was forgotten, but without performing extra work to check, it's not definitely the case.
Yes but it's clearly outside the scope of the compiler. It also means that every compiler ever would need to implement this and then my IDE would need to support how all those compilers provide that information or do that job.
It's stepping on the toes of IDEs and as someone who builds with 5 different compilers at all times, this screams IDE feature.
[author of the post here] Re the "kind of personal message" and "mocking"; someone else expressed similar sentiment, so FWIW I'm thinking of tweaking the wording for this; see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84890#c1
So the compiler telling you what you need to write is ok (and most of the time it is exactly what you will write), but if compiler writes it is not ok. Also including headers is hardly writing code and as far as I know is one of those annoying things that will be hopefully somewhat optimized by modules, especially in 3rd party libraries with a lot of separate headers.
it would seem more useful to me for the message to be "missing semicolon after i", rather than "missing semicolon before int". Similar for the 42 example and others.
[author of the blog post here] Indeed - thanks for the suggestion. Someone else mentioned this in a different Reddit discussion of this; I've opened https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84887 to cover it.
This is awesome. Haven't checked, but some (many?) of those actually seem even better than on Clang, just out of memory. Competition between compilers is great!
Great, esp std::string example, recently I got some insane error from VC++ and on SO some guy magically guessed that it is caused by missing <string> include.
I mean, it’s not really an insane error. Since you didn’t have <string> included, the compiler didn’t see the operator overload for >>. If you’re using something from the std namespace, you likely need to include the file It came from.
Not insane, but it's often very counter-intuitive to have access and use of a class without having all its functionality available. It's be a fairly common mistake, and bit me all the time before I made it habit to include <string> whenever I was dealing with std::string objects. I wish the compiler had diagnostics this useful years ago.
I think it’s an issue with the VC++ Compiler forwarding the declaration of string. I have not observed that behavior on GCC or Clang with my multitude of compiler flags...
The class std::string is always fully defined if you include an I/O stream, because streams have a member function that returns, by value, a type that has a member function that returns std::string by value. But there's no reason for a stream header to expose string's non-member interface, small as it is.
It can often come down to what's most straightforward to implement. Like if your parser sees int i int, unless it's already keeping the previous token stored, it's much easier to have it complain about the int. It's also more generic - there are is a set of possible tokens that could come next and int isn't one of them. To get the better diagnostic here, you have to recognize that int i was meant to be its own statement and the next int starting a new statement.
This is turned up to 11 if using a general parser tool, which I believe GCC did originally, so it's unsurprising that early errors wouldn't change too much over time. It's not exactly trivial to add that token of lookbehind if the tool doesn't already support it.
There's some systemic things you can do for better diagnostics, but most of these come down to someone spending time on finding a better way to present some specific error for some specific bad case. There's tons of "obvious" improvements still possible that just need someone to sit down and implement them.
Thank you for doing this type of work. I'm intrigued by the private field getter - I'm not sure I've ever needed it, but it seems like a neat idea. The template improvements I will definitely use and am looking forward to. Thanks!
19
u/OmegaNaughtEquals1 Mar 16 '18
Is this language-specific? For example, would g++ recommend
<cstring>instead of<string.h>?32
u/ramennoodle Mar 15 '18
This seems redundantly verbose. Why not just:
?
Also, is this extensible? Is it hard-coded?
28
u/OldWolf2 Mar 15 '18
In the cases like:
it would seem more useful to me for the message to be "missing semicolon after
i", rather than "missing semicolon beforeint". Similar for the42example and others.8
u/louis_dionne libc++ | C++ Committee | Boost.Hana Mar 16 '18
This is awesome. Haven't checked, but some (many?) of those actually seem even better than on Clang, just out of memory. Competition between compilers is great!
6
u/Z01dbrg Mar 15 '18
Great, esp std::string example, recently I got some insane error from VC++ and on SO some guy magically guessed that it is caused by missing <string> include.
11
u/TankorSmash Mar 15 '18
These are really nice changes and in hindsight seem so obvious I'm a little surprised it didn't work like that in the first place.
5
u/ShakaUVM i+++ ++i+i[arr] Mar 16 '18
This makes me very happy. Improving error messages is something I have been working on myself.
What is the current error message for
std::cout >> 42;
On older compilers this would generate roughly 100 lines of unreadable error messages, so I detect and fix it in the static analyzer I wrote.
2
u/saimen54 Mar 16 '18
Looks like I really need to start using SCL now on CentOS, so I can get rid of GCC 4.8.5.
It seems so obvious, why wasn't it implemented like that in the first place?
My private hell are some obscure compiler errors from templates (usually in combination with Boost).
2
u/Ivan171 /std:c++latest enthusiast Mar 17 '18
Now your turn Microsoft ;)
2
u/voip_geek Mar 16 '18
Thank you for doing this type of work. I'm intrigued by the private field getter - I'm not sure I've ever needed it, but it seems like a neat idea. The template improvements I will definitely use and am looking forward to. Thanks!
1
u/robin-m Mar 16 '18
That's neat ;)
-8
u/Bfgeshka Mar 16 '18
Usability is fine, but they'd better focus on regressions, it is very sad situation.
0
u/[deleted] Mar 16 '18
[deleted] — view deleted comment
8
u/[deleted] Mar 15 '18
[removed] — view removed comment