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