Skip to content

Commit 9ce0dae

Browse files
authored
[llvm][mustache] Simplify debug logging (#159193)
The existing logging was inconsistent, and we logged too many things. This PR introduces a more principled schema, and eliminates many, redundant log lines.
1 parent 0aa7da0 commit 9ce0dae

File tree

1 file changed

+46
-36
lines changed

1 file changed

+46
-36
lines changed

llvm/lib/Support/Mustache.cpp

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,36 @@ struct Tag {
329329
size_t StartPosition = StringRef::npos;
330330
};
331331

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

374404
static std::optional<std::pair<StringRef, StringRef>>
375405
processTag(const Tag &T, SmallVectorImpl<Token> &Tokens) {
376-
LLVM_DEBUG(dbgs() << " Found tag: \"" << T.FullMatch << "\", Content: \""
377-
<< T.Content << "\"\n");
406+
LLVM_DEBUG(dbgs() << "[Tag] " << T.FullMatch << ", Content: " << T.Content
407+
<< ", Kind: " << tagKindToString(T.TagKind) << "\n");
378408
if (T.TagKind == Tag::Kind::Triple) {
379409
Tokens.emplace_back(T.FullMatch.str(), "&" + T.Content.str(), '&');
380-
LLVM_DEBUG(dbgs() << " Created UnescapeVariable token.\n");
381410
return std::nullopt;
382411
}
383412
StringRef Interpolated = T.Content;
384413
std::string RawBody = T.FullMatch.str();
385414
if (!Interpolated.trim().starts_with("=")) {
386415
char Front = Interpolated.empty() ? ' ' : Interpolated.trim().front();
387416
Tokens.emplace_back(RawBody, Interpolated.str(), Front);
388-
LLVM_DEBUG(dbgs() << " Created tag token of type '" << Front << "'\n");
389417
return std::nullopt;
390418
}
391419
Tokens.emplace_back(RawBody, Interpolated.str(), '=');
@@ -395,8 +423,8 @@ processTag(const Tag &T, SmallVectorImpl<Token> &Tokens) {
395423
DelimSpec = DelimSpec.trim();
396424

397425
std::pair<StringRef, StringRef> Ret = DelimSpec.split(' ');
398-
LLVM_DEBUG(dbgs() << " Found Set Delimiter tag. NewOpen='" << Ret.first
399-
<< "', NewClose='" << Ret.second << "'\n");
426+
LLVM_DEBUG(dbgs() << "[Set Delimiter] NewOpen: " << Ret.first
427+
<< ", NewClose: " << Ret.second << "\n");
400428
return Ret;
401429
}
402430

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

414442
while (Start < Template.size()) {
415-
LLVM_DEBUG(dbgs() << "Loop start. Start=" << Start << ", Open='" << Open
416-
<< "', Close='" << Close << "'\n");
443+
LLVM_DEBUG(dbgs() << "[Tokenize Loop] Start:" << Start << ", Open:'" << Open
444+
<< "', Close:'" << Close << "'\n");
417445
Tag T = findNextTag(Template, Start, Open, Close);
418446

419447
if (T.TagKind == Tag::Kind::None) {
@@ -428,7 +456,6 @@ static SmallVector<Token> tokenize(StringRef Template) {
428456
if (T.StartPosition > Start) {
429457
StringRef Text = Template.substr(Start, T.StartPosition - Start);
430458
Tokens.emplace_back(Text.str());
431-
LLVM_DEBUG(dbgs() << " Created Text token: \"" << Text << "\"\n");
432459
}
433460

434461
if (auto NewDelims = processTag(T, Tokens)) {
@@ -479,7 +506,6 @@ static SmallVector<Token> tokenize(StringRef Template) {
479506
if ((!HasTextBehind && !HasTextAhead) || (!HasTextBehind && Idx == LastIdx))
480507
stripTokenBefore(Tokens, Idx, CurrentToken, CurrentType);
481508
}
482-
LLVM_DEBUG(dbgs() << "Tokenizing finished.\n");
483509
return Tokens;
484510
}
485511

@@ -545,8 +571,8 @@ class AddIndentationStringStream : public MustacheOutputStream {
545571
Indent.resize(Indentation, ' ');
546572

547573
for (char C : Data) {
548-
LLVM_DEBUG(dbgs() << "IndentationStream: NeedsIndent=" << NeedsIndent
549-
<< ", C='" << C << "', Indentation=" << Indentation
574+
LLVM_DEBUG(dbgs() << "[Indentation Stream] NeedsIndent:" << NeedsIndent
575+
<< ", C:'" << C << "', Indentation:" << Indentation
550576
<< "\n");
551577
if (NeedsIndent && C != '\n') {
552578
WrappedStream << Indent;
@@ -654,7 +680,9 @@ void Parser::parseMustache(ASTNode *Parent) {
654680
}
655681
}
656682
static void toMustacheString(const json::Value &Data, raw_ostream &OS) {
657-
LLVM_DEBUG(dbgs() << "toMustacheString: kind=" << (int)Data.kind() << "\n");
683+
LLVM_DEBUG(dbgs() << "[To Mustache String] Kind: "
684+
<< jsonKindToString(Data.kind()) << ", Data: " << Data
685+
<< "\n");
658686
switch (Data.kind()) {
659687
case json::Value::Null:
660688
return;
@@ -667,7 +695,6 @@ static void toMustacheString(const json::Value &Data, raw_ostream &OS) {
667695
}
668696
case json::Value::String: {
669697
auto Str = *Data.getAsString();
670-
LLVM_DEBUG(dbgs() << " --> writing string: \"" << Str << "\"\n");
671698
OS << Str.str();
672699
return;
673700
}
@@ -696,8 +723,8 @@ void ASTNode::renderText(MustacheOutputStream &OS) { OS << Body; }
696723

697724
void ASTNode::renderPartial(const json::Value &CurrentCtx,
698725
MustacheOutputStream &OS) {
699-
LLVM_DEBUG(dbgs() << "renderPartial: Accessor=" << AccessorValue[0]
700-
<< ", Indentation=" << Indentation << "\n");
726+
LLVM_DEBUG(dbgs() << "[Render Partial] Accessor:" << AccessorValue[0]
727+
<< ", Indentation:" << Indentation << "\n");
701728
auto Partial = Ctx.Partials.find(AccessorValue[0]);
702729
if (Partial != Ctx.Partials.end())
703730
renderPartial(CurrentCtx, OS, Partial->getValue().get());
@@ -716,13 +743,12 @@ void ASTNode::renderVariable(const json::Value &CurrentCtx,
716743

717744
void ASTNode::renderUnescapeVariable(const json::Value &CurrentCtx,
718745
MustacheOutputStream &OS) {
719-
LLVM_DEBUG(dbgs() << "renderUnescapeVariable: Accessor=" << AccessorValue[0]
746+
LLVM_DEBUG(dbgs() << "[Render UnescapeVariable] Accessor:" << AccessorValue[0]
720747
<< "\n");
721748
auto Lambda = Ctx.Lambdas.find(AccessorValue[0]);
722749
if (Lambda != Ctx.Lambdas.end()) {
723750
renderLambdas(CurrentCtx, OS, Lambda->getValue());
724751
} else if (const json::Value *ContextPtr = findContext()) {
725-
LLVM_DEBUG(dbgs() << " --> Found context value, writing to stream.\n");
726752
OS.suspendIndentation();
727753
toMustacheString(*ContextPtr, OS);
728754
OS.resumeIndentation();
@@ -792,8 +818,6 @@ void ASTNode::render(const llvm::json::Value &Data, MustacheOutputStream &OS) {
792818
}
793819

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

811-
LLVM_DEBUG(dbgs() << "findContext: ParentContext: ";
812-
if (ParentContext) ParentContext->print(dbgs());
813-
else dbgs() << "nullptr"; dbgs() << "\n");
814-
815835
while (!CurrentContext || !CurrentContext->get(CurrentAccessor)) {
816-
LLVM_DEBUG(dbgs() << "findContext: climbing parent\n");
817836
if (CurrentParent->Ty != Root) {
818837
CurrentContext = CurrentParent->ParentContext->getAsObject();
819838
CurrentParent = CurrentParent->Parent;
820-
LLVM_DEBUG(dbgs() << "findContext: new ParentContext: ";
821-
if (CurrentParent->ParentContext)
822-
CurrentParent->ParentContext->print(dbgs());
823-
else dbgs() << "nullptr"; dbgs() << "\n");
824839
continue;
825840
}
826-
LLVM_DEBUG(dbgs() << "findContext: reached root, not found\n");
827841
return nullptr;
828842
}
829843
const json::Value *Context = nullptr;
@@ -839,9 +853,6 @@ const json::Value *ASTNode::findContext() {
839853
Context = CurrentValue;
840854
}
841855
}
842-
LLVM_DEBUG(dbgs() << "findContext: found value: ";
843-
if (Context) Context->print(dbgs()); else dbgs() << "nullptr";
844-
dbgs() << "\n");
845856
return Context;
846857
}
847858

@@ -853,8 +864,7 @@ void ASTNode::renderChild(const json::Value &Contexts,
853864

854865
void ASTNode::renderPartial(const json::Value &Contexts,
855866
MustacheOutputStream &OS, ASTNode *Partial) {
856-
LLVM_DEBUG(dbgs() << "renderPartial (helper): Indentation=" << Indentation
857-
<< "\n");
867+
LLVM_DEBUG(dbgs() << "[Render Partial Indentation] Indentation: " << Indentation << "\n");
858868
AddIndentationStringStream IS(OS, Indentation);
859869
Partial->render(Contexts, IS);
860870
}

0 commit comments

Comments
 (0)