[unused vars] Improve error messages in templates
This adds the location of the invocation that encountered the
unused var to the stack of 'whence it was called' lines.
Change-Id: I3185372750d15171aa7650d1bf4618d3d6e076eb
diff --git a/src/gn/scope.cc b/src/gn/scope.cc
index 24888cf..d07426a 100644
--- a/src/gn/scope.cc
+++ b/src/gn/scope.cc
@@ -259,6 +259,17 @@
"You set the variable \"" + std::string(pair.first) +
"\" here and it was unused before it went\nout of scope.";
+
+ // Gather the template invocations that led up to this scope.
+ auto entries = GetTemplateInvocationEntries();
+ if (entries.size() != 0) {
+
+ help.append("\n\nVia these template invocations:\n");
+ for (const auto& entry : entries) {
+ help.append(" " + entry.Describe() + "\n");
+ }
+ }
+
const BinaryOpNode* binary = pair.second.value.origin()->AsBinaryOp();
if (binary && binary->op().type() == Token::EQUAL) {
// Make a nicer error message for normal var sets.
diff --git a/src/gn/template.cc b/src/gn/template.cc
index deafa33..d2c6fc6 100644
--- a/src/gn/template.cc
+++ b/src/gn/template.cc
@@ -116,13 +116,21 @@
invoker_value = template_scope.GetMutableValue(variables::kInvoker,
Scope::SEARCH_NESTED, false);
if (invoker_value && invoker_value->type() == Value::SCOPE) {
- if (!invoker_value->scope_value()->CheckForUnusedVars(err))
+ if (!invoker_value->scope_value()->CheckForUnusedVars(err)) {
+ // If there was an error, append the caller location so the error message
+ // displays a stack trace of how it got here.
+ err->AppendSubErr(Err(invocation, "whence it was called."));
return Value();
+ }
}
// Check for unused variables in the template itself.
- if (!template_scope.CheckForUnusedVars(err))
+ if (!template_scope.CheckForUnusedVars(err)) {
+ // If there was an error, append the caller location so the error message
+ // displays a stack trace of how it got here.
+ err->AppendSubErr(Err(invocation, "whence it was called."));
return Value();
+ }
return result;
}