]> granicus.if.org Git - clang/commitdiff
Print a typo correction hint for unknown warning flags.
authorBenjamin Kramer <benny.kra@googlemail.com>
Mon, 14 Nov 2011 23:30:34 +0000 (23:30 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Mon, 14 Nov 2011 23:30:34 +0000 (23:30 +0000)
$ clang -Wololo t.c
warning: unknown warning option '-Wololo'; did you mean '-Wall'? [-Wunknown-warning-option]

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@144591 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticFrontendKinds.td
include/clang/Basic/DiagnosticIDs.h
lib/Basic/DiagnosticIDs.cpp
lib/Frontend/Warnings.cpp
test/Driver/cc-log-diagnostics.c

index c9f5a5c48634be4ed30ae2df42dfd5480dd5f918..91e32200f2f7a6042ba6a02dec677458a6a0ebec 100644 (file)
@@ -148,10 +148,10 @@ def warn_pch_compiler_options_mismatch : Error<
 def err_not_a_pch_file : Error<
     "'%0' does not appear to be a precompiled header file">, DefaultFatal;
 def warn_unknown_warning_option : Warning<
-    "unknown warning option '%0'">,
+    "unknown warning option '%0'; did you mean '%1'?">,
     InGroup<DiagGroup<"unknown-warning-option"> >;
 def warn_unknown_negative_warning_option : Warning<
-    "unknown warning option '%0'">,
+    "unknown warning option '%0'; did you mean '%1'?">,
     InGroup<DiagGroup<"unknown-warning-option"> >, DefaultIgnore;
 def warn_unknown_warning_specifier : Warning<
     "unknown %0 warning specifier: '%1'">,
index 1ff6c1192487a4295831a0674541f2adb2340045..da85ea491bba46546534d20943077a0a24676487 100644 (file)
@@ -262,6 +262,10 @@ public:
   bool getDiagnosticsInGroup(StringRef Group,
                              llvm::SmallVectorImpl<diag::kind> &Diags) const;
 
+  /// \brief Get the warning option with the closest edit distance to the given
+  /// group name.
+  static StringRef getNearestWarningOption(StringRef Group);
+
 private:
   /// \brief Get the set of all diagnostic IDs in the given group.
   ///
index b14cdc9ba3da7b03afc72512a878f045093be180..39fee69adbef28b3ec7a109326a3b20b8a9dc0d4 100644 (file)
@@ -681,6 +681,27 @@ bool DiagnosticIDs::getDiagnosticsInGroup(
   return false;
 }
 
+StringRef DiagnosticIDs::getNearestWarningOption(StringRef Group) {
+  StringRef Best;
+  unsigned BestDistance = 0;
+  for (const WarningOption *i = OptionTable, *e = OptionTable + OptionTableSize;
+       i != e; ++i) {
+    // Don't suggest ignored warning flags.
+    if (!i->Members && !i->SubGroups)
+      continue;
+
+    unsigned Distance = i->getName().edit_distance(Group, true, BestDistance);
+
+    // Check if this is a better match.
+    if (Best.empty() || Distance < BestDistance) {
+      Best = i->getName();
+      BestDistance = Distance;
+    }
+  }
+
+  return Best;
+}
+
 /// ProcessDiag - This is the method used to report a diagnostic that is
 /// finally fully formed.
 bool DiagnosticIDs::ProcessDiag(DiagnosticsEngine &Diag) const {
index 7fcbe3acd4bb81121b77b6ad098dc4760d1066fd..ba0cd38045c68c6988582eedc1d6554dc402fa16 100644 (file)
@@ -120,7 +120,9 @@ void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
         } else if (DiagIDs->getDiagnosticsInGroup(Specifier, _Diags)) {
           Diags.Report(isPositive ? diag::warn_unknown_warning_option :
                        diag::warn_unknown_negative_warning_option)
-            << ("-W" + Opt.str());
+            << ("-W" + Opt.str())
+            << ("-Werror=" +
+                DiagnosticIDs::getNearestWarningOption(Specifier).str());
         }
         continue;
       }
@@ -150,7 +152,9 @@ void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
         } else if (DiagIDs->getDiagnosticsInGroup(Specifier, _Diags)) {
           Diags.Report(isPositive ? diag::warn_unknown_warning_option :
                        diag::warn_unknown_negative_warning_option)
-            << ("-W" + Opt.str());
+            << ("-W" + Opt.str())
+            << ("-Wfatal-errors=" +
+                DiagnosticIDs::getNearestWarningOption(Specifier).str());
         }
         continue;
       }
@@ -158,7 +162,8 @@ void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
       if (Report && DiagIDs->getDiagnosticsInGroup(Opt, _Diags)) {
         Diags.Report(isPositive ? diag::warn_unknown_warning_option :
                      diag::warn_unknown_negative_warning_option)
-          << ("-W" + Opt.str());
+          << ("-W" + Opt.str())
+          << ("-W" + DiagnosticIDs::getNearestWarningOption(Opt).str());
       } else {
         Diags.setDiagnosticGroupMapping(Opt, Mapping);
       }
index 2fdbe5133c5b20d0ef5e7ee029ffe64d2be9bd79..96b2ec3e5dd7ef53fa15b2614cda25aaf6330d06 100644 (file)
@@ -17,7 +17,7 @@ int f0() {}
 // CHECK:       <key>level</key>
 // CHECK:       <string>warning</string>
 // CHECK:       <key>message</key>
-// CHECK:       <string>unknown warning option &apos;-Wfoobar&apos;</string>
+// CHECK:       <string>unknown warning option &apos;-Wfoobar&apos;; did you mean &apos;-W{{.*}}&apos;?</string>
 // CHECK:     </dict>
 // CHECK:     <dict>
 // CHECK:       <key>level</key>