Skip to content

[Shepherd] Do stricter checking of -D command line arguments #10248

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 2 commits into from
Jun 14, 2017
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
8 changes: 8 additions & 0 deletions include/swift/AST/DiagnosticsFrontend.def
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ ERROR(symbol_in_ir_not_in_tbd,none,
"symbol '%0' (%1) is in generated IR file, but not in TBD file",
(StringRef, StringRef))

ERROR(invalid_conditional_compilation_flag,none,
"conditional compilation flags must be valid Swift identifiers (rather than '%0')",
(StringRef))

ERROR(cannot_assign_value_to_conditional_compilation_flag,none,
"conditional compilation flags do not have values in Swift; they are either present or absent"
" (rather than '%0')", (StringRef))

#ifndef DIAG_NO_UNDEF
# if defined(DIAG)
# undef DIAG
Expand Down
14 changes: 13 additions & 1 deletion lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ static void validateArgs(DiagnosticEngine &diags, const ArgList &Args) {
diags.diagnose(SourceLoc(), diag::error_conflicting_options,
"-warnings-as-errors", "-suppress-warnings");
}

// Check for missing debug option when verifying debug info.
if (Args.hasArg(options::OPT_verify_debug_info)) {
bool hasDebugOption = true;
Expand All @@ -168,6 +168,18 @@ static void validateArgs(DiagnosticEngine &diags, const ArgList &Args) {
diags.diagnose(SourceLoc(),
diag::verify_debug_info_requires_debug_option);
}

for (const Arg *A : make_range(Args.filtered_begin(options::OPT_D),
Args.filtered_end())) {
StringRef name = A->getValue();
if (name.find('=') != StringRef::npos)
diags.diagnose(SourceLoc(),
diag::cannot_assign_value_to_conditional_compilation_flag,
name);
else if (!Lexer::isIdentifier(name))
diags.diagnose(SourceLoc(), diag::invalid_conditional_compilation_flag,
name);
}
}

/// Creates an appropriate ToolChain for a given driver and target triple.
Expand Down
4 changes: 2 additions & 2 deletions test/Driver/options-repl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@


// RUN: %swift_driver -lldb-repl -### | %FileCheck -check-prefix=LLDB %s
// RUN: %swift_driver -lldb-repl -DA,B,C -DD -L /path/to/libraries -L /path/to/more/libraries -F /path/to/frameworks -lsomelib -framework SomeFramework -sdk / -I "this folder" -module-name Test -target %target-triple -### | %FileCheck -check-prefix=LLDB-OPTS %s
// RUN: %swift_driver -lldb-repl -D A -DB -D C -DD -L /path/to/libraries -L /path/to/more/libraries -F /path/to/frameworks -lsomelib -framework SomeFramework -sdk / -I "this folder" -module-name Test -target %target-triple -### | %FileCheck -check-prefix=LLDB-OPTS %s

// LLDB: lldb{{"?}} {{"?}}--repl=
// LLDB-NOT: -module-name
// LLDB-NOT: -target

// LLDB-OPTS: lldb{{"?}} "--repl=
// LLDB-OPTS-DAG: -target {{[^ ]+}}
// LLDB-OPTS-DAG: -D A,B,C -D D
// LLDB-OPTS-DAG: -D A -D B -D C -D D
// LLDB-OPTS-DAG: -sdk /
// LLDB-OPTS-DAG: -L /path/to/libraries
// LLDB-OPTS-DAG: -L /path/to/more/libraries
Expand Down
6 changes: 6 additions & 0 deletions test/Frontend/unknown-arguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 %swiftc_driver -D Correct -DAlsoCorrect -D@#%! -D Swift=Cool -D-D -c %s -o %t.o 2>&1 | %FileCheck -check-prefix=INVALID-COND %s
// INVALID-COND: <unknown>:0: error: conditional compilation flags must be valid Swift identifiers (rather than '@#%!')
// INVALID-COND-NEXT: <unknown>:0: error: conditional compilation flags do not have values in Swift; they are either present or absent (rather than 'Swift=Cool')
// INVALID-COND-NEXT: <unknown>:0: error: conditional compilation flags must be valid Swift identifiers (rather than '-D')