@@ -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+ for _ , decl := range pf .Decls {
317+ if fdecl , ok := decl .(* ast.FuncDecl ); ok {
318+ if info .isTest &&
319+ strings .HasPrefix (fdecl .Name .Name , "Benchmark" ) &&
320+ fdecl .Type .Params .NumFields () == 1 {
321+ starExpr , ok := fdecl .Type .Params .List [0 ].Type .(* ast.StarExpr )
322+ if ! ok {
323+ continue
324+ }
325+ //validate that the only function param is of type *testing.B
326+ selector , ok := starExpr .X .(* ast.SelectorExpr )
327+ if ! ok || selector .Sel .String () != "B" {
328+ continue
329+ }
330+ if pkgIdent , ok := selector .X .(* ast.Ident ); ok && pkgIdent .Name == "testing" {
331+ info .hasBenchmarkTests = true
332+ break
333+ }
334+ }
335+ }
336+ }
312337 }
313338
314339 return info
0 commit comments