CommentCommandTraits.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //===--- CommentCommandTraits.cpp - Comment command properties --*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "clang/AST/CommentCommandTraits.h"
  9. #include "llvm/ADT/STLExtras.h"
  10. #include <cassert>
  11. namespace clang {
  12. namespace comments {
  13. #include "clang/AST/CommentCommandInfo.inc"
  14. CommandTraits::CommandTraits(llvm::BumpPtrAllocator &Allocator,
  15. const CommentOptions &CommentOptions)
  16. : NextID(std::size(Commands)), Allocator(Allocator) {
  17. registerCommentOptions(CommentOptions);
  18. }
  19. void CommandTraits::registerCommentOptions(
  20. const CommentOptions &CommentOptions) {
  21. for (CommentOptions::BlockCommandNamesTy::const_iterator
  22. I = CommentOptions.BlockCommandNames.begin(),
  23. E = CommentOptions.BlockCommandNames.end();
  24. I != E; I++) {
  25. registerBlockCommand(*I);
  26. }
  27. }
  28. const CommandInfo *CommandTraits::getCommandInfoOrNULL(StringRef Name) const {
  29. if (const CommandInfo *Info = getBuiltinCommandInfo(Name))
  30. return Info;
  31. return getRegisteredCommandInfo(Name);
  32. }
  33. const CommandInfo *CommandTraits::getCommandInfo(unsigned CommandID) const {
  34. if (const CommandInfo *Info = getBuiltinCommandInfo(CommandID))
  35. return Info;
  36. return getRegisteredCommandInfo(CommandID);
  37. }
  38. const CommandInfo *
  39. CommandTraits::getTypoCorrectCommandInfo(StringRef Typo) const {
  40. // Single-character command impostures, such as \t or \n, should not go
  41. // through the fixit logic.
  42. if (Typo.size() <= 1)
  43. return nullptr;
  44. // The maximum edit distance we're prepared to accept.
  45. const unsigned MaxEditDistance = 1;
  46. unsigned BestEditDistance = MaxEditDistance;
  47. SmallVector<const CommandInfo *, 2> BestCommand;
  48. auto ConsiderCorrection = [&](const CommandInfo *Command) {
  49. StringRef Name = Command->Name;
  50. unsigned MinPossibleEditDistance = abs((int)Name.size() - (int)Typo.size());
  51. if (MinPossibleEditDistance <= BestEditDistance) {
  52. unsigned EditDistance = Typo.edit_distance(Name, true, BestEditDistance);
  53. if (EditDistance < BestEditDistance) {
  54. BestEditDistance = EditDistance;
  55. BestCommand.clear();
  56. }
  57. if (EditDistance == BestEditDistance)
  58. BestCommand.push_back(Command);
  59. }
  60. };
  61. for (const auto &Command : Commands)
  62. ConsiderCorrection(&Command);
  63. for (const auto *Command : RegisteredCommands)
  64. if (!Command->IsUnknownCommand)
  65. ConsiderCorrection(Command);
  66. return BestCommand.size() == 1 ? BestCommand[0] : nullptr;
  67. }
  68. CommandInfo *CommandTraits::createCommandInfoWithName(StringRef CommandName) {
  69. char *Name = Allocator.Allocate<char>(CommandName.size() + 1);
  70. memcpy(Name, CommandName.data(), CommandName.size());
  71. Name[CommandName.size()] = '\0';
  72. // Value-initialize (=zero-initialize in this case) a new CommandInfo.
  73. CommandInfo *Info = new (Allocator) CommandInfo();
  74. Info->Name = Name;
  75. // We only have a limited number of bits to encode command IDs in the
  76. // CommandInfo structure, so the ID numbers can potentially wrap around.
  77. assert((NextID < (1 << CommandInfo::NumCommandIDBits))
  78. && "Too many commands. We have limited bits for the command ID.");
  79. Info->ID = NextID++;
  80. RegisteredCommands.push_back(Info);
  81. return Info;
  82. }
  83. const CommandInfo *CommandTraits::registerUnknownCommand(
  84. StringRef CommandName) {
  85. CommandInfo *Info = createCommandInfoWithName(CommandName);
  86. Info->IsUnknownCommand = true;
  87. return Info;
  88. }
  89. const CommandInfo *CommandTraits::registerBlockCommand(StringRef CommandName) {
  90. CommandInfo *Info = createCommandInfoWithName(CommandName);
  91. Info->IsBlockCommand = true;
  92. return Info;
  93. }
  94. const CommandInfo *CommandTraits::getBuiltinCommandInfo(
  95. unsigned CommandID) {
  96. if (CommandID < std::size(Commands))
  97. return &Commands[CommandID];
  98. return nullptr;
  99. }
  100. const CommandInfo *CommandTraits::getRegisteredCommandInfo(
  101. StringRef Name) const {
  102. for (unsigned i = 0, e = RegisteredCommands.size(); i != e; ++i) {
  103. if (RegisteredCommands[i]->Name == Name)
  104. return RegisteredCommands[i];
  105. }
  106. return nullptr;
  107. }
  108. const CommandInfo *CommandTraits::getRegisteredCommandInfo(
  109. unsigned CommandID) const {
  110. return RegisteredCommands[CommandID - std::size(Commands)];
  111. }
  112. } // end namespace comments
  113. } // end namespace clang