Skip to content

Commit ad68785

Browse files
committed
[flang/clang] Adding use of Clang's diagnostics in Flang
1 parent ab18cc2 commit ad68785

File tree

24 files changed

+242
-51
lines changed

24 files changed

+242
-51
lines changed

clang/include/clang/Basic/Diagnostic.td

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,14 @@ class DiagCategory<string Name> {
5555
}
5656

5757
// Diagnostic Groups.
58-
class DiagGroup<string Name, list<DiagGroup> subgroups = [], code docs = [{}]> {
58+
class DiagGroup<string Name, list<DiagGroup> subgroups = [], code docs = [{}],
59+
bit clangDiag = 1, bit flangDiag = 0> {
5960
string GroupName = Name;
6061
list<DiagGroup> SubGroups = subgroups;
6162
string CategoryName = "";
6263
code Documentation = docs;
64+
bit IsClangDiag = clangDiag;
65+
bit IsFlangDiag = flangDiag;
6366
}
6467
class InGroup<DiagGroup G> { DiagGroup Group = G; }
6568
//class IsGroup<string Name> { DiagGroup Group = DiagGroup<Name>; }

clang/include/clang/Basic/DiagnosticCategories.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ namespace clang {
2121
};
2222

2323
enum class Group {
24-
#define DIAG_ENTRY(GroupName, FlagNameOffset, Members, SubGroups, Docs) \
24+
#define DIAG_ENTRY(GroupName, FlagNameOffset, Members, SubGroups, Docs, \
25+
IsClang, IsFlang) \
2526
GroupName,
2627
#include "clang/Basic/DiagnosticGroups.inc"
2728
#undef CATEGORY

clang/include/clang/Basic/DiagnosticIDs.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,11 @@ class DiagnosticIDs : public RefCountedBase<DiagnosticIDs> {
365365
/// Given a diagnostic group ID, return its documentation.
366366
static StringRef getWarningOptionDocumentation(diag::Group GroupID);
367367

368+
/// Given a diagnostic group ID, return true if its a Flang warning.
369+
static bool isFlangWarningOption(diag::Group Group);
370+
/// Given a diagnostic group ID, return true if its a Clang warning.
371+
static bool isClangWarningOption(diag::Group Group);
372+
368373
void setGroupSeverity(StringRef Group, diag::Severity);
369374
void setGroupNoWarningsAsError(StringRef Group, bool);
370375

clang/lib/Basic/DiagnosticIDs.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,15 +585,18 @@ namespace {
585585
uint16_t Members;
586586
uint16_t SubGroups;
587587
StringRef Documentation;
588+
bool IsClangDiag;
589+
bool IsFlangDiag;
588590

589591
StringRef getName() const { return DiagGroupNames[NameOffset]; }
590592
};
591593
}
592594

593595
// Second the table of options, sorted by name for fast binary lookup.
594596
static const WarningOption OptionTable[] = {
595-
#define DIAG_ENTRY(GroupName, FlagNameOffset, Members, SubGroups, Docs) \
596-
{FlagNameOffset, Members, SubGroups, Docs},
597+
#define DIAG_ENTRY(GroupName, FlagNameOffset, Members, SubGroups, Docs, \
598+
IsClang, IsFlang) \
599+
{FlagNameOffset, Members, SubGroups, Docs, IsClang, IsFlang},
597600
#include "clang/Basic/DiagnosticGroups.inc"
598601
#undef DIAG_ENTRY
599602
};
@@ -607,6 +610,13 @@ StringRef DiagnosticIDs::getWarningOptionForGroup(diag::Group Group) {
607610
return OptionTable[static_cast<int>(Group)].getName();
608611
}
609612

613+
bool DiagnosticIDs::isFlangWarningOption(diag::Group Group) {
614+
return OptionTable[static_cast<int>(Group)].IsFlangDiag;
615+
}
616+
617+
bool DiagnosticIDs::isClangWarningOption(diag::Group Group) {
618+
return OptionTable[static_cast<int>(Group)].IsClangDiag;
619+
}
610620
std::optional<diag::Group>
611621
DiagnosticIDs::getGroupForWarningOption(StringRef Name) {
612622
const auto *Found = llvm::partition_point(

clang/lib/Basic/Warnings.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@ void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
106106
diag::Severity Mapping =
107107
isPositive ? diag::Severity::Warning : diag::Severity::Ignored;
108108

109+
// Check if the warning option is valid for Clang
110+
std::optional<diag::Group> Group = DiagIDs->getGroupForWarningOption(Opt);
111+
if (Group.has_value() && !DiagIDs->isClangWarningOption(Group.value())) {
112+
const unsigned DiagID = Diags.getCustomDiagID(
113+
DiagnosticsEngine::Error,
114+
"Warning option \"%0\" is valid for Fortran but not for C++.");
115+
Diags.Report(DiagID) << Opt;
116+
}
117+
109118
// -Wsystem-headers is a special case, not driven by the option table. It
110119
// cannot be controlled with -Werror.
111120
if (Opt == "system-headers") {

clang/test/TableGen/DiagnosticBase.inc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,13 @@ class DiagCategory<string Name> {
5555
}
5656

5757
// Diagnostic Groups.
58-
class DiagGroup<string Name, list<DiagGroup> subgroups = []> {
58+
class DiagGroup<string Name, list<DiagGroup> subgroups = [], bit clangDiag = 1, bit flangDiag = 0> {
5959
string GroupName = Name;
6060
list<DiagGroup> SubGroups = subgroups;
6161
string CategoryName = "";
6262
code Documentation = [{}];
63+
bit IsClangDiag = clangDiag;
64+
bit IsFlangDiag = flangDiag;
6365
}
6466
class InGroup<DiagGroup G> { DiagGroup Group = G; }
6567
//class IsGroup<string Name> { DiagGroup Group = DiagGroup<Name>; }

clang/tools/diagtool/DiagnosticNames.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ const DiagnosticRecord &diagtool::getDiagnosticForID(short DiagID) {
6262

6363
// Second the table of options, sorted by name for fast binary lookup.
6464
static const GroupRecord OptionTable[] = {
65-
#define DIAG_ENTRY(GroupName, FlagNameOffset, Members, SubGroups, Docs) \
65+
#define DIAG_ENTRY(GroupName, FlagNameOffset, Members, SubGroups, Docs, \
66+
IsClang, IsFlang) \
6667
{FlagNameOffset, Members, SubGroups},
6768
#include "clang/Basic/DiagnosticGroups.inc"
6869
#undef DIAG_ENTRY

clang/utils/TableGen/ClangDiagnosticsEmitter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,6 +1890,10 @@ static void emitDiagTable(DiagsInGroupTy &DiagsInGroup,
18901890

18911891
OS << "R\"(" << StringRef(Documentation).trim() << ")\"";
18921892

1893+
OS << ", /*IsClangDiag*/ "
1894+
<< bool(GroupInfo.Defs.back()->getValueAsBit("IsClangDiag"));
1895+
OS << ", /*IsFlangDiag*/ "
1896+
<< bool(GroupInfo.Defs.back()->getValueAsBit("IsFlangDiag"));
18931897
OS << ")\n";
18941898
}
18951899
OS << "#endif // DIAG_ENTRY\n\n";

flang/include/flang/Frontend/CompilerInvocation.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,18 @@ class TargetMachine;
3333

3434
namespace Fortran::frontend {
3535

36+
/// processWarningOptions - Initialize the diagnostic client and process the
37+
/// warning options specified on the command line.
38+
void processWarningOptions(clang::DiagnosticsEngine &Diags,
39+
const clang::DiagnosticOptions &Opts);
40+
3641
/// Fill out Opts based on the options given in Args.
3742
///
3843
/// When errors are encountered, return false and, if Diags is non-null,
3944
/// report the error(s).
4045
bool parseDiagnosticArgs(clang::DiagnosticOptions &opts,
41-
llvm::opt::ArgList &args);
46+
llvm::opt::ArgList &args,
47+
bool defaultDiagColor = true);
4248

4349
class CompilerInvocationBase {
4450
public:
@@ -174,7 +180,7 @@ class CompilerInvocation : public CompilerInvocationBase {
174180
/// Creates and configures semantics context based on the compilation flags.
175181
std::unique_ptr<Fortran::semantics::SemanticsContext>
176182
getSemanticsCtx(Fortran::parser::AllCookedSources &allCookedSources,
177-
const llvm::TargetMachine &);
183+
const llvm::TargetMachine &, clang::DiagnosticsEngine &diag);
178184

179185
std::string &getModuleDir() { return moduleDir; }
180186
const std::string &getModuleDir() const { return moduleDir; }

flang/include/flang/Semantics/semantics.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "flang/Parser/message.h"
2020
#include "flang/Support/Fortran-features.h"
2121
#include "flang/Support/LangOptions.h"
22+
#include "clang/Basic/Diagnostic.h"
2223
#include <iosfwd>
2324
#include <set>
2425
#include <string>
@@ -68,7 +69,7 @@ class SemanticsContext {
6869
public:
6970
SemanticsContext(const common::IntrinsicTypeDefaultKinds &,
7071
const common::LanguageFeatureControl &, const common::LangOptions &,
71-
parser::AllCookedSources &);
72+
parser::AllCookedSources &, clang::DiagnosticsEngine &);
7273
~SemanticsContext();
7374

7475
const common::IntrinsicTypeDefaultKinds &defaultKinds() const {
@@ -82,6 +83,7 @@ class SemanticsContext {
8283
int doublePrecisionKind() const {
8384
return defaultKinds_.doublePrecisionKind();
8485
}
86+
clang::DiagnosticsEngine &getDiagnostics() const { return diags_; }
8587
int quadPrecisionKind() const { return defaultKinds_.quadPrecisionKind(); }
8688
bool IsEnabled(common::LanguageFeature feature) const {
8789
return languageFeatures_.IsEnabled(feature);
@@ -305,6 +307,7 @@ class SemanticsContext {
305307
const common::IntrinsicTypeDefaultKinds &defaultKinds_;
306308
const common::LanguageFeatureControl &languageFeatures_;
307309
const common::LangOptions &langOpts_;
310+
clang::DiagnosticsEngine &diags_;
308311
parser::AllCookedSources &allCookedSources_;
309312
std::optional<parser::CharBlock> location_;
310313
std::vector<std::string> searchDirectories_;

0 commit comments

Comments
 (0)