Skip to content

Commit 920bcc2

Browse files
authored
Merge pull request #39900 from al45tair/problem/84571859
[Runtime] Add format string attributes.
2 parents bf59b68 + e339feb commit 920bcc2

File tree

6 files changed

+23
-2
lines changed

6 files changed

+23
-2
lines changed

include/swift/Demangling/TypeLookupError.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ static TypeLookupError TypeLookupErrorImpl(const char *fmt, Args... args) {
209209
char *str;
210210
#pragma clang diagnostic push
211211
#pragma clang diagnostic ignored "-Wformat-security"
212+
#pragma clang diagnostic ignored "-Wformat-nonliteral"
212213
swift_asprintf(&str, fmt, args...);
213214
#pragma clang diagnostic pop
214215
return str;

include/swift/Runtime/Config.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,22 @@ extern uintptr_t __COMPATIBILITY_LIBRARIES_CANNOT_CHECK_THE_IS_SWIFT_BIT_DIRECTL
226226
// so changing this value is not sufficient.
227227
#define SWIFT_DEFAULT_LLVM_CC llvm::CallingConv::C
228228

229+
// SWIFT_FORMAT(fmt,first) marks a function as taking a format string argument
230+
// at argument `fmt`, with the first argument for the format string as `first`.
231+
#if __has_attribute(format)
232+
#define SWIFT_FORMAT(fmt,first) __attribute__((format(printf, fmt, first)))
233+
#else
234+
#define SWIFT_FORMAT(fmt,first)
235+
#endif
236+
237+
// SWIFT_VFORMAT(fmt) marks a function as taking a format string argument at
238+
// argument `fmt`, with the arguments in a `va_list`.
239+
#if __has_attribute(format)
240+
#define SWIFT_VFORMAT(fmt) __attribute__((format(printf, fmt, 0)))
241+
#else
242+
#define SWIFT_VFORMAT(fmt)
243+
#endif
244+
229245
// Pointer authentication.
230246
#if __has_feature(ptrauth_calls)
231247
#define SWIFT_PTRAUTH 1

include/swift/Runtime/Debug.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,18 @@ static inline void crash(const char *message) {
8585
// swift::fatalError() halts with a crash log message,
8686
// but makes no attempt to preserve register state.
8787
SWIFT_RUNTIME_ATTRIBUTE_NORETURN
88+
SWIFT_FORMAT(2, 3)
8889
extern void
8990
fatalError(uint32_t flags, const char *format, ...);
9091

9192
/// swift::warning() emits a warning from the runtime.
9293
extern void
94+
SWIFT_VFORMAT(2)
9395
warningv(uint32_t flags, const char *format, va_list args);
9496

9597
/// swift::warning() emits a warning from the runtime.
9698
extern void
99+
SWIFT_FORMAT(2, 3)
97100
warning(uint32_t flags, const char *format, ...);
98101

99102
// swift_dynamicCastFailure halts using fatalError()

stdlib/public/runtime/DynamicCast.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ static HeapObject * getNonNullSrcObject(OpaqueValue *srcValue,
126126

127127
std::string srcTypeName = nameForMetadata(srcType);
128128
std::string destTypeName = nameForMetadata(destType);
129-
const char *msg = "Found unexpected null pointer value"
129+
const char * const msg = "Found unexpected null pointer value"
130130
" while trying to cast value of type '%s' (%p)"
131131
" to '%s' (%p)%s\n";
132132
if (runtime::bincompat::unexpectedObjCNullWhileCastingIsFatal()) {

stdlib/public/runtime/Metadata.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5751,7 +5751,7 @@ diagnoseMetadataDependencyCycle(const Metadata *start,
57515751
&details);
57525752
}
57535753

5754-
fatalError(0, diagnostic.c_str());
5754+
fatalError(0, "%s", diagnostic.c_str());
57555755
}
57565756

57575757
/// Check whether the given metadata dependency is satisfied, and if not,

stdlib/public/runtime/ReflectionMirror.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ static bool _shouldReportMissingReflectionMetadataWarnings() {
339339
/// at runtime. This is usually mostly harmless, but it's good to alert
340340
/// users that it happens.
341341
static void
342+
SWIFT_FORMAT(1, 2)
342343
missing_reflection_metadata_warning(const char *fmt, ...) {
343344
bool shouldWarn =
344345
SWIFT_LAZY_CONSTANT(_shouldReportMissingReflectionMetadataWarnings());

0 commit comments

Comments
 (0)