Accept absolute Windows paths without leading slash in GN commands

Leading slash is removed before path is passed to GN when command is run
on MSYS shell. See http://www.mingw.org/wiki/Posix_path_conversion.

BUG=590686

Review URL: https://codereview.chromium.org/1742303002

Cr-Original-Commit-Position: refs/heads/master@{#385686}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 006cf0f95a92cbd6780b9d85fad3e415fd97bec9
diff --git a/tools/gn/label.cc b/tools/gn/label.cc
index 94c0e71..4545525 100644
--- a/tools/gn/label.cc
+++ b/tools/gn/label.cc
@@ -99,16 +99,13 @@
   size_t offset = 0;
 #if defined(OS_WIN)
   if (IsPathAbsolute(input)) {
-    if (input[0] != '/') {
-      *err = Err(original_value, "Bad absolute path.",
-                 "Absolute paths must be of the form /C:\\ but this is \"" +
-                     input.as_string() + "\".");
-      return false;
-    }
-    if (input.size() > 3 && input[2] == ':' && IsSlash(input[3]) &&
-        base::IsAsciiAlpha(input[1])) {
+    size_t drive_letter_pos = input[0] == '/' ? 1 : 0;
+    if (input.size() > drive_letter_pos + 2 &&
+        input[drive_letter_pos + 1] == ':' &&
+        IsSlash(input[drive_letter_pos + 2]) &&
+        base::IsAsciiAlpha(input[drive_letter_pos])) {
       // Skip over the drive letter colon.
-      offset = 3;
+      offset = drive_letter_pos + 2;
     }
   }
 #endif
diff --git a/tools/gn/label_pattern.cc b/tools/gn/label_pattern.cc
index e107ba4..a330b3b 100644
--- a/tools/gn/label_pattern.cc
+++ b/tools/gn/label_pattern.cc
@@ -130,16 +130,12 @@
   size_t offset = 0;
 #if defined(OS_WIN)
   if (IsPathAbsolute(str)) {
-    if (str[0] != '/') {
-      *err = Err(value, "Bad absolute path.",
-                 "Absolute paths must be of the form /C:\\ but this is \"" +
-                     str.as_string() + "\".");
-      return LabelPattern();
-    }
-    if (str.size() > 3 && str[2] == ':' && IsSlash(str[3]) &&
-        base::IsAsciiAlpha(str[1])) {
+    size_t drive_letter_pos = str[0] == '/' ? 1 : 0;
+    if (str.size() > drive_letter_pos + 2 && str[drive_letter_pos + 1] == ':' &&
+        IsSlash(str[drive_letter_pos + 2]) &&
+        base::IsAsciiAlpha(str[drive_letter_pos])) {
       // Skip over the drive letter colon.
-      offset = 3;
+      offset = drive_letter_pos + 2;
     }
   }
 #endif
diff --git a/tools/gn/label_pattern_unittest.cc b/tools/gn/label_pattern_unittest.cc
index 19e2530..2154af1 100644
--- a/tools/gn/label_pattern_unittest.cc
+++ b/tools/gn/label_pattern_unittest.cc
@@ -28,36 +28,46 @@
   SourceDir current_dir("//foo/");
   PatternCase cases[] = {
     // Missing stuff.
-    { "", false, LabelPattern::MATCH, "", "", "" },
-    { ":", false, LabelPattern::MATCH, "", "", "" },
+    {"", false, LabelPattern::MATCH, "", "", ""},
+    {":", false, LabelPattern::MATCH, "", "", ""},
     // Normal things.
-    { ":bar", true, LabelPattern::MATCH, "//foo/", "bar", "" },
-    { "//la:bar", true, LabelPattern::MATCH, "//la/", "bar", "" },
-    { "*", true, LabelPattern::RECURSIVE_DIRECTORY, "", "", "" },
-    { ":*", true, LabelPattern::DIRECTORY, "//foo/", "", "" },
-    { "la:*", true, LabelPattern::DIRECTORY, "//foo/la/", "", "" },
-    { "la/*:*", true, LabelPattern::RECURSIVE_DIRECTORY, "//foo/la/", "", "" },
-    { "//la:*", true, LabelPattern::DIRECTORY, "//la/", "", "" },
-    { "./*", true, LabelPattern::RECURSIVE_DIRECTORY, "//foo/", "", "" },
-    { "foo/*", true, LabelPattern::RECURSIVE_DIRECTORY, "//foo/foo/", "", "" },
-    { "//l/*", true, LabelPattern::RECURSIVE_DIRECTORY, "//l/", "", "" },
+    {":bar", true, LabelPattern::MATCH, "//foo/", "bar", ""},
+    {"//la:bar", true, LabelPattern::MATCH, "//la/", "bar", ""},
+    {"*", true, LabelPattern::RECURSIVE_DIRECTORY, "", "", ""},
+    {":*", true, LabelPattern::DIRECTORY, "//foo/", "", ""},
+    {"la:*", true, LabelPattern::DIRECTORY, "//foo/la/", "", ""},
+    {"la/*:*", true, LabelPattern::RECURSIVE_DIRECTORY, "//foo/la/", "", ""},
+    {"//la:*", true, LabelPattern::DIRECTORY, "//la/", "", ""},
+    {"./*", true, LabelPattern::RECURSIVE_DIRECTORY, "//foo/", "", ""},
+    {"foo/*", true, LabelPattern::RECURSIVE_DIRECTORY, "//foo/foo/", "", ""},
+    {"//l/*", true, LabelPattern::RECURSIVE_DIRECTORY, "//l/", "", ""},
     // Toolchains.
-    { "//foo()", true, LabelPattern::MATCH, "//foo/", "foo", "" },
-    { "//foo(//bar)", true, LabelPattern::MATCH, "//foo/", "foo", "//bar:bar" },
-    { "//foo:*(//bar)", true, LabelPattern::DIRECTORY, "//foo/", "",
-      "//bar:bar" },
-    { "//foo/*(//bar)", true, LabelPattern::RECURSIVE_DIRECTORY, "//foo/", "",
-      "//bar:bar" },
+    {"//foo()", true, LabelPattern::MATCH, "//foo/", "foo", ""},
+    {"//foo(//bar)", true, LabelPattern::MATCH, "//foo/", "foo", "//bar:bar"},
+    {"//foo:*(//bar)", true, LabelPattern::DIRECTORY, "//foo/", "",
+     "//bar:bar"},
+    {"//foo/*(//bar)", true, LabelPattern::RECURSIVE_DIRECTORY, "//foo/", "",
+     "//bar:bar"},
     // Wildcards in invalid places.
-    { "*foo*:bar", false, LabelPattern::MATCH, "", "", "" },
-    { "foo*:*bar", false, LabelPattern::MATCH, "", "", "" },
-    { "*foo:bar", false, LabelPattern::MATCH, "", "", "" },
-    { "foo:bar*", false, LabelPattern::MATCH, "", "", "" },
-    { "*:*", true, LabelPattern::RECURSIVE_DIRECTORY, "", "", "" },
+    {"*foo*:bar", false, LabelPattern::MATCH, "", "", ""},
+    {"foo*:*bar", false, LabelPattern::MATCH, "", "", ""},
+    {"*foo:bar", false, LabelPattern::MATCH, "", "", ""},
+    {"foo:bar*", false, LabelPattern::MATCH, "", "", ""},
+    {"*:*", true, LabelPattern::RECURSIVE_DIRECTORY, "", "", ""},
     // Invalid toolchain stuff.
-    { "//foo(//foo/bar:*)", false, LabelPattern::MATCH, "", "", "" },
-    { "//foo/*(*)", false, LabelPattern::MATCH, "", "", "" },
-    { "//foo(//bar", false, LabelPattern::MATCH, "", "", "" },
+    {"//foo(//foo/bar:*)", false, LabelPattern::MATCH, "", "", ""},
+    {"//foo/*(*)", false, LabelPattern::MATCH, "", "", ""},
+    {"//foo(//bar", false, LabelPattern::MATCH, "", "", ""},
+    // Absolute paths.
+    {"/la/*", true, LabelPattern::RECURSIVE_DIRECTORY, "/la/", "", ""},
+    {"/la:bar", true, LabelPattern::MATCH, "/la/", "bar", ""},
+#if defined(OS_WIN)
+    {"/C:/la/*", true, LabelPattern::RECURSIVE_DIRECTORY, "/C:/la/", "", ""},
+    {"C:/la/*", true, LabelPattern::RECURSIVE_DIRECTORY, "/C:/la/", "", ""},
+    {"/C:/la:bar", true, LabelPattern::MATCH, "/C:/la/", "bar", ""},
+    {"C:/la:bar", true, LabelPattern::MATCH, "/C:/la/", "bar", ""},
+    {"C:foo", true, LabelPattern::MATCH, "//foo/C/", "foo", ""},
+#endif
   };
 
   for (size_t i = 0; i < arraysize(cases); i++) {
diff --git a/tools/gn/label_unittest.cc b/tools/gn/label_unittest.cc
index 37c097f..986aa9b 100644
--- a/tools/gn/label_unittest.cc
+++ b/tools/gn/label_unittest.cc
@@ -27,46 +27,48 @@
 
 TEST(Label, Resolve) {
   ParseDepStringCase cases[] = {
-      // cur         input                        succ   expected dir          name    tc dir    tc name
-      { "//chrome/", "",                          false, "",                   "",     "",       "" },
-      { "//chrome/", "/",                         false, "",                   "",     "",       "" },
-      { "//chrome/", ":",                         false, "",                   "",     "",       "" },
-      { "//chrome/", "/:",                        false, "",                   "",     "",       "" },
-      { "//chrome/", "blah",                      true,  "//chrome/blah/",     "blah", "//t/",   "d" },
-      { "//chrome/", "blah:bar",                  true,  "//chrome/blah/",     "bar",  "//t/",   "d" },
-      // Absolute paths.
-      { "//chrome/", "/chrome:bar",               true , "/chrome/",           "bar",  "//t/",   "d" },
-      { "//chrome/", "/chrome/:bar",              true,  "/chrome/",           "bar",  "//t/",   "d" },
+    {"//chrome/", "", false, "", "", "", ""},
+    {"//chrome/", "/", false, "", "", "", ""},
+    {"//chrome/", ":", false, "", "", "", ""},
+    {"//chrome/", "/:", false, "", "", "", ""},
+    {"//chrome/", "blah", true, "//chrome/blah/", "blah", "//t/", "d"},
+    {"//chrome/", "blah:bar", true, "//chrome/blah/", "bar", "//t/", "d"},
+    // Absolute paths.
+    {"//chrome/", "/chrome:bar", true, "/chrome/", "bar", "//t/", "d"},
+    {"//chrome/", "/chrome/:bar", true, "/chrome/", "bar", "//t/", "d"},
 #if defined(OS_WIN)
-      { "//chrome/", "/C:/chrome:bar",            true , "/C:/chrome/",        "bar",  "//t/",   "d" },
-      { "//chrome/", "/C:/chrome/:bar",           true,  "/C:/chrome/",        "bar",  "//t/",   "d" },
-      { "//chrome/", "C:/chrome:bar",             false, "",                   "",     "",       "" },
+    {"//chrome/", "/C:/chrome:bar", true, "/C:/chrome/", "bar", "//t/", "d"},
+    {"//chrome/", "/C:/chrome/:bar", true, "/C:/chrome/", "bar", "//t/", "d"},
+    {"//chrome/", "C:/chrome:bar", true, "/C:/chrome/", "bar", "//t/", "d"},
 #endif
-      // Refers to root dir.
-      { "//chrome/", "//:bar",                    true,  "//",                 "bar",  "//t/",   "d" },
-      // Implicit directory
-      { "//chrome/", ":bar",                      true,  "//chrome/",          "bar",  "//t/",   "d" },
-      { "//chrome/renderer/", ":bar",             true,  "//chrome/renderer/", "bar",  "//t/",   "d" },
-      // Implicit names.
-      { "//chrome/", "//base",                    true,  "//base/",            "base", "//t/",   "d" },
-      { "//chrome/", "//base/i18n",               true,  "//base/i18n/",       "i18n", "//t/",   "d" },
-      { "//chrome/", "//base/i18n:foo",           true,  "//base/i18n/",       "foo",  "//t/",   "d" },
-      { "//chrome/", "//",                        false, "",                   "",     "",       "" },
-      // Toolchain parsing.
-      { "//chrome/", "//chrome:bar(//t:n)",       true,  "//chrome/",          "bar",  "//t/",   "n" },
-      { "//chrome/", "//chrome:bar(//t)",         true,  "//chrome/",          "bar",  "//t/",   "t" },
-      { "//chrome/", "//chrome:bar(//t:)",        true,  "//chrome/",          "bar",  "//t/",   "t" },
-      { "//chrome/", "//chrome:bar()",            true,  "//chrome/",          "bar",  "//t/",   "d" },
-      { "//chrome/", "//chrome:bar(foo)",         true,  "//chrome/",          "bar",  "//chrome/foo/", "foo" },
-      { "//chrome/", "//chrome:bar(:foo)",        true,  "//chrome/",          "bar",  "//chrome/",     "foo" },
-      // TODO(brettw) it might be nice to make this an error:
-      //{ "//chrome/", "//chrome:bar())",           false, "",                   "",     "",       "" },
-      { "//chrome/", "//chrome:bar(//t:bar(tc))", false, "",                   "",     "",       "" },
-      { "//chrome/", "//chrome:bar(()",           false, "",                   "",     "",       "" },
-      { "//chrome/", "(t:b)",                     false, "",                   "",     "",       "" },
-      { "//chrome/", ":bar(//t/b)",               true,  "//chrome/",          "bar",  "//t/b/", "b" },
-      { "//chrome/", ":bar(/t/b)",                true,  "//chrome/",          "bar",  "/t/b/",  "b" },
-      { "//chrome/", ":bar(t/b)",                 true,  "//chrome/",          "bar",  "//chrome/t/b/", "b" },
+    // Refers to root dir.
+    {"//chrome/", "//:bar", true, "//", "bar", "//t/", "d"},
+    // Implicit directory
+    {"//chrome/", ":bar", true, "//chrome/", "bar", "//t/", "d"},
+    {"//chrome/renderer/", ":bar", true, "//chrome/renderer/", "bar", "//t/",
+     "d"},
+    // Implicit names.
+    {"//chrome/", "//base", true, "//base/", "base", "//t/", "d"},
+    {"//chrome/", "//base/i18n", true, "//base/i18n/", "i18n", "//t/", "d"},
+    {"//chrome/", "//base/i18n:foo", true, "//base/i18n/", "foo", "//t/", "d"},
+    {"//chrome/", "//", false, "", "", "", ""},
+    // Toolchain parsing.
+    {"//chrome/", "//chrome:bar(//t:n)", true, "//chrome/", "bar", "//t/", "n"},
+    {"//chrome/", "//chrome:bar(//t)", true, "//chrome/", "bar", "//t/", "t"},
+    {"//chrome/", "//chrome:bar(//t:)", true, "//chrome/", "bar", "//t/", "t"},
+    {"//chrome/", "//chrome:bar()", true, "//chrome/", "bar", "//t/", "d"},
+    {"//chrome/", "//chrome:bar(foo)", true, "//chrome/", "bar",
+     "//chrome/foo/", "foo"},
+    {"//chrome/", "//chrome:bar(:foo)", true, "//chrome/", "bar", "//chrome/",
+     "foo"},
+    // TODO(brettw) it might be nice to make this an error:
+    //{"//chrome/", "//chrome:bar())", false, "", "", "", "" },
+    {"//chrome/", "//chrome:bar(//t:bar(tc))", false, "", "", "", ""},
+    {"//chrome/", "//chrome:bar(()", false, "", "", "", ""},
+    {"//chrome/", "(t:b)", false, "", "", "", ""},
+    {"//chrome/", ":bar(//t/b)", true, "//chrome/", "bar", "//t/b/", "b"},
+    {"//chrome/", ":bar(/t/b)", true, "//chrome/", "bar", "/t/b/", "b"},
+    {"//chrome/", ":bar(t/b)", true, "//chrome/", "bar", "//chrome/t/b/", "b"},
   };
 
   Label default_toolchain(SourceDir("//t/"), "d");
@@ -82,12 +84,9 @@
         Label::Resolve(SourceDir(cur.cur_dir), default_toolchain, v, &err);
     EXPECT_EQ(cur.success, !err.has_error()) << i << " " << cur.str;
     if (!err.has_error() && cur.success) {
-      EXPECT_EQ(cur.expected_dir, result.dir().value())
-          << i << " " << cur.str;
-      EXPECT_EQ(cur.expected_name, result.name())
-          << i << " " << cur.str;
-      EXPECT_EQ(cur.expected_toolchain_dir,
-                result.toolchain_dir().value())
+      EXPECT_EQ(cur.expected_dir, result.dir().value()) << i << " " << cur.str;
+      EXPECT_EQ(cur.expected_name, result.name()) << i << " " << cur.str;
+      EXPECT_EQ(cur.expected_toolchain_dir, result.toolchain_dir().value())
           << i << " " << cur.str;
       EXPECT_EQ(cur.expected_toolchain_name, result.toolchain_name())
           << i << " " << cur.str;