Skip to content

Commit 7c10c61

Browse files
committed
[Type checker] Stop (ab)using TypeChecker::Diags to suppress diagnostics.
Take away the type checker constructor that allows one to provide a diagnostic engine different from the one associated with the ASTContext. It doesn’t actually work to suppress diagnostics. Switch all clients over to the constructor that takes only an ASTContext. Introduce the DiagnosticSuppression RAII class so clients that want to suppress diagnostics can suppress *all* diagnostics. Use it where we were previously suppressing diagnostics.
1 parent 33109d8 commit 7c10c61

File tree

6 files changed

+87
-41
lines changed

6 files changed

+87
-41
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===--- InstrumenterSupport.cpp - Instrumenter Support -------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file implements the supporting functions for writing instrumenters of
14+
// the Swift AST.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
#ifndef SWIFT_AST_DIAGNOSTIC_SUPPRESSION_H
19+
#define SWIFT_AST_DIAGNOSTIC_SUPPRESSION_H
20+
21+
#include <vector>
22+
23+
namespace swift {
24+
25+
class DiagnosticConsumer;
26+
class DiagnosticEngine;
27+
28+
/// RAII class that suppresses diagnostics by temporarily disabling all of
29+
/// the diagnostic consumers.
30+
class DiagnosticSuppression {
31+
DiagnosticEngine &diags;
32+
std::vector<DiagnosticConsumer *> consumers;
33+
34+
DiagnosticSuppression(const DiagnosticSuppression &) = delete;
35+
DiagnosticSuppression &operator=(const DiagnosticSuppression &) = delete;
36+
37+
public:
38+
explicit DiagnosticSuppression(DiagnosticEngine &diags);
39+
~DiagnosticSuppression();
40+
};
41+
42+
}
43+
#endif /* SWIFT_AST_DIAGNOSTIC_SUPPRESSION_H */

lib/AST/DiagnosticEngine.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/AST/ASTContext.h"
2020
#include "swift/AST/ASTPrinter.h"
2121
#include "swift/AST/Decl.h"
22+
#include "swift/AST/DiagnosticSuppression.h"
2223
#include "swift/AST/Module.h"
2324
#include "swift/AST/Pattern.h"
2425
#include "swift/AST/PrintOptions.h"
@@ -831,3 +832,14 @@ void DiagnosticEngine::emitDiagnostic(const Diagnostic &diagnostic) {
831832
const char *DiagnosticEngine::diagnosticStringFor(const DiagID id) {
832833
return diagnosticStrings[(unsigned)id];
833834
}
835+
836+
DiagnosticSuppression::DiagnosticSuppression(DiagnosticEngine &diags)
837+
: diags(diags)
838+
{
839+
consumers = diags.takeConsumers();
840+
}
841+
842+
DiagnosticSuppression::~DiagnosticSuppression() {
843+
for (auto consumer : consumers)
844+
diags.addConsumer(*consumer);
845+
}

lib/Sema/InstrumenterSupport.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
//===----------------------------------------------------------------------===//
1717

1818
#include "InstrumenterSupport.h"
19+
#include "swift/AST/DiagnosticSuppression.h"
1920

2021
using namespace swift;
2122
using namespace swift::instrumenter_support;
@@ -77,10 +78,10 @@ void InstrumenterBase::anchor() {}
7778

7879
bool InstrumenterBase::doTypeCheckImpl(ASTContext &Ctx, DeclContext *DC,
7980
Expr * &parsedExpr) {
80-
DiagnosticEngine diags(Ctx.SourceMgr);
81-
ErrorGatherer errorGatherer(diags);
81+
DiagnosticSuppression suppression(Ctx.Diags);
82+
ErrorGatherer errorGatherer(Ctx.Diags);
8283

83-
TypeChecker TC(Ctx, diags);
84+
TypeChecker TC(Ctx);
8485

8586
TC.typeCheckExpression(parsedExpr, DC);
8687

lib/Sema/TypeChecker.cpp

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "swift/AST/ASTWalker.h"
2525
#include "swift/AST/ASTVisitor.h"
2626
#include "swift/AST/Attr.h"
27+
#include "swift/AST/DiagnosticSuppression.h"
2728
#include "swift/AST/ExistentialLayout.h"
2829
#include "swift/AST/Identifier.h"
2930
#include "swift/AST/Initializer.h"
@@ -49,8 +50,8 @@
4950

5051
using namespace swift;
5152

52-
TypeChecker::TypeChecker(ASTContext &Ctx, DiagnosticEngine &Diags)
53-
: Context(Ctx), Diags(Diags)
53+
TypeChecker::TypeChecker(ASTContext &Ctx)
54+
: Context(Ctx), Diags(Ctx.Diags)
5455
{
5556
auto clangImporter =
5657
static_cast<ClangImporter *>(Context.getClangModuleLoader());
@@ -750,13 +751,10 @@ bool swift::performTypeLocChecking(ASTContext &Ctx, TypeLoc &T,
750751
options |= TypeResolutionFlags::SILType;
751752

752753
auto resolution = TypeResolution::forContextual(DC, GenericEnv);
753-
if (ProduceDiagnostics) {
754-
return TypeChecker(Ctx).validateType(T, resolution, options);
755-
} else {
756-
// Set up a diagnostics engine that swallows diagnostics.
757-
DiagnosticEngine Diags(Ctx.SourceMgr);
758-
return TypeChecker(Ctx, Diags).validateType(T, resolution, options);
759-
}
754+
Optional<DiagnosticSuppression> suppression;
755+
if (!ProduceDiagnostics)
756+
suppression.emplace(Ctx.Diags);
757+
return TypeChecker(Ctx).validateType(T, resolution, options);
760758
}
761759

762760
/// Expose TypeChecker's handling of GenericParamList to SIL parsing.
@@ -769,9 +767,8 @@ swift::handleSILGenericParams(ASTContext &Ctx, GenericParamList *genericParams,
769767
void swift::typeCheckCompletionDecl(Decl *D) {
770768
auto &Ctx = D->getASTContext();
771769

772-
// Set up a diagnostics engine that swallows diagnostics.
773-
DiagnosticEngine Diags(Ctx.SourceMgr);
774-
TypeChecker TC(Ctx, Diags);
770+
DiagnosticSuppression suppression(Ctx.Diags);
771+
TypeChecker TC(Ctx);
775772

776773
if (auto ext = dyn_cast<ExtensionDecl>(D))
777774
TC.validateExtension(ext);
@@ -827,15 +824,14 @@ Optional<Type> swift::getTypeOfCompletionContextExpr(
827824
CompletionTypeCheckKind kind,
828825
Expr *&parsedExpr,
829826
ConcreteDeclRef &referencedDecl) {
827+
DiagnosticSuppression suppression(Ctx.Diags);
830828

831829
if (Ctx.getLazyResolver()) {
832830
TypeChecker *TC = static_cast<TypeChecker *>(Ctx.getLazyResolver());
833831
return ::getTypeOfCompletionContextExpr(*TC, DC, kind, parsedExpr,
834832
referencedDecl);
835833
} else {
836-
// Set up a diagnostics engine that swallows diagnostics.
837-
DiagnosticEngine diags(Ctx.SourceMgr);
838-
TypeChecker TC(Ctx, diags);
834+
TypeChecker TC(Ctx);
839835
// Try to solve for the actual type of the expression.
840836
return ::getTypeOfCompletionContextExpr(TC, DC, kind, parsedExpr,
841837
referencedDecl);
@@ -844,29 +840,27 @@ Optional<Type> swift::getTypeOfCompletionContextExpr(
844840

845841
bool swift::typeCheckCompletionSequence(DeclContext *DC, Expr *&parsedExpr) {
846842
auto &ctx = DC->getASTContext();
843+
DiagnosticSuppression suppression(ctx.Diags);
847844
if (ctx.getLazyResolver()) {
848845
TypeChecker *TC = static_cast<TypeChecker *>(ctx.getLazyResolver());
849846
return TC->typeCheckCompletionSequence(parsedExpr, DC);
850847
} else {
851-
// Set up a diagnostics engine that swallows diagnostics.
852-
DiagnosticEngine diags(ctx.SourceMgr);
853-
TypeChecker TC(ctx, diags);
848+
TypeChecker TC(ctx);
854849
return TC.typeCheckCompletionSequence(parsedExpr, DC);
855850
}
856851
}
857852

858853
bool swift::typeCheckExpression(DeclContext *DC, Expr *&parsedExpr) {
859854
auto &ctx = DC->getASTContext();
855+
DiagnosticSuppression suppression(ctx.Diags);
860856
if (ctx.getLazyResolver()) {
861857
TypeChecker *TC = static_cast<TypeChecker *>(ctx.getLazyResolver());
862858
auto resultTy = TC->typeCheckExpression(parsedExpr, DC, TypeLoc(),
863859
ContextualTypePurpose::CTP_Unused,
864860
TypeCheckExprFlags::SuppressDiagnostics);
865861
return !resultTy;
866862
} else {
867-
// Set up a diagnostics engine that swallows diagnostics.
868-
DiagnosticEngine diags(ctx.SourceMgr);
869-
TypeChecker TC(ctx, diags);
863+
TypeChecker TC(ctx);
870864
auto resultTy = TC.typeCheckExpression(parsedExpr, DC, TypeLoc(),
871865
ContextualTypePurpose::CTP_Unused,
872866
TypeCheckExprFlags::SuppressDiagnostics);
@@ -877,35 +871,26 @@ bool swift::typeCheckExpression(DeclContext *DC, Expr *&parsedExpr) {
877871
bool swift::typeCheckAbstractFunctionBodyUntil(AbstractFunctionDecl *AFD,
878872
SourceLoc EndTypeCheckLoc) {
879873
auto &Ctx = AFD->getASTContext();
874+
DiagnosticSuppression suppression(Ctx.Diags);
880875

881-
// Set up a diagnostics engine that swallows diagnostics.
882-
DiagnosticEngine Diags(Ctx.SourceMgr);
883-
884-
TypeChecker TC(Ctx, Diags);
876+
TypeChecker TC(Ctx);
885877
return !TC.typeCheckAbstractFunctionBodyUntil(AFD, EndTypeCheckLoc);
886878
}
887879

888880
bool swift::typeCheckTopLevelCodeDecl(TopLevelCodeDecl *TLCD) {
889881
auto &Ctx = static_cast<Decl *>(TLCD)->getASTContext();
890-
891-
// Set up a diagnostics engine that swallows diagnostics.
892-
DiagnosticEngine Diags(Ctx.SourceMgr);
893-
894-
TypeChecker TC(Ctx, Diags);
882+
DiagnosticSuppression suppression(Ctx.Diags);
883+
TypeChecker TC(Ctx);
895884
TC.typeCheckTopLevelCodeDecl(TLCD);
896885
return true;
897886
}
898887

899-
static void deleteTypeCheckerAndDiags(LazyResolver *resolver) {
900-
DiagnosticEngine &diags = static_cast<TypeChecker*>(resolver)->Diags;
888+
static void deleteTypeChecker(LazyResolver *resolver) {
901889
delete resolver;
902-
delete &diags;
903890
}
904891

905892
OwnedResolver swift::createLazyResolver(ASTContext &Ctx) {
906-
auto diags = new DiagnosticEngine(Ctx.SourceMgr);
907-
return OwnedResolver(new TypeChecker(Ctx, *diags),
908-
&deleteTypeCheckerAndDiags);
893+
return OwnedResolver(new TypeChecker(Ctx), &deleteTypeChecker);
909894
}
910895

911896
// checkForForbiddenPrefix is for testing purposes.

lib/Sema/TypeChecker.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,8 +726,7 @@ class TypeChecker final : public LazyResolver {
726726
Expr* constructCallToSuperInit(ConstructorDecl *ctor, ClassDecl *ClDecl);
727727

728728
public:
729-
TypeChecker(ASTContext &Ctx) : TypeChecker(Ctx, Ctx.Diags) { }
730-
TypeChecker(ASTContext &Ctx, DiagnosticEngine &Diags);
729+
TypeChecker(ASTContext &Ctx);
731730
TypeChecker(const TypeChecker&) = delete;
732731
TypeChecker& operator=(const TypeChecker&) = delete;
733732
~TypeChecker();

test/IRGen/playground.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import Swift
99

1010
@objc class C { }
1111

12+
private func __builtin_log_scope_entry(_ startLine: Int, _ startColumn: Int,
13+
_ endLine: Int, _ endColumn: Int) { }
14+
private func __builtin_log_scope_exit(_ startLine: Int, _ startColumn: Int,
15+
_ endLine: Int, _ endColumn: Int) { }
16+
private func __builtin_send_data<T>(_ object: T) { }
17+
1218
public func anchor() {}
1319

1420
anchor()

0 commit comments

Comments
 (0)