blob: 39cda8783b48e673aba9212712c621dfaff656c7 [file] [log] [blame]
Sebastian Redl48350102009-03-06 17:41:35 +00001//===--- Warnings.cpp - C-Language Front-end ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Command line warning options handler.
11//
12//===----------------------------------------------------------------------===//
13//
14// This file is responsible for handling all warning options. This includes
15// a number of -Wfoo options and their variants, which are driven by TableGen-
Chris Lattnerb089c1d2009-12-23 18:53:37 +000016// generated data, and the special cases -pedantic, -pedantic-errors, -w,
17// -Werror and -Wfatal-errors.
Sebastian Redl48350102009-03-06 17:41:35 +000018//
Sebastian Redlecc54442009-03-07 12:09:25 +000019// Each warning option controls any number of actual warnings.
Sebastian Redl48350102009-03-06 17:41:35 +000020// Given a warning option 'foo', the following are valid:
Chris Lattnerb089c1d2009-12-23 18:53:37 +000021// -Wfoo, -Wno-foo, -Werror=foo, -Wfatal-errors=foo
Sebastian Redl48350102009-03-06 17:41:35 +000022//
Eli Friedman16b7b6f2009-05-19 04:14:29 +000023#include "clang/Frontend/Utils.h"
Sebastian Redl48350102009-03-06 17:41:35 +000024#include "clang/Basic/Diagnostic.h"
25#include "clang/Sema/SemaDiagnostic.h"
26#include "clang/Lex/LexDiagnostic.h"
Daniel Dunbar4c0e8272009-11-12 07:28:44 +000027#include "clang/Frontend/DiagnosticOptions.h"
Chris Lattnere2170ed2009-06-23 01:20:39 +000028#include "clang/Frontend/FrontendDiagnostic.h"
Eli Friedmancec35d72009-05-19 04:30:57 +000029#include <cstring>
Sebastian Redl48350102009-03-06 17:41:35 +000030#include <utility>
31#include <algorithm>
Sebastian Redl48350102009-03-06 17:41:35 +000032using namespace clang;
33
Kovarththanan Rajaratnam9ff84d92010-03-17 09:36:02 +000034void clang::ProcessWarningOptions(Diagnostic &Diags,
Daniel Dunbar4c0e8272009-11-12 07:28:44 +000035 const DiagnosticOptions &Opts) {
Chris Lattnere007de32009-04-15 07:01:18 +000036 Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers
Daniel Dunbar4c0e8272009-11-12 07:28:44 +000037 Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
Chris Lattnerdec49e72010-04-07 20:37:06 +000038
39 // Handle -ferror-limit
40 if (Opts.ErrorLimit)
41 Diags.setErrorLimit(Opts.ErrorLimit);
Sebastian Redl48350102009-03-06 17:41:35 +000042
Chris Lattnerb8e73152009-04-16 05:04:32 +000043 // If -pedantic or -pedantic-errors was specified, then we want to map all
44 // extension diagnostics onto WARNING or ERROR unless the user has futz'd
45 // around with them explicitly.
Daniel Dunbar4c0e8272009-11-12 07:28:44 +000046 if (Opts.PedanticErrors)
Chris Lattnerb8e73152009-04-16 05:04:32 +000047 Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Error);
Daniel Dunbar4c0e8272009-11-12 07:28:44 +000048 else if (Opts.Pedantic)
Chris Lattnerb8e73152009-04-16 05:04:32 +000049 Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Warn);
50 else
51 Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Ignore);
Mike Stump11289f42009-09-09 15:08:12 +000052
Daniel Dunbar4c0e8272009-11-12 07:28:44 +000053 for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
54 const std::string &Opt = Opts.Warnings[i];
Chris Lattner4c73b672009-04-15 04:27:38 +000055 const char *OptStart = &Opt[0];
56 const char *OptEnd = OptStart+Opt.size();
57 assert(*OptEnd == 0 && "Expect null termination for lower-bound search");
Mike Stump11289f42009-09-09 15:08:12 +000058
Chris Lattner4c73b672009-04-15 04:27:38 +000059 // Check to see if this warning starts with "no-", if so, this is a negative
60 // form of the option.
61 bool isPositive = true;
62 if (OptEnd-OptStart > 3 && memcmp(OptStart, "no-", 3) == 0) {
63 isPositive = false;
64 OptStart += 3;
65 }
Sebastian Redl48350102009-03-06 17:41:35 +000066
Chris Lattner384a59c2009-04-15 04:37:12 +000067 // Figure out how this option affects the warning. If -Wfoo, map the
68 // diagnostic to a warning, if -Wno-foo, map it to ignore.
69 diag::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE;
70
71 // -Wsystem-headers is a special case, not driven by the option table. It
72 // cannot be controlled with -Werror.
Chris Lattner4c73b672009-04-15 04:27:38 +000073 if (OptEnd-OptStart == 14 && memcmp(OptStart, "system-headers", 14) == 0) {
74 Diags.setSuppressSystemWarnings(!isPositive);
Sebastian Redl48350102009-03-06 17:41:35 +000075 continue;
76 }
Mike Stump11289f42009-09-09 15:08:12 +000077
Chris Lattner4c73b672009-04-15 04:27:38 +000078 // -Werror/-Wno-error is a special case, not controlled by the option table.
Chris Lattnerddddff42009-04-19 22:07:21 +000079 // It also has the "specifier" form of -Werror=foo and -Werror-foo.
Chris Lattner4c73b672009-04-15 04:27:38 +000080 if (OptEnd-OptStart >= 5 && memcmp(OptStart, "error", 5) == 0) {
81 const char *Specifier = 0;
82 if (OptEnd-OptStart != 5) { // Specifier must be present.
Chris Lattnerddddff42009-04-19 22:07:21 +000083 if ((OptStart[5] != '=' && OptStart[5] != '-') ||
84 OptEnd-OptStart == 6) {
Chris Lattnerb089c1d2009-12-23 18:53:37 +000085 Diags.Report(diag::warn_unknown_warning_specifier)
86 << "-Werror" << ("-W" + Opt);
Chris Lattner3fd869c2009-04-15 22:48:58 +000087 continue;
Chris Lattner4c73b672009-04-15 04:27:38 +000088 }
89 Specifier = OptStart+6;
90 }
Mike Stump11289f42009-09-09 15:08:12 +000091
Chris Lattner4c73b672009-04-15 04:27:38 +000092 if (Specifier == 0) {
Shantonu Sen4a7b9372009-08-14 04:07:15 +000093 Diags.setWarningsAsErrors(isPositive);
Chris Lattner4c73b672009-04-15 04:27:38 +000094 continue;
95 }
Mike Stump11289f42009-09-09 15:08:12 +000096
Chris Lattner384a59c2009-04-15 04:37:12 +000097 // -Werror=foo maps foo to Error, -Wno-error=foo maps it to Warning.
Chris Lattnerf9150ba2009-04-16 04:32:54 +000098 Mapping = isPositive ? diag::MAP_ERROR : diag::MAP_WARNING_NO_WERROR;
Chris Lattner384a59c2009-04-15 04:37:12 +000099 OptStart = Specifier;
Chris Lattner4c73b672009-04-15 04:27:38 +0000100 }
Mike Stump11289f42009-09-09 15:08:12 +0000101
Chris Lattner801fda82009-12-22 23:12:53 +0000102 // -Wfatal-errors is yet another special case.
103 if (OptEnd-OptStart >= 12 && memcmp(OptStart, "fatal-errors", 12) == 0) {
104 const char* Specifier = 0;
105 if (OptEnd-OptStart != 12) {
106 if ((OptStart[12] != '=' && OptStart[12] != '-') ||
107 OptEnd-OptStart == 13) {
Chris Lattnerb089c1d2009-12-23 18:53:37 +0000108 Diags.Report(diag::warn_unknown_warning_specifier)
109 << "-Wfatal-errors" << ("-W" + Opt);
Chris Lattner801fda82009-12-22 23:12:53 +0000110 continue;
111 }
112 Specifier = OptStart + 13;
113 }
114
115 if (Specifier == 0) {
116 Diags.setErrorsAsFatal(isPositive);
117 continue;
118 }
119
120 // -Wfatal-errors=foo maps foo to Fatal, -Wno-fatal-errors=foo
121 // maps it to Error.
122 Mapping = isPositive ? diag::MAP_FATAL : diag::MAP_ERROR_NO_WFATAL;
123 OptStart = Specifier;
124 }
125
Chris Lattnerc6fafed2009-04-19 22:34:23 +0000126 if (Diags.setDiagnosticGroupMapping(OptStart, Mapping))
Daniel Dunbar93097b32009-11-10 23:55:23 +0000127 Diags.Report(diag::warn_unknown_warning_option) << ("-W" + Opt);
Chris Lattnere007de32009-04-15 07:01:18 +0000128 }
Sebastian Redl48350102009-03-06 17:41:35 +0000129}