Skip to content

Commit

Permalink
feat(go_indexer): add support for using file as top-level caller (#5637)
Browse files Browse the repository at this point in the history
  • Loading branch information
schroederc committed May 18, 2023
1 parent 0efe65e commit da6a8bd
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
7 changes: 7 additions & 0 deletions kythe/go/indexer/BUILD
Expand Up @@ -258,6 +258,13 @@ go_indexer_test(
import_path = "test/pkginit",
)

go_indexer_test(
name = "packageinit_file_test",
srcs = ["testdata/basic/packageinit_file.go"],
import_path = "test/pkginit",
use_file_as_top_level_scope = True,
)

go_indexer_test(
name = "types_test",
srcs = ["testdata/types.go"],
Expand Down
2 changes: 2 additions & 0 deletions kythe/go/indexer/cmd/go_indexer/go_indexer.go
Expand Up @@ -52,6 +52,7 @@ var (
verbose = flag.Bool("verbose", false, "Emit verbose log information")
contOnErr = flag.Bool("continue", false, "Log errors encountered during analysis but do not exit unsuccessfully")
useCompilationCorpusForAll = flag.Bool("use_compilation_corpus_for_all", false, "If enabled, all Entry VNames are given the corpus of the compilation unit being indexed. This includes items in the go std library and builtin types.")
useFileAsTopLevelScope = flag.Bool("use_file_as_top_level_scope", false, "If enabled, use the file node for top-level callsite scopes")
overrideStdlibCorpus = flag.String("override_stdlib_corpus", "", "If set, all stdlib nodes are assigned this corpus. Note that this takes precedence over --use_compilation_corpus_for_all")

writeEntry func(context.Context, *spb.Entry) error
Expand Down Expand Up @@ -161,6 +162,7 @@ func indexGo(ctx context.Context, unit *apb.CompilationUnit, f indexer.Fetcher)
DocBase: docURL,
OnlyEmitDocURIsForStandardLibs: *onlyEmitDocURIsForStandardLibs,
UseCompilationCorpusForAll: *useCompilationCorpusForAll,
UseFileAsTopLevelScope: *useFileAsTopLevelScope,
OverrideStdlibCorpus: *overrideStdlibCorpus,
})
}
Expand Down
13 changes: 13 additions & 0 deletions kythe/go/indexer/emit.go
Expand Up @@ -58,6 +58,9 @@ type EmitOptions struct {
// If true, emit childof edges for an anchor's semantic scope.
EmitAnchorScopes bool

// If true, use the enclosing file for top-level callsite scopes.
UseFileAsTopLevelScope bool

// If set, use this as the base URL for links to godoc. The import path is
// appended to the path of this URL to obtain the target URL to link to.
DocBase *url.URL
Expand Down Expand Up @@ -88,6 +91,13 @@ func (e *EmitOptions) emitAnchorScopes() bool {
return e.EmitAnchorScopes
}

func (e *EmitOptions) useFileAsTopLevelScope() bool {
if e == nil {
return false
}
return e.UseFileAsTopLevelScope
}

// shouldEmit reports whether the indexer should emit a node for the given
// vname. Presently this is true if vname denotes a standard library and the
// corresponding option is enabled.
Expand Down Expand Up @@ -1258,6 +1268,9 @@ func (e *emitter) callContext(stack stackFunc) *funcInfo {
case *ast.FuncDecl, *ast.FuncLit:
return e.pi.function[p]
case *ast.File:
if e.opts.useFileAsTopLevelScope() {
return &funcInfo{vname: e.pi.FileVName(p)}
}
fi := e.pi.packageInit[p]
if fi == nil {
// Lazily emit a virtual node to represent the static
Expand Down
12 changes: 12 additions & 0 deletions kythe/go/indexer/testdata/basic/packageinit_file.go
@@ -0,0 +1,12 @@
// Package pkginit tests callgraph relationships for calls occurring at the
// top-level file.
package pkginit

import "fmt"

// Ensure that callsites at the package level are blamed on the file.

//- A=@"fmt.Sprint(27)" ref/call _FmtSprint
//- A childof File
//- File.node/kind file
var p = fmt.Sprint(27)
8 changes: 8 additions & 0 deletions kythe/go/indexer/testdata/go_indexer_test.bzl
Expand Up @@ -172,6 +172,9 @@ def _go_entries(ctx):
if ctx.attr.use_compilation_corpus_for_all:
iargs.append("-use_compilation_corpus_for_all")

if ctx.attr.use_file_as_top_level_scope:
iargs.append("-use_file_as_top_level_scope")

if ctx.attr.override_stdlib_corpus:
iargs.append("-override_stdlib_corpus=%s" % ctx.attr.override_stdlib_corpus)

Expand Down Expand Up @@ -210,6 +213,7 @@ go_entries = rule(
# The suffix used to recognize linkage metadata files, if non-empty.
"metadata_suffix": attr.string(default = ""),
"use_compilation_corpus_for_all": attr.bool(default = False),
"use_file_as_top_level_scope": attr.bool(default = False),
"override_stdlib_corpus": attr.string(default = ""),

# The location of the Go indexer binary.
Expand Down Expand Up @@ -259,6 +263,7 @@ def _go_indexer(
emit_anchor_scopes = False,
allow_duplicates = False,
use_compilation_corpus_for_all = False,
use_file_as_top_level_scope = False,
override_stdlib_corpus = "",
metadata_suffix = "",
extra_extractor_args = []):
Expand All @@ -284,6 +289,7 @@ def _go_indexer(
has_marked_source = has_marked_source,
emit_anchor_scopes = emit_anchor_scopes,
use_compilation_corpus_for_all = use_compilation_corpus_for_all,
use_file_as_top_level_scope = use_file_as_top_level_scope,
override_stdlib_corpus = override_stdlib_corpus,
kzip = ":" + kzip,
metadata_suffix = metadata_suffix,
Expand All @@ -305,6 +311,7 @@ def go_indexer_test(
emit_anchor_scopes = False,
allow_duplicates = False,
use_compilation_corpus_for_all = False,
use_file_as_top_level_scope = False,
override_stdlib_corpus = "",
metadata_suffix = "",
extra_extractor_args = []):
Expand All @@ -315,6 +322,7 @@ def go_indexer_test(
has_marked_source = has_marked_source,
emit_anchor_scopes = emit_anchor_scopes,
use_compilation_corpus_for_all = use_compilation_corpus_for_all,
use_file_as_top_level_scope = use_file_as_top_level_scope,
override_stdlib_corpus = override_stdlib_corpus,
importpath = import_path,
metadata_suffix = metadata_suffix,
Expand Down

0 comments on commit da6a8bd

Please sign in to comment.