Repository: kythe/kythe
Pull request #5973 (2023-12-18 21:54:36)

feat(extraction): allow using a capturing group with generic bazel extractor
This allows for stripping the argument name from arguments like `--src=`


Patch:
diff --git a/kythe/go/extractors/bazel/utils.go b/kythe/go/extractors/bazel/utils.go
index e29efbde8..958e4b1ea 100644
--- a/kythe/go/extractors/bazel/utils.go
+++ b/kythe/go/extractors/bazel/utils.go
@@ -122,6 +122,8 @@ func AddDetail(unit *apb.CompilationUnit, msg proto.Message) error {
 // FindSourceArgs returns a fixup that scans the argument list of a compilation
 // unit for strings matching r. Any that are found, and which also match the
 // names of required input files, are added to the source files of the unit.
+// If the pattern has a single capturing group, the contents of that group will
+// be used rather than the entire matchin.
 func FindSourceArgs(r *regexp.Regexp) func(*apb.CompilationUnit) error {
 	return func(cu *apb.CompilationUnit) error {
 		var inputs stringset.Set
@@ -131,7 +133,17 @@ func FindSourceArgs(r *regexp.Regexp) func(*apb.CompilationUnit) error {
 
 		srcs := stringset.New(cu.SourceFile...)
 		for _, arg := range cu.Argument {
-			if r.MatchString(arg) && inputs.Contains(arg) {
+			switch matches := r.FindStringSubmatch(arg); len(matches) {
+			case 0: // No match.
+				continue
+			case 1: // No capturing groups.
+				break
+			case 2: // Exactly one capturing group.
+				arg = matches[1]
+			default: // More than one capturing group.
+				log.Infof("%v contains multiple capturing groups; using full string %s", r, arg)
+			}
+			if inputs.Contains(arg) {
 				srcs.Add(arg)
 			}
 		}


Test patch:
diff --git a/kythe/go/extractors/bazel/extractor_test.go b/kythe/go/extractors/bazel/extractor_test.go
index ce9d9407a..35d4746c8 100644
--- a/kythe/go/extractors/bazel/extractor_test.go
+++ b/kythe/go/extractors/bazel/extractor_test.go
@@ -316,24 +316,51 @@ func TestFetchInputs(t *testing.T) {
 }
 
 func TestFindSourceArgs(t *testing.T) {
-	unit := &apb.CompilationUnit{
-		RequiredInput: []*apb.CompilationUnit_FileInput{{
-			Info: &apb.FileInfo{Path: "a/b/c.go"},
-		}, {
-			Info: &apb.FileInfo{Path: "d/e/f.cc"},
-		}, {
-			Info: &apb.FileInfo{Path: "old"},
-		}},
-		SourceFile: []string{"old"},
-		// Matches:        no      yes, keep   yes, skip   no
-		Argument: []string{"blah", "a/b/c.go", "p/d/q.go", "quux"},
+	tests := []struct {
+		unit *apb.CompilationUnit
+		r    *regexp.Regexp
+		want []string
+	}{
+		{
+			unit: &apb.CompilationUnit{
+				RequiredInput: []*apb.CompilationUnit_FileInput{{
+					Info: &apb.FileInfo{Path: "a/b/c.go"},
+				}, {
+					Info: &apb.FileInfo{Path: "d/e/f.cc"},
+				}, {
+					Info: &apb.FileInfo{Path: "old"},
+				}},
+				SourceFile: []string{"old"},
+				// Matches:        no      yes, keep   yes, skip   no
+				Argument: []string{"blah", "a/b/c.go", "p/d/q.go", "quux"},
+			},
+			r: regexp.MustCompile(`\.go$`),
+			// Results:      new         extant
+			want: []string{"a/b/c.go", "old"},
+		},
+		{
+			unit: &apb.CompilationUnit{
+				RequiredInput: []*apb.CompilationUnit_FileInput{{
+					Info: &apb.FileInfo{Path: "a/b/c.go"},
+				}, {
+					Info: &apb.FileInfo{Path: "d/e/f.cc"},
+				}, {
+					Info: &apb.FileInfo{Path: "old"},
+				}},
+				SourceFile: []string{"old"},
+				// Matches:        no      yes, keep   yes, skip   no
+				Argument: []string{"blah", "--src=a/b/c.go", "--src=p/d/q.go", "quux"},
+			},
+			r: regexp.MustCompile(`^--src=(.+\.go)$`),
+			// Results:      new         extant
+			want: []string{"a/b/c.go", "old"},
+		},
 	}
-	// Results:      new         extant
-	want := []string{"a/b/c.go", "old"}
 
-	r := regexp.MustCompile(`\.go$`)
-	FindSourceArgs(r)(unit)
-	if got := unit.SourceFile; !reflect.DeepEqual(got, want) {
-		t.Errorf("FindSourceArgs: got %+q, want %+q", got, want)
+	for _, test := range tests {
+		FindSourceArgs(test.r)(test.unit)
+		if got := test.unit.SourceFile; !reflect.DeepEqual(got, test.want) {
+			t.Errorf("FindSourceArgs: got %+q, want %+q", got, test.want)
+		}
 	}
 }