@@ -193,7 +193,7 @@ func (f *CppIncludesFinder) DetectLibraries() error {
193
193
func (f * CppIncludesFinder ) appendIncludeFolder (sourceFilePath * paths.Path , include string , folder * paths.Path ) {
194
194
f .log .Debugf ("Using include folder: %s" , folder )
195
195
f .ctx .IncludeFolders = append (f .ctx .IncludeFolders , folder )
196
- f .cache .ExpectEntry (sourceFilePath , include , folder )
196
+ f .cache .AddAndCheckEntry (sourceFilePath , include , folder )
197
197
}
198
198
199
199
func runCommand (ctx * types.Context , command types.Command ) error {
@@ -238,49 +238,53 @@ type includeCache struct {
238
238
entries []* includeCacheEntry
239
239
}
240
240
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
+ }
245
247
return cache .entries [cache .next ]
246
248
}
247
249
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.
251
259
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 ()
255
262
}
256
263
}
257
264
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 ++
271
275
}
272
276
273
277
if ! cache .valid {
274
- cache .entries = append (cache .entries , entry )
278
+ cache .entries = append (cache .entries , expected )
279
+ cache .next ++
275
280
}
276
281
}
277
282
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.
280
285
func (cache * includeCache ) ExpectEnd () {
281
286
if cache .valid && cache .next < len (cache .entries ) {
282
- cache .valid = false
283
- cache .entries = cache .entries [:cache .next ]
287
+ cache .Invalidate ()
284
288
}
285
289
}
286
290
@@ -362,7 +366,7 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile *SourceFile) error
362
366
var preprocStderr []byte
363
367
var include string
364
368
if unchanged && f .cache .valid {
365
- include = f .cache .Next ().Include
369
+ include = f .cache .Peek ().Include
366
370
if first && f .ctx .Verbose {
367
371
f .ctx .Info (tr ("Using cached library dependencies for file: %[1]s" , sourcePath ))
368
372
}
@@ -388,7 +392,7 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile *SourceFile) error
388
392
389
393
if include == "" {
390
394
// No missing includes found, we're done
391
- f .cache .ExpectEntry (sourcePath , "" , nil )
395
+ f .cache .AddAndCheckEntry (sourcePath , "" , nil )
392
396
return nil
393
397
}
394
398
0 commit comments