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 
18 CommandTraits::CommandTraits(llvm::BumpPtrAllocator &Allocator) :
19     NextID(llvm::array_lengthof(Commands)), Allocator(Allocator)
20 { }
21 
22 const CommandInfo *CommandTraits::getCommandInfoOrNULL(StringRef Name) const {
23   if (const CommandInfo *Info = getBuiltinCommandInfo(Name))
24     return Info;
25   return getRegisteredCommandInfo(Name);
26 }
27 
28 const CommandInfo *CommandTraits::getCommandInfo(unsigned CommandID) const {
29   if (const CommandInfo *Info = getBuiltinCommandInfo(CommandID))
30     return Info;
31   return getRegisteredCommandInfo(CommandID);
32 }
33 
34 const CommandInfo *CommandTraits::registerUnknownCommand(StringRef CommandName) {
35   char *Name = Allocator.Allocate<char>(CommandName.size() + 1);
36   memcpy(Name, CommandName.data(), CommandName.size());
37   Name[CommandName.size()] = '\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   Info->IsUnknownCommand = true;
44 
45   RegisteredCommands.push_back(Info);
46 
47   return Info;
48 }
49 
50 const CommandInfo *CommandTraits::getBuiltinCommandInfo(
51                                                   unsigned CommandID) {
52   if (CommandID < llvm::array_lengthof(Commands))
53     return &Commands[CommandID];
54   return NULL;
55 }
56 
57 const CommandInfo *CommandTraits::getRegisteredCommandInfo(
58                                                   StringRef Name) const {
59   for (unsigned i = 0, e = RegisteredCommands.size(); i != e; ++i) {
60     if (RegisteredCommands[i]->Name == Name)
61       return RegisteredCommands[i];
62   }
63   return NULL;
64 }
65 
66 const CommandInfo *CommandTraits::getRegisteredCommandInfo(
67                                                   unsigned CommandID) const {
68   return RegisteredCommands[CommandID - llvm::array_lengthof(Commands)];
69 }
70 
71 } // end namespace comments
72 } // end namespace clang
73 
74