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

Implement #warning and #error #14048

Merged
merged 8 commits into from Feb 3, 2018

Conversation

Projects
None yet
8 participants
@harlanhaskins
Collaborator

harlanhaskins commented Jan 22, 2018

This PR is an implementation of #warning and #error, which is currently being pitched to swift-evolution was accepted for Swift 5.

@harlanhaskins

This comment has been minimized.

Collaborator

harlanhaskins commented Jan 22, 2018

@swift-ci please smoke test

@harlanhaskins harlanhaskins requested review from DougGregor and jrose-apple and removed request for DougGregor and jrose-apple Jan 22, 2018

@harlanhaskins

This comment has been minimized.

Collaborator

harlanhaskins commented Jan 23, 2018

This currently doesn't handle directives in the middle of switch statements, a la:

switch 4 {
#warning "boo"
default: break
}
@nkcsgexi

This comment has been minimized.

Contributor

nkcsgexi commented Jan 23, 2018

Finally... @harlanhaskins

@harlanhaskins harlanhaskins force-pushed the harlanhaskins:caution-tape branch Feb 1, 2018

@harlanhaskins harlanhaskins changed the title from [Do Not Merge] Implement #warning and #error to [WIP] Implement #warning and #error Feb 2, 2018

@harlanhaskins harlanhaskins force-pushed the harlanhaskins:caution-tape branch to ed5dbd3 Feb 2, 2018

@harlanhaskins harlanhaskins requested review from DougGregor and jrose-apple Feb 2, 2018

@harlanhaskins

This comment has been minimized.

Collaborator

harlanhaskins commented Feb 2, 2018

@swift-ci please smoke test

@harlanhaskins

This comment has been minimized.

Collaborator

harlanhaskins commented Feb 2, 2018

@swift-ci please smoke test

Printer << tok::pound_warning;
}
Printer << '"' << PDD->getMessage()->getValue() << '"';

This comment has been minimized.

@harlanhaskins

harlanhaskins Feb 2, 2018

Collaborator

Before I forget, I need to add parens to this.

@harlanhaskins harlanhaskins force-pushed the harlanhaskins:caution-tape branch to ffcefa7 Feb 2, 2018

@harlanhaskins

This comment has been minimized.

Collaborator

harlanhaskins commented Feb 2, 2018

@swift-ci please smoke test

@@ -188,6 +188,11 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
return false;
}
bool visitPoundDiagnosticDecl(PoundDiagnosticDecl *PDD) {
// By default, ignore #erorr/#warning.

This comment has been minimized.

@skagedal

skagedal Feb 2, 2018

Typo on "erorr" :)

This comment has been minimized.

@harlanhaskins

harlanhaskins Feb 2, 2018

Collaborator

Thanks!

auto string = parseExprStringLiteral();
if (string.isNull())
return makeParserError();

This comment has been minimized.

@jrose-apple

jrose-apple Feb 2, 2018

Member

Suggestion: continue looking for a close-paren if it occurs before the end of the line.

This comment has been minimized.

@harlanhaskins

harlanhaskins Feb 2, 2018

Collaborator

👍

I originally tried checking for #warning “Foo” and FixIt-ing in the surrounding parens. Should I continue with that?

This comment has been minimized.

@jrose-apple

jrose-apple Feb 2, 2018

Member

That's also good, but I'm mostly thinking about #warning(Foo) and worse #warning(Foo is bad, yanno?), where the fix is probably to add quotes.

This comment has been minimized.

@harlanhaskins

harlanhaskins Feb 2, 2018

Collaborator

Also a good idea!

@@ -1098,6 +1098,17 @@ namespace {
PrintWithColorRAII(OS, ParenthesisColor) << ')';
}
void visitPoundDiagnosticDecl(PoundDiagnosticDecl *PDD) {
const char *name = PDD->isError() ?
"pound_error_decl" : "pound_warning_decl";

This comment has been minimized.

@jrose-apple

jrose-apple Feb 2, 2018

Member

Nitpick: seems like having a common pound_diagnostic_decl as the kind and then printing an attribute for error vs. warning would be more inline with the other AST nodes.

This comment has been minimized.

@harlanhaskins

harlanhaskins Feb 2, 2018

Collaborator

👍

@@ -7143,6 +7144,14 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
TC.checkDeclAttributes(ICD);
}
void visitPoundDiagnosticDecl(PoundDiagnosticDecl *PDD) {
if (PDD->hasBeenEmitted()) { return; }
PDD->markEmitted();

This comment has been minimized.

@jrose-apple

jrose-apple Feb 2, 2018

Member

I suspect this can never happen more than once per phase, so you may be able to get rid of hasBeenEmitted.

This comment has been minimized.

@harlanhaskins

harlanhaskins Feb 2, 2018

Collaborator

I’ll try it and see what happens!

This comment has been minimized.

@harlanhaskins

harlanhaskins Feb 2, 2018

Collaborator

Turns out it does happen twice. ☹️

switch 34 {
#warning("warnings can be nested in switch statements") // expected-warning {{warnings can be nested in switch statements}}
#if true
#error("errors can be nested in if-configs inside switch statements too") // expected-error {{errors can be nested in if-configs inside switch statements too}}

This comment has been minimized.

@jrose-apple

jrose-apple Feb 2, 2018

Member

There's a slightly different test where the #error is immediately after a case.

This comment has been minimized.

@jrose-apple

jrose-apple Feb 2, 2018

Member

Oops, sorry, I guess that's just part of the body of the case. It's where the #if is immediately after a case, but contains cases itself.

This comment has been minimized.

@harlanhaskins

harlanhaskins Feb 2, 2018

Collaborator

Okay, I’ll nest a case inside the existing if config and then put a #error in there 👍

@harlanhaskins harlanhaskins force-pushed the harlanhaskins:caution-tape branch to f921dcd Feb 2, 2018

@harlanhaskins

This comment has been minimized.

Collaborator

harlanhaskins commented Feb 2, 2018

@jrose-apple Okay, this latest commit will fix-it:

#warning "foo"
#warning test 123
#warning(test 123)
@harlanhaskins

This comment has been minimized.

Collaborator

harlanhaskins commented Feb 2, 2018

@swift-ci please smoke test

@harlanhaskins harlanhaskins force-pushed the harlanhaskins:caution-tape branch to c0de994 Feb 2, 2018

@harlanhaskins

This comment has been minimized.

Collaborator

harlanhaskins commented Feb 2, 2018

@swift-ci please smoke test

// Catch #warning(oops, forgot the quotes)
SourceLoc wordsStartLoc = Tok.getLoc();
while (!Tok.isAtStartOfLine() && Tok.isNot(tok::r_paren)) {

This comment has been minimized.

@jrose-apple

jrose-apple Feb 2, 2018

Member

Don't forget to consume the r_paren!

This comment has been minimized.

@harlanhaskins

harlanhaskins Feb 2, 2018

Collaborator

Ack! Thanks!

@harlanhaskins harlanhaskins changed the title from [WIP] Implement #warning and #error to Implement #warning and #error Feb 3, 2018

@harlanhaskins harlanhaskins force-pushed the harlanhaskins:caution-tape branch to d4fe20f Feb 3, 2018

@rintaro

This comment has been minimized.

Member

rintaro commented Feb 3, 2018

Could you add check for trailing tokens on the same line? e.g.

#warning("foo bar") var x = 1 // expected-error {{extra tokens following warning directive}}
#error("foo bar"); // expected-error {{extra tokens following error directive}}

I'm not sure this was discussed, but we don't allow this for any directives so far.

@harlanhaskins harlanhaskins force-pushed the harlanhaskins:caution-tape branch to 27c1ffe Feb 3, 2018

@harlanhaskins

This comment has been minimized.

Collaborator

harlanhaskins commented Feb 3, 2018

@swift-ci please smoke test

@harlanhaskins

This comment has been minimized.

Collaborator

harlanhaskins commented Feb 3, 2018

Build timed out?

@swift-ci please smoke test Linux platform

@DougGregor

This looks fantastic

@harlanhaskins

This comment has been minimized.

Collaborator

harlanhaskins commented Feb 3, 2018

🚢

@harlanhaskins harlanhaskins merged commit 5e02d2a into apple:master Feb 3, 2018

2 checks passed

Swift Test Linux Platform (smoke test)
Details
Swift Test OS X Platform (smoke test)
Details
@@ -157,7 +157,7 @@ syn match swiftOperator "\.\.[<.]" skipwhite nextgroup=swiftTypeParameters
syn match swiftChar /'\([^'\\]\|\\\(["'tnr0\\]\|x[0-9a-fA-F]\{2}\|u[0-9a-fA-F]\{4}\|U[0-9a-fA-F]\{8}\)\)'/
syn match swiftPreproc /#\(\<file\>\|\<line\>\|\<function\>\)/
syn match swiftPreproc /^\s*#\(\<if\>\|\<else\>\|\<elseif\>\|\<endif\>\)/
syn match swiftPreproc /^\s*#\(\<if\>\|\<else\>\|\<elseif\>\|\<endif\>\<error\>\|\<warning\>\|\)/

This comment has been minimized.

@natecook1000

natecook1000 Feb 3, 2018

Member

This is what I call fit and finish 💯

@harlanhaskins harlanhaskins deleted the harlanhaskins:caution-tape branch Feb 3, 2018

@DevAndArtist

This comment has been minimized.

DevAndArtist commented Feb 6, 2018

Is this supported?

#warning("""
  This is a very long warning \
  in a codebase that has 80 \
  character line width. ...
  """)
@harlanhaskins

This comment has been minimized.

Collaborator

harlanhaskins commented Feb 6, 2018

Not in the current implementation, but it absolutely should be. I’ll make a PR with that soon. Thanks for noticing!

@harlanhaskins

This comment has been minimized.

Collaborator

harlanhaskins commented Feb 6, 2018

Actually, spoke too soon. Multi line string literals are definitely supported already -- I mistakenly thought they were a different token.

@DougGregor

This comment has been minimized.

Member

DougGregor commented Mar 9, 2018

Hey @harlanhaskins , mind adding a ChangeLog entry for this?

@harlanhaskins

This comment has been minimized.

Collaborator

harlanhaskins commented Mar 9, 2018

@DougGregor Submitted as #15110. Thanks for the reminder!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment