Skip to content

Commit 5bfaff3

Browse files
committed
[Clang] Add native language support via std::messages.
This implementation must be built with libstdc++, and uses GNU Gettext as translation database.
1 parent 2b3baff commit 5bfaff3

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

clang/include/clang/Basic/DiagnosticIDs.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "llvm/ADT/StringRef.h"
2020
#include <optional>
2121
#include <vector>
22+
#include <cstdlib>
23+
#include <locale>
2224

2325
namespace clang {
2426
class DiagnosticsEngine;
@@ -185,6 +187,9 @@ class DiagnosticIDs : public RefCountedBase<DiagnosticIDs> {
185187
/// Information for uniquing and looking up custom diags.
186188
std::unique_ptr<diag::CustomDiagInfo> CustomDiagInfo;
187189

190+
const std::messages<char> &msg;
191+
std::messages<char>::catalog msg_catalog;
192+
188193
public:
189194
DiagnosticIDs();
190195
~DiagnosticIDs();

clang/lib/Basic/DiagnosticIDs.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/ADT/STLExtras.h"
1818
#include "llvm/ADT/SmallVector.h"
1919
#include "llvm/Support/ErrorHandling.h"
20+
#include <cstring>
2021
#include <map>
2122
#include <optional>
2223
using namespace clang;
@@ -389,9 +390,15 @@ namespace clang {
389390
// Common Diagnostic implementation
390391
//===----------------------------------------------------------------------===//
391392

392-
DiagnosticIDs::DiagnosticIDs() {}
393+
static std::locale locale(std::getenv("LANG"));
394+
DiagnosticIDs::DiagnosticIDs()
395+
: msg(std::use_facet<std::messages<char>>(locale)),
396+
msg_catalog(msg.open("clang", locale)) {
397+
}
393398

394-
DiagnosticIDs::~DiagnosticIDs() {}
399+
DiagnosticIDs::~DiagnosticIDs() {
400+
msg.close(msg_catalog);
401+
}
395402

396403
/// getCustomDiagID - Return an ID for a diagnostic with the specified message
397404
/// and level. If this is the first request for this diagnostic, it is
@@ -447,11 +454,28 @@ bool DiagnosticIDs::isDefaultMappingAsError(unsigned DiagID) {
447454

448455
/// getDescription - Given a diagnostic ID, return a description of the
449456
/// issue.
457+
static std::map<unsigned, std::string> descriptions;
450458
StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
459+
StringRef ref;
460+
451461
if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
452-
return Info->getDescription();
453-
assert(CustomDiagInfo && "Invalid CustomDiagInfo");
454-
return CustomDiagInfo->getDescription(DiagID);
462+
{
463+
ref = Info->getDescription();
464+
}
465+
else
466+
{
467+
assert(CustomDiagInfo && "Invalid CustomDiagInfo");
468+
ref = CustomDiagInfo->getDescription(DiagID);
469+
}
470+
471+
if (descriptions.count(DiagID) == 0) {
472+
std::string s;
473+
s = msg.get(msg_catalog, 0, 0, ref.data());
474+
descriptions[DiagID] = s;
475+
// std::strcpy(descriptions[DiagID], s.data());
476+
// descriptions[DiagID] = ref.data();
477+
}
478+
return StringRef(descriptions[DiagID]);
455479
}
456480

457481
static DiagnosticIDs::Level toLevel(diag::Severity SV) {

0 commit comments

Comments
 (0)