CommentCommandTraits.h revision aa58081902ad31927df02e8537d972eabe29d6df
1//===--- CommentCommandTraits.h - 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//  This file defines the class that provides information about comment
11//  commands.
12//
13//===----------------------------------------------------------------------===//
14
15
16#ifndef LLVM_CLANG_AST_COMMENT_COMMAND_TRAITS_H
17#define LLVM_CLANG_AST_COMMENT_COMMAND_TRAITS_H
18
19#include "clang/Basic/LLVM.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/StringSwitch.h"
23
24namespace clang {
25namespace comments {
26
27/// This class provides informaiton about commands that can be used
28/// in comments.
29class CommandTraits {
30public:
31  /// \brief Check if a given command is a verbatim-like block command.
32  ///
33  /// A verbatim-like block command eats every character (except line starting
34  /// decorations) until matching end command is seen or comment end is hit.
35  ///
36  /// \param BeginName name of the command that starts the verbatim block.
37  /// \param [out] EndName name of the command that ends the verbatim block.
38  ///
39  /// \returns true if a given command is a verbatim block command.
40  bool isVerbatimBlockCommand(StringRef StartName, StringRef &EndName) const;
41
42  /// \brief Register a new verbatim block command.
43  void addVerbatimBlockCommand(StringRef BeginName, StringRef EndName);
44
45  /// \brief Check if a given command is a verbatim line command.
46  ///
47  /// A verbatim-like line command eats everything until a newline is seen or
48  /// comment end is hit.
49  bool isVerbatimLineCommand(StringRef Name) const;
50
51  /// \brief Register a new verbatim line command.
52  void addVerbatimLineCommand(StringRef Name);
53
54  /// \brief Check if a given command is a block command (of any kind).
55  bool isBlockCommand(StringRef Name) const;
56
57  /// \brief Check if a given command is introducing documentation for
58  /// a function parameter (\\param or an alias).
59  bool isParamCommand(StringRef Name) const;
60
61  /// \brief Check if a given command is introducing documentation for
62  /// a template parameter (\\tparam or an alias).
63  bool isTParamCommand(StringRef Name) const;
64
65  /// \brief Check if a given command is introducing a brief documentation
66  /// paragraph (\\brief or an alias).
67  bool isBriefCommand(StringRef Name) const;
68
69  /// \brief Check if a given command is \\brief or an alias.
70  bool isReturnsCommand(StringRef Name) const;
71
72  /// \returns the number of word-like arguments for a given block command,
73  /// except for \\param and \\tparam commands -- these have special argument
74  /// parsers.
75  unsigned getBlockCommandNumArgs(StringRef Name) const;
76
77  /// \brief Check if a given command is a inline command (of any kind).
78  bool isInlineCommand(StringRef Name) const;
79
80private:
81  struct VerbatimBlockCommand {
82    StringRef BeginName;
83    StringRef EndName;
84  };
85
86  typedef SmallVector<VerbatimBlockCommand, 4> VerbatimBlockCommandVector;
87
88  /// Registered additional verbatim-like block commands.
89  VerbatimBlockCommandVector VerbatimBlockCommands;
90
91  struct VerbatimLineCommand {
92    StringRef Name;
93  };
94
95  typedef SmallVector<VerbatimLineCommand, 4> VerbatimLineCommandVector;
96
97  /// Registered verbatim-like line commands.
98  VerbatimLineCommandVector VerbatimLineCommands;
99};
100
101inline bool CommandTraits::isBlockCommand(StringRef Name) const {
102  return isBriefCommand(Name) || isReturnsCommand(Name) ||
103      isParamCommand(Name) || isTParamCommand(Name) ||
104      llvm::StringSwitch<bool>(Name)
105      .Case("author", true)
106      .Case("authors", true)
107      .Case("pre", true)
108      .Case("post", true)
109      .Default(false);
110}
111
112inline bool CommandTraits::isParamCommand(StringRef Name) const {
113  return Name == "param";
114}
115
116inline bool CommandTraits::isTParamCommand(StringRef Name) const {
117  return Name == "tparam" || // Doxygen
118         Name == "templatefield"; // HeaderDoc
119}
120
121inline bool CommandTraits::isBriefCommand(StringRef Name) const {
122  return Name == "brief" || Name == "short";
123}
124
125inline bool CommandTraits::isReturnsCommand(StringRef Name) const {
126  return Name == "returns" || Name == "return" || Name == "result";
127}
128
129inline unsigned CommandTraits::getBlockCommandNumArgs(StringRef Name) const {
130  return 0;
131}
132
133inline bool CommandTraits::isInlineCommand(StringRef Name) const {
134  return llvm::StringSwitch<bool>(Name)
135      .Case("b", true)
136      .Cases("c", "p", true)
137      .Cases("a", "e", "em", true)
138      .Default(false);
139}
140
141} // end namespace comments
142} // end namespace clang
143
144#endif
145
146