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-
|
|
|
|
// generated data, and the special cases -pedantic, -pedantic-errors, -w and
|
|
|
|
// -Werror.
|
|
|
|
//
|
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-04-16 08:53:55 +08:00
|
|
|
// -Wfoo, -Wno-foo, -Werror=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-06-23 09:20:39 +08:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2009-04-12 02:40:46 +08:00
|
|
|
#include <cstdio>
|
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,
|
|
|
|
std::vector<std::string> &Warnings,
|
|
|
|
bool Pedantic, bool PedanticErrors,
|
|
|
|
bool NoWarnings) {
|
2009-04-15 15:01:18 +08:00
|
|
|
Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers
|
2009-05-19 09:17:04 +08:00
|
|
|
Diags.setIgnoreAllWarnings(NoWarnings);
|
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-05-19 09:17:04 +08:00
|
|
|
if (PedanticErrors)
|
2009-04-16 13:04:32 +08:00
|
|
|
Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Error);
|
2009-05-19 09:17:04 +08:00
|
|
|
else if (Pedantic)
|
2009-04-16 13:04:32 +08:00
|
|
|
Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Warn);
|
|
|
|
else
|
|
|
|
Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Ignore);
|
|
|
|
|
2009-04-15 12:51:48 +08:00
|
|
|
// FIXME: -Wfatal-errors / -Wfatal-errors=foo
|
2009-04-15 12:27:38 +08:00
|
|
|
|
2009-05-19 09:17:04 +08:00
|
|
|
for (unsigned i = 0, e = Warnings.size(); i != e; ++i) {
|
|
|
|
const std::string &Opt = 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");
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -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-04-16 06:48:58 +08:00
|
|
|
fprintf(stderr, "warning: unknown -Werror warning specifier: -W%s\n",
|
|
|
|
Opt.c_str());
|
|
|
|
continue;
|
2009-04-15 12:27:38 +08:00
|
|
|
}
|
|
|
|
Specifier = OptStart+6;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-15 12:27:38 +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-04-15 12:27:38 +08:00
|
|
|
|
2009-04-20 06:34:23 +08:00
|
|
|
if (Diags.setDiagnosticGroupMapping(OptStart, Mapping))
|
2009-06-23 09:20:39 +08:00
|
|
|
Diags.Report(FullSourceLoc(), diag::warn_unknown_warning_option)
|
|
|
|
<< ("-W" + Opt);
|
2009-03-07 01:41:35 +08:00
|
|
|
}
|
2009-04-15 15:01:18 +08:00
|
|
|
|
2009-03-07 01:41:35 +08:00
|
|
|
return false;
|
|
|
|
}
|