Skip to content

Commit a7c6530

Browse files
committed
fixed docs for includeCache; renamed some methods; added Invalidate() method
1 parent 79fda79 commit a7c6530

File tree

1 file changed

+35
-31
lines changed

1 file changed

+35
-31
lines changed

legacy/builder/container_find_includes.go

+35-31
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func (f *CppIncludesFinder) DetectLibraries() error {
182182
func (f *CppIncludesFinder) appendIncludeFolder(sourceFilePath *paths.Path, include string, folder *paths.Path) {
183183
f.log.Debugf("Using include folder: %s", folder)
184184
f.ctx.IncludeFolders = append(f.ctx.IncludeFolders, folder)
185-
f.cache.ExpectEntry(sourceFilePath, include, folder)
185+
f.cache.AddAndCheckEntry(sourceFilePath, include, folder)
186186
}
187187

188188
func runCommand(ctx *types.Context, command types.Command) error {
@@ -227,49 +227,53 @@ type includeCache struct {
227227
entries []*includeCacheEntry
228228
}
229229

230-
// Return the next cache entry. Should only be called when the cache is
231-
// valid and a next entry is available (the latter can be checked with
232-
// ExpectFile). Does not advance the cache.
233-
func (cache *includeCache) Next() *includeCacheEntry {
230+
// Peek returns the next cache entry if the cache is valid and the next
231+
// entry exists, otherwise it returns nil. Does not advance the cache.
232+
func (cache *includeCache) Peek() *includeCacheEntry {
233+
if !cache.valid || cache.next >= len(cache.entries) {
234+
return nil
235+
}
234236
return cache.entries[cache.next]
235237
}
236238

237-
// Check that the next cache entry is about the given file. If it is
238-
// not, or no entry is available, the cache is invalidated. Does not
239-
// advance the cache.
239+
// Invalidate invalidates the cache.
240+
func (cache *includeCache) Invalidate() {
241+
cache.valid = false
242+
cache.entries = cache.entries[:cache.next]
243+
}
244+
245+
// ExpectFile check that the next cache entry is about the given file.
246+
// If it is not, or no entry is available, the cache is invalidated.
247+
// Does not advance the cache.
240248
func (cache *includeCache) ExpectFile(sourcefile *paths.Path) {
241-
if cache.valid && (cache.next >= len(cache.entries) || !cache.Next().Sourcefile.EqualsTo(sourcefile)) {
242-
cache.valid = false
243-
cache.entries = cache.entries[:cache.next]
249+
if next := cache.Peek(); next == nil || !next.Sourcefile.EqualsTo(sourcefile) {
250+
cache.Invalidate()
244251
}
245252
}
246253

247-
// Check that the next entry matches the given values. If so, advance
248-
// the cache. If not, the cache is invalidated. If the cache is
249-
// invalidated, or was already invalid, an entry with the given values
250-
// is appended.
251-
func (cache *includeCache) ExpectEntry(sourcefile *paths.Path, include string, librarypath *paths.Path) {
252-
entry := &includeCacheEntry{Sourcefile: sourcefile, Include: include, Includepath: librarypath}
253-
if cache.valid {
254-
if cache.next < len(cache.entries) && cache.Next().Equals(entry) {
255-
cache.next++
256-
} else {
257-
cache.valid = false
258-
cache.entries = cache.entries[:cache.next]
259-
}
254+
// AddAndCheckEntry check that the next entry matches the given values.
255+
// If so, advance the cache. If not, the cache is invalidated. If the
256+
// cache is invalidated, or was already invalid, an entry with the given
257+
// values is appended.
258+
func (cache *includeCache) AddAndCheckEntry(sourcefile *paths.Path, include string, librarypath *paths.Path) {
259+
expected := &includeCacheEntry{Sourcefile: sourcefile, Include: include, Includepath: librarypath}
260+
if next := cache.Peek(); next == nil || !next.Equals(expected) {
261+
cache.Invalidate()
262+
} else {
263+
cache.next++
260264
}
261265

262266
if !cache.valid {
263-
cache.entries = append(cache.entries, entry)
267+
cache.entries = append(cache.entries, expected)
268+
cache.next++
264269
}
265270
}
266271

267-
// Check that the cache is completely consumed. If not, the cache is
268-
// invalidated.
272+
// ExpectEnd check that the cache is completely consumed. If not, the
273+
// cache is invalidated.
269274
func (cache *includeCache) ExpectEnd() {
270275
if cache.valid && cache.next < len(cache.entries) {
271-
cache.valid = false
272-
cache.entries = cache.entries[:cache.next]
276+
cache.Invalidate()
273277
}
274278
}
275279

@@ -351,7 +355,7 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile *SourceFile) error
351355
var preprocStderr []byte
352356
var include string
353357
if unchanged && f.cache.valid {
354-
include = f.cache.Next().Include
358+
include = f.cache.Peek().Include
355359
if first && f.ctx.Verbose {
356360
f.ctx.GetLogger().Println("info", "Using cached library dependencies for file: {0}", sourcePath)
357361
}
@@ -377,7 +381,7 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile *SourceFile) error
377381

378382
if include == "" {
379383
// No missing includes found, we're done
380-
f.cache.ExpectEntry(sourcePath, "", nil)
384+
f.cache.AddAndCheckEntry(sourcePath, "", nil)
381385
return nil
382386
}
383387

0 commit comments

Comments
 (0)