Skip to content

[Parse] Improve diagnostic and add fixit to correct '#else if' to '#elseif' #25008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ ERROR(extra_tokens_conditional_compilation_directive,none,
"extra tokens following conditional compilation directive", ())
ERROR(unexpected_rbrace_in_conditional_compilation_block,none,
"unexpected '}' in conditional compilation block", ())
ERROR(unexpected_if_following_else_compilation_directive,none,
"unexpected 'if' keyword following '#else' conditional compilation "
"directive; did you mean '#elseif'?",
())

ERROR(pound_diagnostic_expected_string,none,
"expected string literal in %select{#warning|#error}0 directive",(bool))
Expand Down
6 changes: 6 additions & 0 deletions lib/Parse/ParseIfConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,12 @@ ParserResult<IfConfigDecl> Parser::parseIfConfig(
Expr *Condition = nullptr;
bool isActive = false;

if (!Tok.isAtStartOfLine() && isElse && Tok.is(tok::kw_if)) {
diagnose(Tok, diag::unexpected_if_following_else_compilation_directive)
.fixItReplace(SourceRange(ClauseLoc, consumeToken()), "#elseif");
isElse = false;
}

// Parse the condition. Evaluate it to determine the active
// clause unless we're doing a parse-only pass.
if (isElse) {
Expand Down
11 changes: 11 additions & 0 deletions test/Parse/ConditionalCompilation/basicParseErrors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,14 @@ fn_k()
func undefinedFunc() // ignored.
#endif
undefinedFunc() // expected-error {{use of unresolved identifier 'undefinedFunc'}}

#if FOO
#else if BAR
// expected-error@-1 {{unexpected 'if' keyword following '#else' conditional compilation directive; did you mean '#elseif'?}} {{1-9=#elseif}}
#else
#endif

#if FOO
#else
if true {}
#endif // OK