Skip to content

Commit 3323453

Browse files
committed
fixed docs for includeCache; renamed some methods; added Invalidate() method
1 parent 111d6ea commit 3323453

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
@@ -193,7 +193,7 @@ func (f *CppIncludesFinder) DetectLibraries() error {
193193
func (f *CppIncludesFinder) appendIncludeFolder(sourceFilePath *paths.Path, include string, folder *paths.Path) {
194194
f.log.Debugf("Using include folder: %s", folder)
195195
f.ctx.IncludeFolders = append(f.ctx.IncludeFolders, folder)
196-
f.cache.ExpectEntry(sourceFilePath, include, folder)
196+
f.cache.AddAndCheckEntry(sourceFilePath, include, folder)
197197
}
198198

199199
func runCommand(ctx *types.Context, command types.Command) error {
@@ -238,49 +238,53 @@ type includeCache struct {
238238
entries []*includeCacheEntry
239239
}
240240

241-
// Return the next cache entry. Should only be called when the cache is
242-
// valid and a next entry is available (the latter can be checked with
243-
// ExpectFile). Does not advance the cache.
244-
func (cache *includeCache) Next() *includeCacheEntry {
241+
// Peek returns the next cache entry if the cache is valid and the next
242+
// entry exists, otherwise it returns nil. Does not advance the cache.
243+
func (cache *includeCache) Peek() *includeCacheEntry {
244+
if !cache.valid || cache.next >= len(cache.entries) {
245+
return nil
246+
}
245247
return cache.entries[cache.next]
246248
}
247249

248-
// Check that the next cache entry is about the given file. If it is
249-
// not, or no entry is available, the cache is invalidated. Does not
250-
// advance the cache.
250+
// Invalidate invalidates the cache.
251+
func (cache *includeCache) Invalidate() {
252+
cache.valid = false
253+
cache.entries = cache.entries[:cache.next]
254+
}
255+
256+
// ExpectFile check that the next cache entry is about the given file.
257+
// If it is not, or no entry is available, the cache is invalidated.
258+
// Does not advance the cache.
251259
func (cache *includeCache) ExpectFile(sourcefile *paths.Path) {
252-
if cache.valid && (cache.next >= len(cache.entries) || !cache.Next().Sourcefile.EqualsTo(sourcefile)) {
253-
cache.valid = false
254-
cache.entries = cache.entries[:cache.next]
260+
if next := cache.Peek(); next == nil || !next.Sourcefile.EqualsTo(sourcefile) {
261+
cache.Invalidate()
255262
}
256263
}
257264

258-
// Check that the next entry matches the given values. If so, advance
259-
// the cache. If not, the cache is invalidated. If the cache is
260-
// invalidated, or was already invalid, an entry with the given values
261-
// is appended.
262-
func (cache *includeCache) ExpectEntry(sourcefile *paths.Path, include string, librarypath *paths.Path) {
263-
entry := &includeCacheEntry{Sourcefile: sourcefile, Include: include, Includepath: librarypath}
264-
if cache.valid {
265-
if cache.next < len(cache.entries) && cache.Next().Equals(entry) {
266-
cache.next++
267-
} else {
268-
cache.valid = false
269-
cache.entries = cache.entries[:cache.next]
270-
}
265+
// AddAndCheckEntry check that the next entry matches the given values.
266+
// If so, advance the cache. If not, the cache is invalidated. If the
267+
// cache is invalidated, or was already invalid, an entry with the given
268+
// values is appended.
269+
func (cache *includeCache) AddAndCheckEntry(sourcefile *paths.Path, include string, librarypath *paths.Path) {
270+
expected := &includeCacheEntry{Sourcefile: sourcefile, Include: include, Includepath: librarypath}
271+
if next := cache.Peek(); next == nil || !next.Equals(expected) {
272+
cache.Invalidate()
273+
} else {
274+
cache.next++
271275
}
272276

273277
if !cache.valid {
274-
cache.entries = append(cache.entries, entry)
278+
cache.entries = append(cache.entries, expected)
279+
cache.next++
275280
}
276281
}
277282

278-
// Check that the cache is completely consumed. If not, the cache is
279-
// invalidated.
283+
// ExpectEnd check that the cache is completely consumed. If not, the
284+
// cache is invalidated.
280285
func (cache *includeCache) ExpectEnd() {
281286
if cache.valid && cache.next < len(cache.entries) {
282-
cache.valid = false
283-
cache.entries = cache.entries[:cache.next]
287+
cache.Invalidate()
284288
}
285289
}
286290

@@ -362,7 +366,7 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile *SourceFile) error
362366
var preprocStderr []byte
363367
var include string
364368
if unchanged && f.cache.valid {
365-
include = f.cache.Next().Include
369+
include = f.cache.Peek().Include
366370
if first && f.ctx.Verbose {
367371
f.ctx.Info(tr("Using cached library dependencies for file: %[1]s", sourcePath))
368372
}
@@ -388,7 +392,7 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile *SourceFile) error
388392

389393
if include == "" {
390394
// No missing includes found, we're done
391-
f.cache.ExpectEntry(sourcePath, "", nil)
395+
f.cache.AddAndCheckEntry(sourcePath, "", nil)
392396
return nil
393397
}
394398

0 commit comments

Comments
 (0)