Skip to content

Commit 002927b

Browse files
committed
CommentVisitor
1 parent b88ceb3 commit 002927b

File tree

3 files changed

+189
-126
lines changed

3 files changed

+189
-126
lines changed

src/CommentVisitor.cpp

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
//
2+
// This is a derivative work. originally part of the LLVM Project.
3+
// Licensed under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
// Copyright (c) 2023 Vinnie Falco ([email protected])
8+
//
9+
// Official repository: https://github.com/cppalliance/mrdox
10+
//
11+
12+
#include "CommentVisitor.h "
13+
14+
using clang::comments::FullComment;
15+
16+
namespace clang {
17+
namespace doc {
18+
namespace serialize {
19+
20+
void CommentVisitor::parseComment(const comments::Comment *C) {
21+
CurrentCI.Kind = C->getCommentKindName();
22+
ConstCommentVisitor<CommentVisitor>::visit(C);
23+
for (comments::Comment *Child :
24+
llvm::make_range(C->child_begin(), C->child_end())) {
25+
CurrentCI.Children.emplace_back(std::make_unique<CommentInfo>());
26+
CommentVisitor Visitor(*CurrentCI.Children.back());
27+
Visitor.parseComment(Child);
28+
}
29+
}
30+
31+
void
32+
CommentVisitor::
33+
visitTextComment(
34+
TextComment const* c)
35+
{
36+
// Trim leading whitespace
37+
auto s = c->getText().ltrim();
38+
if(! isWhitespaceOnly(s))
39+
CurrentCI.Text = s;
40+
}
41+
42+
void CommentVisitor::visitInlineCommandComment(
43+
const InlineCommandComment *C) {
44+
CurrentCI.Name = getCommandName(C->getCommandID());
45+
for (unsigned I = 0, E = C->getNumArgs(); I != E; ++I)
46+
CurrentCI.Args.push_back(C->getArgText(I));
47+
}
48+
49+
void CommentVisitor::visitHTMLStartTagComment(
50+
const HTMLStartTagComment *C) {
51+
CurrentCI.Name = C->getTagName();
52+
CurrentCI.SelfClosing = C->isSelfClosing();
53+
for (unsigned I = 0, E = C->getNumAttrs(); I < E; ++I) {
54+
const HTMLStartTagComment::Attribute &Attr = C->getAttr(I);
55+
CurrentCI.AttrKeys.push_back(Attr.Name);
56+
CurrentCI.AttrValues.push_back(Attr.Value);
57+
}
58+
}
59+
60+
void CommentVisitor::visitHTMLEndTagComment(
61+
const HTMLEndTagComment *C) {
62+
CurrentCI.Name = C->getTagName();
63+
CurrentCI.SelfClosing = true;
64+
}
65+
66+
void CommentVisitor::visitBlockCommandComment(
67+
const BlockCommandComment *C) {
68+
CurrentCI.Name = getCommandName(C->getCommandID());
69+
for (unsigned I = 0, E = C->getNumArgs(); I < E; ++I)
70+
CurrentCI.Args.push_back(C->getArgText(I));
71+
}
72+
73+
void CommentVisitor::visitParamCommandComment(
74+
const ParamCommandComment *C) {
75+
CurrentCI.Direction =
76+
ParamCommandComment::getDirectionAsString(C->getDirection());
77+
CurrentCI.Explicit = C->isDirectionExplicit();
78+
if (C->hasParamName())
79+
CurrentCI.ParamName = C->getParamNameAsWritten();
80+
}
81+
82+
void CommentVisitor::visitTParamCommandComment(
83+
const TParamCommandComment *C) {
84+
if (C->hasParamName())
85+
CurrentCI.ParamName = C->getParamNameAsWritten();
86+
}
87+
88+
void CommentVisitor::visitVerbatimBlockComment(
89+
const VerbatimBlockComment *C) {
90+
CurrentCI.Name = getCommandName(C->getCommandID());
91+
CurrentCI.CloseName = C->getCloseName();
92+
}
93+
94+
void CommentVisitor::visitVerbatimBlockLineComment(
95+
const VerbatimBlockLineComment *C) {
96+
if (!isWhitespaceOnly(C->getText()))
97+
CurrentCI.Text = C->getText();
98+
}
99+
100+
void CommentVisitor::visitVerbatimLineComment(
101+
const VerbatimLineComment *C) {
102+
if (!isWhitespaceOnly(C->getText()))
103+
CurrentCI.Text = C->getText();
104+
}
105+
106+
bool CommentVisitor::isWhitespaceOnly(llvm::StringRef S) const {
107+
return llvm::all_of(S, isspace);
108+
}
109+
110+
std::string CommentVisitor::getCommandName(unsigned CommandID) const {
111+
const CommandInfo *Info = CommandTraits::getBuiltinCommandInfo(CommandID);
112+
if (Info)
113+
return Info->Name;
114+
// TODO: Add parsing for \file command.
115+
return "<not a builtin command>";
116+
}
117+
118+
} // namespace serialize
119+
} // namespace doc
120+
} // namespace clang

src/CommentVisitor.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// This is a derivative work. originally part of the LLVM Project.
3+
// Licensed under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
// Copyright (c) 2023 Vinnie Falco ([email protected])
8+
//
9+
// Official repository: https://github.com/cppalliance/mrdox
10+
//
11+
12+
#ifndef MRDOX_COMMENT_VISITOR_HPP
13+
#define MRDOX_COMMENT_VISITOR_HPP
14+
15+
#include "Representation.h"
16+
#ifdef _MSC_VER
17+
#pragma warning(push)
18+
#pragma warning(disable: 5054) // C5054: operator '+': deprecated between enumerations of different types
19+
#include <clang/AST/Comment.h>
20+
#include <clang/AST/CommentVisitor.h>
21+
#pragma warning(pop)
22+
#endif
23+
24+
namespace clang {
25+
namespace doc {
26+
namespace serialize {
27+
28+
// VFALCO refactor this
29+
using namespace comments;
30+
31+
class CommentVisitor
32+
: public ConstCommentVisitor<CommentVisitor>
33+
{
34+
public:
35+
CommentVisitor(CommentInfo &CI) : CurrentCI(CI) {}
36+
37+
void parseComment(const comments::Comment *C);
38+
39+
void visitTextComment(const TextComment *C);
40+
void visitInlineCommandComment(const InlineCommandComment *C);
41+
void visitHTMLStartTagComment(const HTMLStartTagComment *C);
42+
void visitHTMLEndTagComment(const HTMLEndTagComment *C);
43+
void visitBlockCommandComment(const BlockCommandComment *C);
44+
void visitParamCommandComment(const ParamCommandComment *C);
45+
void visitTParamCommandComment(const TParamCommandComment *C);
46+
void visitVerbatimBlockComment(const VerbatimBlockComment *C);
47+
void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
48+
void visitVerbatimLineComment(const VerbatimLineComment *C);
49+
50+
private:
51+
std::string getCommandName(unsigned CommandID) const;
52+
bool isWhitespaceOnly(StringRef S) const;
53+
54+
CommentInfo &CurrentCI;
55+
};
56+
57+
} // namespace serialize
58+
} // namespace doc
59+
} // namespace clang
60+
61+
#endif

src/Serialize.cpp

Lines changed: 8 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111

1212
#include "Serialize.h"
13+
#include "CommentVisitor.h"
1314
#include "BitcodeWriter.h"
1415
#ifdef _MSC_VER
1516
#pragma warning(push)
@@ -71,129 +72,6 @@ llvm::SmallString<128> getInfoRelativePath(const Decl *D) {
7172
return getInfoRelativePath(Namespaces);
7273
}
7374

74-
class ClangDocCommentVisitor
75-
: public ConstCommentVisitor<ClangDocCommentVisitor> {
76-
public:
77-
ClangDocCommentVisitor(CommentInfo &CI) : CurrentCI(CI) {}
78-
79-
void parseComment(const comments::Comment *C);
80-
81-
void visitTextComment(const TextComment *C);
82-
void visitInlineCommandComment(const InlineCommandComment *C);
83-
void visitHTMLStartTagComment(const HTMLStartTagComment *C);
84-
void visitHTMLEndTagComment(const HTMLEndTagComment *C);
85-
void visitBlockCommandComment(const BlockCommandComment *C);
86-
void visitParamCommandComment(const ParamCommandComment *C);
87-
void visitTParamCommandComment(const TParamCommandComment *C);
88-
void visitVerbatimBlockComment(const VerbatimBlockComment *C);
89-
void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C);
90-
void visitVerbatimLineComment(const VerbatimLineComment *C);
91-
92-
private:
93-
std::string getCommandName(unsigned CommandID) const;
94-
bool isWhitespaceOnly(StringRef S) const;
95-
96-
CommentInfo &CurrentCI;
97-
};
98-
99-
void ClangDocCommentVisitor::parseComment(const comments::Comment *C) {
100-
CurrentCI.Kind = C->getCommentKindName();
101-
ConstCommentVisitor<ClangDocCommentVisitor>::visit(C);
102-
for (comments::Comment *Child :
103-
llvm::make_range(C->child_begin(), C->child_end())) {
104-
CurrentCI.Children.emplace_back(std::make_unique<CommentInfo>());
105-
ClangDocCommentVisitor Visitor(*CurrentCI.Children.back());
106-
Visitor.parseComment(Child);
107-
}
108-
}
109-
110-
void
111-
ClangDocCommentVisitor::
112-
visitTextComment(
113-
TextComment const* c)
114-
{
115-
// Trim leading whitespace
116-
auto s = c->getText().ltrim();
117-
if(! isWhitespaceOnly(s))
118-
CurrentCI.Text = s;
119-
}
120-
121-
void ClangDocCommentVisitor::visitInlineCommandComment(
122-
const InlineCommandComment *C) {
123-
CurrentCI.Name = getCommandName(C->getCommandID());
124-
for (unsigned I = 0, E = C->getNumArgs(); I != E; ++I)
125-
CurrentCI.Args.push_back(C->getArgText(I));
126-
}
127-
128-
void ClangDocCommentVisitor::visitHTMLStartTagComment(
129-
const HTMLStartTagComment *C) {
130-
CurrentCI.Name = C->getTagName();
131-
CurrentCI.SelfClosing = C->isSelfClosing();
132-
for (unsigned I = 0, E = C->getNumAttrs(); I < E; ++I) {
133-
const HTMLStartTagComment::Attribute &Attr = C->getAttr(I);
134-
CurrentCI.AttrKeys.push_back(Attr.Name);
135-
CurrentCI.AttrValues.push_back(Attr.Value);
136-
}
137-
}
138-
139-
void ClangDocCommentVisitor::visitHTMLEndTagComment(
140-
const HTMLEndTagComment *C) {
141-
CurrentCI.Name = C->getTagName();
142-
CurrentCI.SelfClosing = true;
143-
}
144-
145-
void ClangDocCommentVisitor::visitBlockCommandComment(
146-
const BlockCommandComment *C) {
147-
CurrentCI.Name = getCommandName(C->getCommandID());
148-
for (unsigned I = 0, E = C->getNumArgs(); I < E; ++I)
149-
CurrentCI.Args.push_back(C->getArgText(I));
150-
}
151-
152-
void ClangDocCommentVisitor::visitParamCommandComment(
153-
const ParamCommandComment *C) {
154-
CurrentCI.Direction =
155-
ParamCommandComment::getDirectionAsString(C->getDirection());
156-
CurrentCI.Explicit = C->isDirectionExplicit();
157-
if (C->hasParamName())
158-
CurrentCI.ParamName = C->getParamNameAsWritten();
159-
}
160-
161-
void ClangDocCommentVisitor::visitTParamCommandComment(
162-
const TParamCommandComment *C) {
163-
if (C->hasParamName())
164-
CurrentCI.ParamName = C->getParamNameAsWritten();
165-
}
166-
167-
void ClangDocCommentVisitor::visitVerbatimBlockComment(
168-
const VerbatimBlockComment *C) {
169-
CurrentCI.Name = getCommandName(C->getCommandID());
170-
CurrentCI.CloseName = C->getCloseName();
171-
}
172-
173-
void ClangDocCommentVisitor::visitVerbatimBlockLineComment(
174-
const VerbatimBlockLineComment *C) {
175-
if (!isWhitespaceOnly(C->getText()))
176-
CurrentCI.Text = C->getText();
177-
}
178-
179-
void ClangDocCommentVisitor::visitVerbatimLineComment(
180-
const VerbatimLineComment *C) {
181-
if (!isWhitespaceOnly(C->getText()))
182-
CurrentCI.Text = C->getText();
183-
}
184-
185-
bool ClangDocCommentVisitor::isWhitespaceOnly(llvm::StringRef S) const {
186-
return llvm::all_of(S, isspace);
187-
}
188-
189-
std::string ClangDocCommentVisitor::getCommandName(unsigned CommandID) const {
190-
const CommandInfo *Info = CommandTraits::getBuiltinCommandInfo(CommandID);
191-
if (Info)
192-
return Info->Name;
193-
// TODO: Add parsing for \file command.
194-
return "<not a builtin command>";
195-
}
196-
19775
// Serializing functions.
19876

19977
std::string getSourceCode(const Decl *D, const SourceRange &R) {
@@ -226,9 +104,13 @@ std::string serialize(std::unique_ptr<Info> &I) {
226104
}
227105
}
228106

229-
static void parseFullComment(const FullComment *C, CommentInfo &CI) {
230-
ClangDocCommentVisitor Visitor(CI);
231-
Visitor.parseComment(C);
107+
void
108+
parseFullComment(
109+
FullComment const* c,
110+
CommentInfo& ci)
111+
{
112+
CommentVisitor Visitor(ci);
113+
Visitor.parseComment(c);
232114
}
233115

234116
static SymbolID getUSRForDecl(const Decl *D) {

0 commit comments

Comments
 (0)