Skip to content

Commit 86abee8

Browse files
committed
Add support for dumping AST comment nodes to JSON.
llvm-svn: 361265
1 parent 6c05312 commit 86abee8

File tree

4 files changed

+1670
-6
lines changed

4 files changed

+1670
-6
lines changed

clang/include/clang/AST/JSONNodeDumper.h

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ class NodeStreamer {
111111
// information presented is correct.
112112
class JSONNodeDumper
113113
: public ConstAttrVisitor<JSONNodeDumper>,
114+
public comments::ConstCommentVisitor<JSONNodeDumper, void,
115+
const comments::FullComment *>,
114116
public ConstTemplateArgumentVisitor<JSONNodeDumper>,
115117
public ConstStmtVisitor<JSONNodeDumper>,
116118
public TypeVisitor<JSONNodeDumper>,
@@ -120,8 +122,12 @@ class JSONNodeDumper
120122

121123
const SourceManager &SM;
122124
PrintingPolicy PrintPolicy;
125+
const comments::CommandTraits *Traits;
123126

124127
using InnerAttrVisitor = ConstAttrVisitor<JSONNodeDumper>;
128+
using InnerCommentVisitor =
129+
comments::ConstCommentVisitor<JSONNodeDumper, void,
130+
const comments::FullComment *>;
125131
using InnerTemplateArgVisitor = ConstTemplateArgumentVisitor<JSONNodeDumper>;
126132
using InnerStmtVisitor = ConstStmtVisitor<JSONNodeDumper>;
127133
using InnerTypeVisitor = TypeVisitor<JSONNodeDumper>;
@@ -163,10 +169,14 @@ class JSONNodeDumper
163169
}
164170
void addPreviousDeclaration(const Decl *D);
165171

172+
StringRef getCommentCommandName(unsigned CommandID) const;
173+
166174
public:
167175
JSONNodeDumper(raw_ostream &OS, const SourceManager &SrcMgr,
168-
const PrintingPolicy &PrintPolicy)
169-
: NodeStreamer(OS), SM(SrcMgr), PrintPolicy(PrintPolicy) {}
176+
const PrintingPolicy &PrintPolicy,
177+
const comments::CommandTraits *Traits)
178+
: NodeStreamer(OS), SM(SrcMgr), PrintPolicy(PrintPolicy), Traits(Traits) {
179+
}
170180

171181
void Visit(const Attr *A);
172182
void Visit(const Stmt *Node);
@@ -237,6 +247,28 @@ class JSONNodeDumper
237247
void VisitLabelStmt(const LabelStmt *LS);
238248
void VisitGotoStmt(const GotoStmt *GS);
239249
void VisitWhileStmt(const WhileStmt *WS);
250+
251+
void visitTextComment(const comments::TextComment *C,
252+
const comments::FullComment *);
253+
void visitInlineCommandComment(const comments::InlineCommandComment *C,
254+
const comments::FullComment *);
255+
void visitHTMLStartTagComment(const comments::HTMLStartTagComment *C,
256+
const comments::FullComment *);
257+
void visitHTMLEndTagComment(const comments::HTMLEndTagComment *C,
258+
const comments::FullComment *);
259+
void visitBlockCommandComment(const comments::BlockCommandComment *C,
260+
const comments::FullComment *);
261+
void visitParamCommandComment(const comments::ParamCommandComment *C,
262+
const comments::FullComment *FC);
263+
void visitTParamCommandComment(const comments::TParamCommandComment *C,
264+
const comments::FullComment *FC);
265+
void visitVerbatimBlockComment(const comments::VerbatimBlockComment *C,
266+
const comments::FullComment *);
267+
void
268+
visitVerbatimBlockLineComment(const comments::VerbatimBlockLineComment *C,
269+
const comments::FullComment *);
270+
void visitVerbatimLineComment(const comments::VerbatimLineComment *C,
271+
const comments::FullComment *);
240272
};
241273

242274
class JSONDumper : public ASTNodeTraverser<JSONDumper, JSONNodeDumper> {
@@ -314,8 +346,9 @@ class JSONDumper : public ASTNodeTraverser<JSONDumper, JSONNodeDumper> {
314346

315347
public:
316348
JSONDumper(raw_ostream &OS, const SourceManager &SrcMgr,
317-
const PrintingPolicy &PrintPolicy)
318-
: NodeDumper(OS, SrcMgr, PrintPolicy) {}
349+
const PrintingPolicy &PrintPolicy,
350+
const comments::CommandTraits *Traits)
351+
: NodeDumper(OS, SrcMgr, PrintPolicy, Traits) {}
319352

320353
JSONNodeDumper &doGetNodeDelegate() { return NodeDumper; }
321354

clang/lib/AST/ASTDumper.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ LLVM_DUMP_METHOD void Decl::dump(raw_ostream &OS, bool Deserialize,
184184
const SourceManager &SM = Ctx.getSourceManager();
185185

186186
if (ADOF_JSON == Format) {
187-
JSONDumper P(OS, SM, Ctx.getPrintingPolicy());
187+
JSONDumper P(OS, SM, Ctx.getPrintingPolicy(),
188+
&Ctx.getCommentCommandTraits());
188189
(void)Deserialize; // FIXME?
189190
P.Visit(this);
190191
} else {

clang/lib/AST/JSONNodeDumper.cpp

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,17 @@ void JSONNodeDumper::Visit(const Decl *D) {
111111
}
112112

113113
void JSONNodeDumper::Visit(const comments::Comment *C,
114-
const comments::FullComment *FC) {}
114+
const comments::FullComment *FC) {
115+
if (!C)
116+
return;
117+
118+
JOS.attribute("id", createPointerRepresentation(C));
119+
JOS.attribute("kind", C->getCommentKindName());
120+
JOS.attribute("loc", createSourceLocation(C->getLocation()));
121+
JOS.attribute("range", createSourceRange(C->getSourceRange()));
122+
123+
InnerCommentVisitor::visit(C, FC);
124+
}
115125

116126
void JSONNodeDumper::Visit(const TemplateArgument &TA, SourceRange R,
117127
const Decl *From, StringRef Label) {
@@ -803,3 +813,131 @@ void JSONNodeDumper::VisitGotoStmt(const GotoStmt *GS) {
803813
void JSONNodeDumper::VisitWhileStmt(const WhileStmt *WS) {
804814
attributeOnlyIfTrue("hasVar", WS->hasVarStorage());
805815
}
816+
817+
StringRef JSONNodeDumper::getCommentCommandName(unsigned CommandID) const {
818+
if (Traits)
819+
return Traits->getCommandInfo(CommandID)->Name;
820+
if (const comments::CommandInfo *Info =
821+
comments::CommandTraits::getBuiltinCommandInfo(CommandID))
822+
return Info->Name;
823+
return "<invalid>";
824+
}
825+
826+
void JSONNodeDumper::visitTextComment(const comments::TextComment *C,
827+
const comments::FullComment *) {
828+
JOS.attribute("text", C->getText());
829+
}
830+
831+
void JSONNodeDumper::visitInlineCommandComment(
832+
const comments::InlineCommandComment *C, const comments::FullComment *) {
833+
JOS.attribute("name", getCommentCommandName(C->getCommandID()));
834+
835+
switch (C->getRenderKind()) {
836+
case comments::InlineCommandComment::RenderNormal:
837+
JOS.attribute("renderKind", "normal");
838+
break;
839+
case comments::InlineCommandComment::RenderBold:
840+
JOS.attribute("renderKind", "bold");
841+
break;
842+
case comments::InlineCommandComment::RenderEmphasized:
843+
JOS.attribute("renderKind", "emphasized");
844+
break;
845+
case comments::InlineCommandComment::RenderMonospaced:
846+
JOS.attribute("renderKind", "monospaced");
847+
break;
848+
}
849+
850+
llvm::json::Array Args;
851+
for (unsigned I = 0, E = C->getNumArgs(); I < E; ++I)
852+
Args.push_back(C->getArgText(I));
853+
854+
if (!Args.empty())
855+
JOS.attribute("args", std::move(Args));
856+
}
857+
858+
void JSONNodeDumper::visitHTMLStartTagComment(
859+
const comments::HTMLStartTagComment *C, const comments::FullComment *) {
860+
JOS.attribute("name", C->getTagName());
861+
attributeOnlyIfTrue("selfClosing", C->isSelfClosing());
862+
attributeOnlyIfTrue("malformed", C->isMalformed());
863+
864+
llvm::json::Array Attrs;
865+
for (unsigned I = 0, E = C->getNumAttrs(); I < E; ++I)
866+
Attrs.push_back(
867+
{{"name", C->getAttr(I).Name}, {"value", C->getAttr(I).Value}});
868+
869+
if (!Attrs.empty())
870+
JOS.attribute("attrs", std::move(Attrs));
871+
}
872+
873+
void JSONNodeDumper::visitHTMLEndTagComment(
874+
const comments::HTMLEndTagComment *C, const comments::FullComment *) {
875+
JOS.attribute("name", C->getTagName());
876+
}
877+
878+
void JSONNodeDumper::visitBlockCommandComment(
879+
const comments::BlockCommandComment *C, const comments::FullComment *) {
880+
JOS.attribute("name", getCommentCommandName(C->getCommandID()));
881+
882+
llvm::json::Array Args;
883+
for (unsigned I = 0, E = C->getNumArgs(); I < E; ++I)
884+
Args.push_back(C->getArgText(I));
885+
886+
if (!Args.empty())
887+
JOS.attribute("args", std::move(Args));
888+
}
889+
890+
void JSONNodeDumper::visitParamCommandComment(
891+
const comments::ParamCommandComment *C, const comments::FullComment *FC) {
892+
switch (C->getDirection()) {
893+
case comments::ParamCommandComment::In:
894+
JOS.attribute("direction", "in");
895+
break;
896+
case comments::ParamCommandComment::Out:
897+
JOS.attribute("direction", "out");
898+
break;
899+
case comments::ParamCommandComment::InOut:
900+
JOS.attribute("direction", "in,out");
901+
break;
902+
}
903+
attributeOnlyIfTrue("explicit", C->isDirectionExplicit());
904+
905+
if (C->hasParamName())
906+
JOS.attribute("param", C->isParamIndexValid() ? C->getParamName(FC)
907+
: C->getParamNameAsWritten());
908+
909+
if (C->isParamIndexValid() && !C->isVarArgParam())
910+
JOS.attribute("paramIdx", C->getParamIndex());
911+
}
912+
913+
void JSONNodeDumper::visitTParamCommandComment(
914+
const comments::TParamCommandComment *C, const comments::FullComment *FC) {
915+
if (C->hasParamName())
916+
JOS.attribute("param", C->isPositionValid() ? C->getParamName(FC)
917+
: C->getParamNameAsWritten());
918+
if (C->isPositionValid()) {
919+
llvm::json::Array Positions;
920+
for (unsigned I = 0, E = C->getDepth(); I < E; ++I)
921+
Positions.push_back(C->getIndex(I));
922+
923+
if (!Positions.empty())
924+
JOS.attribute("positions", std::move(Positions));
925+
}
926+
}
927+
928+
void JSONNodeDumper::visitVerbatimBlockComment(
929+
const comments::VerbatimBlockComment *C, const comments::FullComment *) {
930+
JOS.attribute("name", getCommentCommandName(C->getCommandID()));
931+
JOS.attribute("closeName", C->getCloseName());
932+
}
933+
934+
void JSONNodeDumper::visitVerbatimBlockLineComment(
935+
const comments::VerbatimBlockLineComment *C,
936+
const comments::FullComment *) {
937+
JOS.attribute("text", C->getText());
938+
}
939+
940+
void JSONNodeDumper::visitVerbatimLineComment(
941+
const comments::VerbatimLineComment *C, const comments::FullComment *) {
942+
JOS.attribute("text", C->getText());
943+
}

0 commit comments

Comments
 (0)