Skip to content

Commit 86e34cf

Browse files
committed
[flang] Add options -W[no-]unused-dummy-argument and -W[no-]unused-variable
Addition of two new warning options and a diagnostic system for Flang based on the Clang's diagnostics.
1 parent 2077d40 commit 86e34cf

32 files changed

+518
-53
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/DiagnosticGroups.td

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -885,12 +885,13 @@ def UnevaluatedExpression : DiagGroup<"unevaluated-expression",
885885
def UnusedValue : DiagGroup<"unused-value", [UnusedComparison, UnusedResult,
886886
UnevaluatedExpression]>;
887887
def UnusedConstVariable : DiagGroup<"unused-const-variable">;
888-
def UnusedVariable : DiagGroup<"unused-variable",
889-
[UnusedConstVariable]>;
888+
def UnusedVariable
889+
: DiagGroup<"unused-variable", [UnusedConstVariable], [{}], 1, 1>;
890890
def UnusedButSetVariable : DiagGroup<"unused-but-set-variable">;
891891
def UnusedLocalTypedef : DiagGroup<"unused-local-typedef">;
892892
def UnusedPropertyIvar : DiagGroup<"unused-property-ivar">;
893893
def UnusedGetterReturnValue : DiagGroup<"unused-getter-return-value">;
894+
def UnusedDummyArgument : DiagGroup<"unused-dummy-argument", [], [{}], 0, 1>;
894895
def UsedButMarkedUnused : DiagGroup<"used-but-marked-unused">;
895896
def UsedSearchPath : DiagGroup<"search-path-usage">;
896897
def UserDefinedLiterals : DiagGroup<"user-defined-literals">;

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/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12966,4 +12966,10 @@ def note_acc_atomic_mismatch_compound_operand
1296612966
// AMDGCN builtins diagnostics
1296712967
def err_amdgcn_global_load_lds_size_invalid_value : Error<"invalid size value">;
1296812968
def note_amdgcn_global_load_lds_size_valid_value : Note<"size must be %select{1, 2, or 4|1, 2, 4, 12 or 16}0">;
12969+
12970+
// Flang diagnostics
12971+
def warn_unused_dummy_argument : Warning<"unused dummy argument %0">,
12972+
InGroup<UnusedDummyArgument>,
12973+
DefaultIgnore;
12974+
1296912975
} // end of sema component.

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";

0 commit comments

Comments
 (0)