Skip to content

Commit 85c5781

Browse files
matthijskooijmancmaglie
authored andcommitted
Convert IncludesFinderWithRegExp to a normal function
Signed-off-by: Matthijs Kooijman <matthijs@stdin.nl>
1 parent 140e24f commit 85c5781

4 files changed

+16
-52
lines changed

container_find_includes.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -325,15 +325,14 @@ func findIncludesUntilDone(ctx *types.Context, cache *includeCache, sourceFile t
325325
} else {
326326
commands := []types.Command{
327327
&GCCPreprocRunnerForDiscoveringIncludes{SourceFilePath: sourcePath, TargetFilePath: targetFilePath, Includes: includes},
328-
&IncludesFinderWithRegExp{Source: &ctx.SourceGccMinusE},
329328
}
330329
for _, command := range commands {
331330
err := runCommand(ctx, command)
332331
if err != nil {
333332
return i18n.WrapError(err)
334333
}
335334
}
336-
include = ctx.IncludeJustFound
335+
include = IncludesFinderWithRegExp(ctx, ctx.SourceGccMinusE)
337336
}
338337

339338
if include == "" {

includes_finder_with_regexp.go

+3-11
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,13 @@ import (
3737

3838
var INCLUDE_REGEXP = regexp.MustCompile("(?ms)^\\s*#[ \t]*include\\s*[<\"](\\S+)[\">]")
3939

40-
type IncludesFinderWithRegExp struct {
41-
Source *string
42-
}
43-
44-
func (s *IncludesFinderWithRegExp) Run(ctx *types.Context) error {
45-
source := *s.Source
46-
40+
func IncludesFinderWithRegExp(ctx *types.Context, source string) string {
4741
match := INCLUDE_REGEXP.FindStringSubmatch(source)
4842
if match != nil {
49-
ctx.IncludeJustFound = strings.TrimSpace(match[1])
43+
return strings.TrimSpace(match[1])
5044
} else {
51-
ctx.IncludeJustFound = findIncludeForOldCompilers(source)
45+
return findIncludeForOldCompilers(source)
5246
}
53-
54-
return nil
5547
}
5648

5749
func findIncludeForOldCompilers(source string) string {

test/includes_finder_with_regexp_test.go

+12-38
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,17 @@ func TestIncludesFinderWithRegExp(t *testing.T) {
4343
"#include <SPI.h>\n" +
4444
"^\n" +
4545
"compilation terminated."
46-
ctx.Source = output
46+
include := builder.IncludesFinderWithRegExp(ctx, output)
4747

48-
parser := builder.IncludesFinderWithRegExp{Source: &ctx.Source}
49-
err := parser.Run(ctx)
50-
NoError(t, err)
51-
52-
require.Equal(t, "SPI.h", ctx.IncludeJustFound)
48+
require.Equal(t, "SPI.h", include)
5349
}
5450

5551
func TestIncludesFinderWithRegExpEmptyOutput(t *testing.T) {
5652
ctx := &types.Context{}
5753

58-
output := ""
59-
60-
ctx.Source = output
54+
include := builder.IncludesFinderWithRegExp(ctx, "")
6155

62-
parser := builder.IncludesFinderWithRegExp{Source: &ctx.Source}
63-
err := parser.Run(ctx)
64-
NoError(t, err)
65-
66-
require.Equal(t, "", ctx.IncludeJustFound)
56+
require.Equal(t, "", include)
6757
}
6858

6959
func TestIncludesFinderWithRegExpPaddedIncludes(t *testing.T) {
@@ -73,13 +63,9 @@ func TestIncludesFinderWithRegExpPaddedIncludes(t *testing.T) {
7363
" # include <Wire.h>\n" +
7464
" ^\n" +
7565
"compilation terminated.\n"
76-
ctx.Source = output
77-
78-
parser := builder.IncludesFinderWithRegExp{Source: &ctx.Source}
79-
err := parser.Run(ctx)
80-
NoError(t, err)
66+
include := builder.IncludesFinderWithRegExp(ctx, output)
8167

82-
require.Equal(t, "Wire.h", ctx.IncludeJustFound)
68+
require.Equal(t, "Wire.h", include)
8369
}
8470

8571
func TestIncludesFinderWithRegExpPaddedIncludes2(t *testing.T) {
@@ -89,13 +75,9 @@ func TestIncludesFinderWithRegExpPaddedIncludes2(t *testing.T) {
8975
" #\t\t\tinclude <Wire.h>\n" +
9076
" ^\n" +
9177
"compilation terminated.\n"
92-
ctx.Source = output
78+
include := builder.IncludesFinderWithRegExp(ctx, output)
9379

94-
parser := builder.IncludesFinderWithRegExp{Source: &ctx.Source}
95-
err := parser.Run(ctx)
96-
NoError(t, err)
97-
98-
require.Equal(t, "Wire.h", ctx.IncludeJustFound)
80+
require.Equal(t, "Wire.h", include)
9981
}
10082

10183
func TestIncludesFinderWithRegExpPaddedIncludes3(t *testing.T) {
@@ -104,13 +86,9 @@ func TestIncludesFinderWithRegExpPaddedIncludes3(t *testing.T) {
10486
output := "/some/path/sketch.ino:1:33: fatal error: SPI.h: No such file or directory\n" +
10587
"compilation terminated.\n"
10688

107-
ctx.Source = output
108-
109-
parser := builder.IncludesFinderWithRegExp{Source: &ctx.Source}
110-
err := parser.Run(ctx)
111-
NoError(t, err)
89+
include := builder.IncludesFinderWithRegExp(ctx, output)
11290

113-
require.Equal(t, "SPI.h", ctx.IncludeJustFound)
91+
require.Equal(t, "SPI.h", include)
11492
}
11593

11694
func TestIncludesFinderWithRegExpPaddedIncludes4(t *testing.T) {
@@ -119,11 +97,7 @@ func TestIncludesFinderWithRegExpPaddedIncludes4(t *testing.T) {
11997
output := "In file included from /tmp/arduino_modified_sketch_815412/binouts.ino:52:0:\n" +
12098
"/tmp/arduino_build_static/sketch/regtable.h:31:22: fatal error: register.h: No such file or directory\n"
12199

122-
ctx.Source = output
123-
124-
parser := builder.IncludesFinderWithRegExp{Source: &ctx.Source}
125-
err := parser.Run(ctx)
126-
NoError(t, err)
100+
include := builder.IncludesFinderWithRegExp(ctx, output)
127101

128-
require.Equal(t, "register.h", ctx.IncludeJustFound)
102+
require.Equal(t, "register.h", include)
129103
}

types/context.go

-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ type Context struct {
6464
HeaderToLibraries map[string][]*Library
6565
ImportedLibraries []*Library
6666
LibrariesResolutionResults map[string]LibraryResolutionResult
67-
IncludeJustFound string
6867
IncludeFolders []string
6968
OutputGccMinusM string
7069

0 commit comments

Comments
 (0)