Skip to content

Commit 364d2dc

Browse files
Merge pull request #31129 from ravikandhadai/oslog-diagnostic-improvement
[OSLog Diagnostics] Improve os log diagnostics emitted in the SIL and Sema diagnostic passes.
2 parents 70918dc + 2e0457a commit 364d2dc

File tree

6 files changed

+37
-17
lines changed

6 files changed

+37
-17
lines changed

include/swift/AST/DiagnosticsSIL.def

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,8 @@ ERROR(oslog_constant_eval_trap, none, "%0", (StringRef))
585585
ERROR(oslog_too_many_instructions, none, "interpolated expression and arguments "
586586
"are too complex", ())
587587

588-
ERROR(oslog_invalid_log_message, none, "invalid log message; do not define "
589-
"extensions to types defined in the os module", ())
588+
ERROR(oslog_invalid_log_message, none, "invalid log message; extending "
589+
"types defined in the os module is not supported", ())
590590

591591
NOTE(oslog_const_evaluable_fun_error, none, "'%0' failed evaluation", (StringRef))
592592

@@ -599,8 +599,9 @@ ERROR(oslog_non_constant_interpolation, none, "'OSLogInterpolation' instance "
599599
ERROR(oslog_property_not_constant, none, "'OSLogInterpolation.%0' is not a "
600600
"constant", (StringRef))
601601

602-
ERROR(oslog_message_alive_after_opts, none, "os log string interpolation cannot "
603-
"be used in this context", ())
602+
ERROR(oslog_message_alive_after_opts, none, "string interpolation cannot "
603+
"be used in this context; if you are calling an os_log function, "
604+
"try a different overload", ())
604605

605606
ERROR(oslog_message_explicitly_created, none, "'OSLogMessage' must be "
606607
" created from a string interpolation or string literal", ())
@@ -609,7 +610,7 @@ WARNING(oslog_call_in_unreachable_code, none, "os log call will never be "
609610
"executed and may have undiagnosed errors", ())
610611

611612
ERROR(global_string_pointer_on_non_constant, none, "globalStringTablePointer "
612-
"builtin must used only on string literals", ())
613+
"builtin must be used only on string literals", ())
613614

614615
ERROR(polymorphic_builtin_passed_non_trivial_non_builtin_type, none, "Argument "
615616
"of type %0 can not be passed as an argument to a Polymorphic "

lib/SILOptimizer/Mandatory/OSLogOptimization.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,9 @@ static void deleteInstructionWithUsersAndFixLifetimes(
12101210
/// Try to dead-code eliminate the OSLogMessage instance \c oslogMessage passed
12111211
/// to the os log call and clean up its dependencies. If the instance cannot be
12121212
/// eliminated, emit diagnostics.
1213-
static void tryEliminateOSLogMessage(SingleValueInstruction *oslogMessage) {
1213+
/// \returns true if elimination is successful and false if it is not successful
1214+
/// and diagnostics is emitted.
1215+
static bool tryEliminateOSLogMessage(SingleValueInstruction *oslogMessage) {
12141216
InstructionDeleter deleter;
12151217
// List of instructions that are possibly dead.
12161218
SmallVector<SILInstruction *, 4> worklist = {oslogMessage};
@@ -1249,13 +1251,15 @@ static void tryEliminateOSLogMessage(SingleValueInstruction *oslogMessage) {
12491251
SILFunction *fun = oslogMessage->getFunction();
12501252
diagnose(fun->getASTContext(), oslogMessage->getLoc().getSourceLoc(),
12511253
diag::oslog_message_alive_after_opts);
1254+
return false;
12521255
}
1256+
return true;
12531257
}
12541258

12551259
/// Constant evaluate instructions starting from \p start and fold the uses
12561260
/// of the SIL value \p oslogMessage.
1257-
/// \returns true if the body of the function containing \p oslogMessage is
1258-
/// modified. Returns false otherwise.
1261+
/// \returns true if folding is successful and false if it is not successful and
1262+
/// diagnostics is emitted.
12591263
static bool constantFold(SILInstruction *start,
12601264
SingleValueInstruction *oslogMessage,
12611265
unsigned assertConfig) {
@@ -1282,9 +1286,7 @@ static bool constantFold(SILInstruction *start,
12821286
return false;
12831287

12841288
substituteConstants(state);
1285-
1286-
tryEliminateOSLogMessage(oslogMessage);
1287-
return true;
1289+
return tryEliminateOSLogMessage(oslogMessage);
12881290
}
12891291

12901292
/// Given a call to the initializer of OSLogMessage, which conforms to
@@ -1548,14 +1550,14 @@ class OSLogOptimization : public SILFunctionTransform {
15481550
// The log call is in unreachable code here.
15491551
continue;
15501552
}
1551-
bool bodyModified =
1553+
bool foldingSucceeded =
15521554
constantFold(interpolationStart, oslogInit, assertConfig);
1553-
// If body was not modified, it implies that an error was diagnosed.
1555+
// If folding did not succeeded, it implies that an error was diagnosed.
15541556
// However, this will also trigger a diagnostics later on since
15551557
// _globalStringTablePointerBuiltin would not be passed a string literal.
15561558
// Suppress this error by synthesizing a dummy string literal for the
15571559
// builtin.
1558-
if (!bodyModified)
1560+
if (!foldingSucceeded)
15591561
suppressGlobalStringTablePointerError(oslogInit);
15601562
madeChange = true;
15611563
}

lib/Sema/ConstantnessSemaDiagnostics.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ static Expr *checkConstantness(Expr *expr) {
162162
if (!isa<ApplyExpr>(expr))
163163
return expr;
164164

165+
if (NominalTypeDecl *nominal =
166+
expr->getType()->getNominalOrBoundGenericNominal()) {
167+
if (nominal->getName() == nominal->getASTContext().Id_OSLogMessage)
168+
return expr;
169+
}
170+
165171
ApplyExpr *apply = cast<ApplyExpr>(expr);
166172
ValueDecl *calledValue = apply->getCalledValue();
167173
if (!calledValue)

test/SILOptimizer/OSLogCompilerDiagnosticsTest.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ extension OSLogInterpolation {
2727

2828
func testOSLogInterpolationExtension(a: A) {
2929
_osLogTestHelper("Error at line: \(a: a)")
30-
// expected-error @-1 {{invalid log message; do not define extensions to types defined in the os module}}
30+
// expected-error @-1 {{invalid log message; extending types defined in the os module is not supported}}
3131
// expected-note @-2 {{'OSLogInterpolation.appendLiteral(_:)' failed evaluation}}
3232
// expected-note @-3 {{value mutable by an unevaluated instruction is not a constant}}
3333
}
@@ -87,6 +87,6 @@ func testUnreachableLogCallComplex(c: Color) {
8787
default: // expected-warning {{default will never be executed}}
8888
_osLogTestHelper("Some call \(c)")
8989
// expected-warning@-1 {{os log call will never be executed and may have undiagnosed errors}}
90-
// expected-error@-2 {{globalStringTablePointer builtin must used only on string literals}}
90+
// expected-error@-2 {{globalStringTablePointer builtin must be used only on string literals}}
9191
}
9292
}

test/SILOptimizer/diagnostic_constant_propagation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ func testBuiltinGlobalStringTablePointerNoError() -> UnsafePointer<CChar> {
361361
}
362362

363363
func testBuiltinGlobalStringTablePointerError(s: String) -> UnsafePointer<CChar> {
364-
return _getGlobalStringTablePointer(s) // expected-error {{globalStringTablePointer builtin must used only on string literals}}
364+
return _getGlobalStringTablePointer(s) // expected-error {{globalStringTablePointer builtin must be used only on string literals}}
365365
}
366366

367367
@_transparent

test/Sema/diag_constantness_check_os_log.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ func testOSLogInterpolationExtension(a: MyStruct) {
119119
_osLogTestHelper("Error at line: \(a: a)")
120120
}
121121

122+
func testExplicitOSLogMessageConstruction() {
123+
_osLogTestHelper(OSLogMessage(stringLiteral: "world"))
124+
// expected-error@-1 {{argument must be a string interpolation}}
125+
_osLogTestHelper(
126+
OSLogMessage( // expected-error {{argument must be a string interpolation}}
127+
stringInterpolation:
128+
OSLogInterpolation(
129+
literalCapacity: 0,
130+
interpolationCount: 0)))
131+
}
132+
122133
// Test that @_semantics("oslog.log_with_level") permits values of type OSLog and
123134
// OSLogType to not be constants.
124135

0 commit comments

Comments
 (0)