1313#include " swift/extractor/infra/SwiftBodyEmissionStrategy.h"
1414#include " swift/extractor/infra/SwiftMangledName.h"
1515#include " swift/extractor/config/SwiftExtractorState.h"
16- #include " swift/extractor/infra/log/SwiftLogging .h"
16+ #include " swift/extractor/infra/log/SwiftAssert .h"
1717
1818namespace codeql {
1919
@@ -74,21 +74,20 @@ class SwiftDispatcher {
7474 entry.forEachLabel ([&valid, &entry, this ](const char * field, int index, auto & label) {
7575 using Label = std::remove_reference_t <decltype (label)>;
7676 if (!label.valid ()) {
77- std::cerr << entry.NAME << " has undefined " << field;
78- if (index >= 0 ) {
79- std::cerr << ' [' << index << ' ]' ;
80- }
77+ const char * action;
8178 if constexpr (std::is_base_of_v<typename Label::Tag, UnspecifiedElementTag>) {
82- std::cerr << " , replacing with unspecified element\n " ;
79+ action = " replacing with unspecified element" ;
8380 label = emitUnspecified (idOf (entry), field, index);
8481 } else {
85- std::cerr << " , skipping emission\n " ;
82+ action = " skipping emission" ;
8683 valid = false ;
8784 }
85+ LOG_ERROR (" {} has undefined field {}{}, {}" , entry.NAME , field,
86+ index >= 0 ? (' [' + std::to_string (index) + ' ]' ) : " " , action);
8887 }
8988 });
9089 if (valid) {
91- trap.emit (entry);
90+ trap.emit (entry, /* check */ false );
9291 }
9392 }
9493
@@ -153,7 +152,7 @@ class SwiftDispatcher {
153152 auto & stored = store[e];
154153 if (!stored.valid ()) {
155154 auto inserted = fetching.insert (e);
156- assert (inserted.second && " detected infinite fetchLabel recursion" );
155+ CODEQL_ASSERT (inserted.second , " detected infinite fetchLabel recursion" );
157156 stored = createLabel (e, type);
158157 fetching.erase (e);
159158 }
@@ -165,7 +164,13 @@ class SwiftDispatcher {
165164 TrapLabel<TypeTag> fetchLabel (swift::Type t) { return fetchLabel (t.getPointer ()); }
166165
167166 TrapLabel<AstNodeTag> fetchLabel (swift::ASTNode node) {
168- return fetchLabelFromUnion<AstNodeTag>(node);
167+ auto ret = fetchLabelFromUnion<AstNodeTag>(node);
168+ if (!ret.valid ()) {
169+ // TODO to be more useful, we need a generic way of attaching original source location info
170+ // to logs, this will come in upcoming work
171+ LOG_ERROR (" Unable to fetch label for ASTNode" );
172+ }
173+ return ret;
169174 }
170175
171176 template <typename E, std::enable_if_t <IsFetchable<E*>>* = nullptr >
@@ -177,7 +182,7 @@ class SwiftDispatcher {
177182 template <typename E>
178183 auto createEntry (const E& e) {
179184 auto found = store.find (&e);
180- assert (found != store.end () && " createEntry called on non-fetched label" );
185+ CODEQL_ASSERT (found != store.end (), " createEntry called on non-fetched label" );
181186 auto label = TrapLabel<ConcreteTrapTagOf<E>>::unsafeCreateFromUntyped (found->second );
182187 if constexpr (IsLocatable<E>) {
183188 locationExtractor.attachLocation (sourceManager, e, label);
@@ -307,7 +312,6 @@ class SwiftDispatcher {
307312 // with logical op short-circuiting, this will stop trying on the first successful fetch
308313 bool unionCaseFound = (... || fetchLabelFromUnionCase<Tag, Ts>(u, ret));
309314 if (!unionCaseFound) {
310- // TODO emit error/warning here
311315 return undefined_label;
312316 }
313317 return ret;
@@ -357,7 +361,10 @@ class SwiftDispatcher {
357361 SwiftLocationExtractor& locationExtractor;
358362 SwiftBodyEmissionStrategy& bodyEmissionStrategy;
359363 std::unordered_set<swift::ModuleDecl*> encounteredModules;
360- Logger logger{" dispatcher" };
364+ Logger& logger () {
365+ static Logger ret{" dispatcher" };
366+ return ret;
367+ }
361368};
362369
363370} // namespace codeql
0 commit comments