Skip to content

Commit 63eed0f

Browse files
authored
Merge pull request #10739 from jrose-apple/4.0-conditional-surrender
[4.0] Do stricter checking of -D command line arguments
2 parents 93cfc6e + 9a97d50 commit 63eed0f

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ ERROR(symbol_in_ir_not_in_tbd,none,
194194
"symbol '%0' (%1) is in generated IR file, but not in TBD file",
195195
(StringRef, StringRef))
196196

197+
ERROR(invalid_conditional_compilation_flag,none,
198+
"conditional compilation flags must be valid Swift identifiers (rather than '%0')",
199+
(StringRef))
200+
201+
WARNING(cannot_assign_value_to_conditional_compilation_flag,none,
202+
"conditional compilation flags do not have values in Swift; they are "
203+
"either present or absent (rather than '%0')", (StringRef))
204+
197205
#ifndef DIAG_NO_UNDEF
198206
# if defined(DIAG)
199207
# undef DIAG

lib/Driver/Driver.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ static void validateArgs(DiagnosticEngine &diags, const ArgList &Args) {
161161
diags.diagnose(SourceLoc(), diag::error_conflicting_options,
162162
"-warnings-as-errors", "-suppress-warnings");
163163
}
164-
164+
165165
// Check for missing debug option when verifying debug info.
166166
if (Args.hasArg(options::OPT_verify_debug_info)) {
167167
bool hasDebugOption = true;
@@ -172,6 +172,18 @@ static void validateArgs(DiagnosticEngine &diags, const ArgList &Args) {
172172
diags.diagnose(SourceLoc(),
173173
diag::verify_debug_info_requires_debug_option);
174174
}
175+
176+
for (const Arg *A : make_range(Args.filtered_begin(options::OPT_D),
177+
Args.filtered_end())) {
178+
StringRef name = A->getValue();
179+
if (name.find('=') != StringRef::npos)
180+
diags.diagnose(SourceLoc(),
181+
diag::cannot_assign_value_to_conditional_compilation_flag,
182+
name);
183+
else if (!Lexer::isIdentifier(name))
184+
diags.diagnose(SourceLoc(), diag::invalid_conditional_compilation_flag,
185+
name);
186+
}
175187
}
176188

177189
/// Creates an appropriate ToolChain for a given driver and target triple.

test/Driver/options-repl.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616

1717

1818
// RUN: %swift_driver -lldb-repl -### | %FileCheck -check-prefix=LLDB %s
19-
// 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
19+
// 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
2020

2121
// LLDB: lldb{{"?}} {{"?}}--repl=
2222
// LLDB-NOT: -module-name
2323
// LLDB-NOT: -target
2424

2525
// LLDB-OPTS: lldb{{"?}} "--repl=
2626
// LLDB-OPTS-DAG: -target {{[^ ]+}}
27-
// LLDB-OPTS-DAG: -D A,B,C -D D
27+
// LLDB-OPTS-DAG: -D A -D B -D C -D D
2828
// LLDB-OPTS-DAG: -sdk /
2929
// LLDB-OPTS-DAG: -L /path/to/libraries
3030
// LLDB-OPTS-DAG: -L /path/to/more/libraries

test/Frontend/unknown-arguments.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@
66
// RUN: not %swiftc_driver -c %s -o %t.o -Xfrontend -fake-frontend-arg -Xfrontend fakevalue 2>&1 | %FileCheck -check-prefix=XFRONTEND %s
77

88
// XFRONTEND: <unknown>:0: error: unknown argument: '-fake-frontend-arg'
9+
10+
// 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
11+
// INVALID-COND: <unknown>:0: error: conditional compilation flags must be valid Swift identifiers (rather than '@#%!')
12+
// INVALID-COND-NEXT: <unknown>:0: warning: conditional compilation flags do not have values in Swift; they are either present or absent (rather than 'Swift=Cool')
13+
// INVALID-COND-NEXT: <unknown>:0: error: conditional compilation flags must be valid Swift identifiers (rather than '-D')
14+

0 commit comments

Comments
 (0)