aboutsummaryrefslogtreecommitdiff
path: root/include/clang/Comments/RawCommentList.h
blob: bf0b616383e08f7c7579b4dd3d159cdad58bafe9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//===--- RawCommentList.h - Classes for processing raw comments -*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_COMMENTS_RAW_COMMENT_LIST_H
#define LLVM_CLANG_COMMENTS_RAW_COMMENT_LIST_H

#include "clang/Basic/SourceManager.h"
#include "llvm/ADT/ArrayRef.h"

namespace clang {

class ASTContext;
class ASTReader;

class RawComment {
public:
  enum CommentKind {
    CK_Invalid,      ///< Invalid comment
    CK_OrdinaryBCPL, ///< Any normal BCPL comments
    CK_OrdinaryC,    ///< Any normal C comment
    CK_BCPLSlash,    ///< \code /// stuff \endcode
    CK_BCPLExcl,     ///< \code //! stuff \endcode
    CK_JavaDoc,      ///< \code /** stuff */ \endcode
    CK_Qt,           ///< \code /*! stuff */ \endcode, also used by HeaderDoc
    CK_Merged        ///< Two or more Doxygen comments merged together
  };

  RawComment() : Kind(CK_Invalid), IsAlmostTrailingComment(false) { }

  RawComment(const SourceManager &SourceMgr, SourceRange SR,
             bool Merged = false);

  CommentKind getKind() const LLVM_READONLY {
    return (CommentKind) Kind;
  }

  bool isInvalid() const LLVM_READONLY {
    return Kind == CK_Invalid;
  }

  bool isMerged() const LLVM_READONLY {
    return Kind == CK_Merged;
  }

  /// Returns true if it is a comment that should be put after a member:
  /// \code ///< stuff \endcode
  /// \code //!< stuff \endcode
  /// \code /**< stuff */ \endcode
  /// \code /*!< stuff */ \endcode
  bool isTrailingComment() const LLVM_READONLY {
    assert(isDoxygen());
    return IsTrailingComment;
  }

  /// Returns true if it is a probable typo:
  /// \code //< stuff \endcode
  /// \code /*< stuff */ \endcode
  bool isAlmostTrailingComment() const LLVM_READONLY {
    return IsAlmostTrailingComment;
  }

  /// Returns true if this comment is not a Doxygen comment.
  bool isOrdinary() const LLVM_READONLY {
    return (Kind == CK_OrdinaryBCPL) || (Kind == CK_OrdinaryC);
  }

  /// Returns true if this comment any kind of a Doxygen comment.
  bool isDoxygen() const LLVM_READONLY {
    return !isInvalid() && !isOrdinary();
  }

  /// Returns raw comment text with comment markers.
  StringRef getRawText(const SourceManager &SourceMgr) const {
    if (RawTextValid)
      return RawText;

    RawText = getRawTextSlow(SourceMgr);
    RawTextValid = true;
    return RawText;
  }

  SourceRange getSourceRange() const LLVM_READONLY {
    return Range;
  }

  unsigned getBeginLine(const SourceManager &SM) const;
  unsigned getEndLine(const SourceManager &SM) const;

private:
  SourceRange Range;

  mutable StringRef RawText;
  mutable bool RawTextValid : 1; ///< True if RawText is valid

  unsigned Kind : 3;

  bool IsTrailingComment : 1;
  bool IsAlmostTrailingComment : 1;

  mutable bool BeginLineValid : 1; ///< True if BeginLine is valid
  mutable bool EndLineValid : 1;   ///< True if EndLine is valid
  mutable unsigned BeginLine;      ///< Cached line number
  mutable unsigned EndLine;        ///< Cached line number

  /// \brief Constructor for AST deserialization.
  RawComment(SourceRange SR, CommentKind K, bool IsTrailingComment,
             bool IsAlmostTrailingComment) :
    Range(SR), RawTextValid(false), Kind(K),
    IsTrailingComment(IsTrailingComment),
    IsAlmostTrailingComment(IsAlmostTrailingComment),
    BeginLineValid(false), EndLineValid(false)
  { }

  StringRef getRawTextSlow(const SourceManager &SourceMgr) const;

  friend class ASTReader;
};

/// \brief Compare comments' source locations.
template<>
class BeforeThanCompare<RawComment> {
  const SourceManager &SM;

public:
  explicit BeforeThanCompare(const SourceManager &SM) : SM(SM) { }

  bool operator()(const RawComment &LHS, const SourceRange &RHS) {
    return SM.isBeforeInTranslationUnit(LHS.getSourceRange().getBegin(),
                                        RHS.getBegin());
  }
};

/// \brief This class represents all comments included in the translation unit,
/// sorted in order of appearance in the translation unit.
class RawCommentList {
public:
  RawCommentList(SourceManager &SourceMgr) :
    SourceMgr(SourceMgr), OnlyWhitespaceSeen(true) { }

  void addComment(const RawComment &RC, ASTContext &Context);

  ArrayRef<RawComment> getComments() const {
    return Comments;
  }

private:
  SourceManager &SourceMgr;
  std::vector<RawComment> Comments;
  RawComment LastComment;
  bool OnlyWhitespaceSeen;

  void addCommentsToFront(const std::vector<RawComment> &C) {
    size_t OldSize = Comments.size();
    Comments.resize(C.size() + OldSize);
    std::copy_backward(Comments.begin(), Comments.begin() + OldSize,
                       Comments.end());
    std::copy(C.begin(), C.end(), Comments.begin());
  }

  friend class ASTReader;
};

} // end namespace clang

#endif