Skip to content

Commit

Permalink
use set of source paths open-telemetry#4
Browse files Browse the repository at this point in the history
  • Loading branch information
pdelewski committed May 31, 2023
1 parent 06ccf1f commit 3febc6a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
4 changes: 2 additions & 2 deletions instrgen/driver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func makeAnalysis(projectPaths []string, packagePattern string, debug bool, inst
selectedFunctions[k.TypeHash()] = true
}
analysis := &alib.PackageAnalysis{
ProjectPath: projectPaths[0],
ProjectPaths: projectPaths,
PackagePattern: packagePattern,
RootFunctions: rootFunctions,
FuncDecls: funcDecls,
Expand Down Expand Up @@ -253,7 +253,7 @@ func reqInject(projectPaths []string, packagePattern string, w http.ResponseWrit
}
fmt.Fprintln(instrgenLog, "")
analysis := &alib.PackageAnalysis{
ProjectPath: projectPaths[0],
ProjectPaths: projectPaths,
PackagePattern: packagePattern,
RootFunctions: rootFunctions,
FuncDecls: funcDecls,
Expand Down
68 changes: 35 additions & 33 deletions instrgen/lib/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
// root functions - entry points, function declarations,
// and so on.
type PackageAnalysis struct {
ProjectPath string
ProjectPaths []string
PackagePattern string
RootFunctions []FuncDescriptor
FuncDecls map[FuncDescriptor]bool
Expand Down Expand Up @@ -99,45 +99,47 @@ func addImports(imports []Import, fset *token.FileSet, fileNode *ast.File) {
// Execute function, main entry point to analysis process.
func (analysis *PackageAnalysis) Execute(pass FileAnalysisPass, fileSuffix string) ([]*ast.File, error) {
fset := token.NewFileSet()
cfg := &packages.Config{Fset: fset, Mode: LoadMode, Dir: analysis.ProjectPath}
pkgs, err := packages.Load(cfg, analysis.PackagePattern)
if err != nil {
return nil, err
}
var fileNodeSet []*ast.File
for _, pkg := range pkgs {
fmt.Fprintln(analysis.InstrgenLog, "\t", pkg)
// fileNode represents a translationUnit
var fileNode *ast.File
for _, fileNode = range pkg.Syntax {
fmt.Fprintln(analysis.InstrgenLog, "\t\t", fset.File(fileNode.Pos()).Name())
var out *os.File
out, err = createFile(fset.File(fileNode.Pos()).Name() + fileSuffix)
if err != nil {
return nil, err
}
if len(analysis.RootFunctions) == 0 {
for _, projectPath := range analysis.ProjectPaths {
cfg := &packages.Config{Fset: fset, Mode: LoadMode, Dir: projectPath}
pkgs, err := packages.Load(cfg, analysis.PackagePattern)
if err != nil {
return nil, err
}
for _, pkg := range pkgs {
fmt.Fprintln(analysis.InstrgenLog, "\t", pkg)
// fileNode represents a translationUnit
var fileNode *ast.File
for _, fileNode = range pkg.Syntax {
fmt.Fprintln(analysis.InstrgenLog, "\t\t", fset.File(fileNode.Pos()).Name())
var out *os.File
out, err = createFile(fset.File(fileNode.Pos()).Name() + fileSuffix)
if err != nil {
return nil, err
}
if len(analysis.RootFunctions) == 0 {
e := printer.Fprint(out, fset, fileNode)
if e != nil {
return nil, e
}
continue
}
imports := pass.Execute(fileNode, analysis, pkg, pkgs)
addImports(imports, fset, fileNode)
e := printer.Fprint(out, fset, fileNode)
if e != nil {
return nil, e
}
continue
}
imports := pass.Execute(fileNode, analysis, pkg, pkgs)
addImports(imports, fset, fileNode)
e := printer.Fprint(out, fset, fileNode)
if e != nil {
return nil, e
}
if !analysis.Debug {
oldFileName := fset.File(fileNode.Pos()).Name() + fileSuffix
newFileName := fset.File(fileNode.Pos()).Name()
e = os.Rename(oldFileName, newFileName)
if e != nil {
return nil, e
if !analysis.Debug {
oldFileName := fset.File(fileNode.Pos()).Name() + fileSuffix
newFileName := fset.File(fileNode.Pos()).Name()
e = os.Rename(oldFileName, newFileName)
if e != nil {
return nil, e
}
}
fileNodeSet = append(fileNodeSet, fileNode)
}
fileNodeSet = append(fileNodeSet, fileNode)
}
}
analysis.InstrgenLog.Flush()
Expand Down

0 comments on commit 3febc6a

Please sign in to comment.