Implement #warning and #error#14048
Conversation
d6fd1d2 to
3ca6ec6
Compare
|
@swift-ci please smoke test |
|
This currently doesn't handle directives in the middle of switch statements, a la: switch 4 {
#warning "boo"
default: break
} |
|
Finally... @harlanhaskins |
3ca6ec6 to
76c1eed
Compare
80b24e5 to
ed5dbd3
Compare
|
@swift-ci please smoke test |
|
@swift-ci please smoke test |
lib/AST/ASTPrinter.cpp
Outdated
| Printer << tok::pound_warning; | ||
| } | ||
|
|
||
| Printer << '"' << PDD->getMessage()->getValue() << '"'; |
There was a problem hiding this comment.
Before I forget, I need to add parens to this.
81ea7c5 to
ffcefa7
Compare
|
@swift-ci please smoke test |
lib/AST/ASTWalker.cpp
Outdated
| } | ||
|
|
||
| bool visitPoundDiagnosticDecl(PoundDiagnosticDecl *PDD) { | ||
| // By default, ignore #erorr/#warning. |
|
|
||
| auto string = parseExprStringLiteral(); | ||
| if (string.isNull()) | ||
| return makeParserError(); |
There was a problem hiding this comment.
Suggestion: continue looking for a close-paren if it occurs before the end of the line.
There was a problem hiding this comment.
👍
I originally tried checking for #warning “Foo” and FixIt-ing in the surrounding parens. Should I continue with that?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Also a good idea!
lib/AST/ASTDumper.cpp
Outdated
|
|
||
| void visitPoundDiagnosticDecl(PoundDiagnosticDecl *PDD) { | ||
| const char *name = PDD->isError() ? | ||
| "pound_error_decl" : "pound_warning_decl"; |
There was a problem hiding this comment.
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.
|
|
||
| void visitPoundDiagnosticDecl(PoundDiagnosticDecl *PDD) { | ||
| if (PDD->hasBeenEmitted()) { return; } | ||
| PDD->markEmitted(); |
There was a problem hiding this comment.
I suspect this can never happen more than once per phase, so you may be able to get rid of hasBeenEmitted.
There was a problem hiding this comment.
I’ll try it and see what happens!
There was a problem hiding this comment.
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}} |
There was a problem hiding this comment.
There's a slightly different test where the #error is immediately after a case.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Okay, I’ll nest a case inside the existing if config and then put a #error in there 👍
7ac669f to
f921dcd
Compare
|
@jrose-apple Okay, this latest commit will fix-it: #warning "foo"
#warning test 123
#warning(test 123) |
|
@swift-ci please smoke test |
77f2946 to
c0de994
Compare
|
@swift-ci please smoke test |
| // Catch #warning(oops, forgot the quotes) | ||
| SourceLoc wordsStartLoc = Tok.getLoc(); | ||
|
|
||
| while (!Tok.isAtStartOfLine() && Tok.isNot(tok::r_paren)) { |
There was a problem hiding this comment.
Don't forget to consume the r_paren!
80d0cb7 to
d4fe20f
Compare
|
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. |
a306bee to
27c1ffe
Compare
|
@swift-ci please smoke test |
|
Build timed out? @swift-ci please smoke test Linux platform |
|
🚢 |
|
|
||
| syn match swiftPreproc /#\(\<file\>\|\<line\>\|\<function\>\)/ | ||
| syn match swiftPreproc /^\s*#\(\<if\>\|\<else\>\|\<elseif\>\|\<endif\>\)/ | ||
| syn match swiftPreproc /^\s*#\(\<if\>\|\<else\>\|\<elseif\>\|\<endif\>\<error\>\|\<warning\>\|\)/ |
There was a problem hiding this comment.
This is what I call fit and finish 💯
|
Is this supported? #warning("""
This is a very long warning \
in a codebase that has 80 \
character line width. ...
""")
|
|
Not in the current implementation, but it absolutely should be. I’ll make a PR with that soon. Thanks for noticing! |
|
Actually, spoke too soon. Multi line string literals are definitely supported already -- I mistakenly thought they were a different token. |
|
Hey @harlanhaskins , mind adding a ChangeLog entry for this? |
|
@DougGregor Submitted as #15110. Thanks for the reminder! |
* Implement #warning and #error * Fix #warning/#error in switch statements * Fix AST printing for #warning/#error * Add to test case * Add extra handling to ParseDeclPoundDiagnostic * fix dumping * Consume the right paren even in the failure case * Diagnose extra tokens on the same line after a diagnostic directive
This PR is an implementation of
#warningand#error, whichis currently being pitched to swift-evolutionwas accepted for Swift 5.