#include "clang/Basic/AllDiagnostics.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/DiagnosticOptions.h"
#include <algorithm>
#include <cstring>
#include <utility>
using namespace clang;
static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags,
diag::Flavor Flavor, StringRef Prefix,
StringRef Opt) {
StringRef Suggestion = DiagnosticIDs::getNearestOption(Flavor, Opt);
Diags.Report(diag::warn_unknown_diag_option)
<< (Flavor == diag::Flavor::WarningOrError ? 0 : 1)
<< (Prefix.str() += std::string(Opt)) << !Suggestion.empty()
<< (Prefix.str() += std::string(Suggestion));
}
void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
const DiagnosticOptions &Opts,
bool ReportDiags) {
Diags.setSuppressSystemWarnings(true); Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
Diags.setShowOverloads(Opts.getShowOverloads());
Diags.setElideType(Opts.ElideType);
Diags.setPrintTemplateTree(Opts.ShowTemplateTree);
Diags.setShowColors(Opts.ShowColors);
if (Opts.ErrorLimit)
Diags.setErrorLimit(Opts.ErrorLimit);
if (Opts.TemplateBacktraceLimit)
Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
if (Opts.ConstexprBacktraceLimit)
Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit);
if (Opts.PedanticErrors)
Diags.setExtensionHandlingBehavior(diag::Severity::Error);
else if (Opts.Pedantic)
Diags.setExtensionHandlingBehavior(diag::Severity::Warning);
else
Diags.setExtensionHandlingBehavior(diag::Severity::Ignored);
SmallVector<diag::kind, 10> _Diags;
const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs =
Diags.getDiagnosticIDs();
for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; ++Report) {
bool SetDiagnostic = (Report == 0);
if (!SetDiagnostic && !ReportDiags)
break;
for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
const auto Flavor = diag::Flavor::WarningOrError;
StringRef Opt = Opts.Warnings[i];
StringRef OrigOpt = Opts.Warnings[i];
if (Opt == "format=0")
Opt = "no-format";
bool isPositive = true;
if (Opt.startswith("no-")) {
isPositive = false;
Opt = Opt.substr(3);
}
diag::Severity Mapping =
isPositive ? diag::Severity::Warning : diag::Severity::Ignored;
if (Opt == "system-headers") {
if (SetDiagnostic)
Diags.setSuppressSystemWarnings(!isPositive);
continue;
}
if (Opt == "everything") {
if (SetDiagnostic) {
if (isPositive) {
Diags.setEnableAllWarnings(true);
} else {
Diags.setEnableAllWarnings(false);
Diags.setSeverityForAll(Flavor, diag::Severity::Ignored);
}
}
continue;
}
if (Opt.startswith("error")) {
StringRef Specifier;
if (Opt.size() > 5) { if (Opt[5] != '=' &&
Opt.substr(5) != "-implicit-function-declaration") {
if (Report)
Diags.Report(diag::warn_unknown_warning_specifier)
<< "-Werror" << ("-W" + OrigOpt.str());
continue;
}
Specifier = Opt.substr(6);
}
if (Specifier.empty()) {
if (SetDiagnostic)
Diags.setWarningsAsErrors(isPositive);
continue;
}
if (SetDiagnostic) {
Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive);
} else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
EmitUnknownDiagWarning(Diags, Flavor, "-Werror=", Specifier);
}
continue;
}
if (Opt.startswith("fatal-errors")) {
StringRef Specifier;
if (Opt.size() != 12) {
if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) {
if (Report)
Diags.Report(diag::warn_unknown_warning_specifier)
<< "-Wfatal-errors" << ("-W" + OrigOpt.str());
continue;
}
Specifier = Opt.substr(13);
}
if (Specifier.empty()) {
if (SetDiagnostic)
Diags.setErrorsAsFatal(isPositive);
continue;
}
if (SetDiagnostic) {
Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive);
} else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
EmitUnknownDiagWarning(Diags, Flavor, "-Wfatal-errors=", Specifier);
}
continue;
}
if (Report) {
if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
EmitUnknownDiagWarning(Diags, Flavor, isPositive ? "-W" : "-Wno-",
Opt);
} else {
Diags.setSeverityForGroup(Flavor, Opt, Mapping);
}
}
for (unsigned i = 0, e = Opts.Remarks.size(); i != e; ++i) {
StringRef Opt = Opts.Remarks[i];
const auto Flavor = diag::Flavor::Remark;
bool IsPositive = !Opt.startswith("no-");
if (!IsPositive) Opt = Opt.substr(3);
auto Severity = IsPositive ? diag::Severity::Remark
: diag::Severity::Ignored;
if (Opt == "everything") {
if (SetDiagnostic)
Diags.setSeverityForAll(Flavor, Severity);
continue;
}
if (Report) {
if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
EmitUnknownDiagWarning(Diags, Flavor, IsPositive ? "-R" : "-Rno-",
Opt);
} else {
Diags.setSeverityForGroup(Flavor, Opt,
IsPositive ? diag::Severity::Remark
: diag::Severity::Ignored);
}
}
}
}