Skip to content

Do stricter checking of -D command-line arguments #6813

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

Closed
wants to merge 1 commit into from

Conversation

erikjv
Copy link
Contributor

@erikjv erikjv commented Jan 14, 2017

Check if the condition "name" is a proper identifier, and generate an
error when assigning specific values in -D conditions.

Fixes SR-2404.

@erikjv
Copy link
Contributor Author

erikjv commented Jan 14, 2017

@swift-ci Please smoke test

@gottesmm
Copy link
Contributor

@jrose-apple

@gottesmm
Copy link
Contributor

@swift-ci Please smoke test

@gottesmm
Copy link
Contributor

just a drive by smoke test. Jordan is the right person to review this.

@erikjv
Copy link
Contributor Author

erikjv commented Jan 15, 2017

@gottesmm : should I always mention @jrose-apple when submitting merge requests?

@gottesmm
Copy link
Contributor

Not always. Depends on the area. This is a change to the driver. Jordan wrote most of it IIRC, so he is the right person to review.

@gottesmm
Copy link
Contributor

Also, if you are unsure of who would be good to look at something, feel free to ask. I am sure someone will chime in = ).

Copy link
Contributor

@jrose-apple jrose-apple left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Erik! I do have some suggestions on how to improve this, but I'm glad to have this checked.

if (Lexer::isIdentifier(name))
Opts.addCustomConditionalCompilationFlag(name);
else if (name.find('=') != StringRef::npos)
Diags.diagnose(SourceLoc(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something we should diagnose up at the Driver level, so that it happens once up front instead of once per input file in the module. I suggest adding it to validateArgs in Driver.cpp

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, but then it won't be used with -frontend specified.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not a supported configuration anyway. Anyone using -frontend is supposed to be a compiler developer already. I suppose we could assert it, though, just to have a weak catch.

@@ -168,6 +168,12 @@ ERROR(error_formatting_multiple_file_ranges,none,
ERROR(error_formatting_invalid_range,none,
"file range is invalid", ())

ERROR(invalid_conditional_compilation_flag,none,
"invalid conditional compilation flag in '-D%0'", (StringRef))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. In C the convention is to write -DFOO, but I'm not sure Swift should be encouraging that. -D FOO is equivalent and a bit easier to read.

The error message should also explain what the problem is, perhaps something like "conditional compilation flags must be valid Swift identifiers (rather than '%0')"?

"invalid conditional compilation flag in '-D%0'", (StringRef))

ERROR(cannot_assign_value_to_conditional_compilation_flag,none,
"conditional compilation flags cannot get a specific value assigned in '-D%0'", (StringRef))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: the grammar sounds a little funny to me. Maybe just "conditional compilation flags do not have values in Swift; they are either present or absent". Though I suppose the value the user provided has to be worked in there somewhere, since otherwise they won't know what it's referring to.

@@ -6,3 +6,9 @@
// RUN: not %swiftc_driver -c %s -o %t.o -Xfrontend -fake-frontend-arg -Xfrontend fakevalue 2>&1 | %FileCheck -check-prefix=XFRONTEND %s

// XFRONTEND: <unknown>:0: error: unknown argument: '-fake-frontend-arg'

// RUN: not %swift -D@#%! -DSwift=Cool -D-D -c %s -o %t.o 2>&1 | %FileCheck -check-prefix=CMDLNCOND %s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test (or even just an argument) that shows the split -D, as mentioned above?

(Also, nitpick: there's no need to pick a compact name for the FileCheck prefix. "INVALID-COND" or something is fine.)

@jrose-apple
Copy link
Contributor

And for your other question: the CODE_OWNERS document kind of covers who's in charge of what, but some things fall in the cracks. "Argument validation" is considered a "Driver"-like task even though some of it happens in the Frontend library.

@erikjv
Copy link
Contributor Author

erikjv commented Feb 14, 2017

I have a rebased version with most comments worked in, but I'm wondering if I should move the checks to validateArgs...

Check if the condition "name" is a proper identifier, and generate an
error when assigning specific values in -D conditions.

Fixes SR-2404.
Copy link
Contributor

@jrose-apple jrose-apple left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, I completely missed that you had updated this. Looks good to me!

@jrose-apple
Copy link
Contributor

@swift-ci Please smoke test

@jrose-apple jrose-apple self-assigned this Mar 10, 2017
@jrose-apple
Copy link
Contributor

Heh, looks like there was another test accidentally using this, probably just as a way to test that arbitrary data could be passed through. Can you update that?

@jrose-apple
Copy link
Contributor

Hey, Erik. Are you still working on this one?

@CodaFi
Copy link
Contributor

CodaFi commented Jun 14, 2017

Rebased and merged in #10248

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants