I'm using Visual Studio C++ 2008 Express Edition.
Is it possible to modify the text in the Output pane for compilation (or开发者_运维百科 other) errors?
For example, I might receive an error that reads: error C2556: int Class::getResult(void) + a lot more relative garbage.
I can parse the output text and find and fix my mistakes easily. It would still be nice/useful if I could modify the errors to make them cleaner, shorter, and more friendly. An example would be receiving an error that read: "Source.cc (Line 10): Missing a closing;"
There are a few things you have to understand. The compiler is really doing the best it can; it tells you exactly where it failed, and the entire compilation up until that point. It doesn't know what you intended to write, it knows what you wrote, so how can it possibly assume an error was caused by something else more remote? I think if you were writing it and got to the point of making errors "cleaner, shorter, and more friendly", you'd realize this is much more difficult that it might seem.
There are situations when it can do that, and compilers are getting better at suggesting them it seems. For example this:
if (x);
{
x = 5;
}
Produces a warning:
warning C4390: ';' : empty controlled statement found; is this the intent?
But they can't always do that.
That all said, the error messages are really all you need to solve the problem, once you learn the language a bit and understand what it all means. I think it's an enormous myth and exaggeration that template errors are "evil and cryptic". While they can be verbose, they're really not that hard to understand at all. I don't think it's template errors being cryptic, I think it's the reader not knowing how to read it well, which makes it seem cryptic. But if you take your time, you see exactly what it was trying to do and where it failed.
There were a thing called "concepts" that would appear in the next standard, C++0x, that would fix this up immensely. They were sort of like tags that classes could fit into if they supported everything that tag said they had to. So in std::copy
, instead of errors about being unable to call operator=
on something, along with the errors that propagates, you would simply get something like "Error: Type is not CopyAssignable" and that would be it. Same error, different presentation. And that's also the reason they got cut out, for now. they were proving to be trouble, and since it was syntactic sugar anyway, it's more important to work on other things. Hopefully we'll see them someday.
Now, there is a tool called STLFilt, which will filter template errors concerning things in the STL. I have never used it, but I've heard it recommended more than once. Here's one of their sample filters. You might give it a try.
Since you seem to think this is something possible to do in the language itself: nope. The language has no concepts of what errors messages should look like or what should warn or what shouldn't.
You'll find what I mean in the standard in section 1.4/2. Summarized, it basically says "If there's an error, output something about it". That's it.
It's pretty easy to take your best shot at this problem. The compiler itself is a command-line program named cl.exe
. If you want to filter its output, what you need to do is create a program that's also named cl.exe
. It will need to pass all the command line arguments through to the original cl.exe
. Then it'll take whatever the original one produces as messages on its standard output, parse them, replace them with the messages you prefer, and print those to its own standard output.
When you do that, you'll probably want to at least retain the information about the file and line where the problem occurred in the original format. The IDE parses and uses that to support navigating among errors (e.g., with F4 or double-clicking).
精彩评论