Skip to content

Commit

Permalink
add comments and make some plubic methods private
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed Sep 5, 2023
1 parent 53b84b7 commit 33ccc2c
Showing 1 changed file with 43 additions and 42 deletions.
85 changes: 43 additions & 42 deletions arduino/builder/detector/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,6 @@ func (l *SketchLibrariesDetector) AppendImportedLibraries(library *libraries.Lib
l.importedLibraries = append(l.importedLibraries, library)
}

// UseCachedLibrariesResolution todo
func (l *SketchLibrariesDetector) UseCachedLibrariesResolution() bool {
return l.useCachedLibrariesResolution
}

// PrintUsedAndNotUsedLibraries todo
func (l *SketchLibrariesDetector) PrintUsedAndNotUsedLibraries(sketchError bool) {
// Print this message:
Expand Down Expand Up @@ -182,20 +177,21 @@ func (l *SketchLibrariesDetector) PrintUsedAndNotUsedLibraries(sketchError bool)
time.Sleep(100 * time.Millisecond)
}

// IncludeFolders fixdoc
func (l *SketchLibrariesDetector) IncludeFolders() paths.PathList {
// TODO should we do a deep copy?
return l.includeFolders
}

// AppendIncludeFolder todo should rename this, probably after refactoring the
// appendIncludeFolder todo should rename this, probably after refactoring the
// container_find_includes command.
// Original comment:
// Append the given folder to the include path and match or append it to
// the cache. sourceFilePath and include indicate the source of this
// include (e.g. what #include line in what file it was resolved from)
// and should be the empty string for the default include folders, like
// the core or variant.
func (l *SketchLibrariesDetector) AppendIncludeFolder(
func (l *SketchLibrariesDetector) appendIncludeFolder(
cache *includeCache,
sourceFilePath *paths.Path,
include string,
Expand Down Expand Up @@ -259,28 +255,28 @@ func (l *SketchLibrariesDetector) findIncludes(
cachePath := buildPath.Join("includes.cache")
cache := readCache(cachePath)

l.AppendIncludeFolder(cache, nil, "", buildCorePath)
l.appendIncludeFolder(cache, nil, "", buildCorePath)
if buildVariantPath != nil {
l.AppendIncludeFolder(cache, nil, "", buildVariantPath)
l.appendIncludeFolder(cache, nil, "", buildVariantPath)
}

sourceFileQueue := &UniqueSourceFileQueue{}
sourceFileQueue := &uniqueSourceFileQueue{}

if !l.useCachedLibrariesResolution {
sketch := sketch
mergedfile, err := MakeSourceFile(sketchBuildPath, librariesBuildPath, sketch, paths.New(sketch.MainFile.Base()+".cpp"))
if err != nil {
return errors.WithStack(err)
}
sourceFileQueue.Push(mergedfile)
sourceFileQueue.push(mergedfile)

l.queueSourceFilesFromFolder(sketchBuildPath, librariesBuildPath, sourceFileQueue, sketch, sketchBuildPath, false /* recurse */)
srcSubfolderPath := sketchBuildPath.Join("src")
if srcSubfolderPath.IsDir() {
l.queueSourceFilesFromFolder(sketchBuildPath, librariesBuildPath, sourceFileQueue, sketch, srcSubfolderPath, true /* recurse */)
}

for !sourceFileQueue.Empty() {
for !sourceFileQueue.empty() {
err := l.findIncludesUntilDone(cache, sourceFileQueue, buildProperties, sketchBuildPath, librariesBuildPath, platformArch)
if err != nil {
cachePath.Remove()
Expand Down Expand Up @@ -310,13 +306,13 @@ func (l *SketchLibrariesDetector) findIncludes(

func (l *SketchLibrariesDetector) findIncludesUntilDone(
cache *includeCache,
sourceFileQueue *UniqueSourceFileQueue,
sourceFileQueue *uniqueSourceFileQueue,
buildProperties *properties.Map,
sketchBuildPath *paths.Path,
librariesBuildPath *paths.Path,
platformArch string,
) error {
sourceFile := sourceFileQueue.Pop()
sourceFile := sourceFileQueue.pop()
sourcePath := sourceFile.SourcePath()
targetFilePath := paths.NullPath()
depPath := sourceFile.DepfilePath()
Expand Down Expand Up @@ -414,7 +410,7 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
// include path and queue its source files for further
// include scanning
l.AppendImportedLibraries(library)
l.AppendIncludeFolder(cache, sourcePath, missingIncludeH, library.SourceDir)
l.appendIncludeFolder(cache, sourcePath, missingIncludeH, library.SourceDir)

if library.Precompiled && library.PrecompiledWithSources {
// Fully precompiled libraries should have no dependencies to avoid ABI breakage
Expand All @@ -433,7 +429,7 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
func (l *SketchLibrariesDetector) queueSourceFilesFromFolder(
sketchBuildPath *paths.Path,
librariesBuildPath *paths.Path,
sourceFileQueue *UniqueSourceFileQueue,
sourceFileQueue *uniqueSourceFileQueue,
origin interface{},
folder *paths.Path,
recurse bool,
Expand All @@ -452,7 +448,7 @@ func (l *SketchLibrariesDetector) queueSourceFilesFromFolder(
if err != nil {
return errors.WithStack(err)
}
sourceFileQueue.Push(sourceFile)
sourceFileQueue.push(sourceFile)
}

return nil
Expand Down Expand Up @@ -484,10 +480,12 @@ func (l *SketchLibrariesDetector) failIfImportedLibraryIsWrong() error {
return nil
}

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

// IncludesFinderWithRegExp fixdoc
func IncludesFinderWithRegExp(source string) string {
match := INCLUDE_REGEXP.FindStringSubmatch(source)
match := includeRegexp.FindStringSubmatch(source)
if match != nil {
return strings.TrimSpace(match[1])
}
Expand All @@ -507,7 +505,7 @@ func findIncludeForOldCompilers(source string) string {
return ""
}

type SourceFile struct {
type sourceFile struct {
// Path to the source file within the sketch/library root folder
relativePath *paths.Path

Expand All @@ -528,22 +526,23 @@ type SourceFile struct {
buildRoot *paths.Path
}

func (f *SourceFile) Equals(g *SourceFile) bool {
// Equals fixdoc
func (f *sourceFile) Equals(g *sourceFile) bool {
return f.relativePath.EqualsTo(g.relativePath) &&
f.buildRoot.EqualsTo(g.buildRoot) &&
f.sourceRoot.EqualsTo(g.sourceRoot)
}

// Create a SourceFile containing the given source file path within the
// MakeSourceFile containing the given source file path within the
// given origin. The given path can be absolute, or relative within the
// origin's root source folder
func MakeSourceFile(
sketchBuildPath *paths.Path,
librariesBuildPath *paths.Path,
origin interface{},
path *paths.Path,
) (*SourceFile, error) {
res := &SourceFile{}
) (*sourceFile, error) {

Check failure on line 544 in arduino/builder/detector/detector.go

View workflow job for this annotation

GitHub Actions / check-style (./)

exported func MakeSourceFile returns unexported type *detector.sourceFile, which can be annoying to use
res := &sourceFile{}

switch o := origin.(type) {
case *sketch.Sketch:
Expand All @@ -568,19 +567,23 @@ func MakeSourceFile(
return res, nil
}

func (f *SourceFile) ExtraIncludePath() *paths.Path {
// ExtraIncludePath fixdoc
func (f *sourceFile) ExtraIncludePath() *paths.Path {
return f.extraIncludePath
}

func (f *SourceFile) SourcePath() *paths.Path {
// SourcePath fixdoc
func (f *sourceFile) SourcePath() *paths.Path {
return f.sourceRoot.JoinPath(f.relativePath)
}

func (f *SourceFile) ObjectPath() *paths.Path {
// ObjectPath fixdoc
func (f *sourceFile) ObjectPath() *paths.Path {
return f.buildRoot.Join(f.relativePath.String() + ".o")
}

func (f *SourceFile) DepfilePath() *paths.Path {
// DepfilePath fixdoc
func (f *sourceFile) DepfilePath() *paths.Path {
return f.buildRoot.Join(f.relativePath.String() + ".d")
}

Expand Down Expand Up @@ -658,21 +661,19 @@ func LibrariesLoader(
return lm, resolver, verboseOut.Bytes(), nil
}

func (l *SketchLibrariesDetector) OnlyUpdateCompilationDatabase() bool {
return l.onlyUpdateCompilationDatabase
}

type includeCacheEntry struct {
Sourcefile *paths.Path
Include string
Includepath *paths.Path
}

// String fixdoc
func (entry *includeCacheEntry) String() string {
return fmt.Sprintf("SourceFile: %s; Include: %s; IncludePath: %s",
entry.Sourcefile, entry.Include, entry.Includepath)
}

// Equals fixdoc
func (entry *includeCacheEntry) Equals(other *includeCacheEntry) bool {
return entry.String() == other.String()
}
Expand All @@ -686,14 +687,14 @@ type includeCache struct {
entries []*includeCacheEntry
}

// Return the next cache entry. Should only be called when the cache is
// Next Return the next cache entry. Should only be called when the cache is
// valid and a next entry is available (the latter can be checked with
// ExpectFile). Does not advance the cache.
func (cache *includeCache) Next() *includeCacheEntry {
return cache.entries[cache.next]
}

// Check that the next cache entry is about the given file. If it is
// ExpectFile check that the next cache entry is about the given file. If it is
// not, or no entry is available, the cache is invalidated. Does not
// advance the cache.
func (cache *includeCache) ExpectFile(sourcefile *paths.Path) {
Expand All @@ -703,7 +704,7 @@ func (cache *includeCache) ExpectFile(sourcefile *paths.Path) {
}
}

// Check that the next entry matches the given values. If so, advance
// ExpectEntry check that the next entry matches the given values. If so, advance
// the cache. If not, the cache is invalidated. If the cache is
// invalidated, or was already invalid, an entry with the given values
// is appended.
Expand All @@ -723,7 +724,7 @@ func (cache *includeCache) ExpectEntry(sourcefile *paths.Path, include string, l
}
}

// Check that the cache is completely consumed. If not, the cache is
// ExpectEnd check that the cache is completely consumed. If not, the cache is
// invalidated.
func (cache *includeCache) ExpectEnd() {
if cache.valid && cache.next < len(cache.entries) {
Expand Down Expand Up @@ -771,25 +772,25 @@ func writeCache(cache *includeCache, path *paths.Path) error {
return nil
}

type UniqueSourceFileQueue []*SourceFile
type uniqueSourceFileQueue []*sourceFile

func (queue *UniqueSourceFileQueue) Push(value *SourceFile) {
if !queue.Contains(value) {
func (queue *uniqueSourceFileQueue) push(value *sourceFile) {
if !queue.contains(value) {
*queue = append(*queue, value)
}
}

func (queue UniqueSourceFileQueue) Contains(target *SourceFile) bool {
func (queue uniqueSourceFileQueue) contains(target *sourceFile) bool {
return slices.ContainsFunc(queue, target.Equals)
}

func (queue *UniqueSourceFileQueue) Pop() *SourceFile {
func (queue *uniqueSourceFileQueue) pop() *sourceFile {
old := *queue
x := old[0]
*queue = old[1:]
return x
}

func (queue UniqueSourceFileQueue) Empty() bool {
func (queue uniqueSourceFileQueue) empty() bool {
return len(queue) == 0
}

0 comments on commit 33ccc2c

Please sign in to comment.