From 793547a132108f09f1baf14a61f8cff0a6b61ba0 Mon Sep 17 00:00:00 2001
From: Dale Weiler <killfieldengine@gmail.com>
Date: Fri, 4 Jan 2013 12:07:42 +0000
Subject: [PATCH] Implemented -f[no]enhanced-diagnostics, to enable/disable the
 usage of enhanced diagnostics.

---
 gmqcc.ini.example |  4 ++++
 opts.c            |  1 +
 opts.def          |  1 +
 parser.c          | 27 ++++++++++++++-------------
 4 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/gmqcc.ini.example b/gmqcc.ini.example
index 2c63fcd..6f8f923 100644
--- a/gmqcc.ini.example
+++ b/gmqcc.ini.example
@@ -99,6 +99,10 @@
     # variables with the name 'nil' to be declared.
     PREMISSIVE                   = false
 
+    # Enable enhanced diagnostic messages. i.e provides a "did you mean"
+    # <ident> when you accidently typo. Amongst others
+    ENHANCED_DIAGNOSTICS         = true
+
 # These are all the warnings, usually present via the -W prefix from
 # the command line.
 [warnings]
diff --git a/opts.c b/opts.c
index c5c358d..548381a 100644
--- a/opts.c
+++ b/opts.c
@@ -65,6 +65,7 @@ static void opts_setdefault() {
     opts_set(opts.flags, FTEPP_PREDEFS,                  false);
     opts_set(opts.flags, CORRECT_TERNARY,                true);
     opts_set(opts.flags, BAIL_ON_WERROR,                 true);
+    opts_set(opts.flags, ENHANCED_DIAGNOSTICS,           true);
 }
 
 void opts_init(const char *output, int standard, size_t arraysize) {
diff --git a/opts.def b/opts.def
index 41ff6d4..22e2dab 100644
--- a/opts.def
+++ b/opts.def
@@ -48,6 +48,7 @@
     GMQCC_DEFINE_FLAG(LOOP_LABELS)
     GMQCC_DEFINE_FLAG(UNTYPED_NIL)
     GMQCC_DEFINE_FLAG(PERMISSIVE)
+    GMQCC_DEFINE_FLAG(ENHANCED_DIAGNOSTICS)
 #endif
 
 /* warning flags */
diff --git a/parser.c b/parser.c
index 6c507d1..e93c792 100644
--- a/parser.c
+++ b/parser.c
@@ -1642,22 +1642,23 @@ static ast_expression* parse_expression_leave(parser_t *parser, bool stopatcomma
                      * We should also consider adding correction tables for
                      * other things as well.
                      */
-                    for (i = 0; i < vec_size(parser->correct_variables); i++) {
-                        correct = correct_str(parser->correct_variables[i], parser_tokval(parser));
-                        if (strcmp(correct, parser_tokval(parser))) {
-                            break;
-                        } else if (correct) {
+                    if (OPTS_FLAG(ENHANCED_DIAGNOSTICS)) {
+                        for (i = 0; i < vec_size(parser->correct_variables); i++) {
+                            correct = correct_str(parser->correct_variables[i], parser_tokval(parser));
+                            if (strcmp(correct, parser_tokval(parser))) {
+                                break;
+                            } else if (correct) {
+                                mem_d(correct);
+                            }
+                        }
+
+                        if (correct) {
+                            parseerror(parser, "unexpected ident: %s (did you mean %s?)", parser_tokval(parser), correct);
                             mem_d(correct);
+                            goto onerr;
                         }
                     }
-
-                    if (correct) {
-                        parseerror(parser, "unexpected ident: %s (did you mean %s?)", parser_tokval(parser), correct);
-                        mem_d(correct);
-                    } else {
-                        parseerror(parser, "unexpected ident: %s", parser_tokval(parser));
-                    }
-
+                    parseerror(parser, "unexpected ident: %s", parser_tokval(parser));
                     goto onerr;
                 }
             }