Coverage Report

Created: 2020-09-19 12:23

/Users/buildslave/jenkins/workspace/coverage/llvm-project/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
13
      << (Flavor == diag::Flavor::WarningOrError ? 0 : 
13
)
40
16
      << (Prefix.str() += std::string(Opt)) << !Suggestion.empty()
41
16
      << (Prefix.str() += std::string(Suggestion));
42
16
}
43
44
void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
45
                                  const DiagnosticOptions &Opts,
46
113k
                                  bool ReportDiags) {
47
113k
  Diags.setSuppressSystemWarnings(true);  // Default to -Wno-system-headers
48
113k
  Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
49
113k
  Diags.setShowOverloads(Opts.getShowOverloads());
50
113k
51
113k
  Diags.setElideType(Opts.ElideType);
52
113k
  Diags.setPrintTemplateTree(Opts.ShowTemplateTree);
53
113k
  Diags.setShowColors(Opts.ShowColors);
54
113k
55
  // Handle -ferror-limit
56
113k
  if (Opts.ErrorLimit)
57
42.4k
    Diags.setErrorLimit(Opts.ErrorLimit);
58
113k
  if (Opts.TemplateBacktraceLimit)
59
113k
    Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
60
113k
  if (Opts.ConstexprBacktraceLimit)
61
113k
    Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit);
62
113k
63
  // If -pedantic or -pedantic-errors was specified, then we want to map all
64
  // extension diagnostics onto WARNING or ERROR unless the user has futz'd
65
  // around with them explicitly.
66
113k
  if (Opts.PedanticErrors)
67
234
    Diags.setExtensionHandlingBehavior(diag::Severity::Error);
68
113k
  else if (Opts.Pedantic)
69
523
    Diags.setExtensionHandlingBehavior(diag::Severity::Warning);
70
113k
  else
71
113k
    Diags.setExtensionHandlingBehavior(diag::Severity::Ignored);
72
113k
73
113k
  SmallVector<diag::kind, 10> _Diags;
74
113k
  const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs =
75
113k
    Diags.getDiagnosticIDs();
76
  // We parse the warning options twice.  The first pass sets diagnostic state,
77
  // while the second pass reports warnings/errors.  This has the effect that
78
  // we follow the more canonical "last option wins" paradigm when there are
79
  // conflicting options.
80
314k
  for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; 
++Report200k
) {
81
227k
    bool SetDiagnostic = (Report == 0);
82
227k
83
    // If we've set the diagnostic state and are not reporting diagnostics then
84
    // we're done.
85
227k
    if (!SetDiagnostic && 
!ReportDiags113k
)
86
27.4k
      break;
87
200k
88
498k
    
for (unsigned i = 0, e = Opts.Warnings.size(); 200k
i != e;
++i298k
) {
89
298k
      const auto Flavor = diag::Flavor::WarningOrError;
90
298k
      StringRef Opt = Opts.Warnings[i];
91
298k
      StringRef OrigOpt = Opts.Warnings[i];
92
298k
93
      // Treat -Wformat=0 as an alias for -Wno-format.
94
298k
      if (Opt == "format=0")
95
2
        Opt = "no-format";
96
298k
97
      // Check to see if this warning starts with "no-", if so, this is a
98
      // negative form of the option.
99
298k
      bool isPositive = true;
100
298k
      if (Opt.startswith("no-")) {
101
8.48k
        isPositive = false;
102
8.48k
        Opt = Opt.substr(3);
103
8.48k
      }
104
298k
105
      // Figure out how this option affects the warning.  If -Wfoo, map the
106
      // diagnostic to a warning, if -Wno-foo, map it to ignore.
107
298k
      diag::Severity Mapping =
108
289k
          isPositive ? diag::Severity::Warning : 
diag::Severity::Ignored8.47k
;
109
298k
110
      // -Wsystem-headers is a special case, not driven by the option table.  It
111
      // cannot be controlled with -Werror.
112
298k
      if (Opt == "system-headers") {
113
39
        if (SetDiagnostic)
114
22
          Diags.setSuppressSystemWarnings(!isPositive);
115
39
        continue;
116
39
      }
117
298k
118
      // -Weverything is a special case as well.  It implicitly enables all
119
      // warnings, including ones not explicitly in a warning group.
120
298k
      if (Opt == "everything") {
121
84
        if (SetDiagnostic) {
122
45
          if (isPositive) {
123
29
            Diags.setEnableAllWarnings(true);
124
16
          } else {
125
16
            Diags.setEnableAllWarnings(false);
126
16
            Diags.setSeverityForAll(Flavor, diag::Severity::Ignored);
127
16
          }
128
45
        }
129
84
        continue;
130
84
      }
131
298k
132
      // -Werror/-Wno-error is a special case, not controlled by the option
133
      // table. It also has the "specifier" form of -Werror=foo and -Werror-foo.
134
298k
      if (Opt.startswith("error")) {
135
184k
        StringRef Specifier;
136
184k
        if (Opt.size() > 5) {  // Specifier must be present.
137
180k
          if (
(180k
Opt[5] != '='180k
&&
Opt[5] != '-'10
) || Opt.size() == 6) {
138
0
            if (Report)
139
0
              Diags.Report(diag::warn_unknown_warning_specifier)
140
0
                << "-Werror" << ("-W" + OrigOpt.str());
141
0
            continue;
142
0
          }
143
180k
          Specifier = Opt.substr(6);
144
180k
        }
145
184k
146
184k
        if (Specifier.empty()) {
147
3.49k
          if (SetDiagnostic)
148
1.85k
            Diags.setWarningsAsErrors(isPositive);
149
3.49k
          continue;
150
3.49k
        }
151
180k
152
180k
        if (SetDiagnostic) {
153
          // Set the warning as error flag for this specifier.
154
92.7k
          Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive);
155
87.9k
        } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
156
0
          EmitUnknownDiagWarning(Diags, Flavor, "-Werror=", Specifier);
157
0
        }
158
180k
        continue;
159
180k
      }
160
114k
161
      // -Wfatal-errors is yet another special case.
162
114k
      if (Opt.startswith("fatal-errors")) {
163
6
        StringRef Specifier;
164
6
        if (Opt.size() != 12) {
165
2
          if ((Opt[12] != '=' && 
Opt[12] != '-'0
) || Opt.size() == 13) {
166
0
            if (Report)
167
0
              Diags.Report(diag::warn_unknown_warning_specifier)
168
0
                << "-Wfatal-errors" << ("-W" + OrigOpt.str());
169
0
            continue;
170
0
          }
171
2
          Specifier = Opt.substr(13);
172
2
        }
173
6
174
6
        if (Specifier.empty()) {
175
4
          if (SetDiagnostic)
176
2
            Diags.setErrorsAsFatal(isPositive);
177
4
          continue;
178
4
        }
179
2
180
2
        if (SetDiagnostic) {
181
          // Set the error as fatal flag for this specifier.
182
1
          Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive);
183
1
        } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
184
0
          EmitUnknownDiagWarning(Diags, Flavor, "-Wfatal-errors=", Specifier);
185
0
        }
186
2
        continue;
187
2
      }
188
114k
189
114k
      if (Report) {
190
54.5k
        if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
191
13
          EmitUnknownDiagWarning(Diags, Flavor, isPositive ? 
"-W"11
:
"-Wno-"2
,
192
13
                                 Opt);
193
59.5k
      } else {
194
59.5k
        Diags.setSeverityForGroup(Flavor, Opt, Mapping);
195
59.5k
      }
196
114k
    }
197
200k
198
200k
    for (unsigned i = 0, e = Opts.Remarks.size(); i != e; 
++i659
) {
199
659
      StringRef Opt = Opts.Remarks[i];
200
659
      const auto Flavor = diag::Flavor::Remark;
201
659
202
      // Check to see if this warning starts with "no-", if so, this is a
203
      // negative form of the option.
204
659
      bool IsPositive = !Opt.startswith("no-");
205
659
      if (!IsPositive) 
Opt = Opt.substr(3)17
;
206
659
207
642
      auto Severity = IsPositive ? diag::Severity::Remark
208
17
                                 : diag::Severity::Ignored;
209
659
210
      // -Reverything sets the state of all remarks. Note that all remarks are
211
      // in remark groups, so we don't need a separate 'all remarks enabled'
212
      // flag.
213
659
      if (Opt == "everything") {
214
17
        if (SetDiagnostic)
215
9
          Diags.setSeverityForAll(Flavor, Severity);
216
17
        continue;
217
17
      }
218
642
219
642
      if (Report) {
220
257
        if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
221
3
          EmitUnknownDiagWarning(Diags, Flavor, IsPositive ? 
"-R"1
:
"-Rno-"2
,
222
3
                                 Opt);
223
385
      } else {
224
385
        Diags.setSeverityForGroup(Flavor, Opt,
225
382
                                  IsPositive ? diag::Severity::Remark
226
3
                                             : diag::Severity::Ignored);
227
385
      }
228
642
    }
229
200k
  }
230
113k
}