Skip to content

Commit 3ff8ebb

Browse files
Let IncludesFinderWithRegExp return at most 1 include
Originally, this code was written to find all include statements in a source file, for further processing. However, since a while the include finding was changed to run the preprocessor to see if any includes are missing, and this class is used to find the missing includes in the error message. In practice, the preprocessor will bail out at the first missing include, since there is no meaningful way for it to continue. This means there will be at most one include in the error output. If there would be multiple includes (either because the preprocessor decided to continue, or there is some other output that accidentially matches the regex), it would be harmful to actually process all of them, since the absence of the first include might influence the presence of the second (e.g. in the presence of #ifdefs). This commit enforces that only one include is returned at a time, slightly simplifying the code in the process. The filename and class should be renamed to singular as well, but this will be left for a future commit to reduce noise. Signed-off-by: Matthijs Kooijman <[email protected]>
1 parent 33a565a commit 3ff8ebb

File tree

3 files changed

+11
-15
lines changed

3 files changed

+11
-15
lines changed

src/arduino.cc/builder/container_find_includes.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,13 @@ func findIncludesUntilDone(ctx *types.Context, sourceFilePath string) error {
120120
return i18n.WrapError(err)
121121
}
122122
}
123-
if len(ctx.IncludesJustFound) == 0 {
123+
if ctx.IncludeJustFound == "" {
124124
done = true
125125
} else if len(ctx.ImportedLibraries) == len(importedLibraries) {
126126
err := runCommand(ctx, &GCCPreprocRunner{TargetFileName: constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E})
127127
return i18n.WrapError(err)
128128
}
129129
importedLibraries = ctx.ImportedLibraries
130-
ctx.IncludesJustFound = []string{}
131130
}
132131
return nil
133132
}

src/arduino.cc/builder/includes_finder_with_regexp.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,21 @@ type IncludesFinderWithRegExp struct {
4545
func (s *IncludesFinderWithRegExp) Run(ctx *types.Context) error {
4646
source := *s.Source
4747

48-
matches := INCLUDE_REGEXP.FindAllStringSubmatch(source, -1)
49-
includes := []string{}
50-
for _, match := range matches {
51-
includes = append(includes, strings.TrimSpace(match[1]))
48+
match := INCLUDE_REGEXP.FindStringSubmatch(source)
49+
if match != nil {
50+
ctx.IncludeJustFound = strings.TrimSpace(match[1])
51+
} else {
52+
ctx.IncludeJustFound = findIncludeForOldCompilers(source)
5253
}
53-
if len(includes) == 0 {
54-
include := findIncludesForOldCompilers(source)
55-
if include != "" {
56-
includes = append(includes, include)
57-
}
54+
55+
if ctx.IncludeJustFound != "" {
56+
ctx.Includes = utils.AppendIfNotPresent(ctx.Includes, ctx.IncludeJustFound)
5857
}
5958

60-
ctx.IncludesJustFound = includes
61-
ctx.Includes = utils.AppendIfNotPresent(ctx.Includes, includes...)
6259
return nil
6360
}
6461

65-
func findIncludesForOldCompilers(source string) string {
62+
func findIncludeForOldCompilers(source string) string {
6663
lines := strings.Split(source, "\n")
6764
for _, line := range lines {
6865
splittedLine := strings.Split(line, ":")

src/arduino.cc/builder/types/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type Context struct {
6161
HeaderToLibraries map[string][]*Library
6262
ImportedLibraries []*Library
6363
LibrariesResolutionResults map[string]LibraryResolutionResult
64-
IncludesJustFound []string
64+
IncludeJustFound string
6565
IncludeFolders []string
6666
OutputGccMinusM string
6767

0 commit comments

Comments
 (0)