CommentCommandTraits.cpp 4.6 KB

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