//===--- CommentCommandTraits.cpp - Comment command properties --*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include "clang/AST/CommentCommandTraits.h" #include "llvm/ADT/STLExtras.h" #include namespace clang { namespace comments { #include "clang/AST/CommentCommandInfo.inc" CommandTraits::CommandTraits(llvm::BumpPtrAllocator &Allocator, const CommentOptions &CommentOptions) : NextID(llvm::array_lengthof(Commands)), Allocator(Allocator) { registerCommentOptions(CommentOptions); } void CommandTraits::registerCommentOptions( const CommentOptions &CommentOptions) { for (CommentOptions::BlockCommandNamesTy::const_iterator I = CommentOptions.BlockCommandNames.begin(), E = CommentOptions.BlockCommandNames.end(); I != E; I++) { registerBlockCommand(*I); } } const CommandInfo *CommandTraits::getCommandInfoOrNULL(StringRef Name) const { if (const CommandInfo *Info = getBuiltinCommandInfo(Name)) return Info; return getRegisteredCommandInfo(Name); } const CommandInfo *CommandTraits::getCommandInfo(unsigned CommandID) const { if (const CommandInfo *Info = getBuiltinCommandInfo(CommandID)) return Info; return getRegisteredCommandInfo(CommandID); } const CommandInfo * CommandTraits::getTypoCorrectCommandInfo(StringRef Typo) const { // Single-character command impostures, such as \t or \n, should not go // through the fixit logic. if (Typo.size() <= 1) return nullptr; // The maximum edit distance we're prepared to accept. const unsigned MaxEditDistance = 1; unsigned BestEditDistance = MaxEditDistance; SmallVector BestCommand; auto ConsiderCorrection = [&](const CommandInfo *Command) { StringRef Name = Command->Name; unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size()); if (MinPossibleEditDistance <= BestEditDistance) { unsigned EditDistance = Typo.edit_distance(Name, true, BestEditDistance); if (EditDistance < BestEditDistance) { BestEditDistance = EditDistance; BestCommand.clear(); } if (EditDistance == BestEditDistance) BestCommand.push_back(Command); } }; for (const auto &Command : Commands) ConsiderCorrection(&Command); for (const auto *Command : RegisteredCommands) if (!Command->IsUnknownCommand) ConsiderCorrection(Command); return BestCommand.size() == 1 ? BestCommand[0] : nullptr; } CommandInfo *CommandTraits::createCommandInfoWithName(StringRef CommandName) { char *Name = Allocator.Allocate(CommandName.size() + 1); memcpy(Name, CommandName.data(), CommandName.size()); Name[CommandName.size()] = '\0'; // Value-initialize (=zero-initialize in this case) a new CommandInfo. CommandInfo *Info = new (Allocator) CommandInfo(); Info->Name = Name; // We only have a limited number of bits to encode command IDs in the // CommandInfo structure, so the ID numbers can potentially wrap around. assert((NextID < (1 << CommandInfo::NumCommandIDBits)) && "Too many commands. We have limited bits for the command ID."); Info->ID = NextID++; RegisteredCommands.push_back(Info); return Info; } const CommandInfo *CommandTraits::registerUnknownCommand( StringRef CommandName) { CommandInfo *Info = createCommandInfoWithName(CommandName); Info->IsUnknownCommand = true; return Info; } const CommandInfo *CommandTraits::registerBlockCommand(StringRef CommandName) { CommandInfo *Info = createCommandInfoWithName(CommandName); Info->IsBlockCommand = true; return Info; } const CommandInfo *CommandTraits::getBuiltinCommandInfo( unsigned CommandID) { if (CommandID < llvm::array_lengthof(Commands)) return &Commands[CommandID]; return nullptr; } const CommandInfo *CommandTraits::getRegisteredCommandInfo( StringRef Name) const { for (unsigned i = 0, e = RegisteredCommands.size(); i != e; ++i) { if (RegisteredCommands[i]->Name == Name) return RegisteredCommands[i]; } return nullptr; } const CommandInfo *CommandTraits::getRegisteredCommandInfo( unsigned CommandID) const { return RegisteredCommands[CommandID - llvm::array_lengthof(Commands)]; } } // end namespace comments } // end namespace clang 7c1594f145c931049e1fd9eb056a5987e87e59'>Move UCB-licensed code from 4-clause to 3-clause licence.agc 2002-11-11Fix signed/unsigned comparison warnings.thorpej 2002-06-19Make sure that we free memory and return null if we failed to initialize.christos 2001-12-10Fix off by one bug in t_agetstr - the pointer is moved to one past theblymn 2001-12-02stop t_freent freeing the same memory all the time.blymn 2001-11-05make comment reflect reality (no functional changes)christos 2001-11-02PR/10266: Jason R. Thorpe: curses programs totally broken.christos 2001-10-31PR/10266: t_getstr() leaks memory. This PR will stay in feedbackchristos 2001-01-29- use MAXPATHLEN to get the array size for the path we store.christos 2001-01-09sprinkle some _DIAGASSERT()s inlukem 2000-06-03* Improve the handling of BC and UP in t_goto, t_getent now queriesblymn 2000-06-02Back out previous change. It causes all sorts of problems. Thethorpej 2000-06-02Don't leak memory.christos 2000-06-02correct memory leak due to t_getstr() and realloc(). the commit willitojun 2000-06-01size arg doesn't exist anymore, so don't DIAGASSERT itlukem 2000-05-28* Fixed Makefile to proper set includes pathblymn 2000-05-20* Removed variable names from ansi style prototypeblymn 2000-05-14area is allowed to be NULL, so don't _DIAGASSERT it.lukem 2000-05-12- use strchr instead of indexchristos 2000-05-08* Modified t_getent to ignore the TERMCAP env variable if it contains theblymn 2000-04-20Fixed t_getstr so that limit is only set to 0 iff area is NULL when anblymn 2000-04-19Ensure limit is sane on return from t_getstr if requested entry doesblymn 2000-04-19Added new function t_getterm to return the name string of a termcapblymn 2000-04-18Split private data structure into separate file.blymn 1999-09-20back out the #ifdef _DIAGNOSTIC argument checks; too many people complained.lukem 1999-09-16* use _DIAGASSERT() to check pointer arguments against NULL and filelukem 1999-08-17Added minor tweak to t_getstr, by passing a NULL area pointer the sizeblymn 1999-08-16Updated library minor version and fixed small type glitch (limit is nowblymn 1999-08-15Added new interface to termcap that allows the manipulation of multipleblymn 1999-07-02More trailing white space.simonb 1999-03-22If we have to truncate the entry, try hard to truncate on a whole cap.abs 1998-10-14Clean up lint - one set but unused variable, 3 FALLTHROUGH comments needed,agc 1998-07-27const poisoning.mycroft