Skip to content

Commit c2719fb

Browse files
committed
[llvm][mustache] Simplify debug logging
1 parent 2819519 commit c2719fb

File tree

1 file changed

+43
-33
lines changed

1 file changed

+43
-33
lines changed

llvm/lib/Support/Mustache.cpp

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,36 @@ struct Tag {
332332
size_t StartPosition = StringRef::npos;
333333
};
334334

335+
static const char *tagKindToString(Tag::Kind K) {
336+
switch (K) {
337+
case Tag::Kind::None:
338+
return "None";
339+
case Tag::Kind::Normal:
340+
return "Normal";
341+
case Tag::Kind::Triple:
342+
return "Triple";
343+
}
344+
llvm_unreachable("Unknown Tag::Kind");
345+
}
346+
347+
static const char *jsonKindToString(json::Value::Kind K) {
348+
switch (K) {
349+
case json::Value::Kind::Null:
350+
return "JSON_KIND_NULL";
351+
case json::Value::Kind::Boolean:
352+
return "JSON_KIND_BOOLEAN";
353+
case json::Value::Kind::Number:
354+
return "JSON_KIND_NUMBER";
355+
case json::Value::Kind::String:
356+
return "JSON_KIND_STRING";
357+
case json::Value::Kind::Array:
358+
return "JSON_KIND_ARRAY";
359+
case json::Value::Kind::Object:
360+
return "JSON_KIND_OBJECT";
361+
}
362+
llvm_unreachable("Unknown json::Value::Kind");
363+
}
364+
335365
static Tag findNextTag(StringRef Template, size_t StartPos, StringRef Open,
336366
StringRef Close) {
337367
const StringLiteral TripleOpen("{{{");
@@ -376,19 +406,17 @@ static Tag findNextTag(StringRef Template, size_t StartPos, StringRef Open,
376406

377407
static std::optional<std::pair<StringRef, StringRef>>
378408
processTag(const Tag &T, SmallVectorImpl<Token> &Tokens) {
379-
LLVM_DEBUG(dbgs() << " Found tag: \"" << T.FullMatch << "\", Content: \""
380-
<< T.Content << "\"\n");
409+
LLVM_DEBUG(dbgs() << "[Tag] " << T.FullMatch << ", Content: " << T.Content
410+
<< ", Kind: " << tagKindToString(T.TagKind) << "\n");
381411
if (T.TagKind == Tag::Kind::Triple) {
382412
Tokens.emplace_back(T.FullMatch.str(), "&" + T.Content.str(), '&');
383-
LLVM_DEBUG(dbgs() << " Created UnescapeVariable token.\n");
384413
return std::nullopt;
385414
}
386415
StringRef Interpolated = T.Content;
387416
std::string RawBody = T.FullMatch.str();
388417
if (!Interpolated.trim().starts_with("=")) {
389418
char Front = Interpolated.empty() ? ' ' : Interpolated.trim().front();
390419
Tokens.emplace_back(RawBody, Interpolated.str(), Front);
391-
LLVM_DEBUG(dbgs() << " Created tag token of type '" << Front << "'\n");
392420
return std::nullopt;
393421
}
394422
Tokens.emplace_back(RawBody, Interpolated.str(), '=');
@@ -398,8 +426,8 @@ processTag(const Tag &T, SmallVectorImpl<Token> &Tokens) {
398426
DelimSpec = DelimSpec.trim();
399427

400428
auto [NewOpen, NewClose] = DelimSpec.split(' ');
401-
LLVM_DEBUG(dbgs() << " Found Set Delimiter tag. NewOpen='" << NewOpen
402-
<< "', NewClose='" << NewClose << "'\n");
429+
LLVM_DEBUG(dbgs() << "[Set Delimiter] NewOpen: " << NewOpen
430+
<< ", NewClose: " << NewClose << "\n");
403431
return std::make_pair(NewOpen, NewClose);
404432
}
405433

@@ -408,14 +436,14 @@ processTag(const Tag &T, SmallVectorImpl<Token> &Tokens) {
408436
// but we don't support that here. An unescape variable
409437
// is represented only by {{& variable}}.
410438
static SmallVector<Token> tokenize(StringRef Template) {
411-
LLVM_DEBUG(dbgs() << "Tokenizing template: \"" << Template << "\"\n");
439+
LLVM_DEBUG(dbgs() << "[Tokenize Template] \"" << Template << "\"\n");
412440
SmallVector<Token> Tokens;
413441
SmallString<8> Open("{{");
414442
SmallString<8> Close("}}");
415443
size_t Start = 0;
416444

417445
while (Start < Template.size()) {
418-
LLVM_DEBUG(dbgs() << "Loop start. Start=" << Start << ", Open='" << Open
446+
LLVM_DEBUG(dbgs() << "[Tokenize Loop] Start=" << Start << ", Open='" << Open
419447
<< "', Close='" << Close << "'\n");
420448
Tag T = findNextTag(Template, Start, Open, Close);
421449

@@ -431,7 +459,6 @@ static SmallVector<Token> tokenize(StringRef Template) {
431459
if (T.StartPosition > Start) {
432460
StringRef Text = Template.substr(Start, T.StartPosition - Start);
433461
Tokens.emplace_back(Text.str());
434-
LLVM_DEBUG(dbgs() << " Created Text token: \"" << Text << "\"\n");
435462
}
436463

437464
if (auto NewDelims = processTag(T, Tokens)) {
@@ -482,7 +509,6 @@ static SmallVector<Token> tokenize(StringRef Template) {
482509
if ((!HasTextBehind && !HasTextAhead) || (!HasTextBehind && Idx == LastIdx))
483510
stripTokenBefore(Tokens, Idx, CurrentToken, CurrentType);
484511
}
485-
LLVM_DEBUG(dbgs() << "Tokenizing finished.\n");
486512
return Tokens;
487513
}
488514

@@ -533,7 +559,7 @@ class AddIndentationStringStream : public MustacheOutputStream {
533559
Indent.resize(Indentation, ' ');
534560

535561
for (char C : Data) {
536-
LLVM_DEBUG(dbgs() << "IndentationStream: NeedsIndent=" << NeedsIndent
562+
LLVM_DEBUG(dbgs() << "[IndentationStream] NeedsIndent=" << NeedsIndent
537563
<< ", C='" << C << "', Indentation=" << Indentation
538564
<< "\n");
539565
if (NeedsIndent && C != '\n') {
@@ -642,7 +668,9 @@ void Parser::parseMustache(ASTNode *Parent) {
642668
}
643669
}
644670
static void toMustacheString(const json::Value &Data, raw_ostream &OS) {
645-
LLVM_DEBUG(dbgs() << "toMustacheString: kind=" << (int)Data.kind() << "\n");
671+
LLVM_DEBUG(dbgs() << "[To Mustache String] Kind: "
672+
<< jsonKindToString(Data.kind()) << ", Data: " << Data
673+
<< "\n");
646674
switch (Data.kind()) {
647675
case json::Value::Null:
648676
return;
@@ -655,7 +683,6 @@ static void toMustacheString(const json::Value &Data, raw_ostream &OS) {
655683
}
656684
case json::Value::String: {
657685
auto Str = *Data.getAsString();
658-
LLVM_DEBUG(dbgs() << " --> writing string: \"" << Str << "\"\n");
659686
OS << Str.str();
660687
return;
661688
}
@@ -684,7 +711,7 @@ void ASTNode::renderText(MustacheOutputStream &OS) { OS << Body; }
684711

685712
void ASTNode::renderPartial(const json::Value &CurrentCtx,
686713
MustacheOutputStream &OS) {
687-
LLVM_DEBUG(dbgs() << "renderPartial: Accessor=" << AccessorValue[0]
714+
LLVM_DEBUG(dbgs() << "[Render Partial] Accessor=" << AccessorValue[0]
688715
<< ", Indentation=" << Indentation << "\n");
689716
auto Partial = Ctx.Partials.find(AccessorValue[0]);
690717
if (Partial != Ctx.Partials.end())
@@ -704,13 +731,12 @@ void ASTNode::renderVariable(const json::Value &CurrentCtx,
704731

705732
void ASTNode::renderUnescapeVariable(const json::Value &CurrentCtx,
706733
MustacheOutputStream &OS) {
707-
LLVM_DEBUG(dbgs() << "renderUnescapeVariable: Accessor=" << AccessorValue[0]
734+
LLVM_DEBUG(dbgs() << "[Render UnescapeVariable] Accessor=" << AccessorValue[0]
708735
<< "\n");
709736
auto Lambda = Ctx.Lambdas.find(AccessorValue[0]);
710737
if (Lambda != Ctx.Lambdas.end()) {
711738
renderLambdas(CurrentCtx, OS, Lambda->getValue());
712739
} else if (const json::Value *ContextPtr = findContext()) {
713-
LLVM_DEBUG(dbgs() << " --> Found context value, writing to stream.\n");
714740
OS.suspendIndentation();
715741
toMustacheString(*ContextPtr, OS);
716742
OS.resumeIndentation();
@@ -780,8 +806,6 @@ void ASTNode::render(const llvm::json::Value &Data, MustacheOutputStream &OS) {
780806
}
781807

782808
const json::Value *ASTNode::findContext() {
783-
LLVM_DEBUG(dbgs() << "findContext: AccessorValue[0]=" << AccessorValue[0]
784-
<< "\n");
785809
// The mustache spec allows for dot notation to access nested values
786810
// a single dot refers to the current context.
787811
// We attempt to find the JSON context in the current node, if it is not
@@ -796,22 +820,12 @@ const json::Value *ASTNode::findContext() {
796820
StringRef CurrentAccessor = AccessorValue[0];
797821
ASTNode *CurrentParent = Parent;
798822

799-
LLVM_DEBUG(dbgs() << "findContext: ParentContext: ";
800-
if (ParentContext) ParentContext->print(dbgs());
801-
else dbgs() << "nullptr"; dbgs() << "\n");
802-
803823
while (!CurrentContext || !CurrentContext->get(CurrentAccessor)) {
804-
LLVM_DEBUG(dbgs() << "findContext: climbing parent\n");
805824
if (CurrentParent->Ty != Root) {
806825
CurrentContext = CurrentParent->ParentContext->getAsObject();
807826
CurrentParent = CurrentParent->Parent;
808-
LLVM_DEBUG(dbgs() << "findContext: new ParentContext: ";
809-
if (CurrentParent->ParentContext)
810-
CurrentParent->ParentContext->print(dbgs());
811-
else dbgs() << "nullptr"; dbgs() << "\n");
812827
continue;
813828
}
814-
LLVM_DEBUG(dbgs() << "findContext: reached root, not found\n");
815829
return nullptr;
816830
}
817831
const json::Value *Context = nullptr;
@@ -827,9 +841,6 @@ const json::Value *ASTNode::findContext() {
827841
Context = CurrentValue;
828842
}
829843
}
830-
LLVM_DEBUG(dbgs() << "findContext: found value: ";
831-
if (Context) Context->print(dbgs()); else dbgs() << "nullptr";
832-
dbgs() << "\n");
833844
return Context;
834845
}
835846

@@ -841,8 +852,7 @@ void ASTNode::renderChild(const json::Value &Contexts,
841852

842853
void ASTNode::renderPartial(const json::Value &Contexts,
843854
MustacheOutputStream &OS, ASTNode *Partial) {
844-
LLVM_DEBUG(dbgs() << "renderPartial (helper): Indentation=" << Indentation
845-
<< "\n");
855+
LLVM_DEBUG(dbgs() << "[Render Partial Indentation] " << Indentation << "\n");
846856
AddIndentationStringStream IS(OS, Indentation);
847857
Partial->render(Contexts, IS);
848858
}

0 commit comments

Comments
 (0)