@@ -61,6 +61,9 @@ type fileInfo struct {
6161 // name ends with "_test"
6262 isExternalTest bool
6363
64+ // hasBenchmarkTests is true if the file contains benchmark tests.
65+ hasBenchmarkTests bool
66+
6467 // imports is a list of packages imported by a file. It does not include
6568 // "C" or anything from the standard library.
6669 imports []string
@@ -214,6 +217,7 @@ func otherFileInfo(path string) fileInfo {
214217func goFileInfo (path , srcdir string ) fileInfo {
215218 info := fileNameInfo (path )
216219 fset := token .NewFileSet ()
220+ // parse the file for imports and comments (for CGO)
217221 pf , err := parser .ParseFile (fset , info .path , nil , parser .ImportsOnly | parser .ParseComments )
218222 if err != nil {
219223 log .Printf ("%s: error reading go file: %v" , info .path , err )
@@ -274,8 +278,8 @@ func goFileInfo(path, srcdir string) fileInfo {
274278 }
275279 info .tags = tags
276280
277- if importsEmbed || info .packageName == "main" {
278- pf , err = parser .ParseFile (fset , info .path , nil , parser .ParseComments )
281+ if importsEmbed || info .packageName == "main" || info . isTest {
282+ pf , err = parser .ParseFile (fset , info .path , nil , parser .ParseComments | parser . SkipObjectResolution )
279283 if err != nil {
280284 log .Printf ("%s: error reading go file: %v" , info .path , err )
281285 return info
@@ -309,6 +313,27 @@ func goFileInfo(path, srcdir string) fileInfo {
309313 }
310314 }
311315 }
316+ if info .isTest {
317+ for _ , decl := range pf .Decls {
318+ fdecl , ok := decl .(* ast.FuncDecl )
319+ if ! ok || ! strings .HasPrefix (fdecl .Name .Name , "Benchmark" ) || fdecl .Type .Params .NumFields () != 1 {
320+ continue
321+ }
322+ // validate that the only param is of type *testing.B
323+ starExpr , ok := fdecl .Type .Params .List [0 ].Type .(* ast.StarExpr )
324+ if ! ok {
325+ continue
326+ }
327+ selector , ok := starExpr .X .(* ast.SelectorExpr )
328+ if ! ok || selector .Sel .String () != "B" {
329+ continue
330+ }
331+ if pkgIdent , ok := selector .X .(* ast.Ident ); ok && pkgIdent .Name == "testing" {
332+ info .hasBenchmarkTests = true
333+ break
334+ }
335+ }
336+ }
312337 }
313338
314339 return info
0 commit comments