|
7 | 7 |
|
8 | 8 | #include <patchestry/AST/SNodeDebug.hpp> |
9 | 9 |
|
10 | | -#include <clang/AST/Expr.h> |
11 | | -#include <clang/AST/Stmt.h> |
12 | | -#include <clang/AST/PrettyPrinter.h> |
13 | | - |
14 | | -#include <string> |
15 | | - |
16 | 10 | namespace patchestry::ast { |
17 | 11 |
|
18 | | - namespace { |
19 | | - // Convert std::string_view to llvm::StringRef for LLVM JSON APIs |
20 | | - llvm::StringRef toRef(std::string_view sv) { |
21 | | - return llvm::StringRef(sv.data(), sv.size()); |
22 | | - } |
23 | | - } // namespace |
24 | | - |
25 | | - // ======================================================================== |
26 | | - // Pseudo-C printer |
27 | | - // ======================================================================== |
28 | | - |
29 | | - static void indent(llvm::raw_ostream &os, unsigned level) { |
30 | | - for (unsigned i = 0; i < level; ++i) os << " "; |
31 | | - } |
32 | | - |
33 | | - static void printExpr(const clang::Expr *e, llvm::raw_ostream &os, |
34 | | - clang::ASTContext *ctx) { |
35 | | - if (!e) { os << "/*null*/"; return; } |
36 | | - if (ctx) { |
37 | | - clang::PrintingPolicy pp(ctx->getLangOpts()); |
38 | | - pp.SuppressImplicitBase = true; |
39 | | - e->printPretty(os, nullptr, pp); |
40 | | - } else { |
41 | | - os << "<expr>"; |
42 | | - } |
43 | | - } |
44 | | - |
45 | | - static void printStmt(const clang::Stmt *s, llvm::raw_ostream &os, |
46 | | - clang::ASTContext *ctx, unsigned ind) { |
47 | | - if (!s) return; |
48 | | - if (ctx) { |
49 | | - clang::PrintingPolicy pp(ctx->getLangOpts()); |
50 | | - pp.SuppressImplicitBase = true; |
51 | | - indent(os, ind); |
52 | | - s->printPretty(os, nullptr, pp, ind); |
53 | | - // printPretty may or may not add newline — ensure one |
54 | | - os << "\n"; |
55 | | - } else { |
56 | | - indent(os, ind); |
57 | | - os << "<stmt>;\n"; |
58 | | - } |
59 | | - } |
60 | | - |
61 | | - void printPseudoC(const SNode *node, llvm::raw_ostream &os, |
62 | | - clang::ASTContext *ctx, unsigned ind) { |
63 | | - if (!node) return; |
64 | | - |
65 | | - switch (node->Kind()) { |
66 | | - case SNodeKind::SEQ: { |
67 | | - auto *seq = node->as< SSeq >(); |
68 | | - for (const auto *child : seq->Children()) { |
69 | | - printPseudoC(child, os, ctx, ind); |
70 | | - } |
71 | | - break; |
72 | | - } |
73 | | - case SNodeKind::BLOCK: { |
74 | | - auto *blk = node->as< SBlock >(); |
75 | | - for (auto *s : blk->Stmts()) { |
76 | | - printStmt(s, os, ctx, ind); |
77 | | - } |
78 | | - break; |
79 | | - } |
80 | | - case SNodeKind::IF_THEN_ELSE: { |
81 | | - auto *ite = node->as< SIfThenElse >(); |
82 | | - indent(os, ind); |
83 | | - os << "if ("; |
84 | | - printExpr(ite->Cond(), os, ctx); |
85 | | - os << ") {\n"; |
86 | | - printPseudoC(ite->ThenBranch(), os, ctx, ind + 1); |
87 | | - if (ite->ElseBranch()) { |
88 | | - indent(os, ind); |
89 | | - os << "} else {\n"; |
90 | | - printPseudoC(ite->ElseBranch(), os, ctx, ind + 1); |
91 | | - } |
92 | | - indent(os, ind); |
93 | | - os << "}\n"; |
94 | | - break; |
95 | | - } |
96 | | - case SNodeKind::WHILE: { |
97 | | - auto *w = node->as< SWhile >(); |
98 | | - indent(os, ind); |
99 | | - os << "while ("; |
100 | | - printExpr(w->Cond(), os, ctx); |
101 | | - os << ") {\n"; |
102 | | - printPseudoC(w->Body(), os, ctx, ind + 1); |
103 | | - indent(os, ind); |
104 | | - os << "}\n"; |
105 | | - break; |
106 | | - } |
107 | | - case SNodeKind::DO_WHILE: { |
108 | | - auto *dw = node->as< SDoWhile >(); |
109 | | - indent(os, ind); |
110 | | - os << "do {\n"; |
111 | | - printPseudoC(dw->Body(), os, ctx, ind + 1); |
112 | | - indent(os, ind); |
113 | | - os << "} while ("; |
114 | | - printExpr(dw->Cond(), os, ctx); |
115 | | - os << ");\n"; |
116 | | - break; |
117 | | - } |
118 | | - case SNodeKind::FOR: { |
119 | | - auto *f = node->as< SFor >(); |
120 | | - indent(os, ind); |
121 | | - os << "for ("; |
122 | | - if (f->Init()) { |
123 | | - if (ctx) { |
124 | | - clang::PrintingPolicy pp(ctx->getLangOpts()); |
125 | | - f->Init()->printPretty(os, nullptr, pp); |
126 | | - } else { |
127 | | - os << "<init>"; |
128 | | - } |
129 | | - } |
130 | | - os << "; "; |
131 | | - printExpr(f->Cond(), os, ctx); |
132 | | - os << "; "; |
133 | | - printExpr(f->Inc(), os, ctx); |
134 | | - os << ") {\n"; |
135 | | - printPseudoC(f->Body(), os, ctx, ind + 1); |
136 | | - indent(os, ind); |
137 | | - os << "}\n"; |
138 | | - break; |
139 | | - } |
140 | | - case SNodeKind::SWITCH: { |
141 | | - auto *sw = node->as< SSwitch >(); |
142 | | - indent(os, ind); |
143 | | - os << "switch ("; |
144 | | - printExpr(sw->Discriminant(), os, ctx); |
145 | | - os << ") {\n"; |
146 | | - for (const auto &c : sw->Cases()) { |
147 | | - indent(os, ind); |
148 | | - os << "case "; |
149 | | - printExpr(c.value, os, ctx); |
150 | | - os << ":\n"; |
151 | | - if (c.body) printPseudoC(c.body, os, ctx, ind + 1); |
152 | | - indent(os, ind + 1); |
153 | | - os << "break;\n"; |
154 | | - } |
155 | | - if (sw->DefaultBody()) { |
156 | | - indent(os, ind); |
157 | | - os << "default:\n"; |
158 | | - printPseudoC(sw->DefaultBody(), os, ctx, ind + 1); |
159 | | - } |
160 | | - indent(os, ind); |
161 | | - os << "}\n"; |
162 | | - break; |
163 | | - } |
164 | | - case SNodeKind::GOTO: { |
165 | | - auto *g = node->as< SGoto >(); |
166 | | - indent(os, ind); |
167 | | - os << "goto " << g->Target() << ";\n"; |
168 | | - break; |
169 | | - } |
170 | | - case SNodeKind::LABEL: { |
171 | | - auto *lbl = node->as< SLabel >(); |
172 | | - indent(os, ind > 0 ? ind - 1 : 0); |
173 | | - os << lbl->Name() << ":\n"; |
174 | | - if (lbl->Body()) printPseudoC(lbl->Body(), os, ctx, ind); |
175 | | - break; |
176 | | - } |
177 | | - case SNodeKind::BREAK: { |
178 | | - indent(os, ind); |
179 | | - os << "break;\n"; |
180 | | - break; |
181 | | - } |
182 | | - case SNodeKind::CONTINUE: { |
183 | | - indent(os, ind); |
184 | | - os << "continue;\n"; |
185 | | - break; |
186 | | - } |
187 | | - case SNodeKind::RETURN: { |
188 | | - auto *ret = node->as< SReturn >(); |
189 | | - indent(os, ind); |
190 | | - os << "return"; |
191 | | - if (ret->Value()) { |
192 | | - os << " "; |
193 | | - printExpr(ret->Value(), os, ctx); |
194 | | - } |
195 | | - os << ";\n"; |
196 | | - break; |
197 | | - } |
198 | | - } |
199 | | - } |
200 | | - |
201 | | - // ======================================================================== |
202 | | - // DOT emitter |
203 | | - // ======================================================================== |
204 | | - |
205 | 12 | namespace { |
206 | 13 | struct DotEmitter { |
207 | 14 | llvm::raw_ostream &os; |
@@ -302,128 +109,12 @@ namespace patchestry::ast { |
302 | 109 | }; |
303 | 110 | } // namespace |
304 | 111 |
|
305 | | - void emitDOT(const SNode *node, llvm::raw_ostream &os) { |
| 112 | + void EmitDot(const SNode *node, llvm::raw_ostream &os) { |
306 | 113 | os << "digraph SNodeTree {\n"; |
307 | 114 | os << " node [shape=box, fontname=\"Courier\"];\n"; |
308 | 115 | DotEmitter emitter{os}; |
309 | 116 | emitter.emit(node); |
310 | 117 | os << "}\n"; |
311 | 118 | } |
312 | 119 |
|
313 | | - // ======================================================================== |
314 | | - // JSON emitter |
315 | | - // ======================================================================== |
316 | | - |
317 | | - void emitJSON(const SNode *node, llvm::json::OStream &jos, |
318 | | - clang::ASTContext *ctx) { |
319 | | - if (!node) { jos.value(nullptr); return; } |
320 | | - |
321 | | - jos.object([&] { |
322 | | - jos.attribute("kind", node->KindName()); |
323 | | - |
324 | | - switch (node->Kind()) { |
325 | | - case SNodeKind::SEQ: { |
326 | | - auto *seq = node->as< SSeq >(); |
327 | | - jos.attributeArray("children", [&] { |
328 | | - for (const auto *child : seq->Children()) { |
329 | | - emitJSON(child, jos, ctx); |
330 | | - } |
331 | | - }); |
332 | | - break; |
333 | | - } |
334 | | - case SNodeKind::BLOCK: { |
335 | | - auto *blk = node->as< SBlock >(); |
336 | | - if (!blk->Label().empty()) { |
337 | | - jos.attribute("label", toRef(blk->Label())); |
338 | | - } |
339 | | - jos.attribute("stmt_count", static_cast< int64_t >(blk->Size())); |
340 | | - break; |
341 | | - } |
342 | | - case SNodeKind::IF_THEN_ELSE: { |
343 | | - auto *ite = node->as< SIfThenElse >(); |
344 | | - if (ite->Cond() && ctx) { |
345 | | - std::string cond_str; |
346 | | - llvm::raw_string_ostream rso(cond_str); |
347 | | - ite->Cond()->printPretty(rso, nullptr, ctx->getPrintingPolicy()); |
348 | | - jos.attribute("cond", cond_str); |
349 | | - } |
350 | | - jos.attributeBegin("then"); |
351 | | - emitJSON(ite->ThenBranch(), jos, ctx); |
352 | | - jos.attributeEnd(); |
353 | | - if (ite->ElseBranch()) { |
354 | | - jos.attributeBegin("else"); |
355 | | - emitJSON(ite->ElseBranch(), jos, ctx); |
356 | | - jos.attributeEnd(); |
357 | | - } |
358 | | - break; |
359 | | - } |
360 | | - case SNodeKind::WHILE: { |
361 | | - auto *w = node->as< SWhile >(); |
362 | | - jos.attributeBegin("body"); |
363 | | - emitJSON(w->Body(), jos, ctx); |
364 | | - jos.attributeEnd(); |
365 | | - break; |
366 | | - } |
367 | | - case SNodeKind::DO_WHILE: { |
368 | | - auto *dw = node->as< SDoWhile >(); |
369 | | - jos.attributeBegin("body"); |
370 | | - emitJSON(dw->Body(), jos, ctx); |
371 | | - jos.attributeEnd(); |
372 | | - break; |
373 | | - } |
374 | | - case SNodeKind::FOR: { |
375 | | - auto *f = node->as< SFor >(); |
376 | | - jos.attributeBegin("body"); |
377 | | - emitJSON(f->Body(), jos, ctx); |
378 | | - jos.attributeEnd(); |
379 | | - break; |
380 | | - } |
381 | | - case SNodeKind::SWITCH: { |
382 | | - auto *sw = node->as< SSwitch >(); |
383 | | - jos.attributeArray("cases", [&] { |
384 | | - for (const auto &c : sw->Cases()) { |
385 | | - jos.object([&] { |
386 | | - jos.attributeBegin("body"); |
387 | | - emitJSON(c.body, jos, ctx); |
388 | | - jos.attributeEnd(); |
389 | | - }); |
390 | | - } |
391 | | - }); |
392 | | - if (sw->DefaultBody()) { |
393 | | - jos.attributeBegin("default"); |
394 | | - emitJSON(sw->DefaultBody(), jos, ctx); |
395 | | - jos.attributeEnd(); |
396 | | - } |
397 | | - break; |
398 | | - } |
399 | | - case SNodeKind::GOTO: { |
400 | | - auto *g = node->as< SGoto >(); |
401 | | - jos.attribute("target", toRef(g->Target())); |
402 | | - break; |
403 | | - } |
404 | | - case SNodeKind::LABEL: { |
405 | | - auto *lbl = node->as< SLabel >(); |
406 | | - jos.attribute("name", toRef(lbl->Name())); |
407 | | - jos.attributeBegin("body"); |
408 | | - emitJSON(lbl->Body(), jos, ctx); |
409 | | - jos.attributeEnd(); |
410 | | - break; |
411 | | - } |
412 | | - case SNodeKind::BREAK: { |
413 | | - auto *brk = node->as< SBreak >(); |
414 | | - if (brk->Depth() > 1) |
415 | | - jos.attribute("depth", static_cast< int64_t >(brk->Depth())); |
416 | | - break; |
417 | | - } |
418 | | - case SNodeKind::CONTINUE: |
419 | | - break; |
420 | | - case SNodeKind::RETURN: { |
421 | | - auto *ret = node->as< SReturn >(); |
422 | | - jos.attribute("has_value", ret->Value() != nullptr); |
423 | | - break; |
424 | | - } |
425 | | - } |
426 | | - }); |
427 | | - } |
428 | | - |
429 | 120 | } // namespace patchestry::ast |
0 commit comments