Skip to content

Commit db3796d

Browse files
authored
Merge pull request #10248 from CodaFi/conditional-surrender
[Shepherd] Do stricter checking of -D command line arguments
2 parents 3af359c + e778c8c commit db3796d

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
@@ -183,6 +183,14 @@ ERROR(symbol_in_ir_not_in_tbd,none,
183183
"symbol '%0' (%1) is in generated IR file, but not in TBD file",
184184
(StringRef, StringRef))
185185

186+
ERROR(invalid_conditional_compilation_flag,none,
187+
"conditional compilation flags must be valid Swift identifiers (rather than '%0')",
188+
(StringRef))
189+
190+
ERROR(cannot_assign_value_to_conditional_compilation_flag,none,
191+
"conditional compilation flags do not have values in Swift; they are either present or absent"
192+
" (rather than '%0')", (StringRef))
193+
186194
#ifndef DIAG_NO_UNDEF
187195
# if defined(DIAG)
188196
# undef DIAG

lib/Driver/Driver.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ static void validateArgs(DiagnosticEngine &diags, const ArgList &Args) {
157157
diags.diagnose(SourceLoc(), diag::error_conflicting_options,
158158
"-warnings-as-errors", "-suppress-warnings");
159159
}
160-
160+
161161
// Check for missing debug option when verifying debug info.
162162
if (Args.hasArg(options::OPT_verify_debug_info)) {
163163
bool hasDebugOption = true;
@@ -168,6 +168,18 @@ static void validateArgs(DiagnosticEngine &diags, const ArgList &Args) {
168168
diags.diagnose(SourceLoc(),
169169
diag::verify_debug_info_requires_debug_option);
170170
}
171+
172+
for (const Arg *A : make_range(Args.filtered_begin(options::OPT_D),
173+
Args.filtered_end())) {
174+
StringRef name = A->getValue();
175+
if (name.find('=') != StringRef::npos)
176+
diags.diagnose(SourceLoc(),
177+
diag::cannot_assign_value_to_conditional_compilation_flag,
178+
name);
179+
else if (!Lexer::isIdentifier(name))
180+
diags.diagnose(SourceLoc(), diag::invalid_conditional_compilation_flag,
181+
name);
182+
}
171183
}
172184

173185
/// 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: error: 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)