Skip to content

Commit b04be66

Browse files
committed
fixed docs for includeCache; renamed some methods; added Invalidate() method
1 parent 0479b6a commit b04be66

File tree

1 file changed

+35
-31
lines changed

1 file changed

+35
-31
lines changed

legacy/builder/container_find_includes.go

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func (f *CppIncludesFinder) DetectLibraries() error {
183183
func (f *CppIncludesFinder) appendIncludeFolder(sourceFilePath *paths.Path, include string, folder *paths.Path) {
184184
f.log.Debugf("Using include folder: %s", folder)
185185
f.ctx.IncludeFolders = append(f.ctx.IncludeFolders, folder)
186-
f.cache.ExpectEntry(sourceFilePath, include, folder)
186+
f.cache.AddAndCheckEntry(sourceFilePath, include, folder)
187187
}
188188

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

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

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

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

263267
if !cache.valid {
264-
cache.entries = append(cache.entries, entry)
268+
cache.entries = append(cache.entries, expected)
269+
cache.next++
265270
}
266271
}
267272

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

@@ -352,7 +356,7 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile *SourceFile) error
352356
var preprocStderr []byte
353357
var include string
354358
if unchanged && f.cache.valid {
355-
include = f.cache.Next().Include
359+
include = f.cache.Peek().Include
356360
if first && f.ctx.Verbose {
357361
f.ctx.GetLogger().Println("info", "Using cached library dependencies for file: {0}", sourcePath)
358362
}
@@ -378,7 +382,7 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile *SourceFile) error
378382

379383
if include == "" {
380384
// No missing includes found, we're done
381-
f.cache.ExpectEntry(sourcePath, "", nil)
385+
f.cache.AddAndCheckEntry(sourcePath, "", nil)
382386
return nil
383387
}
384388

0 commit comments

Comments
 (0)