Skip to content

Commit 01a0585

Browse files
committed
Compiler: add warning control over max_identifier_length
* pass warning flags to parser and tokenizer * let specific projects allow longer identifiers with `$warnings no-max-identifier-length`
1 parent 226ddf7 commit 01a0585

12 files changed

Lines changed: 65 additions & 13 deletions

common/build_target.c2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ public fn void Target.disableWarnings(Target* t) {
203203
t.warnings.no_unreachable_code = true;
204204
t.warnings.no_unknown_attribute = true;
205205
t.warnings.no_deprecated = true;
206+
t.warnings.no_max_identifier_length = true;
206207
}
207208

208209
public fn void Target.enableWarnings(Target* t) {
@@ -219,6 +220,7 @@ public fn void Target.enableWarnings(Target* t) {
219220
t.warnings.no_unreachable_code = false;
220221
t.warnings.no_unknown_attribute = false;
221222
t.warnings.no_deprecated = false;
223+
t.warnings.no_max_identifier_length = false;
222224
}
223225

224226
public fn const warning_flags.Flags* Target.getWarnings(const Target* t) {

common/warning_flags.c2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public type Flags struct {
2929
bool no_unreachable_code;
3030
bool no_unknown_attribute;
3131
bool no_deprecated;
32+
bool no_max_identifier_length;
3233
bool are_errors;
3334
}
3435

compiler/c2recipe_parser.c2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,9 @@ fn void Parser.parseWarnings(Parser* p) {
590590
case "deprecated":
591591
warnings.no_deprecated = disable;
592592
break;
593+
case "max-identifier-length":
594+
warnings.no_max_identifier_length = disable;
595+
break;
593596
case "promote-to-error":
594597
warnings.are_errors = !disable;
595598
break;

compiler/compiler.c2

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import string_pool;
4040
import string_utils;
4141
import target_info;
4242
import utils;
43+
import warning_flags;
4344

4445
import c_errno local;
4546
import ctype;
@@ -117,6 +118,7 @@ type Compiler struct {
117118
build_file.Info* build_info; // no ownership, can be nil
118119
build_target.Target* target; // no ownership
119120
const Options* opts; // no ownership
121+
const warning_flags.Flags* warnings; // no ownership
120122
target_info.Info targetInfo;
121123
PluginHandler* pluginHandler; // no ownership
122124

@@ -182,6 +184,7 @@ fn void Compiler.build(Compiler* c,
182184
c.target = target;
183185
c.opts = opts;
184186
c.pluginHandler = pluginHandler;
187+
c.warnings = target.getWarnings();
185188

186189
if (opts.bootstrap) {
187190
c.target.setBackEnd(C);
@@ -195,7 +198,8 @@ fn void Compiler.build(Compiler* c,
195198
c.target.setNoBuild();
196199
}
197200

198-
diags.setWarningAsError(target.getWarnings().are_errors);
201+
diags.setWarningAsError(c.warnings.are_errors);
202+
199203
c.diags.clear();
200204

201205
c.context = ast_context.create(16*1024);
@@ -286,7 +290,8 @@ fn void Compiler.build(Compiler* c,
286290
c.astPool,
287291
c.builder,
288292
&c.kwinfo,
289-
target.getFeatures());
293+
target.getFeatures(),
294+
c.warnings);
290295

291296
ast.initialize(c.context, c.astPool, c.targetInfo.intWidth / 8, color.useColor());
292297

@@ -295,7 +300,7 @@ fn void Compiler.build(Compiler* c,
295300
c.astPool,
296301
c.builder,
297302
&c.allmodules,
298-
c.target.getWarnings());
303+
c.warnings);
299304

300305
if (opts.show_libs) {
301306
c.showAllLibs();

parser/c2_parser.c2

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import token local;
3434
#if DumpTokens
3535
import utils;
3636
#endif
37+
import warning_flags;
3738

3839
import stdarg local;
3940
import stdlib local;
@@ -58,8 +59,9 @@ public type Parser struct @(opaque) {
5859
diagnostics.Diags* diags;
5960
string_pool.Pool* pool;
6061
Builder* builder;
61-
const string_list.List* features;
6262
const keywords.Info* kwinfo;
63+
const string_list.List* features;
64+
const warning_flags.Flags* warnings;
6365
bool is_interface;
6466
bool is_generated;
6567
u32 va_list_idx;
@@ -81,15 +83,17 @@ public fn Parser* create(SourceMgr* sm,
8183
string_pool.Pool* pool,
8284
ast_builder.Builder* builder,
8385
const keywords.Info* kwinfo,
84-
const string_list.List* features)
86+
const string_list.List* features,
87+
const warning_flags.Flags* warnings)
8588
{
8689
Parser* p = calloc(1, sizeof(Parser));
8790
p.sm = sm;
8891
p.diags = diags;
8992
p.pool = pool;
9093
p.builder = builder;
91-
p.features = features;
9294
p.kwinfo = kwinfo;
95+
p.features = features;
96+
p.warnings = warnings;
9397
p.va_list_idx = pool.addStr("va_list", true);
9498
p.varargs_idx = pool.addStr("varargs", true);
9599
p.stdarg_idx = pool.addStr("stdarg", true);
@@ -127,6 +131,7 @@ public fn void Parser.parse(Parser* p, i32 file_id, bool is_interface, bool is_g
127131
p.sm.get_offset(p.file_id),
128132
p.kwinfo,
129133
p.features,
134+
p.warnings,
130135
nil,
131136
nil,
132137
nil,
@@ -149,6 +154,7 @@ public fn void Parser.parse(Parser* p, i32 file_id, bool is_interface, bool is_g
149154
p.sm.get_offset(p.file_id),
150155
p.kwinfo,
151156
p.features,
157+
p.warnings,
152158
Parser.on_tokenizer_error,
153159
p,
154160
false);

parser/c2_tokenizer.c2

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import string_list;
2323
import string_pool;
2424
import token local;
2525
import utf8;
26+
import warning_flags;
2627

2728
import string;
2829
import stdlib;
@@ -264,6 +265,8 @@ public type Tokenizer struct {
264265

265266
string_pool.Pool* pool; // no ownership
266267
string_buffer.Buf* buf; // no ownership, used for strings and character constants
268+
const warning_flags.Flags* warnings;
269+
267270
ErrorFn on_error;
268271
void* on_error_arg;
269272

@@ -277,7 +280,7 @@ public type Tokenizer struct {
277280

278281
char[256] error_msg;
279282
}
280-
static_assert(408, sizeof(Tokenizer));
283+
static_assert(416, sizeof(Tokenizer));
281284

282285
public fn void Tokenizer.init(Tokenizer* t,
283286
string_pool.Pool* pool,
@@ -286,6 +289,7 @@ public fn void Tokenizer.init(Tokenizer* t,
286289
SrcLoc loc_start,
287290
const keywords.Info* kwinfo,
288291
const string_list.List* features,
292+
const warning_flags.Flags* warnings,
289293
ErrorFn on_error,
290294
void* on_error_arg,
291295
bool raw_mode)
@@ -299,6 +303,7 @@ public fn void Tokenizer.init(Tokenizer* t,
299303
t.line_start = input;
300304
t.pool = pool;
301305
t.buf = buf;
306+
t.warnings = warnings;
302307
t.on_error = on_error;
303308
t.on_error_arg = on_error_arg;
304309

@@ -684,6 +689,15 @@ fn void Tokenizer.error(Tokenizer* t, Token* result, const char* format @(printf
684689
if (t.on_error) t.on_error(t.on_error_arg, FatalError, result.loc, t.error_msg);
685690
}
686691

692+
fn void Tokenizer.warning(Tokenizer* t, SrcLoc loc, const char* format @(printf_format), ...) {
693+
va_list args;
694+
va_start(args, format);
695+
vsnprintf(t.error_msg, sizeof(t.error_msg), format, args);
696+
va_end(args);
697+
698+
if (t.on_error) t.on_error(t.on_error_arg, Warning, loc, t.error_msg);
699+
}
700+
687701
// generate an error but keep parsing
688702
fn void Tokenizer.num_error(Tokenizer* t, Token* result, const char* p, const char* format @(printf_format), ...) {
689703
va_list args;
@@ -719,9 +733,8 @@ fn void Tokenizer.lex_identifier(Tokenizer* t, Token* result) {
719733
while (Identifier_char[(u8)(*end)]) end++;
720734

721735
usize len = (usize)(end - start);
722-
if (len > constants.MaxIdentifierLen && !t.raw_mode) {
723-
t.error(result, "identifier too long (max %d chars)", constants.MaxIdentifierLen);
724-
return;
736+
if (len > constants.MaxIdentifierLen && !t.raw_mode && t.warnings && !t.warnings.no_max_identifier_length) {
737+
t.warning(result.loc, "identifier too long (max %d chars)", constants.MaxIdentifierLen);
725738
}
726739
t.cur += len;
727740
result.name_idx = t.pool.add(start, len, true);

recipe.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ executable c2cat
405405
common/file/reader.c2
406406
common/string_list.c2
407407
common/utf8.c2
408+
common/warning_flags.c2
408409

409410
parser/c2_tokenizer.c2
410411
parser/keywords.c2

test/naming/max_identifier_type.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ type T123456789012345678901234567890 struct {
55
i32 x;
66
}
77

8-
type thirty_two_is_too_long_for_an_id struct { // @error{identifier too long (max 31 chars)}
8+
type Thirty_two_is_too_long_for_an_id struct { // @warning{identifier too long (max 31 chars)}
99
i32 x;
1010
}
1111

test/naming/max_identifier_var.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module test;
33

44
fn void test1() {
55
i32 thirty_one_is_fine_for_a_name__;
6-
i32 thirty_two_is_too_long_for_an_id; // @error{identifier too long (max 31 chars)}
6+
i32 thirty_two_is_too_long_for_an_id; // @warning{identifier too long (max 31 chars)}
77
}
88

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @warnings{no-unused}
2+
// @warnings{no-max-identifier-length}
3+
module test;
4+
5+
type T123456789012345678901234567890 struct {
6+
i32 x;
7+
}
8+
9+
type Very_long_type_names_are_ok_if_requested_for_specific_targets struct {
10+
i32 x;
11+
}
12+

0 commit comments

Comments
 (0)