[error messages] Centralize decision logic for showing toolchains in errors

Create a central implementation for determining if a set
of labels need to show their toolchain or not when
creating error messages.

Change-Id: I9015340930e9e0a2e91f7aa9704a141464d60ce7
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/14700
Commit-Queue: Brett Wilson <brettw@chromium.org>
Reviewed-by: Brett Wilson <brettw@chromium.org>
diff --git a/src/gn/settings.cc b/src/gn/settings.cc
index 154e0af..c9a36c9 100644
--- a/src/gn/settings.cc
+++ b/src/gn/settings.cc
@@ -27,3 +27,12 @@
   if (!toolchain_output_dir_.is_null())
     toolchain_gen_dir_ = SourceDir(toolchain_output_dir_.value() + "gen/");
 }
+
+bool Settings::ShouldShowToolchain(
+    std::initializer_list<const Label*> labels) const {
+  for (auto label : labels) {
+    if (label->GetToolchainLabel() != default_toolchain_label_)
+      return true;
+  }
+  return false;
+}
diff --git a/src/gn/settings.h b/src/gn/settings.h
index 9b83b60..79d1904 100644
--- a/src/gn/settings.h
+++ b/src/gn/settings.h
@@ -86,6 +86,11 @@
     greedy_target_generation_ = gtg;
   }
 
+  // Returns true if any Label in the list provided is not in the default
+  // toolchain, and therefore any error message that involves this list of
+  // Labels should print the toolchain for each Label.
+  bool ShouldShowToolchain(std::initializer_list<const Label*> labels) const;
+
  private:
   const BuildSettings* build_settings_;
 
diff --git a/src/gn/visibility.cc b/src/gn/visibility.cc
index acbb500..61423ff 100644
--- a/src/gn/visibility.cc
+++ b/src/gn/visibility.cc
@@ -92,15 +92,21 @@
                                      const Item* to,
                                      Err* err) {
   if (!to->visibility().CanSeeMe(from->label())) {
-    bool with_toolchain =
-        !from->settings()->is_default() || !to->settings()->is_default();
+    bool with_toolchain = from->settings()->ShouldShowToolchain({
+        &to->label(),
+        &from->label(),
+    });
     std::string to_label = to->label().GetUserVisibleName(with_toolchain);
     std::string from_label = from->label().GetUserVisibleName(with_toolchain);
     *err = Err(from->defined_from(), "Dependency not allowed.",
-               "The item " + from_label + "\n"
-                   "can not depend on " + to_label + "\n"
-                   "because it is not in " + to_label + "'s visibility list: " +
-                   to->visibility().Describe(0, true));
+               "The item " + from_label +
+                   "\n"
+                   "can not depend on " +
+                   to_label +
+                   "\n"
+                   "because it is not in " +
+                   to_label +
+                   "'s visibility list: " + to->visibility().Describe(0, true));
     return false;
   }
   return true;