Skip to content

Commit e45eae5

Browse files
committed
Add diagnostics for a parameter that is a function, and a mismatched #else / #endif
Closes #1169 Closes #1170
1 parent 16686c1 commit e45eae5

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ venv/*
3636

3737
# VSCode workspace directory
3838
.vscode/
39+
buildh2.bat
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Microsoft (R) C/C++ Optimizing Compiler Version 19.40.33811 for x64
1+
Microsoft (R) C/C++ Optimizing Compiler Version 19.40.33812 for x64
22
Copyright (C) Microsoft Corporation. All rights reserved.
33

source/io.h

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -461,20 +461,30 @@ class braces_tracker
461461
// --- Preprocessor matching functions - #if/#else/#endif
462462

463463
// Entering an #if
464-
auto found_pre_if() -> void {
465-
assert(std::ssize(preprocessor) > 0);
464+
auto found_pre_if(lineno_t) -> void {
466465
preprocessor.push_back({});
467466
}
468467

469468
// Encountered an #else
470-
auto found_pre_else() -> void {
471-
assert(std::ssize(preprocessor) > 1);
469+
auto found_pre_else(lineno_t lineno) -> void {
470+
if (std::ssize(preprocessor) < 2) {
471+
errors.emplace_back(
472+
lineno,
473+
"#else does not match a prior #if"
474+
);
475+
}
476+
472477
preprocessor.back().found_preprocessor_else();
473478
}
474479

475480
// Exiting an #endif
476-
auto found_pre_endif() -> void {
477-
assert(std::ssize(preprocessor) > 1);
481+
auto found_pre_endif(lineno_t lineno) -> void {
482+
if (std::ssize(preprocessor) < 2) {
483+
errors.emplace_back(
484+
lineno,
485+
"#endif does not match a prior #if"
486+
);
487+
}
478488

479489
// If the #if/#else/#endif introduced the same net number of braces,
480490
// then we will have recorded that number too many open braces, and
@@ -898,11 +908,11 @@ class source
898908
{
899909
switch (pre) {
900910
break;case preprocessor_conditional::pre_if:
901-
braces.found_pre_if();
911+
braces.found_pre_if( cpp2::unsafe_narrow<lineno_t>(std::ssize(lines)) );
902912
break;case preprocessor_conditional::pre_else:
903-
braces.found_pre_else();
913+
braces.found_pre_else( cpp2::unsafe_narrow<lineno_t>(std::ssize(lines)) );
904914
break;case preprocessor_conditional::pre_endif:
905-
braces.found_pre_endif();
915+
braces.found_pre_endif( cpp2::unsafe_narrow<lineno_t>(std::ssize(lines)) );
906916
break;default:
907917
assert(false);
908918
}

source/parse.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7902,6 +7902,11 @@ class parser
79027902

79037903
// And some error checks
79047904
//
7905+
if (n->declaration->is_function()) {
7906+
error("a parameter cannot be a function", false);
7907+
return {};
7908+
}
7909+
79057910
if (
79067911
n->mod != parameter_declaration_node::modifier::none
79077912
&& !n->declaration->has_name("this")

0 commit comments

Comments
 (0)