• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
10 #include "clang/AST/CommentCommandTraits.h"
11 #include "llvm/ADT/STLExtras.h"
12 
13 namespace clang {
14 namespace comments {
15 
16 #include "clang/AST/CommentCommandInfo.inc"
17 
CommandTraits(llvm::BumpPtrAllocator & Allocator)18 CommandTraits::CommandTraits(llvm::BumpPtrAllocator &Allocator) :
19     NextID(llvm::array_lengthof(Commands)), Allocator(Allocator)
20 { }
21 
getCommandInfoOrNULL(StringRef Name) const22 const CommandInfo *CommandTraits::getCommandInfoOrNULL(StringRef Name) const {
23   if (const CommandInfo *Info = getBuiltinCommandInfo(Name))
24     return Info;
25   return getRegisteredCommandInfo(Name);
26 }
27 
getCommandInfo(unsigned CommandID) const28 const CommandInfo *CommandTraits::getCommandInfo(unsigned CommandID) const {
29   if (const CommandInfo *Info = getBuiltinCommandInfo(CommandID))
30     return Info;
31   return getRegisteredCommandInfo(CommandID);
32 }
33 
registerUnknownCommand(StringRef CommandName)34 const CommandInfo *CommandTraits::registerUnknownCommand(StringRef CommandName) {
35   char *Name = Allocator.Allocate<char>(CommandName.size());
36   memcpy(Name, CommandName.data(), CommandName.size());
37   Name[CommandName.size() + 1] = '\0';
38 
39   // Value-initialize (=zero-initialize in this case) a new CommandInfo.
40   CommandInfo *Info = new (Allocator) CommandInfo();
41   Info->Name = Name;
42   Info->ID = NextID++;
43 
44   RegisteredCommands.push_back(Info);
45 
46   return Info;
47 }
48 
getBuiltinCommandInfo(unsigned CommandID)49 const CommandInfo *CommandTraits::getBuiltinCommandInfo(
50                                                   unsigned CommandID) {
51   if (CommandID < llvm::array_lengthof(Commands))
52     return &Commands[CommandID];
53   return NULL;
54 }
55 
getRegisteredCommandInfo(StringRef Name) const56 const CommandInfo *CommandTraits::getRegisteredCommandInfo(
57                                                   StringRef Name) const {
58   for (unsigned i = 0, e = RegisteredCommands.size(); i != e; ++i) {
59     if (RegisteredCommands[i]->Name == Name)
60       return RegisteredCommands[i];
61   }
62   return NULL;
63 }
64 
getRegisteredCommandInfo(unsigned CommandID) const65 const CommandInfo *CommandTraits::getRegisteredCommandInfo(
66                                                   unsigned CommandID) const {
67   return RegisteredCommands[CommandID - llvm::array_lengthof(Commands)];
68 }
69 
70 } // end namespace comments
71 } // end namespace clang
72 
73