Skip to content

Commit caa6b5d

Browse files
committed
Do stricter checking of -D command-line arguments
Check if the condition "name" is a proper identifier, and generate an error when assigning specific values in -D conditions. Fixes SR-2404.
1 parent c080be7 commit caa6b5d

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,14 @@ ERROR(error_formatting_invalid_range,none,
171171
WARNING(stats_disabled,none,
172172
"compiler was not built with support for collecting statistics", ())
173173

174+
ERROR(invalid_conditional_compilation_flag,none,
175+
"conditional compilation flags must be valid Swift identifiers (rather than '%0')",
176+
(StringRef))
177+
178+
ERROR(cannot_assign_value_to_conditional_compilation_flag,none,
179+
"conditional compilation flags do not have values in Swift; they are either present or absent"
180+
" (rather than '%0')", (StringRef))
181+
174182
#ifndef DIAG_NO_UNDEF
175183
# if defined(DIAG)
176184
# undef DIAG

lib/Driver/Driver.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,18 @@ static void validateArgs(DiagnosticEngine &diags, const ArgList &Args) {
155155
diags.diagnose(SourceLoc(), diag::error_conflicting_options,
156156
"-warnings-as-errors", "-suppress-warnings");
157157
}
158+
159+
for (const Arg *A : make_range(Args.filtered_begin(options::OPT_D),
160+
Args.filtered_end())) {
161+
StringRef name = A->getValue();
162+
if (name.find('=') != StringRef::npos)
163+
diags.diagnose(SourceLoc(),
164+
diag::cannot_assign_value_to_conditional_compilation_flag,
165+
name);
166+
else if (!Lexer::isIdentifier(name))
167+
diags.diagnose(SourceLoc(), diag::invalid_conditional_compilation_flag,
168+
name);
169+
}
158170
}
159171

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

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)