Skip to content

Commit

Permalink
return pointer to Result
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed Feb 6, 2024
1 parent 7c81dbf commit 9be2db0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ import (
func PreprocessSketchWithArduinoPreprocessor(
sk *sketch.Sketch, buildPath *paths.Path, includeFolders paths.PathList,
lineOffset int, buildProperties *properties.Map, onlyUpdateCompilationDatabase bool,
) (Result, error) {
) (*Result, error) {
verboseOut := &bytes.Buffer{}
normalOut := &bytes.Buffer{}
if err := buildPath.Join("preproc").MkdirAll(); err != nil {
return Result{}, err
return nil, err
}

sourceFile := buildPath.Join("sketch", sk.MainFile.Base()+".cpp")
Expand All @@ -46,7 +46,7 @@ func PreprocessSketchWithArduinoPreprocessor(
verboseOut.Write(gccResult.Stdout())
verboseOut.Write(gccResult.Stderr())
if err != nil {
return Result{}, err
return nil, err
}

arduiniPreprocessorProperties := properties.NewMap()
Expand All @@ -59,18 +59,18 @@ func PreprocessSketchWithArduinoPreprocessor(
arduiniPreprocessorProperties.SetPath("source_file", targetFile)
pattern := arduiniPreprocessorProperties.Get("pattern")
if pattern == "" {
return Result{}, errors.New(tr("arduino-preprocessor pattern is missing"))
return nil, errors.New(tr("arduino-preprocessor pattern is missing"))
}

commandLine := arduiniPreprocessorProperties.ExpandPropsInString(pattern)
parts, err := properties.SplitQuotedString(commandLine, `"'`, false)
if err != nil {
return Result{}, err
return nil, err
}

command, err := paths.NewProcess(nil, parts...)
if err != nil {
return Result{}, err
return nil, err
}
if runtime.GOOS == "windows" {
// chdir in the uppermost directory to avoid UTF-8 bug in clang (https://github.com/arduino/arduino-preprocessor/issues/2)
Expand All @@ -81,13 +81,13 @@ func PreprocessSketchWithArduinoPreprocessor(
commandStdOut, commandStdErr, err := command.RunAndCaptureOutput(context.Background())
verboseOut.Write(commandStdErr)
if err != nil {
return Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err
return &Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err
}
result := utils.NormalizeUTF8(commandStdOut)

destFile := buildPath.Join(sk.MainFile.Base() + ".cpp")
if err := destFile.WriteFile(result); err != nil {
return Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err
return &Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err
}
return Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err
return &Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err
}
20 changes: 10 additions & 10 deletions internal/arduino/builder/internal/preprocessor/ctags.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ var DebugPreprocessor bool
func PreprocessSketchWithCtags(
sketch *sketch.Sketch, buildPath *paths.Path, includes paths.PathList,
lineOffset int, buildProperties *properties.Map, onlyUpdateCompilationDatabase bool,
) (Result, error) {
) (*Result, error) {
// Create a temporary working directory
tmpDir, err := paths.MkTempDir("", "")
if err != nil {
return Result{}, err
return nil, err
}
defer tmpDir.RemoveAll()
ctagsTarget := tmpDir.Join("sketch_merged.cpp")
Expand All @@ -61,32 +61,32 @@ func PreprocessSketchWithCtags(
stderr.Write(result.Stderr())
if err != nil {
if !onlyUpdateCompilationDatabase {
return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
}

// Do not bail out if we are generating the compile commands database
stderr.WriteString(fmt.Sprintf("%s: %s",
tr("An error occurred adding prototypes"),
tr("the compilation database may be incomplete or inaccurate")))
if err := sourceFile.CopyTo(ctagsTarget); err != nil {
return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
}
}

if src, err := ctagsTarget.ReadFile(); err == nil {
filteredSource := filterSketchSource(sketch, bytes.NewReader(src), false)
if err := ctagsTarget.WriteFile([]byte(filteredSource)); err != nil {
return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
}
} else {
return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
}

// Run CTags on gcc-preprocessed source
ctagsOutput, ctagsStdErr, err := RunCTags(ctagsTarget, buildProperties)
stderr.Write(ctagsStdErr)
if err != nil {
return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
}

// Parse CTags output
Expand All @@ -101,13 +101,13 @@ func PreprocessSketchWithCtags(
if sourceData, err := sourceFile.ReadFile(); err == nil {
source = string(sourceData)
} else {
return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
}
source = strings.ReplaceAll(source, "\r\n", "\n")
source = strings.ReplaceAll(source, "\r", "\n")
sourceRows := strings.Split(source, "\n")
if isFirstFunctionOutsideOfSource(firstFunctionLine, sourceRows) {
return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, nil
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, nil
}

insertionLine := firstFunctionLine + lineOffset - 1
Expand All @@ -133,7 +133,7 @@ func PreprocessSketchWithCtags(

// Write back arduino-preprocess output to the sourceFile
err = sourceFile.WriteFile([]byte(preprocessedSource))
return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
}

func composePrototypeSection(line int, prototypes []*ctags.Prototype) string {
Expand Down
10 changes: 6 additions & 4 deletions internal/arduino/builder/preprocess_sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ func (b *Builder) preprocessSketch(includes paths.PathList) error {
b.sketch, b.buildPath, includes, b.lineOffset,
b.buildProperties, b.onlyUpdateCompilationDatabase,
)
if b.logger.Verbose() {
b.logger.WriteStdout(result.Stdout())
if result != nil {
if b.logger.Verbose() {
b.logger.WriteStdout(result.Stdout())
}
b.logger.WriteStdout(result.Stderr())
b.diagnosticStore.Parse(result.Args(), result.Stderr())
}
b.logger.WriteStdout(result.Stderr())
b.diagnosticStore.Parse(result.Args(), result.Stderr())

return err
}

0 comments on commit 9be2db0

Please sign in to comment.