@@ -183,7 +183,7 @@ func (f *CppIncludesFinder) DetectLibraries() error {
183
183
func (f * CppIncludesFinder ) appendIncludeFolder (sourceFilePath * paths.Path , include string , folder * paths.Path ) {
184
184
f .log .Debugf ("Using include folder: %s" , folder )
185
185
f .ctx .IncludeFolders = append (f .ctx .IncludeFolders , folder )
186
- f .cache .ExpectEntry (sourceFilePath , include , folder )
186
+ f .cache .AddAndCheckEntry (sourceFilePath , include , folder )
187
187
}
188
188
189
189
func runCommand (ctx * types.Context , command types.Command ) error {
@@ -228,49 +228,53 @@ type includeCache struct {
228
228
entries []* includeCacheEntry
229
229
}
230
230
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
+ }
235
237
return cache .entries [cache .next ]
236
238
}
237
239
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.
241
249
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 ()
245
252
}
246
253
}
247
254
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 ++
261
265
}
262
266
263
267
if ! cache .valid {
264
- cache .entries = append (cache .entries , entry )
268
+ cache .entries = append (cache .entries , expected )
269
+ cache .next ++
265
270
}
266
271
}
267
272
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.
270
275
func (cache * includeCache ) ExpectEnd () {
271
276
if cache .valid && cache .next < len (cache .entries ) {
272
- cache .valid = false
273
- cache .entries = cache .entries [:cache .next ]
277
+ cache .Invalidate ()
274
278
}
275
279
}
276
280
@@ -352,7 +356,7 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile *SourceFile) error
352
356
var preprocStderr []byte
353
357
var include string
354
358
if unchanged && f .cache .valid {
355
- include = f .cache .Next ().Include
359
+ include = f .cache .Peek ().Include
356
360
if first && f .ctx .Verbose {
357
361
f .ctx .GetLogger ().Println ("info" , "Using cached library dependencies for file: {0}" , sourcePath )
358
362
}
@@ -378,7 +382,7 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile *SourceFile) error
378
382
379
383
if include == "" {
380
384
// No missing includes found, we're done
381
- f .cache .ExpectEntry (sourcePath , "" , nil )
385
+ f .cache .AddAndCheckEntry (sourcePath , "" , nil )
382
386
return nil
383
387
}
384
388
0 commit comments