CommentCommandTraits.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //===--- CommentCommandTraits.cpp - Comment command properties --*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "clang/AST/CommentCommandTraits.h"
  10. #include "llvm/ADT/STLExtras.h"
  11. namespace clang {
  12. namespace comments {
  13. #include "clang/AST/CommentCommandInfo.inc"
  14. CommandTraits::CommandTraits(llvm::BumpPtrAllocator &Allocator) :
  15. NextID(llvm::array_lengthof(Commands)), Allocator(Allocator)
  16. { }
  17. const CommandInfo *CommandTraits::getCommandInfoOrNULL(StringRef Name) const {
  18. if (const CommandInfo *Info = getBuiltinCommandInfo(Name))
  19. return Info;
  20. return getRegisteredCommandInfo(Name);
  21. }
  22. const CommandInfo *CommandTraits::getCommandInfo(unsigned CommandID) const {
  23. if (const CommandInfo *Info = getBuiltinCommandInfo(CommandID))
  24. return Info;
  25. return getRegisteredCommandInfo(CommandID);
  26. }
  27. const CommandInfo *CommandTraits::registerUnknownCommand(StringRef CommandName) {
  28. char *Name = Allocator.Allocate<char>(CommandName.size() + 1);
  29. memcpy(Name, CommandName.data(), CommandName.size());
  30. Name[CommandName.size()] = '\0';
  31. // Value-initialize (=zero-initialize in this case) a new CommandInfo.
  32. CommandInfo *Info = new (Allocator) CommandInfo();
  33. Info->Name = Name;
  34. Info->ID = NextID++;
  35. Info->IsUnknownCommand = true;
  36. RegisteredCommands.push_back(Info);
  37. return Info;
  38. }
  39. const CommandInfo *CommandTraits::getBuiltinCommandInfo(
  40. unsigned CommandID) {
  41. if (CommandID < llvm::array_lengthof(Commands))
  42. return &Commands[CommandID];
  43. return NULL;
  44. }
  45. const CommandInfo *CommandTraits::getRegisteredCommandInfo(
  46. StringRef Name) const {
  47. for (unsigned i = 0, e = RegisteredCommands.size(); i != e; ++i) {
  48. if (RegisteredCommands[i]->Name == Name)
  49. return RegisteredCommands[i];
  50. }
  51. return NULL;
  52. }
  53. const CommandInfo *CommandTraits::getRegisteredCommandInfo(
  54. unsigned CommandID) const {
  55. return RegisteredCommands[CommandID - llvm::array_lengthof(Commands)];
  56. }
  57. } // end namespace comments
  58. } // end namespace clang