Skip to content

Commit 8665e0c

Browse files
[Localization] Move localization allocator from DiagEngine to the localization producer
1 parent d4ae826 commit 8665e0c

File tree

7 files changed

+60
-56
lines changed

7 files changed

+60
-56
lines changed

include/swift/AST/DiagnosticEngine.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include "llvm/Support/Allocator.h"
3030
#include "llvm/Support/FileSystem.h"
3131
#include "llvm/Support/Path.h"
32-
#include "llvm/Support/StringSaver.h"
3332
#include "llvm/Support/SaveAndRestore.h"
3433
#include "llvm/Support/VersionTuple.h"
3534

@@ -725,11 +724,6 @@ namespace swift {
725724
/// diagnostic message.
726725
std::unique_ptr<diag::LocalizationProducer> localization;
727726

728-
/// This allocator will retain localized diagnostic strings containing the
729-
/// diagnostic's message and identifier for the duration of compiler invocation.
730-
llvm::BumpPtrAllocator localizationAllocator;
731-
llvm::StringSaver localizationSaver;
732-
733727
/// The number of open diagnostic transactions. Diagnostics are only
734728
/// emitted once all transactions have closed.
735729
unsigned TransactionCount = 0;
@@ -754,8 +748,7 @@ namespace swift {
754748
public:
755749
explicit DiagnosticEngine(SourceManager &SourceMgr)
756750
: SourceMgr(SourceMgr), ActiveDiagnostic(),
757-
TransactionStrings(TransactionAllocator),
758-
localizationSaver(localizationAllocator) {}
751+
TransactionStrings(TransactionAllocator) {}
759752

760753
/// hadAnyError - return true if any *error* diagnostics have been emitted.
761754
bool hadAnyError() const { return state.hadAnyError(); }
@@ -815,16 +808,15 @@ namespace swift {
815808
if (llvm::sys::fs::exists(filePath)) {
816809
if (auto file = llvm::MemoryBuffer::getFile(filePath)) {
817810
localization = std::make_unique<diag::SerializedLocalizationProducer>(
818-
std::move(file.get()), localizationSaver,
819-
getPrintDiagnosticNames());
811+
std::move(file.get()), getPrintDiagnosticNames());
820812
}
821813
} else {
822814
llvm::sys::path::replace_extension(filePath, ".yaml");
823815
// In case of missing localization files, we should fallback to messages
824816
// from `.def` files.
825817
if (llvm::sys::fs::exists(filePath)) {
826818
localization = std::make_unique<diag::YAMLLocalizationProducer>(
827-
filePath.str(), localizationSaver, getPrintDiagnosticNames());
819+
filePath.str(), getPrintDiagnosticNames());
828820
}
829821
}
830822
}

include/swift/Localization/LocalizationFormat.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,16 @@ class SerializedLocalizationWriter {
155155
};
156156

157157
class LocalizationProducer {
158-
llvm::Optional<llvm::StringSaver> localizationSaver;
158+
/// This allocator will retain localized diagnostic strings containing the
159+
/// diagnostic's message and identifier for the duration of compiler
160+
/// invocation
161+
llvm::BumpPtrAllocator localizationAllocator;
162+
llvm::StringSaver localizationSaver;
159163
bool printDiagnosticName;
160164

161165
public:
162-
LocalizationProducer(llvm::Optional<llvm::StringSaver> localizationSaver =
163-
llvm::Optional<llvm::StringSaver>(),
164-
bool printDiagnosticName = false)
165-
: localizationSaver(localizationSaver),
166+
LocalizationProducer(bool printDiagnosticName = false)
167+
: localizationSaver(localizationAllocator),
166168
printDiagnosticName(printDiagnosticName) {}
167169

168170
/// If the message isn't available/localized in current context
@@ -186,8 +188,6 @@ class YAMLLocalizationProducer final : public LocalizationProducer {
186188
std::vector<std::string> unknownIDs;
187189
explicit YAMLLocalizationProducer(
188190
llvm::StringRef filePath,
189-
llvm::Optional<llvm::StringSaver> localizationSaver =
190-
llvm::Optional<llvm::StringSaver>(),
191191
bool printDiagnosticName = false);
192192

193193
/// Iterate over all of the available (non-empty) translations
@@ -210,8 +210,6 @@ class SerializedLocalizationProducer final : public LocalizationProducer {
210210
public:
211211
explicit SerializedLocalizationProducer(
212212
std::unique_ptr<llvm::MemoryBuffer> buffer,
213-
llvm::Optional<llvm::StringSaver> localizationSaver =
214-
llvm::Optional<llvm::StringSaver>(),
215213
bool printDiagnosticName = false);
216214

217215
protected:

lib/Localization/LocalizationFormat.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,16 @@ LocalizationProducer::getMessageOr(swift::DiagID id,
9999
return defaultMessage;
100100
llvm::StringRef diagnosticName(diagnosticNameStrings[(unsigned)id]);
101101
if (printDiagnosticName) {
102-
assert(localizationSaver);
103102
auto localizedDebugDiagnosticMessage =
104-
localizationSaver->save(localizedMessage.str() + diagnosticName.str());
103+
localizationSaver.save(localizedMessage.str() + diagnosticName.str());
105104
return localizedDebugDiagnosticMessage;
106105
}
107106
return localizedMessage;
108107
}
109108

110109
SerializedLocalizationProducer::SerializedLocalizationProducer(
111-
std::unique_ptr<llvm::MemoryBuffer> buffer,
112-
llvm::Optional<llvm::StringSaver> localizationSaver,
113-
bool printDiagnosticName)
114-
: LocalizationProducer(localizationSaver, printDiagnosticName),
115-
Buffer(std::move(buffer)) {
110+
std::unique_ptr<llvm::MemoryBuffer> buffer, bool printDiagnosticName)
111+
: LocalizationProducer(printDiagnosticName), Buffer(std::move(buffer)) {
116112
auto base =
117113
reinterpret_cast<const unsigned char *>(Buffer.get()->getBufferStart());
118114
auto tableOffset = endian::read<offset_type>(base, little);
@@ -128,11 +124,9 @@ SerializedLocalizationProducer::getMessage(swift::DiagID id) const {
128124
return {(const char *)value.getDataPtr(), value.getDataLen()};
129125
}
130126

131-
YAMLLocalizationProducer::YAMLLocalizationProducer(
132-
llvm::StringRef filePath,
133-
llvm::Optional<llvm::StringSaver> localizationSaver,
134-
bool printDiagnosticName)
135-
: LocalizationProducer(localizationSaver, printDiagnosticName) {
127+
YAMLLocalizationProducer::YAMLLocalizationProducer(llvm::StringRef filePath,
128+
bool printDiagnosticName)
129+
: LocalizationProducer(printDiagnosticName) {
136130
auto FileBufOrErr = llvm::MemoryBuffer::getFileOrSTDIN(filePath);
137131
llvm::MemoryBuffer *document = FileBufOrErr->get();
138132
diag::LocalizationInput yin(document->getBuffer());
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: not %target-swift-frontend -debug-diagnostic-names -localization-path %S/Inputs -locale en -typecheck %s 2>&1 | %FileCheck %s --check-prefix=CHECK_NAMES
2+
3+
_ = "HI!
4+
// CHECK_NAMES: error: unterminated string literal [lex_unterminated_string]{{$}}
5+
6+
var self1 = self1
7+
// CHECK_NAMES: error: circular reference [circular_reference]{{$}}
8+
// CHECK_NAMES: note: through reference here [circular_reference_through]{{$}}
9+
10+
struct Broken {
11+
var b : Bool = True
12+
}
13+
// CHECK_NAMES: error: cannot find 'True' in scope [cannot_find_in_scope]{{$}}
Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
// RUN: not %target-swift-frontend -typecheck %s 2>&1 -localization-path %S/Inputs -locale en | %FileCheck %s --check-prefix=CHECK_NONAMES
2-
// RUN: not %target-swift-frontend -debug-diagnostic-names -localization-path %S/Inputs -locale en -typecheck %s 2>&1 | %FileCheck %s --check-prefix=CHECK_NAMES
1+
// RUN: %target-typecheck-verify-swift -localization-path %S/Inputs -locale en
32

43
_ = "HI!
5-
// CHECK_NONAMES: error: unterminated string literal{{$}}
6-
// CHECK_NAMES: error: unterminated string literal [lex_unterminated_string]{{$}}
7-
4+
// expected-error@-1{{unterminated string literal}}
85
var self1 = self1
9-
// CHECK_NONAMES: error: circular reference{{$}}
10-
// CHECK_NONAMES: note: through reference here{{$}}
11-
// CHECK_NAMES: error: circular reference [circular_reference]{{$}}
12-
// CHECK_NAMES: note: through reference here [circular_reference_through]{{$}}
6+
// expected-note@-1 2{{through reference here}}
7+
// expected-error@-2 {{circular reference}}
138
149
struct Broken {
15-
var b : Bool = True
10+
var b : Bool = True // expected-error{{cannot find 'True' in scope}}
1611
}
17-
// CHECK_NONAMES: error: cannot find 'True' in scope{{$}}
18-
// CHECK_NAMES: error: cannot find 'True' in scope [cannot_find_in_scope]{{$}}
12+
var v1 : Int[1 // expected-error {{expected ']' in array type}} expected-note {{to match this opening '['}}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: swift-serialize-diagnostics --input-file-path=%S/Inputs/fr.yaml --output-directory=%t/
3+
// RUN: swift-serialize-diagnostics --input-file-path=%S/Inputs/en.yaml --output-directory=%t/ 2>&1 | %FileCheck %s
4+
// RUN: not %target-swift-frontend -debug-diagnostic-names -localization-path %S/Inputs -locale fr -typecheck %s 2>&1 | %FileCheck %s --check-prefix=CHECK_NAMES
5+
6+
// CHECK: These diagnostic IDs are no longer availiable: 'not_available_in_def, not_available_in_def_2, not_available_in_def_3, not_available_in_def_4, not_available_in_def_5'
7+
_ = "HI!
8+
// CHECK_NAMES: error: chaîne non terminée littérale [lex_unterminated_string]{{$}}
9+
10+
// FIXME: This used to produce a localized diagnostic.
11+
12+
var self1 = self1
13+
// CHECK_NAMES: error: circular reference [circular_reference]{{$}}
14+
// CHECK_NAMES: note: through reference here [circular_reference_through]{{$}}
15+
16+
struct Broken {
17+
var b : Bool = True
18+
}
19+
// CHECK_NAMES: error: impossible de trouver 'True' portée [cannot_find_in_scope]{{$}}
Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
// RUN: %empty-directory(%t)
22
// RUN: swift-serialize-diagnostics --input-file-path=%S/Inputs/fr.yaml --output-directory=%t/
33
// RUN: swift-serialize-diagnostics --input-file-path=%S/Inputs/en.yaml --output-directory=%t/ 2>&1 | %FileCheck %s
4-
// RUN: not %target-swift-frontend -typecheck %s 2>&1 -localization-path %S/Inputs -locale fr | %FileCheck %s --check-prefix=CHECK_NONAMES
5-
// RUN: not %target-swift-frontend -debug-diagnostic-names -localization-path %S/Inputs -locale fr -typecheck %s 2>&1 | %FileCheck %s --check-prefix=CHECK_NAMES
4+
// RUN: %target-typecheck-verify-swift -localization-path %t -locale fr
65

76
// CHECK: These diagnostic IDs are no longer availiable: 'not_available_in_def, not_available_in_def_2, not_available_in_def_3, not_available_in_def_4, not_available_in_def_5'
87
_ = "HI!
9-
// CHECK_NONAMES: error: chaîne non terminée littérale{{$}}
10-
// CHECK_NAMES: error: chaîne non terminée littérale [lex_unterminated_string]{{$}}
8+
// expected-error@-1{{chaîne non terminée littérale}}
119
1210
// FIXME: This used to produce a localized diagnostic.
1311
14-
var self1 = self1
15-
// CHECK_NONAMES: error: circular reference{{$}}
16-
// CHECK_NONAMES: note: through reference here{{$}}
17-
// CHECK_NAMES: error: circular reference [circular_reference]{{$}}
18-
// CHECK_NAMES: note: through reference here [circular_reference_through]{{$}}
12+
var self1 = self1 // expected-note 2{{through reference here}}
13+
// expected-error@-1 {{circular reference}}
1914
2015
struct Broken {
21-
var b : Bool = True
16+
var b : Bool = True // expected-error{{impossible de trouver 'True' portée}}
2217
}
23-
// CHECK_NONAMES: error: impossible de trouver 'True' portée{{$}}
24-
// CHECK_NAMES: error: impossible de trouver 'True' portée [cannot_find_in_scope]{{$}}
18+
var v1 : Int[1 // expected-error {{expected ']' in array type}} expected-note {{to match this opening '['}}

0 commit comments

Comments
 (0)