From: Peter Collingbourne
In some cases, the front end emits diagnostics when it is clear @@ -422,14 +422,14 @@ deprecated syntax that is easily rewritten into a more modern form. Clang tries very hard to emit the diagnostic and recover gracefully in these and other cases.
-However, for these cases where the fix is obvious, the diagnostic -can be annotated with a code -modification "hint" that describes how to change the code referenced -by the diagnostic to fix the problem. For example, it might add the -missing semicolon at the end of the statement or rewrite the use of a -deprecated construct into something more palatable. Here is one such -example C++ front end, where we warn about the right-shift operator -changing meaning from C++98 to C++0x:
+However, for these cases where the fix is obvious, the diagnostic +can be annotated with a hint (referred to as a "fix-it hint") that +describes how to change the code referenced by the diagnostic to fix +the problem. For example, it might add the missing semicolon at the +end of the statement or rewrite the use of a deprecated construct +into something more palatable. Here is one such example from the C++ +front end, where we warn about the right-shift operator changing +meaning from C++98 to C++0x:
test.cpp:3:7: warning: use of right-shift operator ('>>') in template argument will require parentheses in C++0x @@ -438,33 +438,31 @@ A<100 >> 2> *a; ( )-
Here, the code modification hint is suggesting that parentheses be -added, and showing exactly where those parentheses would be inserted -into the source code. The code modification hints themselves describe -what changes to make to the source code in an abstract manner, which -the text diagnostic printer renders as a line of "insertions" below -the caret line. Other diagnostic -clients might choose to render the code differently (e.g., as -markup inline) or even give the user the ability to automatically fix -the problem.
- -All code modification hints are described by the
-CodeModificationHint
class, instances of which should be
-attached to the diagnostic using the << operator in the same way
-that highlighted source ranges and arguments are passed to the
-diagnostic. Code modification hints can be created with one of three
-constructors:
Here, the fix-it hint is suggesting that parentheses be added, +and showing exactly where those parentheses would be inserted into the +source code. The fix-it hints themselves describe what changes to make +to the source code in an abstract manner, which the text diagnostic +printer renders as a line of "insertions" below the caret line. Other diagnostic clients might choose +to render the code differently (e.g., as markup inline) or even give +the user the ability to automatically fix the problem.
+ +All fix-it hints are described by the FixItHint
class,
+instances of which should be attached to the diagnostic using the
+<< operator in the same way that highlighted source ranges and
+arguments are passed to the diagnostic. Fix-it hints can be created
+with one of three constructors:
CodeModificationHint::CreateInsertion(Loc, Code)
FixItHint::CreateInsertion(Loc, Code)
Code
(a string) should be inserted
before the source location Loc
.CodeModificationHint::CreateRemoval(Range)
FixItHint::CreateRemoval(Range)
Range
should be removed.CodeModificationHint::CreateReplacement(Range, Code)
FixItHint::CreateReplacement(Range, Code)
Range
should be removed, and replaced with the given Code
string.