1 //===--- CommentCommandTraits.cpp - Comment command properties --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/AST/CommentCommandTraits.h"
11 #include "llvm/ADT/STLExtras.h"
16 #include "clang/AST/CommentCommandInfo.inc"
18 CommandTraits::CommandTraits(llvm::BumpPtrAllocator &Allocator,
19 const CommentOptions &CommentOptions) :
20 NextID(llvm::array_lengthof(Commands)), Allocator(Allocator) {
21 registerCommentOptions(CommentOptions);
24 void CommandTraits::registerCommentOptions(
25 const CommentOptions &CommentOptions) {
26 for (CommentOptions::BlockCommandNamesTy::const_iterator
27 I = CommentOptions.BlockCommandNames.begin(),
28 E = CommentOptions.BlockCommandNames.end();
30 registerBlockCommand(*I);
34 const CommandInfo *CommandTraits::getCommandInfoOrNULL(StringRef Name) const {
35 if (const CommandInfo *Info = getBuiltinCommandInfo(Name))
37 return getRegisteredCommandInfo(Name);
40 const CommandInfo *CommandTraits::getCommandInfo(unsigned CommandID) const {
41 if (const CommandInfo *Info = getBuiltinCommandInfo(CommandID))
43 return getRegisteredCommandInfo(CommandID);
46 CommandInfo *CommandTraits::createCommandInfoWithName(StringRef CommandName) {
47 char *Name = Allocator.Allocate<char>(CommandName.size() + 1);
48 memcpy(Name, CommandName.data(), CommandName.size());
49 Name[CommandName.size()] = '\0';
51 // Value-initialize (=zero-initialize in this case) a new CommandInfo.
52 CommandInfo *Info = new (Allocator) CommandInfo();
56 RegisteredCommands.push_back(Info);
61 const CommandInfo *CommandTraits::registerUnknownCommand(
62 StringRef CommandName) {
63 CommandInfo *Info = createCommandInfoWithName(CommandName);
64 Info->IsUnknownCommand = true;
68 const CommandInfo *CommandTraits::registerBlockCommand(StringRef CommandName) {
69 CommandInfo *Info = createCommandInfoWithName(CommandName);
70 Info->IsBlockCommand = true;
74 const CommandInfo *CommandTraits::getBuiltinCommandInfo(
76 if (CommandID < llvm::array_lengthof(Commands))
77 return &Commands[CommandID];
81 const CommandInfo *CommandTraits::getRegisteredCommandInfo(
82 StringRef Name) const {
83 for (unsigned i = 0, e = RegisteredCommands.size(); i != e; ++i) {
84 if (RegisteredCommands[i]->Name == Name)
85 return RegisteredCommands[i];
90 const CommandInfo *CommandTraits::getRegisteredCommandInfo(
91 unsigned CommandID) const {
92 return RegisteredCommands[CommandID - llvm::array_lengthof(Commands)];
95 } // end namespace comments
96 } // end namespace clang