Skip to content

Commit 4891ded

Browse files
facchinmcmaglie
andauthored
sketch: fixed prototype generation when included libraries contains very long lines (#2935)
* ctags: filterSketchSource: enlarge buffer size to handle long lines Fixes arduino/ArduinoCore-zephyr#140 * Added unti tests * Fixed linter warning --------- Co-authored-by: Cristian Maglie <c.maglie@arduino.cc>
1 parent 241e7d4 commit 4891ded

File tree

3 files changed

+145668
-2
lines changed

3 files changed

+145668
-2
lines changed

internal/arduino/builder/internal/preprocessor/ctags.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func PreprocessSketchWithCtags(
7474
}
7575

7676
if src, err := ctagsTarget.ReadFile(); err == nil {
77-
filteredSource := filterSketchSource(sketch, bytes.NewReader(src), false)
77+
filteredSource := filterSketchSource(sketch, bytes.NewReader(src), false, stderr)
7878
if err := ctagsTarget.WriteFile([]byte(filteredSource)); err != nil {
7979
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
8080
}
@@ -208,7 +208,7 @@ func RunCTags(ctx context.Context, sourceFile *paths.Path, buildProperties *prop
208208
return stdout, stderr, args, err
209209
}
210210

211-
func filterSketchSource(sketch *sketch.Sketch, source io.Reader, removeLineMarkers bool) string {
211+
func filterSketchSource(sketch *sketch.Sketch, source io.Reader, removeLineMarkers bool, stderr *bytes.Buffer) string {
212212
fileNames := paths.NewPathList()
213213
fileNames.Add(sketch.MainFile)
214214
fileNames.AddAll(sketch.OtherSketchFiles)
@@ -217,6 +217,11 @@ func filterSketchSource(sketch *sketch.Sketch, source io.Reader, removeLineMarke
217217
filtered := ""
218218

219219
scanner := bufio.NewScanner(source)
220+
221+
var buf []byte
222+
const maxTokenSize = 1024 * 1024 // 1 MB
223+
scanner.Buffer(buf, maxTokenSize)
224+
220225
for scanner.Scan() {
221226
line := scanner.Text()
222227
if filename := cpp.ParseLineMarker(line); filename != nil {
@@ -230,6 +235,11 @@ func filterSketchSource(sketch *sketch.Sketch, source io.Reader, removeLineMarke
230235
filtered += line + "\n"
231236
}
232237
}
238+
if errors.Is(scanner.Err(), bufio.ErrTooLong) {
239+
fmt.Fprintf(stderr, "%s: %s",
240+
i18n.Tr("An error occurred adding prototypes"),
241+
i18n.Tr("line too long\n"))
242+
}
233243

234244
return filtered
235245
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2025 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package preprocessor
17+
18+
import (
19+
"bytes"
20+
"fmt"
21+
"testing"
22+
23+
"github.com/arduino/arduino-cli/internal/arduino/sketch"
24+
"github.com/arduino/go-paths-helper"
25+
"github.com/stretchr/testify/require"
26+
)
27+
28+
func TestCtagsSketchFilter(t *testing.T) {
29+
sourcePath := paths.New("testdata", "sketch_merged.cpp")
30+
f, err := sourcePath.Open()
31+
require.NoError(t, err)
32+
t.Cleanup(func() { f.Close() })
33+
34+
sketch := &sketch.Sketch{
35+
MainFile: paths.New("/home/megabug/Arduino/Test/Test.ino"),
36+
}
37+
stderr := bytes.NewBuffer(nil)
38+
res := filterSketchSource(sketch, f, false, stderr)
39+
require.Empty(t, stderr)
40+
require.NotEmpty(t, res)
41+
fmt.Println(res)
42+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy