12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- #include < sched.h>
16
-
17
15
#include < filesystem> // NOLINT
18
16
#include < memory>
19
17
#include < string>
24
22
#include " init.h"
25
23
#include " indexer/frontend/frontend.h"
26
24
#include " indexer/index/file_copier.h"
27
- #include " indexer/index/in_memory_index.h"
28
25
#include " indexer/index/sqlite.h"
29
26
#include " indexer/merge_queue.h"
30
27
#include " absl/flags/flag.h"
39
36
ABSL_FLAG (std::string, source_dir, " " , " Source directory" );
40
37
ABSL_FLAG (std::string, build_dir, " " , " Build directory" );
41
38
ABSL_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" );
43
42
ABSL_FLAG (std::vector<std::string>, extra_dirs, {" /" },
44
43
" Additional source directory/-ies (comma-separated)" );
45
44
ABSL_FLAG (std::string, dry_run_regex, " " ,
@@ -51,25 +50,42 @@ ABSL_FLAG(int, merge_queue_size, 16, "Length of merge queues");
51
50
ABSL_FLAG (bool , enable_expensive_checks, false ,
52
51
" Enable expensive database integrity checks" );
53
52
ABSL_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" ;
54
68
55
69
int main (int argc, char ** argv) {
56
- using oss_fuzz::indexer::InMemoryIndex ;
70
+ using oss_fuzz::indexer::FileCopier ;
57
71
using oss_fuzz::indexer::MergeQueue;
72
+ using oss_fuzz::indexer::SaveAsSqlite;
58
73
using clang::tooling::AllTUsToolExecutor;
59
74
using clang::tooling::CompilationDatabase;
60
75
61
76
#ifdef NO_CHANGE_ROOT_AND_USER
62
77
// When running inside a container, we cannot drop privileges.
63
78
InitGoogleExceptChangeRootAndUser (argv[0 ], &argc, &argv, true );
64
79
#else
65
- InitGoogle (absl::NullSafeStringView ( argv[0 ]) , &argc, &argv, true );
80
+ InitGoogle (argv[0 ], &argc, &argv, true );
66
81
#endif
67
82
68
83
const std::string& source_dir = absl::GetFlag (FLAGS_source_dir);
69
84
const std::string& build_dir = absl::GetFlag (FLAGS_build_dir);
70
85
const std::string& index_dir = absl::GetFlag (FLAGS_index_dir);
86
+ const std::string& database_only = absl::GetFlag (FLAGS_database_only);
71
87
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) ;
73
89
74
90
#ifndef NDEBUG
75
91
LOG (ERROR) << " indexer is built without optimisations. Use 'blaze run -c opt'"
@@ -88,7 +104,25 @@ int main(int argc, char** argv) {
88
104
clang::tooling::Filter.setValue (dry_run_regex);
89
105
}
90
106
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 '" << index_path
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);
92
126
93
127
std::unique_ptr<MergeQueue> merge_queue = MergeQueue::Create (
94
128
absl::GetFlag (FLAGS_merge_queues), absl::GetFlag (FLAGS_merge_queue_size));
0 commit comments