Add compiler guards for GCC directives

This commit is contained in:
Bruce Hill 2025-03-28 15:31:53 -04:00
parent 4de0fee8f6
commit ca76fb335a
16 changed files with 90 additions and 0 deletions

View File

@ -2152,10 +2152,14 @@ CORD compile_string_literal(CORD literal)
{
CORD code = "\"";
CORD_pos i;
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion"
#endif
CORD_FOR(i, literal) {
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
char c = CORD_pos_fetch(i);
switch (c) {
case '\\': code = CORD_cat(code, "\\\\"); break;
@ -2181,10 +2185,14 @@ CORD compile_string_literal(CORD literal)
static bool string_literal_is_all_ascii(CORD literal)
{
CORD_pos i;
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion"
#endif
CORD_FOR(i, literal) {
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
if (!isascii(CORD_pos_fetch(i)))
return false;
}

View File

@ -20,10 +20,14 @@ public CORD CORD_quoted(CORD str)
{
CORD quoted = "\"";
CORD_pos i;
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion"
#endif
CORD_FOR(i, str) {
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
char c = CORD_pos_fetch(i);
switch (c) {
case '\a': quoted = CORD_cat(quoted, "\\a"); break;

View File

@ -231,8 +231,10 @@ static PARSER(parse_deserialize);
//
// Convert an escape sequence like \n to a string
//
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstack-protector"
#endif
static const char *unescape(parse_ctx_t *ctx, const char **out) {
const char **endpos = out;
const char *escape = *out;
@ -292,7 +294,9 @@ static const char *unescape(parse_ctx_t *ctx, const char **out) {
return GC_strndup(escape+1, 1);
}
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
// Indent is in number of spaces (assuming that \t is 4 spaces)
PUREFUNC static INLINE int64_t get_indent(parse_ctx_t *ctx, const char *pos)

View File

@ -332,8 +332,10 @@ void run(env_t *env, ast_t *ast)
}
}
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstack-protector"
#endif
void eval(env_t *env, ast_t *ast, void *dest)
{
type_t *t = get_type(env, ast);
@ -542,6 +544,8 @@ void eval(env_t *env, ast_t *ast, void *dest)
print_err("Eval not implemented for ", ast_to_str(ast));
}
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0

View File

@ -277,8 +277,10 @@ MACROLIKE PUREFUNC bool Int$is_negative(Int_t x) {
// Constructors/conversion functions:
// Int constructors:
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
#endif
MACROLIKE PUREFUNC Int_t Int$from_num(double n, bool truncate) {
mpz_t result;
mpz_init_set_d(result, n);
@ -425,6 +427,8 @@ MACROLIKE PUREFUNC Int8_t Int8$from_int16(Int16_t i16, bool truncate) {
fail("Integer is too big to fit in a 8-bit integer: ", i16);
return i8;
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0

View File

@ -36,8 +36,10 @@ MACROLIKE CONSTFUNC double Num$clamped(double x, double low, double high) {
return (x <= low) ? low : (x >= high ? high : x);
}
MACROLIKE CONSTFUNC double Num$from_num32(Num32_t n) { return (double)n; }
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
#endif
MACROLIKE CONSTFUNC double Num$from_int(Int_t i, bool truncate) {
if likely (i.small & 0x1) {
double ret = (double)(i.small >> 2);
@ -55,7 +57,9 @@ MACROLIKE CONSTFUNC double Num$from_int(Int_t i, bool truncate) {
return ret;
}
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
MACROLIKE CONSTFUNC double Num$from_int64(Int64_t i, bool truncate) {
double n = (double)i;
if unlikely (!truncate && (Int64_t)n != i)
@ -88,8 +92,10 @@ MACROLIKE CONSTFUNC float Num32$clamped(float x, float low, float high) {
return (x <= low) ? low : (x >= high ? high : x);
}
MACROLIKE CONSTFUNC float Num32$from_num(Num_t n) { return (float)n; }
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
#endif
MACROLIKE CONSTFUNC float Num32$from_int(Int_t i, bool truncate) {
if likely (i.small & 0x1) {
float ret = (float)(i.small >> 2);
@ -107,7 +113,9 @@ MACROLIKE CONSTFUNC float Num32$from_int(Int_t i, bool truncate) {
return ret;
}
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
MACROLIKE CONSTFUNC float Num32$from_int64(Int64_t i, bool truncate) {
float n = (float)i;
if unlikely (!truncate && (Int64_t)n != i)

View File

@ -24,7 +24,12 @@ struct RNGState_t {
uint8_t random_bytes[1024];
};
#ifdef __TINYC__
// TinyCC doesn't implement _Thread_local
public _Thread_local RNG_t default_rng = (struct RNGState_t[1]){};
#else
public RNG_t default_rng = (struct RNGState_t[1]){};
#endif
PUREFUNC static Text_t RNG$as_text(const void *rng, bool colorize, const TypeInfo_t*)
{

View File

@ -26,6 +26,11 @@ Num_t RNG$num(RNG_t rng, Num_t min, Num_t max);
Num32_t RNG$num32(RNG_t rng, Num32_t min, Num32_t max);
extern const TypeInfo_t RNG$info;
// TinyCC doesn't implement _Thread_local
#ifdef __TINYC__
extern RNG_t default_rng;
#else
extern _Thread_local RNG_t default_rng;
#endif
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0

View File

@ -49,13 +49,17 @@ public uint64_t TOMO_HASH_KEY[2] = {23, 42}; // Randomized in tomo_init()
PUREFUNC public uint64_t siphash24(const uint8_t *src, size_t src_sz) {
siphash sh;
if ((uint64_t)src % __alignof__(uint64_t) == 0) {
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-align"
#endif
const uint64_t *in = (uint64_t*)src;
/* Find largest src_sz evenly divisible by 8 bytes. */
const ptrdiff_t src_sz_nearest_8bits = ((ptrdiff_t)src_sz >> 3) << 3;
const uint64_t *goal = (uint64_t*)(src + src_sz_nearest_8bits);
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
siphashinit(&sh, src_sz);
src_sz -= (size_t)src_sz_nearest_8bits;
while (in < goal) {

View File

@ -231,8 +231,10 @@ static Table_t parse_table(const TypeInfo_t *table, int n, char *args[])
return Table$from_entries(entries, table);
}
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstack-protector"
#endif
public void _tomo_parse_args(int argc, char *argv[], Text_t usage, Text_t help, int spec_len, cli_arg_t spec[spec_len])
{
bool populated_args[spec_len];
@ -442,7 +444,9 @@ public void _tomo_parse_args(int argc, char *argv[], Text_t usage, Text_t help,
}
}
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
static void print_stack_line(FILE *out, OptionalText_t fn_name, const char *filename, int64_t line_num)
{

View File

@ -14,8 +14,10 @@
#include "text.h"
#include "util.h"
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstack-protector"
#endif
PUREFUNC public uint64_t Struct$hash(const void *obj, const TypeInfo_t *type)
{
if (type->StructInfo.num_fields == 0)
@ -50,7 +52,9 @@ PUREFUNC public uint64_t Struct$hash(const void *obj, const TypeInfo_t *type)
}
return siphash24((void*)field_hashes, sizeof(field_hashes));
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
PUREFUNC public uint64_t PackedData$hash(const void *obj, const TypeInfo_t *type)
{

View File

@ -215,8 +215,10 @@ static void hashmap_resize_buckets(Table_t *t, uint32_t new_capacity, const Type
}
// Return address of value
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstack-protector"
#endif
public void *Table$reserve(Table_t *t, const void *key, const void *value, const TypeInfo_t *type)
{
assert(type->tag == TableInfo);
@ -278,7 +280,9 @@ public void *Table$reserve(Table_t *t, const void *key, const void *value, const
Table$set_bucket(t, entry, entry_index, type);
return entry + value_offset(type);
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
public void Table$set(Table_t *t, const void *key, const void *value, const TypeInfo_t *type)
{
@ -768,8 +772,10 @@ public void Table$serialize(const void *obj, FILE *out, Table_t *pointers, const
Optional$serialize(&t->fallback, out, pointers, Optional$info(sizeof(void*), __alignof__(void*), Pointer$info("&", type)));
}
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstack-protector"
#endif
public void Table$deserialize(FILE *in, void *outval, Array_t *pointers, const TypeInfo_t *type)
{
int64_t len;
@ -788,6 +794,8 @@ public void Table$deserialize(FILE *in, void *outval, Array_t *pointers, const T
*(Table_t*)outval = t;
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1

View File

@ -134,8 +134,10 @@ static const TypeInfo_t GraphemeClusterInfo = {
},
};
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstack-protector"
#endif
public int32_t get_synthetic_grapheme(const ucs4_t *codepoints, int64_t utf32_len)
{
ucs4_t length_prefixed[1+utf32_len];
@ -224,7 +226,9 @@ public int32_t get_synthetic_grapheme(const ucs4_t *codepoints, int64_t utf32_le
last_grapheme = grapheme_id;
return grapheme_id;
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
int text_visualize(FILE *stream, Text_t t, int depth)
{

View File

@ -82,8 +82,10 @@ typedef struct {
bool h:1, c:1, o:1;
} staleness_t;
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstack-protector"
#endif
int main(int argc, char *argv[])
{
// Get the file modification time of the compiler, so we
@ -251,7 +253,9 @@ int main(int argc, char *argv[])
}
return 0;
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
Text_t escape_lib_name(Text_t lib_name)
{

View File

@ -21,8 +21,10 @@
type_t *parse_type_ast(env_t *env, type_ast_t *ast)
{
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch-default"
#endif
switch (ast->tag) {
case VarTypeAST: {
const char *name = Match(ast, VarTypeAST)->name;
@ -134,7 +136,9 @@ type_t *parse_type_ast(env_t *env, type_ast_t *ast)
}
case UnknownTypeAST: code_err(ast, "I don't know how to get this type");
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
errx(1, "Unreachable");
}
@ -564,8 +568,10 @@ type_t *get_clause_type(env_t *env, type_t *subject_t, when_clause_t *clause)
type_t *get_type(env_t *env, ast_t *ast)
{
if (!ast) return NULL;
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch-default"
#endif
switch (ast->tag) {
case None: {
if (!Match(ast, None)->type)
@ -1398,7 +1404,9 @@ type_t *get_type(env_t *env, ast_t *ast)
case Unknown: code_err(ast, "I can't figure out the type of: ", ast_to_str(ast));
case Deserialize: return parse_type_ast(env, Match(ast, Deserialize)->type);
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
code_err(ast, "I can't figure out the type of: ", ast_to_str(ast));
}
@ -1544,13 +1552,17 @@ PUREFUNC bool is_constant(env_t *env, ast_t *ast)
case TextLiteral: {
CORD literal = Match(ast, TextLiteral)->cord;
CORD_pos i;
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion"
#endif
CORD_FOR(i, literal) {
if (!isascii(CORD_pos_fetch(i)))
return false; // Non-ASCII requires grapheme logic, not constant
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
return true; // Literal ASCII string, OK
}
case Not: return is_constant(env, Match(ast, Not)->value);

View File

@ -487,8 +487,10 @@ PUREFUNC size_t type_size(type_t *t)
if (t == THREAD_TYPE) return sizeof(pthread_t*);
if (t == PATH_TYPE) return sizeof(Path_t);
if (t == PATH_TYPE_TYPE) return sizeof(PathType_t);
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch-default"
#endif
switch (t->tag) {
case UnknownType: case AbortType: case ReturnType: case VoidType: return 0;
case MemoryType: errx(1, "Memory has undefined type size");
@ -567,7 +569,9 @@ PUREFUNC size_t type_size(type_t *t)
case TypeInfoType: return sizeof(TypeInfo_t);
case ModuleType: return 0;
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
errx(1, "This should not be reachable");
}
@ -576,8 +580,10 @@ PUREFUNC size_t type_align(type_t *t)
if (t == THREAD_TYPE) return __alignof__(pthread_t*);
if (t == PATH_TYPE) return __alignof__(Path_t);
if (t == PATH_TYPE_TYPE) return __alignof__(PathType_t);
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch-default"
#endif
switch (t->tag) {
case UnknownType: case AbortType: case ReturnType: case VoidType: return 0;
case MemoryType: errx(1, "Memory has undefined type alignment");
@ -642,7 +648,9 @@ PUREFUNC size_t type_align(type_t *t)
case TypeInfoType: return __alignof__(TypeInfo_t);
case ModuleType: return 0;
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
errx(1, "This should not be reachable");
}