Skip to content

Commit e65a0b6

Browse files
refactor: Pass parser options uniformly (#381)
1 parent cbd2131 commit e65a0b6

File tree

5 files changed

+35
-33
lines changed

5 files changed

+35
-33
lines changed

indexer/CompilationDatabase.cc

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -564,17 +564,24 @@ compdb::File::openAndExitOnErrors(const StdPath &path,
564564
return compdbFile;
565565
}
566566

567-
void ResumableParser::initialize(compdb::File compdb, size_t refillCount,
568-
ParseOptions options) {
567+
// static
568+
ParseOptions ParseOptions::create(size_t refillCount, bool forTesting) {
569+
ENFORCE(refillCount > 0);
570+
return ParseOptions{refillCount, /*inferResourceDir*/ !forTesting,
571+
/*skipNonMainFileTuEntries*/ !forTesting,
572+
/*checkFilesExist*/ !forTesting};
573+
}
574+
575+
void ResumableParser::initialize(compdb::File compdb, ParseOptions options) {
569576
auto averageJobSize = compdb.sizeInBytes() / compdb.commandCount();
570577
// Some customers have averageJobSize = 150KiB.
571578
// If numWorkers == 300 (very high core count machine),
572579
// then the computed hint will be ~88MiB. The 128MiB is rounded up from 88MiB.
573580
// The fudge factor of 2 is to allow for oversized jobs.
574-
auto bufferSize =
575-
std::min(size_t(128 * 1024 * 1024), averageJobSize * 2 * refillCount);
581+
auto bufferSize = std::min(size_t(128 * 1024 * 1024),
582+
averageJobSize * 2 * options.refillCount);
576583
std::fseek(compdb.file, 0, SEEK_SET);
577-
this->handler = CommandObjectHandler(refillCount);
584+
this->handler = CommandObjectHandler(options.refillCount);
578585
this->jsonStreamBuffer.resize(bufferSize);
579586
this->compDbStream =
580587
rapidjson::FileReadStream(compdb.file, this->jsonStreamBuffer.data(),
@@ -590,8 +597,7 @@ void ResumableParser::initialize(compdb::File compdb, size_t refillCount,
590597
llvm::Regex(fmt::format(".+({})$", fmt::join(extensions, "|")));
591598
}
592599

593-
void ResumableParser::parseMore(std::vector<compdb::CommandObject> &out,
594-
bool checkFilesExist) {
600+
void ResumableParser::parseMore(std::vector<compdb::CommandObject> &out) {
595601
if (this->reader.IterativeParseComplete()) {
596602
if (this->reader.HasParseError()) {
597603
spdlog::error(
@@ -646,7 +652,7 @@ void ResumableParser::parseMore(std::vector<compdb::CommandObject> &out,
646652
continue;
647653
}
648654
}
649-
if (checkFilesExist
655+
if (this->options.checkFilesExist
650656
&& !doesFileExist(cmd.filePath, cmd.workingDirectory)) {
651657
++this->stats.skippedNonExistentTuFile;
652658
continue;

indexer/CompilationDatabase.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,12 @@ struct ToolchainConfig {
109109
};
110110

111111
struct ParseOptions {
112+
size_t refillCount;
112113
bool inferResourceDir;
113114
bool skipNonMainFileEntries;
115+
bool checkFilesExist;
114116

115-
explicit ParseOptions(bool isTesting = false)
116-
: inferResourceDir(!isTesting), skipNonMainFileEntries(!isTesting) {}
117-
118-
ParseOptions(bool inferResourceDir, bool skipNonMainFileEntries)
119-
: inferResourceDir(inferResourceDir),
120-
skipNonMainFileEntries(skipNonMainFileEntries) {}
117+
static ParseOptions create(size_t refillCount, bool forTesting = false);
121118
};
122119

123120
struct ParseStats {
@@ -156,11 +153,10 @@ class ResumableParser {
156153
/// If \param inferResourceDir is set, then the parser will automatically
157154
/// add extra '-resource-dir' '<path>' arguments to the parsed
158155
/// CompileCommands' CommandLine field.
159-
void initialize(compdb::File compdb, size_t refillCount, ParseOptions);
156+
void initialize(compdb::File compdb, ParseOptions);
160157

161-
// Parses at most refillCount elements (passed during initialization)
162-
// from the compilation database passed during initialization.
163-
void parseMore(std::vector<CommandObject> &out, bool checkFilesExist = true);
158+
/// Parses at most \c options.refillCount elements into \param out.
159+
void parseMore(std::vector<CommandObject> &out);
164160

165161
private:
166162
void tryInferResourceDir(const std::string &directoryPath,

indexer/Driver.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,8 +1209,8 @@ class Driver {
12091209
// FIXME(def: resource-dir-extra): If we're passed in a resource dir
12101210
// as an extra argument, we should not pass it here.
12111211
this->compdbParser.initialize(
1212-
compdbFile, this->refillCount(),
1213-
compdb::ParseOptions(this->options.isTesting));
1212+
compdbFile, compdb::ParseOptions::create(this->refillCount(),
1213+
this->options.isTesting));
12141214
return FileGuard(compdbFile.file);
12151215
}
12161216

indexer/Worker.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ Worker::Worker(WorkerOptions &&options)
145145
this->options.compdbPath,
146146
compdb::ValidationOptions{.checkDirectoryPathsAreAbsolute = true});
147147
compdb::ResumableParser parser{};
148-
// See FIXME(ref: resource-dir-extra)
149-
parser.initialize(compdbFile, std::numeric_limits<size_t>::max(),
150-
compdb::ParseOptions{/*isTesting*/ false});
148+
parser.initialize(compdbFile,
149+
compdb::ParseOptions::create(
150+
/*refillCount*/ std::numeric_limits<size_t>::max()));
151151
parser.parseMore(this->compileCommands);
152152
std::fclose(compdbFile.file);
153153
break;

test/test_main.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,12 @@ TEST_CASE("COMPDB_PARSING") {
159159
scip_clang::compdb::ResumableParser parser;
160160

161161
std::vector<CompDbTestCase> testCases{};
162-
testCases.push_back(CompDbTestCase{
163-
"simple.json", 3, {2, 3, 4}, compdb::ParseOptions(/*isTesting*/ true)});
164-
testCases.push_back(
165-
CompDbTestCase{"skipping.json",
166-
9,
167-
{4},
168-
compdb::ParseOptions(/*inferResourceDir*/ false,
169-
/*skipNonMainFileEntries*/ true)});
162+
auto testOptions =
163+
compdb::ParseOptions::create(/*refillCount*/ 1, /*forTesting*/ true);
164+
testCases.push_back(CompDbTestCase{"simple.json", 3, {2, 3, 4}, testOptions});
165+
auto testOptionsSkip = testOptions;
166+
testOptionsSkip.skipNonMainFileEntries = true;
167+
testCases.push_back(CompDbTestCase{"skipping.json", 9, {4}, testOptionsSkip});
170168

171169
auto dataDir =
172170
std::filesystem::current_path().append("test").append("compdb");
@@ -190,14 +188,16 @@ TEST_CASE("COMPDB_PARSING") {
190188

191189
for (auto refillCount : testCase.refillCountsToTry) {
192190
compdb::ResumableParser parser{};
193-
parser.initialize(compdbFile, refillCount, testCase.parseOptions);
191+
auto parseOptions = testCase.parseOptions;
192+
parseOptions.refillCount = refillCount;
193+
parser.initialize(compdbFile, parseOptions);
194194
std::vector<std::vector<compdb::CommandObject>> commandGroups;
195195
std::string buffer;
196196
llvm::raw_string_ostream outStr(buffer);
197197
llvm::yaml::Output yamlOut(outStr);
198198
while (true) {
199199
std::vector<compdb::CommandObject> commands;
200-
parser.parseMore(commands, /*checkFilesExist*/ false);
200+
parser.parseMore(commands);
201201
if (commands.size() == 0) {
202202
break;
203203
}

0 commit comments

Comments
 (0)