blob: e24d542c9623535f46db3d7133bc7bdebd318e26 [file] [log] [blame]
Dmitri Gribenkoaa580812012-08-09 00:03:17 +00001//===--- 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"
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000011#include "llvm/ADT/STLExtras.h"
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000012
13namespace clang {
14namespace comments {
15
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000016#include "clang/AST/CommentCommandInfo.inc"
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000017
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +000018CommandTraits::CommandTraits(llvm::BumpPtrAllocator &Allocator,
19 const CommentOptions &CommentOptions) :
20 NextID(llvm::array_lengthof(Commands)), Allocator(Allocator) {
21 registerCommentOptions(CommentOptions);
22}
23
24void CommandTraits::registerCommentOptions(
25 const CommentOptions &CommentOptions) {
26 for (CommentOptions::BlockCommandNamesTy::const_iterator
27 I = CommentOptions.BlockCommandNames.begin(),
28 E = CommentOptions.BlockCommandNames.end();
29 I != E; I++) {
30 registerBlockCommand(*I);
31 }
32}
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000033
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000034const CommandInfo *CommandTraits::getCommandInfoOrNULL(StringRef Name) const {
35 if (const CommandInfo *Info = getBuiltinCommandInfo(Name))
36 return Info;
37 return getRegisteredCommandInfo(Name);
38}
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000039
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000040const CommandInfo *CommandTraits::getCommandInfo(unsigned CommandID) const {
41 if (const CommandInfo *Info = getBuiltinCommandInfo(CommandID))
42 return Info;
43 return getRegisteredCommandInfo(CommandID);
44}
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000045
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +000046CommandInfo *CommandTraits::createCommandInfoWithName(StringRef CommandName) {
Eli Friedman116bb092012-09-11 00:36:26 +000047 char *Name = Allocator.Allocate<char>(CommandName.size() + 1);
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000048 memcpy(Name, CommandName.data(), CommandName.size());
Eli Friedman116bb092012-09-11 00:36:26 +000049 Name[CommandName.size()] = '\0';
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000050
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000051 // Value-initialize (=zero-initialize in this case) a new CommandInfo.
52 CommandInfo *Info = new (Allocator) CommandInfo();
53 Info->Name = Name;
54 Info->ID = NextID++;
55
56 RegisteredCommands.push_back(Info);
57
58 return Info;
59}
60
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +000061const CommandInfo *CommandTraits::registerUnknownCommand(
62 StringRef CommandName) {
63 CommandInfo *Info = createCommandInfoWithName(CommandName);
64 Info->IsUnknownCommand = true;
65 return Info;
66}
67
68const CommandInfo *CommandTraits::registerBlockCommand(StringRef CommandName) {
69 CommandInfo *Info = createCommandInfoWithName(CommandName);
70 Info->IsBlockCommand = true;
71 return Info;
72}
73
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000074const CommandInfo *CommandTraits::getBuiltinCommandInfo(
75 unsigned CommandID) {
76 if (CommandID < llvm::array_lengthof(Commands))
77 return &Commands[CommandID];
78 return NULL;
79}
80
81const 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];
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000086 }
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000087 return NULL;
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000088}
89
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000090const CommandInfo *CommandTraits::getRegisteredCommandInfo(
91 unsigned CommandID) const {
92 return RegisteredCommands[CommandID - llvm::array_lengthof(Commands)];
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000093}
94
95} // end namespace comments
96} // end namespace clang
97