Commit c7cdce13 authored by Robert Griesemer's avatar Robert Griesemer

godoc: make ?m=src mode deterministic

Merge package files in the go/ast MergePackageFiles
function always	    in the same order (sorted by filename)
instead	 of map iteration order to obtain the same
package	 file each time.  This functionality is used
by godoc when displaying packages in ?m=src mode.

Also: minor cleanup in godoc.go.

R=rsc
CC=golang-dev
https://golang.org/cl/5540054
parent 1627b46e
...@@ -1086,18 +1086,18 @@ func (h *httpHandler) getPageInfo(abspath, relpath, pkgname string, mode PageInf ...@@ -1086,18 +1086,18 @@ func (h *httpHandler) getPageInfo(abspath, relpath, pkgname string, mode PageInf
var past *ast.File var past *ast.File
var pdoc *doc.Package var pdoc *doc.Package
if pkg != nil { if pkg != nil {
var docMode doc.Mode
if mode&noFiltering != 0 {
docMode = doc.AllDecls
}
if mode&showSource == 0 { if mode&showSource == 0 {
// show extracted documentation // show extracted documentation
pdoc = doc.New(pkg, path.Clean(relpath), docMode) // no trailing '/' in importpath var m doc.Mode
if mode&noFiltering != 0 {
m = doc.AllDecls
}
pdoc = doc.New(pkg, path.Clean(relpath), m) // no trailing '/' in importpath
} else { } else {
// show source code // show source code
// TODO(gri) Consider eliminating export filtering in this mode, // TODO(gri) Consider eliminating export filtering in this mode,
// or perhaps eliminating the mode altogether. // or perhaps eliminating the mode altogether.
if docMode&doc.AllDecls == 0 { if mode&noFiltering == 0 {
ast.PackageExports(pkg) ast.PackageExports(pkg)
} }
past = ast.MergePackageFiles(pkg, ast.FilterUnassociatedComments) past = ast.MergePackageFiles(pkg, ast.FilterUnassociatedComments)
......
...@@ -4,7 +4,10 @@ ...@@ -4,7 +4,10 @@
package ast package ast
import "go/token" import (
"go/token"
"sort"
)
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Export filtering // Export filtering
...@@ -291,29 +294,35 @@ var separator = &Comment{noPos, "//"} ...@@ -291,29 +294,35 @@ var separator = &Comment{noPos, "//"}
// //
func MergePackageFiles(pkg *Package, mode MergeMode) *File { func MergePackageFiles(pkg *Package, mode MergeMode) *File {
// Count the number of package docs, comments and declarations across // Count the number of package docs, comments and declarations across
// all package files. // all package files. Also, compute sorted list of filenames, so that
// subsequent iterations can always iterate in the same order.
ndocs := 0 ndocs := 0
ncomments := 0 ncomments := 0
ndecls := 0 ndecls := 0
for _, f := range pkg.Files { filenames := make([]string, len(pkg.Files))
i := 0
for filename, f := range pkg.Files {
filenames[i] = filename
i++
if f.Doc != nil { if f.Doc != nil {
ndocs += len(f.Doc.List) + 1 // +1 for separator ndocs += len(f.Doc.List) + 1 // +1 for separator
} }
ncomments += len(f.Comments) ncomments += len(f.Comments)
ndecls += len(f.Decls) ndecls += len(f.Decls)
} }
sort.Strings(filenames)
// Collect package comments from all package files into a single // Collect package comments from all package files into a single
// CommentGroup - the collected package documentation. The order // CommentGroup - the collected package documentation. In general
// is unspecified. In general there should be only one file with // there should be only one file with a package comment; but it's
// a package comment; but it's better to collect extra comments // better to collect extra comments than drop them on the floor.
// than drop them on the floor.
var doc *CommentGroup var doc *CommentGroup
var pos token.Pos var pos token.Pos
if ndocs > 0 { if ndocs > 0 {
list := make([]*Comment, ndocs-1) // -1: no separator before first group list := make([]*Comment, ndocs-1) // -1: no separator before first group
i := 0 i := 0
for _, f := range pkg.Files { for _, filename := range filenames {
f := pkg.Files[filename]
if f.Doc != nil { if f.Doc != nil {
if i > 0 { if i > 0 {
// not the first group - add separator // not the first group - add separator
...@@ -342,7 +351,8 @@ func MergePackageFiles(pkg *Package, mode MergeMode) *File { ...@@ -342,7 +351,8 @@ func MergePackageFiles(pkg *Package, mode MergeMode) *File {
funcs := make(map[string]int) // map of global function name -> decls index funcs := make(map[string]int) // map of global function name -> decls index
i := 0 // current index i := 0 // current index
n := 0 // number of filtered entries n := 0 // number of filtered entries
for _, f := range pkg.Files { for _, filename := range filenames {
f := pkg.Files[filename]
for _, d := range f.Decls { for _, d := range f.Decls {
if mode&FilterFuncDuplicates != 0 { if mode&FilterFuncDuplicates != 0 {
// A language entity may be declared multiple // A language entity may be declared multiple
...@@ -398,7 +408,8 @@ func MergePackageFiles(pkg *Package, mode MergeMode) *File { ...@@ -398,7 +408,8 @@ func MergePackageFiles(pkg *Package, mode MergeMode) *File {
var imports []*ImportSpec var imports []*ImportSpec
if mode&FilterImportDuplicates != 0 { if mode&FilterImportDuplicates != 0 {
seen := make(map[string]bool) seen := make(map[string]bool)
for _, f := range pkg.Files { for _, filename := range filenames {
f := pkg.Files[filename]
for _, imp := range f.Imports { for _, imp := range f.Imports {
if path := imp.Path.Value; !seen[path] { if path := imp.Path.Value; !seen[path] {
// TODO: consider handling cases where: // TODO: consider handling cases where:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment