From d89df5a534738e80917de272bb36bf00e7d79593 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Wed, 1 Jun 2011 22:45:49 +0000 Subject: Improve the readability of the "Expressive Diagnostics" page, from Dave Yost! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@132432 91177308-0d34-0410-b5e6-96231b3b80d8 --- www/diagnostics.html | 123 +++++++++++++++++++++++++-------------------------- 1 file changed, 60 insertions(+), 63 deletions(-) (limited to 'www/diagnostics.html') diff --git a/www/diagnostics.html b/www/diagnostics.html index f4f923948a..48d222abb2 100644 --- a/www/diagnostics.html +++ b/www/diagnostics.html @@ -2,7 +2,7 @@ "http://www.w3.org/TR/html4/strict.dtd"> - + Clang - Expressive Diagnostics @@ -34,10 +34,18 @@ that embed Clang and extract equivalent information through internal APIs.-->

Column Numbers and Caret Diagnostics

First, all diagnostics produced by clang include full column number -information, and use this to print "caret diagnostics". This is a feature -provided by many commercial compilers, but is generally missing from open source +information. The clang command-line compiler driver uses this information +to print "caret diagnostics". +(IDEs can use the information to display in-line error markup.) +Precise error location in the source is a feature provided by many commercial +compilers, but is generally missing from open source compilers. This is nice because it makes it very easy to understand exactly -what is wrong in a particular piece of code, an example is:

+what is wrong in a particular piece of code

+ +

The caret (the blue "^" character) exactly shows where the problem is, even +inside of a string. This makes it really easy to jump to the problem and +helps when multiple instances of the same character occur on a line. (We'll +revisit this more in following examples.)

   $ gcc-4.2 -fsyntax-only -Wformat format-strings.c
@@ -48,17 +56,18 @@ what is wrong in a particular piece of code, an example is:

^
-

The caret (the blue "^" character) exactly shows where the problem is, even -inside of the string. This makes it really easy to jump to the problem and -helps when multiple instances of the same character occur on a line. We'll -revisit this more in following examples.

-

Range Highlighting for Related Text

Clang captures and accurately tracks range information for expressions, statements, and other constructs in your program and uses this to make -diagnostics highlight related information. For example, here's a somewhat -nonsensical example to illustrate this:

+diagnostics highlight related information. In the following somewhat +nonsensical example you can see that you don't even need to see the original source code to +understand what is wrong based on the Clang error. Because clang prints a +caret, you know exactly which plus it is complaining about. The range +information highlights the left and right side of the plus which makes it +immediately obvious what the compiler is talking about. +Range information is very useful for +cases involving precedence issues and many other cases.

   $ gcc-4.2 -fsyntax-only t.c
@@ -69,21 +78,19 @@ nonsensical example to illustrate this:

~~~~~~~~~~~~~~ ^ ~~~~~
-

Here you can see that you don't even need to see the original source code to -understand what is wrong based on the Clang error: Because clang prints a -caret, you know exactly which plus it is complaining about. The range -information highlights the left and right side of the plus which makes it -immediately obvious what the compiler is talking about, which is very useful for -cases involving precedence issues and many other cases.

-

Precision in Wording

A detail is that we have tried really hard to make the diagnostics that come out of clang contain exactly the pertinent information about what is wrong and why. In the example above, we tell you what the inferred types are for the left and right hand sides, and we don't repeat what is obvious from the -caret (that this is a "binary +"). Many other examples abound, here is a simple -one:

+caret (e.g., that this is a "binary +").

+ +

Many other examples abound. In the following example, not only do we tell you that there is a problem with the * +and point to it, we say exactly why and tell you what the type is (in case it is +a complicated subexpression, such as a call to an overloaded function). This +sort of attention to detail makes it much easier to understand and fix problems +quickly.

   $ gcc-4.2 -fsyntax-only t.c
@@ -94,12 +101,6 @@ one:

^~~~~~~~
-

In this example, not only do we tell you that there is a problem with the * -and point to it, we say exactly why and tell you what the type is (in case it is -a complicated subexpression, such as a call to an overloaded function). This -sort of attention to detail makes it much easier to understand and fix problems -quickly.

-

No Pretty Printing of Expressions in Diagnostics

Since Clang has range highlighting, it never needs to pretty print your code @@ -127,8 +128,10 @@ typename in diagnostics. However, sometimes very simple typedefs can wrap trivial types and it is important to strip off the typedef to understand what is going on. Clang aims to handle both cases well.

-

For example, here is an example that shows where it is important to preserve -a typedef in C:

+

The following example shows where it is important to preserve +a typedef in C. Here the type printed by GCC isn't even valid, but if the error +were about a very long and complicated type (as often happens in C++) the error +message would be ugly just because it was long and hard to read.

   $ gcc-4.2 -fsyntax-only t.c
@@ -139,10 +142,9 @@ a typedef in C:

~~~~~~~~^~
-

Here the type printed by GCC isn't even valid, but if the error were about a -very long and complicated type (as often happens in C++) the error message would -be ugly just because it was long and hard to read. Here's an example where it -is useful for the compiler to expose underlying details of a typedef:

+

The following example shows where it is useful for the compiler to expose +underlying details of a typedef. If the user was somehow confused about how the +system "pid_t" typedef is defined, Clang helpfully displays it with "aka".

   $ gcc-4.2 -fsyntax-only t.c
@@ -153,9 +155,6 @@ is useful for the compiler to expose underlying details of a typedef:

~~~~~ ^
-

If the user was somehow confused about how the system "pid_t" typedef is -defined, Clang helpfully displays it with "aka".

-

In C++, type preservation includes retaining any qualification written into type names. For example, if we take a small snippet of code such as:

@@ -205,8 +204,10 @@ in source code. When Clang produces a diagnostic about a particular problem that it can work around (e.g., non-standard or redundant syntax, missing keywords, common mistakes, etc.), it may also provide specific guidance in the form of a code transformation to correct the -problem. For example, here Clang warns about the use of a GCC -extension that has been considered obsolete since 1993:

+problem. In the following example, Clang warns about the use of a GCC +extension that has been considered obsolete since 1993. The underlined +code should be removed, then replaced with the code below the +caret line (".x =" or ".y =", respectively).

   $ clang t.c
@@ -220,11 +221,12 @@ extension that has been considered obsolete since 1993:

.y =
-

The underlined code should be removed, then replaced with the code below the -caret line (".x =" or ".y =", respectively). "Fix-it" hints are most useful for +

"Fix-it" hints are most useful for working around common user errors and misconceptions. For example, C++ users commonly forget the syntax for explicit specialization of class templates, -as in the following error:

+as in the error in the following example. Again, after describing the problem, +Clang provides the fix--add template<>--as part of the +diagnostic.

   $ clang t.cpp
@@ -234,14 +236,14 @@ as in the following error:

template<>
-

Again, after describing the problem, Clang provides the fix--add template<>--as part of the diagnostic.

-

Automatic Macro Expansion

Many errors happen in macros that are sometimes deeply nested. With traditional compilers, you need to dig deep into the definition of the macro to -understand how you got into trouble. Here's a simple example that shows how -Clang helps you out:

+understand how you got into trouble. The following simple example shows how +Clang helps you out by automatically printing instantiation information and +nested range information for diagnostics as they are instantiated through macros +and also shows how some of the other pieces work in a bigger example.

   $ gcc-4.2 -fsyntax-only t.c
@@ -256,10 +258,7 @@ Clang helps you out:

~~~ ^ ~~~
-

This shows how clang automatically prints instantiation information and -nested range information for diagnostics as they are instantiated through macros -and also shows how some of the other pieces work in a bigger example. Here's -another real world warning that occurs in the "window" Unix package (which +

Here's another real world warning that occurs in the "window" Unix package (which implements the "wwopen" class of APIs):

@@ -275,14 +274,19 @@ implements the "wwopen" class of APIs):

^
-

In practice, we've found that this is actually more useful in multiply nested +

In practice, we've found that Clang's treatment of macros is actually more useful in multiply nested macros that in simple ones.

Quality of Implementation and Attention to Detail

Finally, we have put a lot of work polishing the little things, because -little things add up over time and contribute to a great user experience. Three -examples are:

+little things add up over time and contribute to a great user experience.

+ +

The following example shows a trivial little tweak, where we tell you to put the semicolon at +the end of the line that is missing it (line 4) instead of at the beginning of +the following line (line 5). This is particularly important with fixit hints +and caret diagnostics, because otherwise you don't get the important context. +

   $ gcc-4.2 t.c
@@ -295,12 +299,9 @@ examples are:

;
-

This shows a trivial little tweak, where we tell you to put the semicolon at -the end of the line that is missing it (line 4) instead of at the beginning of -the following line (line 5). This is particularly important with fixit hints -and caret diagnostics, because otherwise you don't get the important context. -A second example is: -

+

The following example shows much better error recovery than GCC. The message coming out +of GCC is completely useless for diagnosing the problem. Clang tries much harder +and produces a much more useful diagnosis of the problem.

   $ gcc-4.2 t.c
@@ -311,9 +312,8 @@ A second example is:
   ^
 
-

This shows an example of much better error recovery. The message coming out -of GCC is completely useless for diagnosing the problem, Clang tries much harder -and produces a much more useful diagnosis of the problem.

+

The following example shows that we recover from the simple case of +forgetting a ; after a struct definition much better than GCC.

   $ cat t.cc
@@ -339,9 +339,6 @@ and produces a much more useful diagnosis of the problem.

;
-

This shows that we recover from the simple case of forgetting a ; after -a struct definition much better than GCC.

-

While each of these details is minor, we feel that they all add up to provide a much more polished experience.

-- cgit v1.2.1