Commit 4a695d2c authored by Robert Griesemer's avatar Robert Griesemer

go/parser: restrict ParseDir to files with suffix ".go"

Fixes #5956.

R=rsc, r
CC=golang-dev
https://golang.org/cl/11813043
parent f3310124
...@@ -15,6 +15,7 @@ import ( ...@@ -15,6 +15,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings"
) )
// If src != nil, readSource converts src to a []byte if possible; // If src != nil, readSource converts src to a []byte if possible;
...@@ -115,11 +116,13 @@ func ParseFile(fset *token.FileSet, filename string, src interface{}, mode Mode) ...@@ -115,11 +116,13 @@ func ParseFile(fset *token.FileSet, filename string, src interface{}, mode Mode)
return return
} }
// ParseDir calls ParseFile for the files in the directory specified by path and // ParseDir calls ParseFile for all files with names ending in ".go" in the
// returns a map of package name -> package AST with all the packages found. If // directory specified by path and returns a map of package name -> package
// filter != nil, only the files with os.FileInfo entries passing through the filter // AST with all the packages found.
// are considered. The mode bits are passed to ParseFile unchanged. Position //
// information is recorded in the file set fset. // If filter != nil, only the files with os.FileInfo entries passing through
// the filter (and ending in ".go") are considered. The mode bits are passed
// to ParseFile unchanged. Position information is recorded in fset.
// //
// If the directory couldn't be read, a nil map and the respective error are // If the directory couldn't be read, a nil map and the respective error are
// returned. If a parse error occurred, a non-nil but incomplete map and the // returned. If a parse error occurred, a non-nil but incomplete map and the
...@@ -139,7 +142,7 @@ func ParseDir(fset *token.FileSet, path string, filter func(os.FileInfo) bool, m ...@@ -139,7 +142,7 @@ func ParseDir(fset *token.FileSet, path string, filter func(os.FileInfo) bool, m
pkgs = make(map[string]*ast.Package) pkgs = make(map[string]*ast.Package)
for _, d := range list { for _, d := range list {
if filter == nil || filter(d) { if strings.HasSuffix(d.Name(), ".go") && (filter == nil || filter(d)) {
filename := filepath.Join(path, d.Name()) filename := filepath.Join(path, d.Name())
if src, err := ParseFile(fset, filename, nil, mode); err == nil { if src, err := ParseFile(fset, filename, nil, mode); err == nil {
name := src.Name.Name name := src.Name.Name
......
...@@ -34,13 +34,12 @@ func TestParse(t *testing.T) { ...@@ -34,13 +34,12 @@ func TestParse(t *testing.T) {
func nameFilter(filename string) bool { func nameFilter(filename string) bool {
switch filename { switch filename {
case "parser.go": case "parser.go", "interface.go", "parser_test.go":
case "interface.go": return true
case "parser_test.go": case "parser.go.orig":
default: return true // permit but should be ignored by ParseDir
return false
} }
return true return false
} }
func dirFilter(f os.FileInfo) bool { return nameFilter(f.Name()) } func dirFilter(f os.FileInfo) bool { return nameFilter(f.Name()) }
...@@ -51,14 +50,17 @@ func TestParseDir(t *testing.T) { ...@@ -51,14 +50,17 @@ func TestParseDir(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("ParseDir(%s): %v", path, err) t.Fatalf("ParseDir(%s): %v", path, err)
} }
if len(pkgs) != 1 { if n := len(pkgs); n != 1 {
t.Errorf("incorrect number of packages: %d", len(pkgs)) t.Errorf("got %d packages; want 1", n)
} }
pkg := pkgs["parser"] pkg := pkgs["parser"]
if pkg == nil { if pkg == nil {
t.Errorf(`package "parser" not found`) t.Errorf(`package "parser" not found`)
return return
} }
if n := len(pkg.Files); n != 3 {
t.Errorf("got %d package files; want 3", n)
}
for filename := range pkg.Files { for filename := range pkg.Files {
if !nameFilter(filename) { if !nameFilter(filename) {
t.Errorf("unexpected package file: %s", filename) t.Errorf("unexpected package file: %s", filename)
......
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