diff options
author | Fariborz Jahanian <fjahanian@apple.com> | 2013-05-08 19:21:00 +0000 |
---|---|---|
committer | Fariborz Jahanian <fjahanian@apple.com> | 2013-05-08 19:21:00 +0000 |
commit | 6c7a16666dbba49b9ef41c28ff7cbddf3e250213 (patch) | |
tree | 120c8544a57738c87929364c3b884fe273f5c9c8 /clang/lib | |
parent | 53caf563a9934088d39b008bd9b0de6adcc003c7 (diff) | |
download | bcm5719-llvm-6c7a16666dbba49b9ef41c28ff7cbddf3e250213.tar.gz bcm5719-llvm-6c7a16666dbba49b9ef41c28ff7cbddf3e250213.zip |
documentation parsing. Patch to do typo correction for
documentation commands. Patch was reviewed, along with
great suggestions for improvement, by Doug.
// rdar://12381408
llvm-svn: 181458
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/AST/CommentCommandTraits.cpp | 27 | ||||
-rw-r--r-- | clang/lib/AST/CommentLexer.cpp | 14 |
2 files changed, 39 insertions, 2 deletions
diff --git a/clang/lib/AST/CommentCommandTraits.cpp b/clang/lib/AST/CommentCommandTraits.cpp index e24d542c962..e4cc84afb78 100644 --- a/clang/lib/AST/CommentCommandTraits.cpp +++ b/clang/lib/AST/CommentCommandTraits.cpp @@ -43,6 +43,33 @@ const CommandInfo *CommandTraits::getCommandInfo(unsigned CommandID) const { return getRegisteredCommandInfo(CommandID); } +const CommandInfo * +CommandTraits::getTypoCorrectCommandInfo(StringRef Typo) const { + const unsigned MaxEditDistance = 1; + unsigned BestEditDistance = MaxEditDistance + 1; + SmallVector<const CommandInfo *, 2> BestCommand; + + int NumOfCommands = sizeof(Commands) / sizeof(CommandInfo); + for (int i = 0; i < NumOfCommands; i++) { + StringRef Name = Commands[i].Name; + unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size()); + if (MinPossibleEditDistance > 0 && + Typo.size() / MinPossibleEditDistance < 1) + continue; + unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance); + if (EditDistance > MaxEditDistance) + continue; + if (EditDistance == BestEditDistance) + BestCommand.push_back(&Commands[i]); + else if (EditDistance < BestEditDistance) { + BestCommand.clear(); + BestCommand.push_back(&Commands[i]); + BestEditDistance = EditDistance; + } + } + return (BestCommand.size() != 1) ? NULL : BestCommand[0]; +} + CommandInfo *CommandTraits::createCommandInfoWithName(StringRef CommandName) { char *Name = Allocator.Allocate<char>(CommandName.size() + 1); memcpy(Name, CommandName.data(), CommandName.size()); diff --git a/clang/lib/AST/CommentLexer.cpp b/clang/lib/AST/CommentLexer.cpp index 70410d61085..a59badaeaf1 100644 --- a/clang/lib/AST/CommentLexer.cpp +++ b/clang/lib/AST/CommentLexer.cpp @@ -265,6 +265,7 @@ const char *findCCommentEnd(const char *BufferPtr, const char *BufferEnd) { } llvm_unreachable("buffer end hit before '*/' was seen"); } + } // unnamed namespace void Lexer::lexCommentText(Token &T) { @@ -354,8 +355,17 @@ void Lexer::lexCommentText(Token &T) { if (!Info) { formTokenWithChars(T, TokenPtr, tok::unknown_command); T.setUnknownCommandName(CommandName); - Diag(T.getLocation(), diag::warn_unknown_comment_command_name); - return; + if (Info = Traits.getTypoCorrectCommandInfo(CommandName)) { + StringRef CorrectedName = Info->Name; + SourceRange CommandRange(T.getLocation().getLocWithOffset(1), + T.getEndLocation()); + Diag(T.getLocation(), diag::warn_correct_comment_command_name) + << CommandName << CorrectedName + << FixItHint::CreateReplacement(CommandRange, CorrectedName); + } else { + Diag(T.getLocation(), diag::warn_unknown_comment_command_name); + return; + } } if (Info->IsVerbatimBlockCommand) { setupAndLexVerbatimBlock(T, TokenPtr, *BufferPtr, Info); |