1212//  See the License for the specific language governing permissions and
1313//  limitations under the License.
1414
15- #include  < sched.h> 
16- 
1715#include  < filesystem> //  NOLINT
1816#include  < memory> 
1917#include  < string> 
2422#include  " init.h" 
2523#include  " indexer/frontend/frontend.h" 
2624#include  " indexer/index/file_copier.h" 
27- #include  " indexer/index/in_memory_index.h" 
2825#include  " indexer/index/sqlite.h" 
2926#include  " indexer/merge_queue.h" 
3027#include  " absl/flags/flag.h" 
3936ABSL_FLAG (std::string, source_dir, " " " Source directory" 
4037ABSL_FLAG (std::string, build_dir, " " " Build directory" 
4138ABSL_FLAG (std::string, index_dir, " " 
42-           " Output index file directory (should be empty if it exists)" 
39+           " Output index file directory (should be empty if it exists for a new " 
40+           " index or contain the old index for a --delta); required unless" 
41+           " --database_only is specified" 
4342ABSL_FLAG (std::vector<std::string>, extra_dirs, {" /" 
4443          " Additional source directory/-ies (comma-separated)" 
4544ABSL_FLAG (std::string, dry_run_regex, " " 
@@ -51,25 +50,42 @@ ABSL_FLAG(int, merge_queue_size, 16, "Length of merge queues");
5150ABSL_FLAG (bool , enable_expensive_checks, false ,
5251          " Enable expensive database integrity checks" 
5352ABSL_FLAG (bool , ignore_indexing_errors, false , " Ignore indexing errors" 
53+ ABSL_FLAG (bool , delta, false ,
54+           " Has no effect with --database_only (which can however be directly " 
55+           " pointed to the delta database filename for the same effect). Expects" 
56+           "  --index_dir to point to an existing database. Stores a delta" 
57+           "  database alongside it only for the translation units in" 
58+           " `compile_commands.json` (for incremental indexing). IMPORTANT: The " 
59+           " latter should contain all the translation units dependent on any " 
60+           " files changed since the original index was built. Source files that " 
61+           " get indexed are copied to --index_dir (again)" 
62+ ABSL_FLAG (std::string, database_only, " " 
63+           " Do not copy source files, only build the index database at the given" 
64+           "  location (--index_dir is not effective in that case)" 
65+ 
66+ static  constexpr  char  kIndexDbName [] = " db.sqlite" 
67+ static  constexpr  char  kDeltaDbName [] = " delta.sqlite" 
5468
5569int  main (int  argc, char ** argv) {
56-   using  oss_fuzz::indexer::InMemoryIndex ;
70+   using  oss_fuzz::indexer::FileCopier ;
5771  using  oss_fuzz::indexer::MergeQueue;
72+   using  oss_fuzz::indexer::SaveAsSqlite;
5873  using  clang::tooling::AllTUsToolExecutor;
5974  using  clang::tooling::CompilationDatabase;
6075
6176#ifdef  NO_CHANGE_ROOT_AND_USER
6277  //  When running inside a container, we cannot drop privileges.
6378  InitGoogleExceptChangeRootAndUser (argv[0 ], &argc, &argv, true );
6479#else 
65-   InitGoogle (absl::NullSafeStringView ( argv[0 ]) , &argc, &argv, true );
80+   InitGoogle (argv[0 ], &argc, &argv, true );
6681#endif 
6782
6883  const  std::string& source_dir = absl::GetFlag (FLAGS_source_dir);
6984  const  std::string& build_dir = absl::GetFlag (FLAGS_build_dir);
7085  const  std::string& index_dir = absl::GetFlag (FLAGS_index_dir);
86+   const  std::string& database_only = absl::GetFlag (FLAGS_database_only);
7187  const  std::vector<std::string>& extra_dirs = absl::GetFlag (FLAGS_extra_dirs);
72-   auto  index_path =  std::filesystem::path (index_dir) /  " db.sqlite " 
88+   const   bool  delta =  absl::GetFlag (FLAGS_delta) ;
7389
7490#ifndef  NDEBUG
7591  LOG (ERROR) << " indexer is built without optimisations. Use 'blaze run -c opt'" 
@@ -88,7 +104,25 @@ int main(int argc, char** argv) {
88104    clang::tooling::Filter.setValue (dry_run_regex);
89105  }
90106
91-   oss_fuzz::indexer::FileCopier file_copier (source_dir, index_dir, extra_dirs);
107+   std::string index_path = database_only;
108+   FileCopier::Behavior behavior = FileCopier::Behavior::kNoOp ;
109+   if  (database_only.empty ()) {
110+     if  (delta) {
111+       index_path = std::filesystem::path (index_dir) / kDeltaDbName ;
112+       behavior = FileCopier::Behavior::kOverwriteExistingFiles ;
113+     } else  {
114+       index_path = std::filesystem::path (index_dir) / kIndexDbName ;
115+       if  (std::filesystem::exists (index_path)) {
116+         LOG (ERROR) << " Index database '" 
117+                    << " ' already exists. Either specify an empty --index_dir or" 
118+                       " use --delta or --database_only" 
119+         return  1 ;
120+       }
121+       behavior = FileCopier::Behavior::kFailOnExistingFiles ;
122+     }
123+   }
124+ 
125+   FileCopier file_copier (source_dir, index_dir, extra_dirs, behavior);
92126
93127  std::unique_ptr<MergeQueue> merge_queue = MergeQueue::Create (
94128      absl::GetFlag (FLAGS_merge_queues), absl::GetFlag (FLAGS_merge_queue_size));
0 commit comments