2009-03-07 01:41:35 +08:00
|
|
|
//===--- Warnings.cpp - C-Language Front-end ------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Command line warning options handler.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is responsible for handling all warning options. This includes
|
|
|
|
// a number of -Wfoo options and their variants, which are driven by TableGen-
|
2009-12-24 02:53:37 +08:00
|
|
|
// generated data, and the special cases -pedantic, -pedantic-errors, -w,
|
|
|
|
// -Werror and -Wfatal-errors.
|
2009-03-07 01:41:35 +08:00
|
|
|
//
|
2009-03-07 20:09:25 +08:00
|
|
|
// Each warning option controls any number of actual warnings.
|
2009-03-07 01:41:35 +08:00
|
|
|
// Given a warning option 'foo', the following are valid:
|
2009-12-24 02:53:37 +08:00
|
|
|
// -Wfoo, -Wno-foo, -Werror=foo, -Wfatal-errors=foo
|
2009-03-07 01:41:35 +08:00
|
|
|
//
|
2009-05-19 12:14:29 +08:00
|
|
|
#include "clang/Frontend/Utils.h"
|
2009-03-07 01:41:35 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Sema/SemaDiagnostic.h"
|
|
|
|
#include "clang/Lex/LexDiagnostic.h"
|
2009-11-12 15:28:44 +08:00
|
|
|
#include "clang/Frontend/DiagnosticOptions.h"
|
2009-06-23 09:20:39 +08:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2009-05-19 12:30:57 +08:00
|
|
|
#include <cstring>
|
2009-03-07 01:41:35 +08:00
|
|
|
#include <utility>
|
|
|
|
#include <algorithm>
|
|
|
|
using namespace clang;
|
|
|
|
|
2009-05-19 09:17:04 +08:00
|
|
|
bool clang::ProcessWarningOptions(Diagnostic &Diags,
|
2009-11-12 15:28:44 +08:00
|
|
|
const DiagnosticOptions &Opts) {
|
2009-04-15 15:01:18 +08:00
|
|
|
Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers
|
2009-11-12 15:28:44 +08:00
|
|
|
Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
|
2009-03-07 01:41:35 +08:00
|
|
|
|
2009-04-16 13:04:32 +08:00
|
|
|
// If -pedantic or -pedantic-errors was specified, then we want to map all
|
|
|
|
// extension diagnostics onto WARNING or ERROR unless the user has futz'd
|
|
|
|
// around with them explicitly.
|
2009-11-12 15:28:44 +08:00
|
|
|
if (Opts.PedanticErrors)
|
2009-04-16 13:04:32 +08:00
|
|
|
Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Error);
|
2009-11-12 15:28:44 +08:00
|
|
|
else if (Opts.Pedantic)
|
2009-04-16 13:04:32 +08:00
|
|
|
Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Warn);
|
|
|
|
else
|
|
|
|
Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Ignore);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-12 15:28:44 +08:00
|
|
|
for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
|
|
|
|
const std::string &Opt = Opts.Warnings[i];
|
2009-04-15 12:27:38 +08:00
|
|
|
const char *OptStart = &Opt[0];
|
|
|
|
const char *OptEnd = OptStart+Opt.size();
|
|
|
|
assert(*OptEnd == 0 && "Expect null termination for lower-bound search");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-15 12:27:38 +08:00
|
|
|
// Check to see if this warning starts with "no-", if so, this is a negative
|
|
|
|
// form of the option.
|
|
|
|
bool isPositive = true;
|
|
|
|
if (OptEnd-OptStart > 3 && memcmp(OptStart, "no-", 3) == 0) {
|
|
|
|
isPositive = false;
|
|
|
|
OptStart += 3;
|
|
|
|
}
|
2009-03-07 01:41:35 +08:00
|
|
|
|
2009-04-15 12:37:12 +08:00
|
|
|
// Figure out how this option affects the warning. If -Wfoo, map the
|
|
|
|
// diagnostic to a warning, if -Wno-foo, map it to ignore.
|
|
|
|
diag::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE;
|
|
|
|
|
|
|
|
// -Wsystem-headers is a special case, not driven by the option table. It
|
|
|
|
// cannot be controlled with -Werror.
|
2009-04-15 12:27:38 +08:00
|
|
|
if (OptEnd-OptStart == 14 && memcmp(OptStart, "system-headers", 14) == 0) {
|
|
|
|
Diags.setSuppressSystemWarnings(!isPositive);
|
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-15 12:27:38 +08:00
|
|
|
// -Werror/-Wno-error is a special case, not controlled by the option table.
|
2009-04-20 06:07:21 +08:00
|
|
|
// It also has the "specifier" form of -Werror=foo and -Werror-foo.
|
2009-04-15 12:27:38 +08:00
|
|
|
if (OptEnd-OptStart >= 5 && memcmp(OptStart, "error", 5) == 0) {
|
|
|
|
const char *Specifier = 0;
|
|
|
|
if (OptEnd-OptStart != 5) { // Specifier must be present.
|
2009-04-20 06:07:21 +08:00
|
|
|
if ((OptStart[5] != '=' && OptStart[5] != '-') ||
|
|
|
|
OptEnd-OptStart == 6) {
|
2009-12-24 02:53:37 +08:00
|
|
|
Diags.Report(diag::warn_unknown_warning_specifier)
|
|
|
|
<< "-Werror" << ("-W" + Opt);
|
2009-04-16 06:48:58 +08:00
|
|
|
continue;
|
2009-04-15 12:27:38 +08:00
|
|
|
}
|
|
|
|
Specifier = OptStart+6;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-15 12:27:38 +08:00
|
|
|
if (Specifier == 0) {
|
2009-08-14 12:07:15 +08:00
|
|
|
Diags.setWarningsAsErrors(isPositive);
|
2009-04-15 12:27:38 +08:00
|
|
|
continue;
|
2009-03-07 01:41:35 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-15 12:37:12 +08:00
|
|
|
// -Werror=foo maps foo to Error, -Wno-error=foo maps it to Warning.
|
2009-04-16 12:32:54 +08:00
|
|
|
Mapping = isPositive ? diag::MAP_ERROR : diag::MAP_WARNING_NO_WERROR;
|
2009-04-15 12:37:12 +08:00
|
|
|
OptStart = Specifier;
|
2009-03-07 01:41:35 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-12-23 07:12:53 +08:00
|
|
|
// -Wfatal-errors is yet another special case.
|
|
|
|
if (OptEnd-OptStart >= 12 && memcmp(OptStart, "fatal-errors", 12) == 0) {
|
|
|
|
const char* Specifier = 0;
|
|
|
|
if (OptEnd-OptStart != 12) {
|
|
|
|
if ((OptStart[12] != '=' && OptStart[12] != '-') ||
|
|
|
|
OptEnd-OptStart == 13) {
|
2009-12-24 02:53:37 +08:00
|
|
|
Diags.Report(diag::warn_unknown_warning_specifier)
|
|
|
|
<< "-Wfatal-errors" << ("-W" + Opt);
|
2009-12-23 07:12:53 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Specifier = OptStart + 13;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Specifier == 0) {
|
|
|
|
Diags.setErrorsAsFatal(isPositive);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -Wfatal-errors=foo maps foo to Fatal, -Wno-fatal-errors=foo
|
|
|
|
// maps it to Error.
|
|
|
|
Mapping = isPositive ? diag::MAP_FATAL : diag::MAP_ERROR_NO_WFATAL;
|
|
|
|
OptStart = Specifier;
|
|
|
|
}
|
|
|
|
|
2009-04-20 06:34:23 +08:00
|
|
|
if (Diags.setDiagnosticGroupMapping(OptStart, Mapping))
|
2009-11-11 07:55:23 +08:00
|
|
|
Diags.Report(diag::warn_unknown_warning_option) << ("-W" + Opt);
|
2009-03-07 01:41:35 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-07 01:41:35 +08:00
|
|
|
return false;
|
|
|
|
}
|