1

Modern GCC provides the -fdiagnostics-generate-patch flag:

   Options to Control Diagnostic Messages Formatting
[...]
       -fdiagnostics-generate-patch
           Print fix-it hints to stderr in unified diff format, after any diagnostics are printed.

For example, given this intentionally erroneous C program:

int main()
{
    int f1;
    fl = printf("Hello, World!\n");
}

gcc -fdiagnostics-generate-patch produces the usual output:

main.c: In function ‘main’:
main.c:4:5: error: ‘fl’ undeclared (first use in this function); did you mean ‘f1’?
    4 |     fl = printf("Hello, World!\n");
      |     ^~
      |     f1
main.c:4:5: note: each undeclared identifier is reported only once for each function it appears in
main.c:4:10: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
    4 |     fl = printf("Hello, World!\n");
      |          ^~~~~~
main.c:4:10: warning: incompatible implicit declaration of built-in function ‘printf’
main.c:1:1: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
  +++ |+#include <stdio.h>
    1 | int main()

followed by a diff-like patch:

--- main.c
+++ main.c
@@ -1,5 +1,6 @@
+#include <stdio.h>
 int main()
 {
     int f1;
-    fl = printf("Hello, World!\n");
+    f1 = printf("Hello, World!\n");
 }

which is obviously very handy when fixing small mistakes but might not always be right.

Building on how vim already makes the "edit/compile/fix cycle" smoother (quickfix, :compiler, 'errorformat', :make, ...), I was wondering if there was a way to automatically capture the suggested patch (which would have to be extracted from the regular output) and display it using :diff in order to allow the user to easily review and apply (:diffget/:diffput) the change, if it exists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.