Index: include/clang/Basic/DiagnosticDriverKinds.td =================================================================== --- include/clang/Basic/DiagnosticDriverKinds.td +++ include/clang/Basic/DiagnosticDriverKinds.td @@ -277,4 +277,6 @@ InGroup; def err_drv_unsupported_linker : Error<"unsupported value '%0' for -linker option">; +def err_drv_defsym_invalid_format : Error<"defsym must be of the form: sym=value: %0">; +def err_drv_defsym_invalid_symval : Error<"Value is not an integer: %0">; } Index: lib/Driver/Tools.cpp =================================================================== --- lib/Driver/Tools.cpp +++ lib/Driver/Tools.cpp @@ -3116,6 +3116,24 @@ Value.startswith("-mhwdiv") || Value.startswith("-march")) { // Do nothing, we'll validate it later. } else if (Value == "-defsym") { + if (A->getNumValues() != 2) { + D.Diag(diag::err_drv_defsym_invalid_format) << Value; + break; + } + const char *S = A->getValue(1); + auto Pair = StringRef(S).split('='); + auto Sym = Pair.first; + auto SVal = Pair.second; + + if (Sym.empty() || SVal.empty()) { + D.Diag(diag::err_drv_defsym_invalid_format) << S; + break; + } + int64_t IVal; + if (SVal.getAsInteger(0, IVal)) { + D.Diag(diag::err_drv_defsym_invalid_symval) << SVal; + break; + } CmdArgs.push_back(Value.data()); TakeNextArg = true; } else { Index: test/Driver/defsym.s =================================================================== --- test/Driver/defsym.s +++ test/Driver/defsym.s @@ -17,6 +17,21 @@ // CHECK-DEFSYM-ERR1: error: defsym must be of the form: sym=value: abc= // RUN: not %clang -c -integrated-as -o /dev/null %s \ -// RUN: -Wa,-defsym,abc=1a2b3c \ +// RUN: -Wa,-defsym,=123 \ // RUN: 2>&1 | FileCheck %s --check-prefix=CHECK-DEFSYM-ERR2 -// CHECK-DEFSYM-ERR2: error: Value is not an integer: 1a2b3c +// CHECK-DEFSYM-ERR2: error: defsym must be of the form: sym=value: =123 + +// RUN: not %clang -c -integrated-as -o /dev/null %s \ +// RUN: -Wa,-defsym,abc=1a2b3c \ +// RUN: 2>&1 | FileCheck %s --check-prefix=CHECK-DEFSYM-ERR3 +// CHECK-DEFSYM-ERR3: error: Value is not an integer: 1a2b3c + +// RUN: not %clang -c -integrated-as -o /dev/null %s \ +// RUN: -Wa,-defsym \ +// RUN: 2>&1 | FileCheck %s --check-prefix=CHECK-DEFSYM-ERR4 + +// RUN: not %clang -c -integrated-as -o /dev/null %s \ +// RUN: -Wa,-defsym, \ +// RUN: 2>&1 | FileCheck %s --check-prefix=CHECK-DEFSYM-ERR4 + +// CHECK-DEFSYM-ERR4: error: defsym must be of the form: sym=value: -defsym Index: tools/driver/cc1as_main.cpp =================================================================== --- tools/driver/cc1as_main.cpp +++ tools/driver/cc1as_main.cpp @@ -426,10 +426,13 @@ // Set values for symbols, if any. for (auto &S : Opts.SymbolDefs) { - if (Ctx.setSymbolValue(Parser->getStreamer(), S)) { - Failed = true; - break; - } + auto Pair = StringRef(S).split('='); + auto Sym = Pair.first; + auto Val = Pair.second; + int64_t Value; + // We have already error checked this in the driver. + Val.getAsInteger(0, Value); + Ctx.setSymbolValue(Parser->getStreamer(), Sym, Value); } if (!Failed) {