parseFiles parses the loaded list of files and returns this list.
()
| 454 | |
| 455 | // parseFiles parses the loaded list of files and returns this list. |
| 456 | func (p *Package) parseFiles() ([]*ast.File, error) { |
| 457 | var files []*ast.File |
| 458 | var fileErrs []error |
| 459 | |
| 460 | // Parse all files (including CgoFiles). |
| 461 | parseFile := func(file string) { |
| 462 | if !filepath.IsAbs(file) { |
| 463 | file = filepath.Join(p.Dir, file) |
| 464 | } |
| 465 | f, err := p.parseFile(file, parser.ParseComments) |
| 466 | if err != nil { |
| 467 | fileErrs = append(fileErrs, err) |
| 468 | return |
| 469 | } |
| 470 | files = append(files, f) |
| 471 | } |
| 472 | for _, file := range p.GoFiles { |
| 473 | parseFile(file) |
| 474 | } |
| 475 | for _, file := range p.CgoFiles { |
| 476 | parseFile(file) |
| 477 | } |
| 478 | |
| 479 | // Do CGo processing. |
| 480 | // This is done when there are any CgoFiles at all. In that case, len(files) |
| 481 | // should be non-zero. However, if len(GoFiles) == 0 and len(CgoFiles) == 1 |
| 482 | // and there is a syntax error in a CGo file, len(files) may be 0. Don't try |
| 483 | // to call cgo.Process in that case as it will only cause issues. |
| 484 | if len(p.CgoFiles) != 0 && len(files) != 0 { |
| 485 | var initialCFlags []string |
| 486 | initialCFlags = append(initialCFlags, p.program.config.CFlags(true)...) |
| 487 | initialCFlags = append(initialCFlags, "-I"+p.Dir) |
| 488 | generated, headerCode, cflags, ldflags, accessedFiles, errs := cgo.Process(files, p.program.workingDir, p.ImportPath, p.program.fset, initialCFlags, p.program.config.GOOS()) |
| 489 | p.CFlags = append(initialCFlags, cflags...) |
| 490 | p.CGoHeaders = headerCode |
| 491 | for path, hash := range accessedFiles { |
| 492 | p.FileHashes[path] = hash |
| 493 | } |
| 494 | if errs != nil { |
| 495 | fileErrs = append(fileErrs, errs...) |
| 496 | } |
| 497 | files = append(files, generated...) |
| 498 | p.program.LDFlags = append(p.program.LDFlags, ldflags...) |
| 499 | } |
| 500 | |
| 501 | // Only return an error after CGo processing, so that errors in parsing and |
| 502 | // CGo can be reported together. |
| 503 | if len(fileErrs) != 0 { |
| 504 | return nil, Errors{p, fileErrs} |
| 505 | } |
| 506 | |
| 507 | return files, nil |
| 508 | } |
| 509 | |
| 510 | // extractEmbedLines finds all //go:embed lines in the package and matches them |
| 511 | // against EmbedFiles from `go list`. |