blob: e7e40fd1090f756aa3a2603e098aae04a37a3920 [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 Gribenkoe4330a32012-09-10 20:32:42 +000018CommandTraits::CommandTraits(llvm::BumpPtrAllocator &Allocator) :
19 NextID(llvm::array_lengthof(Commands)), Allocator(Allocator)
20{ }
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000021
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000022const CommandInfo *CommandTraits::getCommandInfoOrNULL(StringRef Name) const {
23 if (const CommandInfo *Info = getBuiltinCommandInfo(Name))
24 return Info;
25 return getRegisteredCommandInfo(Name);
26}
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000027
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000028const CommandInfo *CommandTraits::getCommandInfo(unsigned CommandID) const {
29 if (const CommandInfo *Info = getBuiltinCommandInfo(CommandID))
30 return Info;
31 return getRegisteredCommandInfo(CommandID);
32}
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000033
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000034const CommandInfo *CommandTraits::registerUnknownCommand(StringRef CommandName) {
Eli Friedman116bb092012-09-11 00:36:26 +000035 char *Name = Allocator.Allocate<char>(CommandName.size() + 1);
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000036 memcpy(Name, CommandName.data(), CommandName.size());
Eli Friedman116bb092012-09-11 00:36:26 +000037 Name[CommandName.size()] = '\0';
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000038
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000039 // Value-initialize (=zero-initialize in this case) a new CommandInfo.
40 CommandInfo *Info = new (Allocator) CommandInfo();
41 Info->Name = Name;
42 Info->ID = NextID++;
Dmitri Gribenkob0b8a962012-09-11 19:22:03 +000043 Info->IsUnknownCommand = true;
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000044
45 RegisteredCommands.push_back(Info);
46
47 return Info;
48}
49
50const CommandInfo *CommandTraits::getBuiltinCommandInfo(
51 unsigned CommandID) {
52 if (CommandID < llvm::array_lengthof(Commands))
53 return &Commands[CommandID];
54 return NULL;
55}
56
57const 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];
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000062 }
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000063 return NULL;
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000064}
65
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +000066const CommandInfo *CommandTraits::getRegisteredCommandInfo(
67 unsigned CommandID) const {
68 return RegisteredCommands[CommandID - llvm::array_lengthof(Commands)];
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000069}
70
71} // end namespace comments
72} // end namespace clang
73