1 //===--- Warnings.cpp - C-Language Front-end ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Command line warning options handler.
12 //===----------------------------------------------------------------------===//
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-
16 // generated data, and the special cases -pedantic, -pedantic-errors, -w,
17 // -Werror and -Wfatal-errors.
19 // Each warning option controls any number of actual warnings.
20 // Given a warning option 'foo', the following are valid:
21 // -Wfoo, -Wno-foo, -Werror=foo, -Wfatal-errors=foo
23 #include "clang/Frontend/Utils.h"
24 #include "clang/Basic/Diagnostic.h"
25 #include "clang/Sema/SemaDiagnostic.h"
26 #include "clang/Lex/LexDiagnostic.h"
27 #include "clang/Frontend/DiagnosticOptions.h"
28 #include "clang/Frontend/FrontendDiagnostic.h"
32 using namespace clang
;
34 void clang::ProcessWarningOptions(Diagnostic
&Diags
,
35 const DiagnosticOptions
&Opts
) {
36 Diags
.setSuppressSystemWarnings(true); // Default to -Wno-system-headers
37 Diags
.setIgnoreAllWarnings(Opts
.IgnoreWarnings
);
38 Diags
.setShowOverloads(
39 static_cast<Diagnostic::OverloadsShown
>(Opts
.ShowOverloads
));
41 // Handle -ferror-limit
43 Diags
.setErrorLimit(Opts
.ErrorLimit
);
44 if (Opts
.TemplateBacktraceLimit
)
45 Diags
.setTemplateBacktraceLimit(Opts
.TemplateBacktraceLimit
);
47 // If -pedantic or -pedantic-errors was specified, then we want to map all
48 // extension diagnostics onto WARNING or ERROR unless the user has futz'd
49 // around with them explicitly.
50 if (Opts
.PedanticErrors
)
51 Diags
.setExtensionHandlingBehavior(Diagnostic::Ext_Error
);
52 else if (Opts
.Pedantic
)
53 Diags
.setExtensionHandlingBehavior(Diagnostic::Ext_Warn
);
55 Diags
.setExtensionHandlingBehavior(Diagnostic::Ext_Ignore
);
57 for (unsigned i
= 0, e
= Opts
.Warnings
.size(); i
!= e
; ++i
) {
58 const std::string
&Opt
= Opts
.Warnings
[i
];
59 const char *OptStart
= &Opt
[0];
60 const char *OptEnd
= OptStart
+Opt
.size();
61 assert(*OptEnd
== 0 && "Expect null termination for lower-bound search");
63 // Check to see if this warning starts with "no-", if so, this is a negative
64 // form of the option.
65 bool isPositive
= true;
66 if (OptEnd
-OptStart
> 3 && memcmp(OptStart
, "no-", 3) == 0) {
71 // Figure out how this option affects the warning. If -Wfoo, map the
72 // diagnostic to a warning, if -Wno-foo, map it to ignore.
73 diag::Mapping Mapping
= isPositive
? diag::MAP_WARNING
: diag::MAP_IGNORE
;
75 // -Wsystem-headers is a special case, not driven by the option table. It
76 // cannot be controlled with -Werror.
77 if (OptEnd
-OptStart
== 14 && memcmp(OptStart
, "system-headers", 14) == 0) {
78 Diags
.setSuppressSystemWarnings(!isPositive
);
82 // -Werror/-Wno-error is a special case, not controlled by the option table.
83 // It also has the "specifier" form of -Werror=foo and -Werror-foo.
84 if (OptEnd
-OptStart
>= 5 && memcmp(OptStart
, "error", 5) == 0) {
85 const char *Specifier
= 0;
86 if (OptEnd
-OptStart
!= 5) { // Specifier must be present.
87 if ((OptStart
[5] != '=' && OptStart
[5] != '-') ||
88 OptEnd
-OptStart
== 6) {
89 Diags
.Report(diag::warn_unknown_warning_specifier
)
90 << "-Werror" << ("-W" + Opt
);
93 Specifier
= OptStart
+6;
97 Diags
.setWarningsAsErrors(isPositive
);
101 // -Werror=foo maps foo to Error, -Wno-error=foo maps it to Warning.
102 Mapping
= isPositive
? diag::MAP_ERROR
: diag::MAP_WARNING_NO_WERROR
;
103 OptStart
= Specifier
;
106 // -Wfatal-errors is yet another special case.
107 if (OptEnd
-OptStart
>= 12 && memcmp(OptStart
, "fatal-errors", 12) == 0) {
108 const char* Specifier
= 0;
109 if (OptEnd
-OptStart
!= 12) {
110 if ((OptStart
[12] != '=' && OptStart
[12] != '-') ||
111 OptEnd
-OptStart
== 13) {
112 Diags
.Report(diag::warn_unknown_warning_specifier
)
113 << "-Wfatal-errors" << ("-W" + Opt
);
116 Specifier
= OptStart
+ 13;
119 if (Specifier
== 0) {
120 Diags
.setErrorsAsFatal(isPositive
);
124 // -Wfatal-errors=foo maps foo to Fatal, -Wno-fatal-errors=foo
126 Mapping
= isPositive
? diag::MAP_FATAL
: diag::MAP_ERROR_NO_WFATAL
;
127 OptStart
= Specifier
;
130 if (Diags
.setDiagnosticGroupMapping(OptStart
, Mapping
))
131 Diags
.Report(diag::warn_unknown_warning_option
) << ("-W" + Opt
);