Create a function to calculate relative labels.

This can be used wherever we need to calculate `gn edit` commands.

Change-Id: If7f23ca125bb46bbd9a3e7aeb623043c6a6a6964
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/28820
Reviewed-by: Takuto Ikuta <tikuta@google.com>
Commit-Queue: Matt Stark <msta@google.com>
diff --git a/src/gn/command_suggest.cc b/src/gn/command_suggest.cc
index 5442eb3..d2a37c5 100644
--- a/src/gn/command_suggest.cc
+++ b/src/gn/command_suggest.cc
@@ -904,9 +904,7 @@
               "and remove that dependency.\n");
         }
       }
-      std::string label_str = label.dir() == includer->label().dir()
-                                  ? ":" + label.name()
-                                  : label.GetUserVisibleName(current_toolchain);
+      std::string label_str = label.AsRelative(includer->label());
       EditCommand edit{
           .command = {"add", std::string(dep_field), label_str},
           .target = includer->label().GetUserVisibleName(current_toolchain),
diff --git a/src/gn/label.cc b/src/gn/label.cc
index 887f135..c179cf9 100644
--- a/src/gn/label.cc
+++ b/src/gn/label.cc
@@ -23,6 +23,32 @@
   return dir.value();
 }
 
+std::string FormatLabel(const Label& label,
+                        bool include_dir,
+                        bool include_toolchain) {
+  std::string ret;
+  if (label.dir().is_null())
+    return ret;
+
+  ret.reserve((include_dir ? label.dir().value().size() : 0) +
+              label.name().size() + 1);
+  if (include_dir)
+    ret = DirWithNoTrailingSlash(label.dir());
+  ret.push_back(':');
+  ret.append(label.name());
+
+  if (include_toolchain) {
+    ret.push_back('(');
+    if (!label.toolchain_dir().is_null() && !label.toolchain_name().empty()) {
+      ret.append(DirWithNoTrailingSlash(label.toolchain_dir()));
+      ret.push_back(':');
+      ret.append(label.toolchain_name());
+    }
+    ret.push_back(')');
+  }
+  return ret;
+}
+
 // Given the separate-out input (everything before the colon) in the dep rule,
 // computes the final build rule. Sets err on failure. On success,
 // |*used_implicit| will be set to whether the implicit current directory was
@@ -326,26 +352,7 @@
 }
 
 std::string Label::GetUserVisibleName(bool include_toolchain) const {
-  std::string ret;
-  ret.reserve(dir_.value().size() + name_.str().size() + 1);
-
-  if (dir_.is_null())
-    return ret;
-
-  ret = DirWithNoTrailingSlash(dir_);
-  ret.push_back(':');
-  ret.append(name_.str());
-
-  if (include_toolchain) {
-    ret.push_back('(');
-    if (!toolchain_dir_.is_null() && !toolchain_name_.empty()) {
-      ret.append(DirWithNoTrailingSlash(toolchain_dir_));
-      ret.push_back(':');
-      ret.append(toolchain_name_.str());
-    }
-    ret.push_back(')');
-  }
-  return ret;
+  return FormatLabel(*this, /*include_dir=*/true, include_toolchain);
 }
 
 std::string Label::GetUserVisibleName(const Label& default_toolchain) const {
@@ -354,6 +361,11 @@
   return GetUserVisibleName(include_toolchain);
 }
 
+std::string Label::AsRelative(const Label& other) const {
+  return FormatLabel(*this, /*include_dir=*/dir_ != other.dir(),
+                     !ToolchainsEqual(other));
+}
+
 std::string Pretty(const Label& label) {
   return label.GetUserVisibleName(true);
 }
diff --git a/src/gn/label.h b/src/gn/label.h
index 4ce1882..12b602b 100644
--- a/src/gn/label.h
+++ b/src/gn/label.h
@@ -69,6 +69,11 @@
   // non-default ones, so this can make certain output more clear.
   std::string GetUserVisibleName(const Label& default_toolchain) const;
 
+  // Returns a string that can be used to refer to this label from other.
+  // * Includes a toolchain iff the toolchains differ.
+  // * Uses a relative label iff the directories match.
+  std::string AsRelative(const Label& other) const;
+
   // Return true if this instance matches |other|. |default_toolchain| is
   // used when either label doesn't have a toolchain suffix to ensure
   // canonical and non-canonical labels are compared correctly.
diff --git a/src/gn/label_unittest.cc b/src/gn/label_unittest.cc
index 3cdad5f..91d4724 100644
--- a/src/gn/label_unittest.cc
+++ b/src/gn/label_unittest.cc
@@ -288,3 +288,39 @@
         << "expected match: " << (test_case.expected ? "true" : "false");
   }
 }
+
+TEST(Label, AsRelative) {
+  const SourceDir dir_a("//a/");
+  const SourceDir dir_b("//b/");
+  const SourceDir root_dir("//");
+  const SourceDir tc_dir("//t/");
+  const SourceDir alt_tc_dir("//u/");
+
+  const struct TestCase {
+    Label label;
+    Label other;
+    const char* expected;
+  } test_cases[] = {
+      // Same directory and toolchain.
+      {Label(dir_a, "foo", tc_dir, "default"),
+       Label(dir_a, "bar", tc_dir, "default"), ":foo"},
+      // Root directory and same toolchain.
+      {Label(root_dir, "foo", tc_dir, "default"),
+       Label(root_dir, "bar", tc_dir, "default"), ":foo"},
+      // Different directory, same toolchain.
+      {Label(dir_a, "foo", tc_dir, "default"),
+       Label(dir_b, "bar", tc_dir, "default"), "//a:foo"},
+      {Label(root_dir, "foo", tc_dir, "default"),
+       Label(dir_b, "bar", tc_dir, "default"), "//:foo"},
+      // Same directory, different toolchain.
+      {Label(dir_a, "foo", alt_tc_dir, "alt"),
+       Label(dir_a, "bar", tc_dir, "default"), ":foo(//u:alt)"},
+      // Different directory and toolchain.
+      {Label(dir_a, "foo", alt_tc_dir, "alt"),
+       Label(dir_b, "bar", tc_dir, "default"), "//a:foo(//u:alt)"},
+  };
+
+  for (const auto& test_case : test_cases) {
+    EXPECT_EQ(test_case.expected, test_case.label.AsRelative(test_case.other));
+  }
+}