This is an archive of the discontinued LLVM Phabricator instance.

[Basic] Emit warning flag suggestion only in case there's existing flag *similar* to the unknown one
Needs ReviewPublic

Authored by jkorous on Jul 24 2018, 8:23 AM.

Details

Summary

In case user specified non-existent warning flag clang was suggesting an existing on that was in some cased very different. This patch should ensure that either a relevant warning flag is suggested or none (which is in line with unknown generic command line option handling).

Examples:
warning: unknown warning option '-Whiskey'; did you mean '-Wpacked'?
warning: unknown warning option '-Weverthin'; did you mean '-Wsection'?

rdar://problem/37755482

I changed interface of DiagnosticIDs::getNearestOption() to return the edit distance between nonexistent flag and the closest exiting one. In principle this code is analogous to OptTable::findNearest() for generic command line options but I don't find them similar enough in order to factor out some shared implementation. I tentatively selected maximum edit distance as 2 (CompilerInvocation::CreateFromArgs() uses maximum edit distance of 1 for generic case).

Diff Detail

Repository
rC Clang

Event Timeline

jkorous created this revision.Jul 24 2018, 8:23 AM
jkorous retitled this revision from [Basic] Emit warning flag suggestion only in case there's *similar* existing flag for given unknown one to [Basic] Emit warning flag suggestion only in case there's existing flag *similar* to the unknown one.Jul 24 2018, 8:24 AM

Thanks for working on this!
It looks like we only have one client of getNearestOption. Wouldn't it make sense to fold the check into the function itself?

I like that idea! It looks like it's living in a wrong place anyway as it doesn't need access to any of implementation details (private members) of DiagnosticID. I would still like to preserve it as a function so this block of code has clear semantics and interface.
How about I refactor it to a static free function in Warnings.cpp?

jkorous planned changes to this revision.Aug 10 2018, 9:40 AM
jkorous requested review of this revision.Nov 6 2018, 12:22 PM

I tried to move the getNearestOption() to it's only client - EmitUnknownDiagWarning() but it turned out to be a significant change because of clang/Basic/DiagnosticGroups.inc use in DiagnosticIDs.cpp.
I suggest we leave that for eventual future refactoring since it would blow up scope of this patch a lot.

Thanks for working on this, some minor comments:

Basic/DiagnosticIDs.cpp
612

Do we a test case that covers this line?

Basic/Warnings.cpp
34

It would be helpful to have a brief comment explaining the rationale behind using the number 2.

35

NIT: Missing //end anonymous namespace.

48

Is it ever possible to have an edit distance of 0? Wouldn't it mean the strings are the same?

Have you considered the same approach as typo correction? I.e. for the max allowed edit distance use percentage of the input size. For example, something similar to [TypoCorrectionConsumer::addName](https://github.com/apple/swift-clang/blob/4d16b315bc18647ca6715b7b4ea95a26a68fb5ed/lib/Sema/SemaLookup.cpp#L3992-L4008).

bruno resigned from this revision.Nov 9 2020, 12:34 PM