Skip to content

Commit e239ee8

Browse files
[NFC] Rewrite and restructure diagnostics handling in FrontendTool
Extract the logics for emitting diagnostics, especially the parseable output message, from FrontendTools so it is easier to reason.
1 parent 03b0f0c commit e239ee8

File tree

4 files changed

+605
-447
lines changed

4 files changed

+605
-447
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//===--- DiagnosticHelper.h - Diagnostic Helper -----------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 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 exposes helper class to emit diagnostics from swift-frontend.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef SWIFT_FRONTEND_DIAGNOSTIC_HELPER_H
18+
#define SWIFT_FRONTEND_DIAGNOSTIC_HELPER_H
19+
20+
#include "swift/Basic/LLVM.h"
21+
22+
namespace swift {
23+
class CompilerInstance;
24+
class CompilerInvocation;
25+
26+
class DiagnosticHelper {
27+
private:
28+
class Implementation;
29+
Implementation &Impl;
30+
31+
public:
32+
/// Create a DiagnosticHelper class to emit diagnostics from frontend actions.
33+
static DiagnosticHelper create(CompilerInstance &instance,
34+
bool useQuasiPID = false);
35+
36+
/// Initialized all DiagConsumers and add to the CompilerInstance.
37+
void initDiagConsumers(CompilerInvocation &invocation);
38+
39+
/// Begin emitting the message, specifically the parseable output message.
40+
void beginMessage(CompilerInvocation &invocation,
41+
ArrayRef<const char *> args);
42+
43+
/// End emitting all diagnostics. This has to be called if beginMessage() is
44+
/// called.
45+
void endMessage(int retCode);
46+
47+
/// Set if printing output should be suppressed.
48+
void setSuppressOutput(bool suppressOutput);
49+
50+
/// Helper function to emit fatal error.
51+
void diagnoseFatalError(const char *reason, bool shouldCrash);
52+
53+
DiagnosticHelper(const DiagnosticHelper &) = delete;
54+
DiagnosticHelper(DiagnosticHelper &&) = delete;
55+
DiagnosticHelper &operator=(const DiagnosticHelper &) = delete;
56+
DiagnosticHelper &operator=(DiagnosticHelper &&) = delete;
57+
~DiagnosticHelper();
58+
59+
private:
60+
DiagnosticHelper(CompilerInstance &instance, bool useQuasiPID);
61+
};
62+
63+
} // namespace swift
64+
65+
#endif

lib/Frontend/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_swift_host_library(swiftFrontend STATIC
1010
CompileJobCacheResult.cpp
1111
CompilerInvocation.cpp
1212
DependencyVerifier.cpp
13+
DiagnosticHelper.cpp
1314
DiagnosticVerifier.cpp
1415
Frontend.cpp
1516
FrontendInputsAndOutputs.cpp

0 commit comments

Comments
 (0)