Skip to content
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
5 changes: 5 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -10448,6 +10448,9 @@ def warn_format_conversion_argument_type_mismatch : Warning<
"format specifies type %0 but the argument has "
"%select{type|underlying type}2 %1">,
InGroup<Format>;
def err_format_conversion_argument_type_mismatch : Error<
"format specifies type %0 but the argument has "
"%select{type|underlying type}2 %1">;
def warn_format_conversion_argument_type_mismatch_pedantic : Extension<
warn_format_conversion_argument_type_mismatch.Summary>,
InGroup<FormatPedantic>;
Expand Down Expand Up @@ -10497,6 +10500,8 @@ def warn_printf_asterisk_missing_arg : Warning<
def warn_printf_asterisk_wrong_type : Warning<
"field %select{width|precision}0 should have type %1, but argument has type %2">,
InGroup<Format>;
def err_printf_asterisk_wrong_type : Error<
"field %select{width|precision}0 should have type %1, but argument has type %2">;
def warn_printf_nonsensical_optional_amount: Warning<
"%select{field width|precision}0 used with '%1' conversion specifier, resulting in undefined behavior">,
InGroup<Format>;
Expand Down
23 changes: 18 additions & 5 deletions clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7653,6 +7653,14 @@ void CheckPrintfHandler::handleInvalidMaskType(StringRef MaskType) {
S.Diag(getLocationOfByte(MaskType.data()), diag::err_invalid_mask_type_size);
}

// Error out if struct or complex type argments are passed to os_log.
static bool isInvalidOSLogArgTypeForCodeGen(FormatStringType FSType,
QualType T) {
if (FSType != FormatStringType::OSLog)
return false;
return T->isRecordType() || T->isComplexType();
}

bool CheckPrintfHandler::HandleAmount(
const analyze_format_string::OptionalAmount &Amt, unsigned k,
const char *startSpecifier, unsigned specifierLen) {
Expand Down Expand Up @@ -7685,11 +7693,14 @@ bool CheckPrintfHandler::HandleAmount(
assert(AT.isValid());

if (!AT.matchesType(S.Context, T)) {
EmitFormatDiagnostic(S.PDiag(diag::warn_printf_asterisk_wrong_type)
<< k << AT.getRepresentativeTypeName(S.Context)
<< T << Arg->getSourceRange(),
unsigned DiagID = isInvalidOSLogArgTypeForCodeGen(FSType, T)
? diag::err_printf_asterisk_wrong_type
: diag::warn_printf_asterisk_wrong_type;
EmitFormatDiagnostic(S.PDiag(DiagID)
<< k << AT.getRepresentativeTypeName(S.Context)
<< T << Arg->getSourceRange(),
getLocationOfByte(Amt.getStart()),
/*IsStringLocation*/true,
/*IsStringLocation*/ true,
getSpecifierRange(startSpecifier, specifierLen));
// Don't do any more checking. We will just emit
// spurious errors.
Expand Down Expand Up @@ -8744,7 +8755,9 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
Diag = diag::warn_format_conversion_argument_type_mismatch_confusion;
break;
case ArgType::NoMatch:
Diag = diag::warn_format_conversion_argument_type_mismatch;
Diag = isInvalidOSLogArgTypeForCodeGen(FSType, ExprTy)
? diag::err_format_conversion_argument_type_mismatch
: diag::warn_format_conversion_argument_type_mismatch;
break;
}

Expand Down
22 changes: 22 additions & 0 deletions clang/test/SemaObjC/os_log.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %clang_cc1 -verify %s

struct S {
int a[4];
};

struct S s;
_Complex float cf;

void test_builtin_os_log_invalid_arg(void *buf) {
__builtin_os_log_format(buf, "%*.*f", s, 5, 1.3); // expected-error {{field width should have type 'int', but argument has type 'struct S'}}
__builtin_os_log_format(buf, "%*.*f", 1, s, 1.3); // expected-error {{field precision should have type 'int', but argument has type 'struct S'}}
__builtin_os_log_format(buf, "%*.*f", 1, 5, s); // expected-error {{format specifies type 'double' but the argument has type 'struct S'}}

__builtin_os_log_format(buf, "%*.*f", cf, 5, 1.3); // expected-error {{field width should have type 'int', but argument has type '_Complex float'}}
__builtin_os_log_format(buf, "%*.*f", 1, cf, 1.3); // expected-error {{field precision should have type 'int', but argument has type '_Complex float'}}
__builtin_os_log_format(buf, "%*.*f", 1, 5, cf); // expected-error {{format specifies type 'double' but the argument has type '_Complex float'}}

__builtin_os_log_format(buf, "%*.*f", (void *)0, 5, 1.3); // expected-warning {{field width should have type 'int', but argument has type 'void *'}}
__builtin_os_log_format(buf, "%*.*f", 1, (void *)0, 1.3); // expected-warning {{field precision should have type 'int', but argument has type 'void *'}}
__builtin_os_log_format(buf, "%*.*f", 1, 5, (void *)0); // expected-warning {{format specifies type 'double' but the argument has type 'void *'}}
}