Index: include/clang/Basic/DiagnosticDriverKinds.td =================================================================== --- include/clang/Basic/DiagnosticDriverKinds.td +++ include/clang/Basic/DiagnosticDriverKinds.td @@ -11,6 +11,8 @@ def err_drv_no_such_file : Error<"no such file or directory: '%0'">; def err_drv_unsupported_opt : Error<"unsupported option '%0'">; +def err_drv_unsupported_opt_with_suggestion + : Error<"unsupported option '%0', did you mean '%1'?">; def err_drv_unsupported_opt_for_target : Error< "unsupported option '%0' for target '%1'">; def err_drv_unsupported_option_argument : Error< @@ -135,9 +137,14 @@ def err_drv_I_dash_not_supported : Error< "'%0' not supported, please use -iquote instead">; def err_drv_unknown_argument : Error<"unknown argument: '%0'">; +def err_drv_unknown_argument_with_suggestion + : Error<"unknown argument '%0', did you mean '%1'?">; def warn_drv_unknown_argument_clang_cl : Warning< "unknown argument ignored in clang-cl: '%0'">, InGroup; +def warn_drv_unknown_argument_clang_cl_with_suggestion : Warning< + "unknown argument ignored in clang-cl '%0' (did you mean '%1'?)">, + InGroup; def warn_drv_ycyu_no_arg_clang_cl : Warning< "support for '%0' without a filename not implemented yet; flag ignored">, Index: lib/Driver/Driver.cpp =================================================================== --- lib/Driver/Driver.cpp +++ lib/Driver/Driver.cpp @@ -188,9 +188,19 @@ // Check for unsupported options. for (const Arg *A : Args) { if (A->getOption().hasFlag(options::Unsupported)) { - Diag(diag::err_drv_unsupported_opt) << A->getAsString(Args); - ContainsError |= Diags.getDiagnosticLevel(diag::err_drv_unsupported_opt, - SourceLocation()) > + unsigned DiagID; + auto ArgString = A->getAsString(Args); + std::string Nearest; + if (getOpts().findNearest( + ArgString, Nearest, IncludedFlagsBitmask, + ExcludedFlagsBitmask | options::Unsupported) > 1) { + DiagID = diag::err_drv_unsupported_opt; + Diag(DiagID) << ArgString; + } else { + DiagID = diag::err_drv_unsupported_opt_with_suggestion; + Diag(DiagID) << ArgString << Nearest; + } + ContainsError |= Diags.getDiagnosticLevel(DiagID, SourceLocation()) > DiagnosticsEngine::Warning; continue; } @@ -205,11 +215,20 @@ } for (const Arg *A : Args.filtered(options::OPT_UNKNOWN)) { - auto ID = IsCLMode() ? diag::warn_drv_unknown_argument_clang_cl - : diag::err_drv_unknown_argument; - - Diags.Report(ID) << A->getAsString(Args); - ContainsError |= Diags.getDiagnosticLevel(ID, SourceLocation()) > + unsigned DiagID; + auto ArgString = A->getAsString(Args); + std::string Nearest; + if (getOpts().findNearest( + ArgString, Nearest, IncludedFlagsBitmask, ExcludedFlagsBitmask) > 1) { + DiagID = IsCLMode() ? diag::warn_drv_unknown_argument_clang_cl + : diag::err_drv_unknown_argument; + Diags.Report(DiagID) << ArgString; + } else { + DiagID = IsCLMode() ? diag::warn_drv_unknown_argument_clang_cl_with_suggestion + : diag::err_drv_unknown_argument_with_suggestion; + Diags.Report(DiagID) << ArgString << Nearest; + } + ContainsError |= Diags.getDiagnosticLevel(DiagID, SourceLocation()) > DiagnosticsEngine::Warning; } Index: test/Driver/unknown-arg.c =================================================================== --- test/Driver/unknown-arg.c +++ test/Driver/unknown-arg.c @@ -1,9 +1,15 @@ // RUN: not %clang %s -cake-is-lie -%0 -%d -HHHH -munknown-to-clang-option -print-stats -funknown-to-clang-option -### 2>&1 | \ // RUN: FileCheck %s +// RUN: not %clang %s -stdlibs=foo -hell -### 2>&1 | \ +// RUN: FileCheck %s --check-prefix=DID-YOU-MEAN // RUN: %clang_cl -cake-is-lie -%0 -%d -HHHH -munknown-to-clang-option -print-stats -funknown-to-clang-option -### -c -- %s 2>&1 | \ // RUN: FileCheck %s --check-prefix=CL +// RUN: %clang_cl -Brepo -### -- %s 2>&1 | \ +// RUN: FileCheck %s --check-prefix=CL-DID-YOU-MEAN // RUN: not %clang_cl -cake-is-lie -%0 -%d -HHHH -munknown-to-clang-option -print-stats -funknown-to-clang-option -c -Werror=unknown-argument -### -- %s 2>&1 | \ // RUN: FileCheck %s --check-prefix=CL-ERROR +// RUN: not %clang_cl -helo -Werror=unknown-argument -### -- %s 2>&1 | \ +// RUN: FileCheck %s --check-prefix=CL-ERROR-DID-YOU-MEAN // RUN: %clang_cl -cake-is-lie -%0 -%d -HHHH -munknown-to-clang-option -print-stats -funknown-to-clang-option -c -Wno-unknown-argument -### -- %s 2>&1 | \ // RUN: FileCheck %s --check-prefix=SILENT @@ -14,6 +20,8 @@ // CHECK: error: unknown argument: '-munknown-to-clang-option' // CHECK: error: unknown argument: '-print-stats' // CHECK: error: unknown argument: '-funknown-to-clang-option' +// DID-YOU-MEAN: error: unknown argument '-stdlibs=foo', did you mean '-stdlib=foo'? +// DID-YOU-MEAN: error: unknown argument '-hell', did you mean '-help'? // CL: warning: unknown argument ignored in clang-cl: '-cake-is-lie' // CL: warning: unknown argument ignored in clang-cl: '-%0' // CL: warning: unknown argument ignored in clang-cl: '-%d' @@ -21,6 +29,7 @@ // CL: warning: unknown argument ignored in clang-cl: '-munknown-to-clang-option' // CL: warning: unknown argument ignored in clang-cl: '-print-stats' // CL: warning: unknown argument ignored in clang-cl: '-funknown-to-clang-option' +// CL-DID-YOU-MEAN: warning: unknown argument ignored in clang-cl '-Brepo' (did you mean '-Brepro'?) // CL-ERROR: error: unknown argument ignored in clang-cl: '-cake-is-lie' // CL-ERROR: error: unknown argument ignored in clang-cl: '-%0' // CL-ERROR: error: unknown argument ignored in clang-cl: '-%d' @@ -28,6 +37,7 @@ // CL-ERROR: error: unknown argument ignored in clang-cl: '-munknown-to-clang-option' // CL-ERROR: error: unknown argument ignored in clang-cl: '-print-stats' // CL-ERROR: error: unknown argument ignored in clang-cl: '-funknown-to-clang-option' +// CL-ERROR-DID-YOU-MEAN: error: unknown argument ignored in clang-cl '-helo' (did you mean '-help'?) // SILENT-NOT: error: // SILENT-NOT: warning: Index: test/Driver/unsupported-option.c =================================================================== --- /dev/null +++ test/Driver/unsupported-option.c @@ -0,0 +1,7 @@ +// RUN: not %clang %s --hedonism -### 2>&1 | \ +// RUN: FileCheck %s +// RUN: not %clang %s --hell -### 2>&1 | \ +// RUN: FileCheck %s --check-prefix=DID-YOU-MEAN + +// CHECK: error: unsupported option '--hedonism' +// DID-YOU-MEAN: error: unsupported option '--hell', did you mean '--help'?