Skip to content

Commit fc9e493

Browse files
committed
Suppress warnings for non-primary inputs
Don't emit warnings if specified at the command line or when working on a non-primary input file. https://bugs.swift.org/browse/SR-1012 rdar://problem/25282622
1 parent 15467ac commit fc9e493

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

lib/Frontend/Frontend.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ void CompilerInstance::performSema() {
430430
if (BufferID == PrimaryBufferID)
431431
setPrimarySourceFile(NextInput);
432432

433+
auto &Diags = NextInput->getASTContext().Diags;
434+
auto DidSuppressWarnings = Diags.getSuppressWarnings();
435+
Diags.setSuppressWarnings(DidSuppressWarnings || BufferID != PrimaryBufferID);
436+
433437
bool Done;
434438
do {
435439
// Parser may stop at some erroneous constructions like #else, #endif
@@ -438,6 +442,8 @@ void CompilerInstance::performSema() {
438442
&PersistentState, DelayedCB.get());
439443
} while (!Done);
440444

445+
Diags.setSuppressWarnings(DidSuppressWarnings);
446+
441447
performNameBinding(*NextInput);
442448
}
443449

@@ -471,6 +477,11 @@ void CompilerInstance::performSema() {
471477

472478
SourceFile &MainFile =
473479
MainModule->getMainSourceFile(Invocation.getSourceFileKind());
480+
481+
auto &Diags = MainFile.getASTContext().Diags;
482+
auto DidSuppressWarnings = Diags.getSuppressWarnings();
483+
Diags.setSuppressWarnings(DidSuppressWarnings || !mainIsPrimary);
484+
474485
SILParserState SILContext(TheSILModule.get());
475486
unsigned CurTUElem = 0;
476487
bool Done;
@@ -488,6 +499,8 @@ void CompilerInstance::performSema() {
488499
}
489500
CurTUElem = MainFile.Decls.size();
490501
} while (!Done);
502+
503+
Diags.setSuppressWarnings(DidSuppressWarnings);
491504

492505
if (mainIsPrimary && !Context->hadError() &&
493506
Invocation.getFrontendOptions().PlaygroundTransform)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#if os(asdf)
2+
public func bar(x: Int, _ y: Int) -> Int {
3+
return x + y
4+
}
5+
#endif
6+

test/Parse/warning_primary_file.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %target-swift-frontend -parse -verify -primary-file %s %S/Inputs/warning_nonprimary_file.swift
2+
3+
// Tests that parse warnings are only emitted for the primary
4+
// input. Below, we expect a warning diagnostic to be emitted
5+
// because this is the primary file.
6+
//
7+
// However, we also pull in ./warning_nonprimary_file.swift,
8+
// which would emit a warning for an unknown operating system
9+
// configuration argument, but doesn't because it's not the
10+
// primary input.
11+
12+
public func foo(x: Int, y: Int) -> Int {
13+
return x + y
14+
}
15+
16+
public func baz(x: Int, y: Int) -> Int {
17+
var z = x // expected-warning {{variable 'z' was never mutated}}
18+
return z + y
19+
}
20+

0 commit comments

Comments
 (0)