]> granicus.if.org Git - clang/commitdiff
When the # of top-level declarations changes after reparsing a
authorDouglas Gregor <dgregor@apple.com>
Tue, 17 Aug 2010 00:40:40 +0000 (00:40 +0000)
committerDouglas Gregor <dgregor@apple.com>
Tue, 17 Aug 2010 00:40:40 +0000 (00:40 +0000)
translation unit, refresh code-completion results because they've
probably changed. However, enforce a cooldown period between
refreshes, to avoid thrashing.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@111218 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Frontend/ASTUnit.h
lib/Frontend/ASTUnit.cpp

index 77a641a2c5d036ab0b9a96bbead4fe69c7f604bf..3426e433c5a2b765041554cd7e435d24c7a215ea 100644 (file)
@@ -259,6 +259,20 @@ private:
   /// type, which is used for type equality comparisons.
   llvm::StringMap<unsigned> CachedCompletionTypes;
   
+  /// \brief The number of top-level declarations present the last time we
+  /// cached code-completion results.
+  ///
+  /// The value is used to help detect when we should repopulate the global
+  /// completion cache.
+  unsigned NumTopLevelDeclsAtLastCompletionCache;
+
+  /// \brief The number of reparses left until we'll consider updating the
+  /// code-completion cache.
+  ///
+  /// This is meant to avoid thrashing during reparsing, by not allowing the
+  /// code-completion cache to be updated on every reparse.
+  unsigned CacheCodeCompletionCoolDown;
+  
   /// \brief Cache any "global" code-completion results, so that we can avoid
   /// recomputing them with each completion.
   void CacheCodeCompletionResults();
index c66f08df3d4f3fb5b99a7ae0b026e8ad2cbe14fb..428647f03f41eb7157a99edbdda3813062288199 100644 (file)
@@ -53,7 +53,9 @@ ASTUnit::ASTUnit(bool _MainFileIsAST)
   : CaptureDiagnostics(false), MainFileIsAST(_MainFileIsAST), 
     CompleteTranslationUnit(true), ConcurrencyCheckValue(CheckUnlocked), 
     PreambleRebuildCounter(0), SavedMainFileBuffer(0),
-    ShouldCacheCodeCompletionResults(false) { 
+    ShouldCacheCodeCompletionResults(false),
+    NumTopLevelDeclsAtLastCompletionCache(0),
+    CacheCodeCompletionCoolDown(0) { 
 }
 
 ASTUnit::~ASTUnit() {
@@ -285,6 +287,10 @@ void ASTUnit::CacheCodeCompletionResults() {
 
   if (CachingTimer)
     CachingTimer->stopTimer();
+  
+  // Make a note of the state when we performed this caching.
+  NumTopLevelDeclsAtLastCompletionCache = top_level_size();
+  CacheCodeCompletionCoolDown = 15;
 }
 
 void ASTUnit::ClearCachedCompletionResults() {
@@ -1411,6 +1417,14 @@ bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) {
   bool Result = Parse(OverrideMainBuffer);  
   if (ReparsingTimer)
     ReparsingTimer->stopTimer();
+  
+  if (ShouldCacheCodeCompletionResults) {
+    if (CacheCodeCompletionCoolDown > 0)
+      --CacheCodeCompletionCoolDown;
+    else if (top_level_size() != NumTopLevelDeclsAtLastCompletionCache)
+      CacheCodeCompletionResults();
+  }
+  
   return Result;
 }