Skip to content

Commit

Permalink
Add diagnostics for a parameter that is a function, and a mismatched …
Browse files Browse the repository at this point in the history
…#else / #endif

Closes hsutter#1169
Closes hsutter#1170
  • Loading branch information
hsutter committed Jul 20, 2024
1 parent 16686c1 commit e45eae5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ venv/*

# VSCode workspace directory
.vscode/
buildh2.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Microsoft (R) C/C++ Optimizing Compiler Version 19.40.33811 for x64
Microsoft (R) C/C++ Optimizing Compiler Version 19.40.33812 for x64
Copyright (C) Microsoft Corporation. All rights reserved.

28 changes: 19 additions & 9 deletions source/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -461,20 +461,30 @@ class braces_tracker
// --- Preprocessor matching functions - #if/#else/#endif

// Entering an #if
auto found_pre_if() -> void {
assert(std::ssize(preprocessor) > 0);
auto found_pre_if(lineno_t) -> void {
preprocessor.push_back({});
}

// Encountered an #else
auto found_pre_else() -> void {
assert(std::ssize(preprocessor) > 1);
auto found_pre_else(lineno_t lineno) -> void {
if (std::ssize(preprocessor) < 2) {
errors.emplace_back(
lineno,
"#else does not match a prior #if"
);
}

preprocessor.back().found_preprocessor_else();
}

// Exiting an #endif
auto found_pre_endif() -> void {
assert(std::ssize(preprocessor) > 1);
auto found_pre_endif(lineno_t lineno) -> void {
if (std::ssize(preprocessor) < 2) {
errors.emplace_back(
lineno,
"#endif does not match a prior #if"
);
}

// If the #if/#else/#endif introduced the same net number of braces,
// then we will have recorded that number too many open braces, and
Expand Down Expand Up @@ -898,11 +908,11 @@ class source
{
switch (pre) {
break;case preprocessor_conditional::pre_if:
braces.found_pre_if();
braces.found_pre_if( cpp2::unsafe_narrow<lineno_t>(std::ssize(lines)) );
break;case preprocessor_conditional::pre_else:
braces.found_pre_else();
braces.found_pre_else( cpp2::unsafe_narrow<lineno_t>(std::ssize(lines)) );
break;case preprocessor_conditional::pre_endif:
braces.found_pre_endif();
braces.found_pre_endif( cpp2::unsafe_narrow<lineno_t>(std::ssize(lines)) );
break;default:
assert(false);
}
Expand Down
5 changes: 5 additions & 0 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -7902,6 +7902,11 @@ class parser

// And some error checks
//
if (n->declaration->is_function()) {
error("a parameter cannot be a function", false);
return {};
}

if (
n->mod != parameter_declaration_node::modifier::none
&& !n->declaration->has_name("this")
Expand Down

0 comments on commit e45eae5

Please sign in to comment.