Skip to content

Commit d61e366

Browse files
committed
Swift: replace assert with CODEQL_ASSERT
1 parent bce483d commit d61e366

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

swift/logging/SwiftDiagnostics.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,17 @@
55
#include "absl/strings/str_join.h"
66
#include "absl/strings/str_cat.h"
77
#include "absl/strings/str_split.h"
8+
#include "swift/logging/SwiftAssert.h"
89

910
namespace codeql {
11+
12+
namespace {
13+
Logger& logger() {
14+
static Logger ret{"diagnostics"};
15+
return ret;
16+
}
17+
} // namespace
18+
1019
void SwiftDiagnosticsSource::emit(std::ostream& out,
1120
std::string_view timestamp,
1221
std::string_view message) const {
@@ -36,6 +45,10 @@ std::string SwiftDiagnosticsSource::sourceId() const {
3645
std::replace(ret.begin(), ret.end(), '_', '-');
3746
return ret;
3847
}
48+
void SwiftDiagnosticsSource::inscribeImpl(const SwiftDiagnosticsSource* source) {
49+
auto [it, inserted] = map().emplace(source->id, source);
50+
CODEQL_ASSERT(inserted, "duplicate diagnostics source detected with id {}", source->id);
51+
}
3952

4053
void SwiftDiagnosticsDumper::write(const char* buffer, std::size_t bufferSize) {
4154
binlog::Range range{buffer, bufferSize};

swift/logging/SwiftDiagnostics.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ struct SwiftDiagnosticsSource {
3131
template <const SwiftDiagnosticsSource* Spec>
3232
static void inscribe() {
3333
static std::once_flag once;
34-
std::call_once(once, [] {
35-
auto [it, inserted] = map().emplace(Spec->id, Spec);
36-
assert(inserted);
37-
});
34+
std::call_once(once, inscribeImpl, Spec);
3835
}
3936

4037
// gets a previously inscribed SwiftDiagnosticsSource for the given id. Will abort if none exists
@@ -47,6 +44,8 @@ struct SwiftDiagnosticsSource {
4744
void emit(std::ostream& out, std::string_view timestamp, std::string_view message) const;
4845

4946
private:
47+
static void inscribeImpl(const SwiftDiagnosticsSource* Spec);
48+
5049
std::string sourceId() const;
5150
using Map = std::unordered_map<std::string, const SwiftDiagnosticsSource*>;
5251

0 commit comments

Comments
 (0)