Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Basic/Warnings.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- Warnings.cpp - C-Language Front-end ------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// Command line warning options handler.
10
//
11
//===----------------------------------------------------------------------===//
12
//
13
// This file is responsible for handling all warning options. This includes
14
// a number of -Wfoo options and their variants, which are driven by TableGen-
15
// generated data, and the special cases -pedantic, -pedantic-errors, -w,
16
// -Werror and -Wfatal-errors.
17
//
18
// Each warning option controls any number of actual warnings.
19
// Given a warning option 'foo', the following are valid:
20
//    -Wfoo, -Wno-foo, -Werror=foo, -Wfatal-errors=foo
21
//
22
// Remark options are also handled here, analogously, except that they are much
23
// simpler because a remark can't be promoted to an error.
24
#include "clang/Basic/AllDiagnostics.h"
25
#include "clang/Basic/Diagnostic.h"
26
#include "clang/Basic/DiagnosticOptions.h"
27
#include <algorithm>
28
#include <cstring>
29
#include <utility>
30
using namespace clang;
31
32
// EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning
33
// opts
34
static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags,
35
                                   diag::Flavor Flavor, StringRef Prefix,
36
16
                                   StringRef Opt) {
37
16
  StringRef Suggestion = DiagnosticIDs::getNearestOption(Flavor, Opt);
38
16
  Diags.Report(diag::warn_unknown_diag_option)
39
16
    << (Flavor == diag::Flavor::WarningOrError ? 
013
:
13
) << (Prefix.str() += Opt)
40
16
    << !Suggestion.empty() << (Prefix.str() += Suggestion);
41
16
}
42
43
void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
44
                                  const DiagnosticOptions &Opts,
45
79.0k
                                  bool ReportDiags) {
46
79.0k
  Diags.setSuppressSystemWarnings(true);  // Default to -Wno-system-headers
47
79.0k
  Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
48
79.0k
  Diags.setShowOverloads(Opts.getShowOverloads());
49
79.0k
50
79.0k
  Diags.setElideType(Opts.ElideType);
51
79.0k
  Diags.setPrintTemplateTree(Opts.ShowTemplateTree);
52
79.0k
  Diags.setShowColors(Opts.ShowColors);
53
79.0k
54
79.0k
  // Handle -ferror-limit
55
79.0k
  if (Opts.ErrorLimit)
56
28.5k
    Diags.setErrorLimit(Opts.ErrorLimit);
57
79.0k
  if (Opts.TemplateBacktraceLimit)
58
79.0k
    Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
59
79.0k
  if (Opts.ConstexprBacktraceLimit)
60
79.0k
    Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit);
61
79.0k
62
79.0k
  // If -pedantic or -pedantic-errors was specified, then we want to map all
63
79.0k
  // extension diagnostics onto WARNING or ERROR unless the user has futz'd
64
79.0k
  // around with them explicitly.
65
79.0k
  if (Opts.PedanticErrors)
66
318
    Diags.setExtensionHandlingBehavior(diag::Severity::Error);
67
78.7k
  else if (Opts.Pedantic)
68
445
    Diags.setExtensionHandlingBehavior(diag::Severity::Warning);
69
78.3k
  else
70
78.3k
    Diags.setExtensionHandlingBehavior(diag::Severity::Ignored);
71
79.0k
72
79.0k
  SmallVector<diag::kind, 10> _Diags;
73
79.0k
  const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs =
74
79.0k
    Diags.getDiagnosticIDs();
75
79.0k
  // We parse the warning options twice.  The first pass sets diagnostic state,
76
79.0k
  // while the second pass reports warnings/errors.  This has the effect that
77
79.0k
  // we follow the more canonical "last option wins" paradigm when there are
78
79.0k
  // conflicting options.
79
211k
  for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; 
++Report132k
) {
80
158k
    bool SetDiagnostic = (Report == 0);
81
158k
82
158k
    // If we've set the diagnostic state and are not reporting diagnostics then
83
158k
    // we're done.
84
158k
    if (!SetDiagnostic && 
!ReportDiags79.0k
)
85
25.7k
      break;
86
132k
87
316k
    
for (unsigned i = 0, e = Opts.Warnings.size(); 132k
i != e;
++i184k
) {
88
184k
      const auto Flavor = diag::Flavor::WarningOrError;
89
184k
      StringRef Opt = Opts.Warnings[i];
90
184k
      StringRef OrigOpt = Opts.Warnings[i];
91
184k
92
184k
      // Treat -Wformat=0 as an alias for -Wno-format.
93
184k
      if (Opt == "format=0")
94
2
        Opt = "no-format";
95
184k
96
184k
      // Check to see if this warning starts with "no-", if so, this is a
97
184k
      // negative form of the option.
98
184k
      bool isPositive = true;
99
184k
      if (Opt.startswith("no-")) {
100
33.2k
        isPositive = false;
101
33.2k
        Opt = Opt.substr(3);
102
33.2k
      }
103
184k
104
184k
      // Figure out how this option affects the warning.  If -Wfoo, map the
105
184k
      // diagnostic to a warning, if -Wno-foo, map it to ignore.
106
184k
      diag::Severity Mapping =
107
184k
          isPositive ? 
diag::Severity::Warning151k
:
diag::Severity::Ignored33.2k
;
108
184k
109
184k
      // -Wsystem-headers is a special case, not driven by the option table.  It
110
184k
      // cannot be controlled with -Werror.
111
184k
      if (Opt == "system-headers") {
112
37
        if (SetDiagnostic)
113
21
          Diags.setSuppressSystemWarnings(!isPositive);
114
37
        continue;
115
37
      }
116
184k
117
184k
      // -Weverything is a special case as well.  It implicitly enables all
118
184k
      // warnings, including ones not explicitly in a warning group.
119
184k
      if (Opt == "everything") {
120
77
        if (SetDiagnostic) {
121
41
          if (isPositive) {
122
25
            Diags.setEnableAllWarnings(true);
123
25
          } else {
124
16
            Diags.setEnableAllWarnings(false);
125
16
            Diags.setSeverityForAll(Flavor, diag::Severity::Ignored);
126
16
          }
127
41
        }
128
77
        continue;
129
77
      }
130
184k
131
184k
      // -Werror/-Wno-error is a special case, not controlled by the option
132
184k
      // table. It also has the "specifier" form of -Werror=foo and -Werror-foo.
133
184k
      if (Opt.startswith("error")) {
134
80.8k
        StringRef Specifier;
135
80.8k
        if (Opt.size() > 5) {  // Specifier must be present.
136
79.5k
          if ((Opt[5] != '=' && 
Opt[5] != '-'10
) || Opt.size() == 6) {
137
0
            if (Report)
138
0
              Diags.Report(diag::warn_unknown_warning_specifier)
139
0
                << "-Werror" << ("-W" + OrigOpt.str());
140
0
            continue;
141
0
          }
142
79.5k
          Specifier = Opt.substr(6);
143
79.5k
        }
144
80.8k
145
80.8k
        if (Specifier.empty()) {
146
1.25k
          if (SetDiagnostic)
147
737
            Diags.setWarningsAsErrors(isPositive);
148
1.25k
          continue;
149
1.25k
        }
150
79.5k
151
79.5k
        if (SetDiagnostic) {
152
43.2k
          // Set the warning as error flag for this specifier.
153
43.2k
          Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive);
154
43.2k
        } else 
if (36.2k
DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)36.2k
) {
155
0
          EmitUnknownDiagWarning(Diags, Flavor, "-Werror=", Specifier);
156
0
        }
157
79.5k
        continue;
158
79.5k
      }
159
103k
160
103k
      // -Wfatal-errors is yet another special case.
161
103k
      if (Opt.startswith("fatal-errors")) {
162
6
        StringRef Specifier;
163
6
        if (Opt.size() != 12) {
164
2
          if ((Opt[12] != '=' && 
Opt[12] != '-'0
) || Opt.size() == 13) {
165
0
            if (Report)
166
0
              Diags.Report(diag::warn_unknown_warning_specifier)
167
0
                << "-Wfatal-errors" << ("-W" + OrigOpt.str());
168
0
            continue;
169
0
          }
170
2
          Specifier = Opt.substr(13);
171
2
        }
172
6
173
6
        if (Specifier.empty()) {
174
4
          if (SetDiagnostic)
175
2
            Diags.setErrorsAsFatal(isPositive);
176
4
          continue;
177
4
        }
178
2
179
2
        if (SetDiagnostic) {
180
1
          // Set the error as fatal flag for this specifier.
181
1
          Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive);
182
1
        } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
183
0
          EmitUnknownDiagWarning(Diags, Flavor, "-Wfatal-errors=", Specifier);
184
0
        }
185
2
        continue;
186
2
      }
187
103k
188
103k
      if (Report) {
189
45.8k
        if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
190
13
          EmitUnknownDiagWarning(Diags, Flavor, isPositive ? 
"-W"11
:
"-Wno-"2
,
191
13
                                 Opt);
192
57.4k
      } else {
193
57.4k
        Diags.setSeverityForGroup(Flavor, Opt, Mapping);
194
57.4k
      }
195
103k
    }
196
132k
197
132k
    for (unsigned i = 0, e = Opts.Remarks.size(); i != e; 
++i577
) {
198
577
      StringRef Opt = Opts.Remarks[i];
199
577
      const auto Flavor = diag::Flavor::Remark;
200
577
201
577
      // Check to see if this warning starts with "no-", if so, this is a
202
577
      // negative form of the option.
203
577
      bool IsPositive = !Opt.startswith("no-");
204
577
      if (!IsPositive) 
Opt = Opt.substr(3)17
;
205
577
206
577
      auto Severity = IsPositive ? 
diag::Severity::Remark560
207
577
                                 : 
diag::Severity::Ignored17
;
208
577
209
577
      // -Reverything sets the state of all remarks. Note that all remarks are
210
577
      // in remark groups, so we don't need a separate 'all remarks enabled'
211
577
      // flag.
212
577
      if (Opt == "everything") {
213
17
        if (SetDiagnostic)
214
9
          Diags.setSeverityForAll(Flavor, Severity);
215
17
        continue;
216
17
      }
217
560
218
560
      if (Report) {
219
225
        if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
220
3
          EmitUnknownDiagWarning(Diags, Flavor, IsPositive ? 
"-R"1
:
"-Rno-"2
,
221
3
                                 Opt);
222
335
      } else {
223
335
        Diags.setSeverityForGroup(Flavor, Opt,
224
335
                                  IsPositive ? 
diag::Severity::Remark332
225
335
                                             : 
diag::Severity::Ignored3
);
226
335
      }
227
560
    }
228
132k
  }
229
79.0k
}