Skip to content

Commit 434186f

Browse files
committed
Refactor: create QLJS_CONSTEXPR_ASSERT macro
Express intent by creating a documented QLJS_CONSTEXPR_ASSERT macro for our constexpr asm("") hack.
1 parent fabcc1d commit 434186f

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

src/quick-lint-js/assert.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@
7474

7575
#define QLJS_ASSERT_TRAP() QLJS_CRASH_ALLOWING_CORE_DUMP()
7676

77+
// QLJS_CONSTEXPR_ASSERT is like QLJS_ALWAYS_ASSERT, except:
78+
//
79+
// * It must only be called in a constexpr function.
80+
// * It must only be called at compile time (i.e. not a run-time constexpr
81+
// function).
82+
// * It works only with some compilers.
83+
#if __cpp_constexpr >= 201907L && !defined(_MSC_VER)
84+
#define QLJS_CONSTEXPR_ASSERT(...) \
85+
do { \
86+
if (__VA_ARGS__) { \
87+
} else { \
88+
asm(""); \
89+
} \
90+
} while (false)
91+
#else
92+
#define QLJS_CONSTEXPR_ASSERT(...) QLJS_NEVER_ASSERT(__VA_ARGS__)
93+
#endif
94+
7795
namespace quick_lint_js {
7896
void report_assertion_failure(const char *qljs_file_name, int qljs_line,
7997
const char *qljs_function_name,

src/quick-lint-js/translation-table-generated.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <cstddef>
1010
#include <cstdint>
1111
#include <iterator>
12+
#include <quick-lint-js/assert.h>
1213
#include <quick-lint-js/consteval.h>
1314
#include <quick-lint-js/hash-fnv.h>
1415
#include <quick-lint-js/translation-table.h>
@@ -430,12 +431,9 @@ QLJS_CONSTEVAL std::uint16_t translation_table_const_hash_table_look_up(
430431
}
431432
}
432433

433-
// Helpfully fail compilation if this file is out of date.
434-
#if __cpp_constexpr >= 201907L && !defined(_MSC_VER)
435434
// If you see an error with the following line, translation-table-generated.h
436435
// is out of date. Run tools/update-translator-sources to rebuild this file.
437-
asm("");
438-
#endif
436+
QLJS_CONSTEXPR_ASSERT(false);
439437

440438
return 0;
441439
}

tools/compile-translations.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ func WriteTranslationTableHeader(table *TranslationTable, path string) error {
443443
#include <cstddef>
444444
#include <cstdint>
445445
#include <iterator>
446+
#include <quick-lint-js/assert.h>
446447
#include <quick-lint-js/consteval.h>
447448
#include <quick-lint-js/hash-fnv.h>
448449
#include <quick-lint-js/translation-table.h>
@@ -490,12 +491,9 @@ using namespace std::literals::string_view_literals;
490491
}
491492
}
492493
493-
// Helpfully fail compilation if this file is out of date.
494-
#if __cpp_constexpr >= 201907L && !defined(_MSC_VER)
495494
// If you see an error with the following line, translation-table-generated.h
496495
// is out of date. Run tools/update-translator-sources to rebuild this file.
497-
asm("");
498-
#endif
496+
QLJS_CONSTEXPR_ASSERT(false);
499497
500498
return 0;
501499
}

0 commit comments

Comments
 (0)