Skip to content

Commit dbfd85c

Browse files
committed
Swift: replace assertions and prints in main and SwiftExtractor
1 parent f42975f commit dbfd85c

File tree

4 files changed

+36
-32
lines changed

4 files changed

+36
-32
lines changed

swift/extractor/SwiftExtractor.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,23 @@
1414
#include "swift/extractor/infra/SwiftLocationExtractor.h"
1515
#include "swift/extractor/infra/SwiftBodyEmissionStrategy.h"
1616
#include "swift/extractor/mangler/SwiftMangler.h"
17+
#include "swift/extractor/infra/log/SwiftAssert.h"
1718

1819
using namespace codeql;
1920
using namespace std::string_literals;
2021
namespace fs = std::filesystem;
2122

23+
Logger& main_logger::logger() {
24+
static Logger ret{"main"};
25+
return ret;
26+
}
27+
28+
using namespace main_logger;
29+
2230
static void ensureDirectory(const char* label, const fs::path& dir) {
2331
std::error_code ec;
2432
fs::create_directories(dir, ec);
25-
if (ec) {
26-
std::cerr << "Cannot create " << label << " directory: " << ec.message() << "\n";
27-
std::abort();
28-
}
33+
CODEQL_ASSERT(!ec, "Cannot create {} directory ({})", label, ec);
2934
}
3035

3136
static void archiveFile(const SwiftExtractorConfiguration& config, swift::SourceFile& file) {
@@ -36,11 +41,7 @@ static void archiveFile(const SwiftExtractorConfiguration& config, swift::Source
3641

3742
std::error_code ec;
3843
fs::copy(source, destination, fs::copy_options::overwrite_existing, ec);
39-
40-
if (ec) {
41-
std::cerr << "Cannot archive source file " << source << " -> " << destination << ": "
42-
<< ec.message() << "\n";
43-
}
44+
CODEQL_ASSERT(!ec, "Cannot archive source file {} -> {} ({})", source, destination, ec);
4445
}
4546

4647
static fs::path getFilename(swift::ModuleDecl& module,
@@ -243,11 +244,9 @@ void codeql::extractExtractLazyDeclarations(SwiftExtractorState& state,
243244
// Just in case
244245
const int upperBound = 100;
245246
int iteration = 0;
246-
while (!state.pendingDeclarations.empty() && iteration++ < upperBound) {
247+
while (!state.pendingDeclarations.empty()) {
248+
CODEQL_ASSERT(iteration++ < upperBound,
249+
"Swift extractor reached upper bound while extracting lazy declarations");
247250
extractLazy(state, compiler);
248251
}
249-
if (iteration >= upperBound) {
250-
std::cerr << "Swift extractor reached upper bound while extracting lazy declarations\n";
251-
abort();
252-
}
253252
}

swift/extractor/SwiftExtractor.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@
88
namespace codeql {
99
void extractSwiftFiles(SwiftExtractorState& state, swift::CompilerInstance& compiler);
1010
void extractExtractLazyDeclarations(SwiftExtractorState& state, swift::CompilerInstance& compiler);
11+
12+
class Logger;
13+
namespace main_logger {
14+
Logger& logger();
15+
}
1116
} // namespace codeql

swift/extractor/main.cpp

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
#include "swift/extractor/invocation/SwiftInvocationExtractor.h"
1717
#include "swift/extractor/trap/TrapDomain.h"
1818
#include "swift/extractor/infra/file/Path.h"
19-
#include "swift/extractor/infra/log/SwiftLogging.h"
19+
#include "swift/extractor/infra/log/SwiftAssert.h"
2020

2121
using namespace std::string_literals;
22+
using namespace codeql::main_logger;
2223

2324
const std::string_view codeql::logRootName = "extractor";
2425

@@ -30,9 +31,10 @@ static void lockOutputSwiftModuleTraps(codeql::SwiftExtractorState& state,
3031
!module.empty()) {
3132
if (auto target = codeql::createTargetTrapDomain(state, codeql::resolvePath(module),
3233
codeql::TrapType::module)) {
33-
target->emit("// trap file deliberately empty\n"
34-
"// this swiftmodule was created during the build, so its entities must have"
35-
" been extracted directly from source files");
34+
target->emitComment(
35+
"trap file deliberately empty\n"
36+
" * this swiftmodule was created during the build, so its entities must have\n"
37+
" * been extracted directly from source files\n");
3638
}
3739
}
3840
}
@@ -43,7 +45,6 @@ static void processFrontendOptions(codeql::SwiftExtractorState& state,
4345
auto& inOuts = options.InputsAndOutputs;
4446
std::vector<swift::InputFile> inputs;
4547
inOuts.forEachInput([&](const swift::InputFile& input) {
46-
std::cerr << input.getFileName() << ":\n";
4748
swift::PrimarySpecificPaths psp{};
4849
if (std::filesystem::path output = input.getPrimarySpecificPaths().OutputFilename;
4950
!output.empty()) {
@@ -142,7 +143,7 @@ static bool checkRunUnderFilter(int argc, char* const* argv) {
142143
// An example usage is to run the extractor under `gdbserver :1234` when the
143144
// arguments match a given source file.
144145
static void checkWhetherToRunUnderTool(int argc, char* const* argv) {
145-
assert(argc > 0);
146+
if (argc == 0) return;
146147

147148
auto runUnder = getenv("CODEQL_EXTRACTOR_SWIFT_RUN_UNDER");
148149
if (runUnder == nullptr || !checkRunUnderFilter(argc, argv)) {
@@ -168,10 +169,7 @@ codeql::TrapDomain invocationTrapDomain(codeql::SwiftExtractorState& state) {
168169
auto filename = std::to_string(timestamp) + '-' + std::to_string(getpid());
169170
auto target = std::filesystem::path("invocations") / std::filesystem::path(filename);
170171
auto maybeDomain = codeql::createTargetTrapDomain(state, target, codeql::TrapType::invocation);
171-
if (!maybeDomain) {
172-
std::cerr << "Cannot create invocation trap file: " << target << "\n";
173-
abort();
174-
}
172+
CODEQL_ASSERT(maybeDomain, "Cannot create invocation trap file for {}", target);
175173
return std::move(maybeDomain.value());
176174
}
177175

@@ -219,11 +217,8 @@ int main(int argc, char** argv, char** envp) {
219217
initializeSwiftModules();
220218

221219
const auto configuration = configure(argc, argv);
222-
{
223-
codeql::Logger logger{"main"};
224-
LOG_INFO("calling extractor with arguments \"{}\"", argDump(argc, argv));
225-
LOG_DEBUG("environment:\n{}\n", envDump(envp));
226-
}
220+
LOG_INFO("calling extractor with arguments \"{}\"", argDump(argc, argv));
221+
LOG_DEBUG("environment:\n{}\n", envDump(envp));
227222

228223
auto openInterception = codeql::setupFileInterception(configuration);
229224

swift/extractor/trap/TrapDomain.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@ class TrapDomain {
2626
}
2727

2828
template <typename... Args>
29-
void debug(const Args&... args) {
30-
out << "/* DEBUG:\n";
29+
void emitComment(const Args&... args) {
30+
out << "/* ";
3131
(out << ... << args);
32-
out << "\n*/\n";
32+
out << " */\n";
33+
}
34+
35+
template <typename... Args>
36+
void debug(const Args&... args) {
37+
emitComment("DEBUG:\n", args..., '\n');
3338
}
3439

3540
template <typename Tag>

0 commit comments

Comments
 (0)