本文整理汇总了C++中StringRef::edit_distance方法的典型用法代码示例。如果您正苦于以下问题:C++ StringRef::edit_distance方法的具体用法?C++ StringRef::edit_distance怎么用?C++ StringRef::edit_distance使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StringRef
的用法示例。
在下文中一共展示了StringRef::edit_distance方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getCallEditDistance
static unsigned getCallEditDistance(DeclName writtenName,
DeclName correctedName,
unsigned maxEditDistance) {
// TODO: consider arguments.
// TODO: maybe ignore certain kinds of missing / present labels for the
// first argument label?
// TODO: word-based rather than character-based?
if (writtenName.getBaseName().getKind() !=
correctedName.getBaseName().getKind()) {
return UnreasonableCallEditDistance;
}
if (writtenName.getBaseName().getKind() != DeclBaseName::Kind::Normal) {
return 0;
}
StringRef writtenBase = writtenName.getBaseName().userFacingName();
StringRef correctedBase = correctedName.getBaseName().userFacingName();
unsigned distance = writtenBase.edit_distance(correctedBase, maxEditDistance);
// Bound the distance to UnreasonableCallEditDistance.
if (distance >= maxEditDistance ||
distance > (correctedBase.size() + 2) / 3) {
return UnreasonableCallEditDistance;
}
return distance;
}
示例2: ComputeMatchDistance
unsigned Pattern::ComputeMatchDistance(StringRef Buffer,
const StringMap<StringRef> &VariableTable) const {
// Just compute the number of matching characters. For regular expressions, we
// just compare against the regex itself and hope for the best.
//
// FIXME: One easy improvement here is have the regex lib generate a single
// example regular expression which matches, and use that as the example
// string.
StringRef ExampleString(FixedStr);
if (ExampleString.empty())
ExampleString = RegExStr;
// Only compare up to the first line in the buffer, or the string size.
StringRef BufferPrefix = Buffer.substr(0, ExampleString.size());
BufferPrefix = BufferPrefix.split('\n').first;
return BufferPrefix.edit_distance(ExampleString);
}
示例3: getCallEditDistance
static unsigned getCallEditDistance(DeclName argName, DeclName paramName,
unsigned maxEditDistance) {
// TODO: consider arguments.
// TODO: maybe ignore certain kinds of missing / present labels for the
// first argument label?
// TODO: word-based rather than character-based?
StringRef argBase = argName.getBaseName().str();
StringRef paramBase = paramName.getBaseName().str();
unsigned distance = argBase.edit_distance(paramBase, maxEditDistance);
// Bound the distance to UnreasonableCallEditDistance.
if (distance >= maxEditDistance ||
distance > (paramBase.size() + 2) / 3) {
return UnreasonableCallEditDistance;
}
return distance;
}
示例4: if
static void
HelperTypoCorrectCommandInfo(SmallVectorImpl<const CommandInfo *> &BestCommand,
StringRef Typo, const CommandInfo *Command) {
const unsigned MaxEditDistance = 1;
unsigned BestEditDistance = MaxEditDistance + 1;
StringRef Name = Command->Name;
unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
if (MinPossibleEditDistance > 0 &&
Typo.size() / MinPossibleEditDistance < 1)
return;
unsigned EditDistance = Typo.edit_distance(Name, true, MaxEditDistance);
if (EditDistance > MaxEditDistance)
return;
if (EditDistance == BestEditDistance)
BestCommand.push_back(Command);
else if (EditDistance < BestEditDistance) {
BestCommand.clear();
BestCommand.push_back(Command);
BestEditDistance = EditDistance;
}
}
示例5: ModuleLoadResult
//.........这里部分代码省略.........
if (!Module) {
// If we loaded the module directly, without finding a module map first,
// we'll have loaded the module's information from the module itself.
Module = PP->getHeaderSearchInfo().getModuleMap()
.findModule((Path[0].first->getName()));
}
if (Module)
Module->setASTFile(ModuleFile);
// Cache the result of this top-level module lookup for later.
Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first;
}
// If we never found the module, fail.
if (!Module)
return ModuleLoadResult();
// Verify that the rest of the module path actually corresponds to
// a submodule.
if (Path.size() > 1) {
for (unsigned I = 1, N = Path.size(); I != N; ++I) {
StringRef Name = Path[I].first->getName();
clang::Module *Sub = Module->findSubmodule(Name);
if (!Sub) {
// Attempt to perform typo correction to find a module name that works.
llvm::SmallVector<StringRef, 2> Best;
unsigned BestEditDistance = (std::numeric_limits<unsigned>::max)();
for (clang::Module::submodule_iterator J = Module->submodule_begin(),
JEnd = Module->submodule_end();
J != JEnd; ++J) {
unsigned ED = Name.edit_distance((*J)->Name,
/*AllowReplacements=*/true,
BestEditDistance);
if (ED <= BestEditDistance) {
if (ED < BestEditDistance) {
Best.clear();
BestEditDistance = ED;
}
Best.push_back((*J)->Name);
}
}
// If there was a clear winner, user it.
if (Best.size() == 1) {
getDiagnostics().Report(Path[I].second,
diag::err_no_submodule_suggest)
<< Path[I].first << Module->getFullModuleName() << Best[0]
<< SourceRange(Path[0].second, Path[I-1].second)
<< FixItHint::CreateReplacement(SourceRange(Path[I].second),
Best[0]);
Sub = Module->findSubmodule(Best[0]);
}
}
if (!Sub) {
// No submodule by this name. Complain, and don't look for further
// submodules.
getDiagnostics().Report(Path[I].second, diag::err_no_submodule)
<< Path[I].first << Module->getFullModuleName()
<< SourceRange(Path[0].second, Path[I-1].second);
break;
示例6: HandleEndOfFile
/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
/// the current file. This either returns the EOF token or pops a level off
/// the include stack and keeps going.
bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
assert(!CurTokenLexer &&
"Ending a file when currently in a macro!");
// See if this file had a controlling macro.
if (CurPPLexer) { // Not ending a macro, ignore it.
if (const IdentifierInfo *ControllingMacro =
CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
// Okay, this has a controlling macro, remember in HeaderFileInfo.
if (const FileEntry *FE =
SourceMgr.getFileEntryForID(CurPPLexer->getFileID())) {
HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
if (MacroInfo *MI =
getMacroInfo(const_cast<IdentifierInfo*>(ControllingMacro))) {
MI->UsedForHeaderGuard = true;
}
if (const IdentifierInfo *DefinedMacro =
CurPPLexer->MIOpt.GetDefinedMacro()) {
if (!ControllingMacro->hasMacroDefinition() &&
DefinedMacro != ControllingMacro &&
HeaderInfo.FirstTimeLexingFile(FE)) {
// If the edit distance between the two macros is more than 50%,
// DefinedMacro may not be header guard, or can be header guard of
// another header file. Therefore, it maybe defining something
// completely different. This can be observed in the wild when
// handling feature macros or header guards in different files.
const StringRef ControllingMacroName = ControllingMacro->getName();
const StringRef DefinedMacroName = DefinedMacro->getName();
const size_t MaxHalfLength = std::max(ControllingMacroName.size(),
DefinedMacroName.size()) / 2;
const unsigned ED = ControllingMacroName.edit_distance(
DefinedMacroName, true, MaxHalfLength);
if (ED <= MaxHalfLength) {
// Emit a warning for a bad header guard.
Diag(CurPPLexer->MIOpt.GetMacroLocation(),
diag::warn_header_guard)
<< CurPPLexer->MIOpt.GetMacroLocation() << ControllingMacro;
Diag(CurPPLexer->MIOpt.GetDefinedLocation(),
diag::note_header_guard)
<< CurPPLexer->MIOpt.GetDefinedLocation() << DefinedMacro
<< ControllingMacro
<< FixItHint::CreateReplacement(
CurPPLexer->MIOpt.GetDefinedLocation(),
ControllingMacro->getName());
}
}
}
}
}
}
// Complain about reaching a true EOF within arc_cf_code_audited.
// We don't want to complain about reaching the end of a macro
// instantiation or a _Pragma.
if (PragmaARCCFCodeAuditedLoc.isValid() &&
!isEndOfMacro && !(CurLexer && CurLexer->Is_PragmaLexer)) {
Diag(PragmaARCCFCodeAuditedLoc, diag::err_pp_eof_in_arc_cf_code_audited);
// Recover by leaving immediately.
PragmaARCCFCodeAuditedLoc = SourceLocation();
}
// If this is a #include'd file, pop it off the include stack and continue
// lexing the #includer file.
if (!IncludeMacroStack.empty()) {
// If we lexed the code-completion file, act as if we reached EOF.
if (isCodeCompletionEnabled() && CurPPLexer &&
SourceMgr.getLocForStartOfFile(CurPPLexer->getFileID()) ==
CodeCompletionFileLoc) {
if (CurLexer) {
Result.startToken();
CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
CurLexer.reset();
} else {
assert(CurPTHLexer && "Got EOF but no current lexer set!");
CurPTHLexer->getEOF(Result);
CurPTHLexer.reset();
}
CurPPLexer = nullptr;
return true;
}
if (!isEndOfMacro && CurPPLexer &&
SourceMgr.getIncludeLoc(CurPPLexer->getFileID()).isValid()) {
// Notify SourceManager to record the number of FileIDs that were created
// during lexing of the #include'd file.
unsigned NumFIDs =
SourceMgr.local_sloc_entry_size() -
CurPPLexer->getInitialNumSLocEntries() + 1/*#include'd file*/;
SourceMgr.setNumCreatedFIDsForFileID(CurPPLexer->getFileID(), NumFIDs);
}
FileID ExitedFID;
//.........这里部分代码省略.........
示例7: HandleEndOfFile
/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
/// the current file. This either returns the EOF token or pops a level off
/// the include stack and keeps going.
bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
assert(!CurTokenLexer &&
"Ending a file when currently in a macro!");
// If we have an unclosed module region from a pragma at the end of a
// module, complain and close it now.
// FIXME: This is not correct if we are building a module from PTH.
const bool LeavingSubmodule = CurLexer && CurLexerSubmodule;
if ((LeavingSubmodule || IncludeMacroStack.empty()) &&
!BuildingSubmoduleStack.empty() &&
BuildingSubmoduleStack.back().IsPragma) {
Diag(BuildingSubmoduleStack.back().ImportLoc,
diag::err_pp_module_begin_without_module_end);
Module *M = LeaveSubmodule(/*ForPragma*/true);
Result.startToken();
const char *EndPos = getCurLexerEndPos();
CurLexer->BufferPtr = EndPos;
CurLexer->FormTokenWithChars(Result, EndPos, tok::annot_module_end);
Result.setAnnotationEndLoc(Result.getLocation());
Result.setAnnotationValue(M);
return true;
}
// See if this file had a controlling macro.
if (CurPPLexer) { // Not ending a macro, ignore it.
if (const IdentifierInfo *ControllingMacro =
CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
// Okay, this has a controlling macro, remember in HeaderFileInfo.
if (const FileEntry *FE = CurPPLexer->getFileEntry()) {
HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
if (MacroInfo *MI =
getMacroInfo(const_cast<IdentifierInfo*>(ControllingMacro)))
MI->setUsedForHeaderGuard(true);
if (const IdentifierInfo *DefinedMacro =
CurPPLexer->MIOpt.GetDefinedMacro()) {
if (!isMacroDefined(ControllingMacro) &&
DefinedMacro != ControllingMacro &&
HeaderInfo.FirstTimeLexingFile(FE)) {
// If the edit distance between the two macros is more than 50%,
// DefinedMacro may not be header guard, or can be header guard of
// another header file. Therefore, it maybe defining something
// completely different. This can be observed in the wild when
// handling feature macros or header guards in different files.
const StringRef ControllingMacroName = ControllingMacro->getName();
const StringRef DefinedMacroName = DefinedMacro->getName();
const size_t MaxHalfLength = std::max(ControllingMacroName.size(),
DefinedMacroName.size()) / 2;
const unsigned ED = ControllingMacroName.edit_distance(
DefinedMacroName, true, MaxHalfLength);
if (ED <= MaxHalfLength) {
// Emit a warning for a bad header guard.
Diag(CurPPLexer->MIOpt.GetMacroLocation(),
diag::warn_header_guard)
<< CurPPLexer->MIOpt.GetMacroLocation() << ControllingMacro;
Diag(CurPPLexer->MIOpt.GetDefinedLocation(),
diag::note_header_guard)
<< CurPPLexer->MIOpt.GetDefinedLocation() << DefinedMacro
<< ControllingMacro
<< FixItHint::CreateReplacement(
CurPPLexer->MIOpt.GetDefinedLocation(),
ControllingMacro->getName());
}
}
}
}
}
}
// Complain about reaching a true EOF within arc_cf_code_audited.
// We don't want to complain about reaching the end of a macro
// instantiation or a _Pragma.
if (PragmaARCCFCodeAuditedLoc.isValid() &&
!isEndOfMacro && !(CurLexer && CurLexer->Is_PragmaLexer)) {
Diag(PragmaARCCFCodeAuditedLoc, diag::err_pp_eof_in_arc_cf_code_audited);
// Recover by leaving immediately.
PragmaARCCFCodeAuditedLoc = SourceLocation();
}
// Complain about reaching a true EOF within assume_nonnull.
// We don't want to complain about reaching the end of a macro
// instantiation or a _Pragma.
if (PragmaAssumeNonNullLoc.isValid() &&
!isEndOfMacro && !(CurLexer && CurLexer->Is_PragmaLexer)) {
Diag(PragmaAssumeNonNullLoc, diag::err_pp_eof_in_assume_nonnull);
// Recover by leaving immediately.
PragmaAssumeNonNullLoc = SourceLocation();
}
bool LeavingPCHThroughHeader = false;
// If this is a #include'd file, pop it off the include stack and continue
// lexing the #includer file.
//.........这里部分代码省略.........