Refactor all ctor callsites of PathFragment to instead call a static 'create' method.

This paves the way for changing PathFragment to e.g. an abstract class with multiple subclasses. This way we can split out the windows-specific stuff into one of these concrete classes, making the code more readable and also saving memory (since the shallow heap size of the NonWindowsPathFragment subclass will hopefully be smaller than that of the current PathFragment).

This also lets us pursue gc churn optimizations. We can now do interning in PathFragment#create and can also get rid of unnecessary intermediate PathFragment allocations.

RELNOTES: None

PiperOrigin-RevId: 152145768
diff --git a/src/test/java/com/google/devtools/build/lib/pkgcache/IOExceptionsTest.java b/src/test/java/com/google/devtools/build/lib/pkgcache/IOExceptionsTest.java
index 9f5edaf..6472f01 100644
--- a/src/test/java/com/google/devtools/build/lib/pkgcache/IOExceptionsTest.java
+++ b/src/test/java/com/google/devtools/build/lib/pkgcache/IOExceptionsTest.java
@@ -79,7 +79,7 @@
 
   @Override
   protected FileSystem createFileSystem() {
-    return new InMemoryFileSystem(BlazeClock.instance(), new PathFragment(FS_ROOT)) {
+    return new InMemoryFileSystem(BlazeClock.instance(), PathFragment.create(FS_ROOT)) {
       @Override
       public FileStatus stat(Path path, boolean followSymlinks) throws IOException {
         String crash = crashMessage.apply(path);
diff --git a/src/test/java/com/google/devtools/build/lib/pkgcache/IncrementalLoadingTest.java b/src/test/java/com/google/devtools/build/lib/pkgcache/IncrementalLoadingTest.java
index de78d51..bc5800e 100644
--- a/src/test/java/com/google/devtools/build/lib/pkgcache/IncrementalLoadingTest.java
+++ b/src/test/java/com/google/devtools/build/lib/pkgcache/IncrementalLoadingTest.java
@@ -524,7 +524,7 @@
       Path path = workspace.getRelative(fileName);
       Preconditions.checkState(!path.exists());
       FileSystemUtils.createDirectoryAndParents(path.getParentDirectory());
-      path.createSymbolicLink(new PathFragment(target));
+      path.createSymbolicLink(PathFragment.create(target));
       changes.add(path);
     }
 
@@ -546,7 +546,7 @@
       Path symlink = workspace.getRelative(fileName);
       Preconditions.checkState(symlink.exists());
       symlink.delete();
-      symlink.createSymbolicLink(new PathFragment(newTarget));
+      symlink.createSymbolicLink(PathFragment.create(newTarget));
       changes.add(symlink);
     }
 
diff --git a/src/test/java/com/google/devtools/build/lib/pkgcache/LoadingPhaseRunnerTest.java b/src/test/java/com/google/devtools/build/lib/pkgcache/LoadingPhaseRunnerTest.java
index 9cf3254..4ca07f9 100644
--- a/src/test/java/com/google/devtools/build/lib/pkgcache/LoadingPhaseRunnerTest.java
+++ b/src/test/java/com/google/devtools/build/lib/pkgcache/LoadingPhaseRunnerTest.java
@@ -468,8 +468,8 @@
     tester.getWorkspace().getChild("broken").createDirectory();
 
     // Create a circular symlink.
-    tester.getWorkspace().getRelative(new PathFragment("broken/BUILD"))
-        .createSymbolicLink(new PathFragment("BUILD"));
+    tester.getWorkspace().getRelative(PathFragment.create("broken/BUILD"))
+        .createSymbolicLink(PathFragment.create("BUILD"));
 
     assertCircularSymlinksDuringTargetParsing("//broken/...");
   }
@@ -479,10 +479,10 @@
     tester.getWorkspace().getChild("broken").createDirectory();
 
     // Create a circular symlink.
-    tester.getWorkspace().getRelative(new PathFragment("broken/BUILD"))
-        .createSymbolicLink(new PathFragment("x"));
-    tester.getWorkspace().getRelative(new PathFragment("broken/x"))
-        .createSymbolicLink(new PathFragment("BUILD"));
+    tester.getWorkspace().getRelative(PathFragment.create("broken/BUILD"))
+        .createSymbolicLink(PathFragment.create("x"));
+    tester.getWorkspace().getRelative(PathFragment.create("broken/x"))
+        .createSymbolicLink(PathFragment.create("BUILD"));
 
     assertCircularSymlinksDuringTargetParsing("//broken/...");
   }
diff --git a/src/test/java/com/google/devtools/build/lib/pkgcache/TargetPatternEvaluatorIOTest.java b/src/test/java/com/google/devtools/build/lib/pkgcache/TargetPatternEvaluatorIOTest.java
index 3b63fbe..471f884 100644
--- a/src/test/java/com/google/devtools/build/lib/pkgcache/TargetPatternEvaluatorIOTest.java
+++ b/src/test/java/com/google/devtools/build/lib/pkgcache/TargetPatternEvaluatorIOTest.java
@@ -58,7 +58,7 @@
 
   @Override
   protected FileSystem createFileSystem() {
-    return new InMemoryFileSystem(BlazeClock.instance(), new PathFragment(FS_ROOT)) {
+    return new InMemoryFileSystem(BlazeClock.instance(), PathFragment.create(FS_ROOT)) {
       @Override
       public FileStatus stat(Path path, boolean followSymlinks) throws IOException {
         FileStatus defaultResult = super.stat(path, followSymlinks);
diff --git a/src/test/java/com/google/devtools/build/lib/pkgcache/TargetPatternEvaluatorTest.java b/src/test/java/com/google/devtools/build/lib/pkgcache/TargetPatternEvaluatorTest.java
index 33e736a..da003ad 100644
--- a/src/test/java/com/google/devtools/build/lib/pkgcache/TargetPatternEvaluatorTest.java
+++ b/src/test/java/com/google/devtools/build/lib/pkgcache/TargetPatternEvaluatorTest.java
@@ -143,7 +143,7 @@
 
   private void invalidate(String file) throws InterruptedException {
     skyframeExecutor.invalidateFilesUnderPathForTesting(reporter,
-        ModifiedFileSet.builder().modify(new PathFragment(file)).build(), rootDirectory);
+        ModifiedFileSet.builder().modify(PathFragment.create(file)).build(), rootDirectory);
   }
 
   private void invalidate(ModifiedFileSet modifiedFileSet) throws InterruptedException {
@@ -340,9 +340,9 @@
     scratch.file("nest/nest/BUILD",
         "cc_library(name = 'nested2', srcs = [ ])");
 
-    updateOffset(new PathFragment("nest"));
+    updateOffset(PathFragment.create("nest"));
     assertThat(parseList(":all")).containsExactlyElementsIn(labels("//nest:nested1"));
-    updateOffset(new PathFragment("nest/nest"));
+    updateOffset(PathFragment.create("nest/nest"));
     assertThat(parseList(":all")).containsExactlyElementsIn(labels("//nest/nest:nested2"));
   }
 
@@ -556,7 +556,7 @@
     scratch.file("x/z/BUILD", "cc_library(name='z')");
     setDeletedPackages(Sets.newHashSet(PackageIdentifier.createInMainRepo("x/y")));
 
-    parser.updateOffset(new PathFragment("x"));
+    parser.updateOffset(PathFragment.create("x"));
     assertEquals(Sets.newHashSet(Label.parseAbsolute("//x/z")),
         targetsToLabels(getFailFast(parser.parseTargetPattern(parsingListener, "...", false))));
   }
@@ -786,7 +786,7 @@
   public void testSetOffset() throws Exception {
     assertEquals("//foo:foo1", parseIndividualTarget("foo:foo1").toString());
 
-    parser.updateOffset(new PathFragment("foo"));
+    parser.updateOffset(PathFragment.create("foo"));
     assertEquals("//foo:foo1", parseIndividualTarget(":foo1").toString());
   }
 
@@ -970,7 +970,7 @@
     assertThat(parseList("//h/...")).containsExactlyElementsIn(labels("//h"));
 
     ModifiedFileSet modifiedFileSet = ModifiedFileSet.builder()
-        .modify(new PathFragment("h/i/j/BUILD")).build();
+        .modify(PathFragment.create("h/i/j/BUILD")).build();
     invalidate(modifiedFileSet);
 
     assertThat(parseList("//h/...")).containsExactly(Label.parseAbsolute("//h/i/j:j"),
@@ -991,11 +991,11 @@
 
     scratch.file("h/i/j/k/BUILD", "sh_library(name='l')");
     ModifiedFileSet modifiedFileSet = ModifiedFileSet.builder()
-        .modify(new PathFragment("h"))
-        .modify(new PathFragment("h/i"))
-        .modify(new PathFragment("h/i/j"))
-        .modify(new PathFragment("h/i/j/k"))
-        .modify(new PathFragment("h/i/j/k/BUILD"))
+        .modify(PathFragment.create("h"))
+        .modify(PathFragment.create("h/i"))
+        .modify(PathFragment.create("h/i/j"))
+        .modify(PathFragment.create("h/i/j/k"))
+        .modify(PathFragment.create("h/i/j/k/BUILD"))
         .build();
     invalidate(modifiedFileSet);
     reporter.addHandler(failFastHandler);
@@ -1007,7 +1007,7 @@
   public void testBrokenSymlinkRepaired() throws Exception {
     reporter.removeHandler(failFastHandler);
     Path tuv = scratch.dir("t/u/v");
-    tuv.getChild("BUILD").createSymbolicLink(new PathFragment("../../BUILD"));
+    tuv.getChild("BUILD").createSymbolicLink(PathFragment.create("../../BUILD"));
 
     try {
       parseList("//t/...");
@@ -1018,7 +1018,7 @@
 
     scratch.file("t/BUILD", "sh_library(name='t')");
     ModifiedFileSet modifiedFileSet = ModifiedFileSet.builder()
-        .modify(new PathFragment("t/BUILD"))
+        .modify(PathFragment.create("t/BUILD"))
         .build();
 
     invalidate(modifiedFileSet);
@@ -1033,7 +1033,7 @@
   public void testInfiniteTreeFromSymlinks() throws Exception {
     reporter.removeHandler(failFastHandler);
     Path ab = scratch.dir("a/b");
-    ab.getChild("c").createSymbolicLink(new PathFragment("../b"));
+    ab.getChild("c").createSymbolicLink(PathFragment.create("../b"));
     scratch.file("a/b/BUILD", "filegroup(name='g')");
     ResolvedTargets<Target> result = parseTargetPatternList(parser, parsingListener,
         ImmutableList.of("//a/b/..."), true);
@@ -1045,7 +1045,7 @@
   public void testSymlinkCycle() throws Exception {
     reporter.removeHandler(failFastHandler);
     Path ab = scratch.dir("a/b");
-    ab.getChild("c").createSymbolicLink(new PathFragment("c"));
+    ab.getChild("c").createSymbolicLink(PathFragment.create("c"));
     scratch.file("a/b/BUILD", "filegroup(name='g')");
     ResolvedTargets<Target> result = parseTargetPatternList(parser, parsingListener,
         ImmutableList.of("//a/b/..."), true);
@@ -1060,13 +1060,13 @@
     scratch.dir("from-c");
     scratch.file("from-c/BUILD", "filegroup(name = 'from-c')");
     Path ab = scratch.dir("a/b");
-    ab.getChild("symlink").createSymbolicLink(new PathFragment("../../from-b"));
+    ab.getChild("symlink").createSymbolicLink(PathFragment.create("../../from-b"));
     scratch.dir("a/b/not-a-symlink");
     scratch.file("a/b/not-a-symlink/BUILD", "filegroup(name = 'not-a-symlink')");
     scratch.file(
         "a/b/DONT_FOLLOW_SYMLINKS_WHEN_TRAVERSING_THIS_DIRECTORY_VIA_A_RECURSIVE_TARGET_PATTERN");
     Path ac = scratch.dir("a/c");
-    ac.getChild("symlink").createSymbolicLink(new PathFragment("../../from-c"));
+    ac.getChild("symlink").createSymbolicLink(PathFragment.create("../../from-c"));
     ResolvedTargets<Target> result = parseTargetPatternList(parser, parsingListener,
         ImmutableList.of("//a/..."), true);
     assertThat(targetsToLabels(result.getTargets())).containsExactly(