Coverage Report

Created: 2020-02-25 14:32

/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
16
      << (Flavor == diag::Flavor::WarningOrError ? 
013
:
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
91.5k
                                  bool ReportDiags) {
47
91.5k
  Diags.setSuppressSystemWarnings(true);  // Default to -Wno-system-headers
48
91.5k
  Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
49
91.5k
  Diags.setShowOverloads(Opts.getShowOverloads());
50
91.5k
51
91.5k
  Diags.setElideType(Opts.ElideType);
52
91.5k
  Diags.setPrintTemplateTree(Opts.ShowTemplateTree);
53
91.5k
  Diags.setShowColors(Opts.ShowColors);
54
91.5k
55
91.5k
  // Handle -ferror-limit
56
91.5k
  if (Opts.ErrorLimit)
57
28.0k
    Diags.setErrorLimit(Opts.ErrorLimit);
58
91.5k
  if (Opts.TemplateBacktraceLimit)
59
91.5k
    Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
60
91.5k
  if (Opts.ConstexprBacktraceLimit)
61
91.5k
    Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit);
62
91.5k
63
91.5k
  // If -pedantic or -pedantic-errors was specified, then we want to map all
64
91.5k
  // extension diagnostics onto WARNING or ERROR unless the user has futz'd
65
91.5k
  // around with them explicitly.
66
91.5k
  if (Opts.PedanticErrors)
67
215
    Diags.setExtensionHandlingBehavior(diag::Severity::Error);
68
91.2k
  else if (Opts.Pedantic)
69
471
    Diags.setExtensionHandlingBehavior(diag::Severity::Warning);
70
90.8k
  else
71
90.8k
    Diags.setExtensionHandlingBehavior(diag::Severity::Ignored);
72
91.5k
73
91.5k
  SmallVector<diag::kind, 10> _Diags;
74
91.5k
  const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs =
75
91.5k
    Diags.getDiagnosticIDs();
76
91.5k
  // We parse the warning options twice.  The first pass sets diagnostic state,
77
91.5k
  // while the second pass reports warnings/errors.  This has the effect that
78
91.5k
  // we follow the more canonical "last option wins" paradigm when there are
79
91.5k
  // conflicting options.
80
249k
  for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; 
++Report157k
) {
81
183k
    bool SetDiagnostic = (Report == 0);
82
183k
83
183k
    // If we've set the diagnostic state and are not reporting diagnostics then
84
183k
    // we're done.
85
183k
    if (!SetDiagnostic && 
!ReportDiags91.5k
)
86
25.4k
      break;
87
157k
88
263k
    
for (unsigned i = 0, e = Opts.Warnings.size(); 157k
i != e;
++i106k
) {
89
106k
      const auto Flavor = diag::Flavor::WarningOrError;
90
106k
      StringRef Opt = Opts.Warnings[i];
91
106k
      StringRef OrigOpt = Opts.Warnings[i];
92
106k
93
106k
      // Treat -Wformat=0 as an alias for -Wno-format.
94
106k
      if (Opt == "format=0")
95
2
        Opt = "no-format";
96
106k
97
106k
      // Check to see if this warning starts with "no-", if so, this is a
98
106k
      // negative form of the option.
99
106k
      bool isPositive = true;
100
106k
      if (Opt.startswith("no-")) {
101
5.45k
        isPositive = false;
102
5.45k
        Opt = Opt.substr(3);
103
5.45k
      }
104
106k
105
106k
      // Figure out how this option affects the warning.  If -Wfoo, map the
106
106k
      // diagnostic to a warning, if -Wno-foo, map it to ignore.
107
106k
      diag::Severity Mapping =
108
106k
          isPositive ? 
diag::Severity::Warning100k
:
diag::Severity::Ignored5.44k
;
109
106k
110
106k
      // -Wsystem-headers is a special case, not driven by the option table.  It
111
106k
      // cannot be controlled with -Werror.
112
106k
      if (Opt == "system-headers") {
113
37
        if (SetDiagnostic)
114
21
          Diags.setSuppressSystemWarnings(!isPositive);
115
37
        continue;
116
37
      }
117
106k
118
106k
      // -Weverything is a special case as well.  It implicitly enables all
119
106k
      // warnings, including ones not explicitly in a warning group.
120
106k
      if (Opt == "everything") {
121
84
        if (SetDiagnostic) {
122
45
          if (isPositive) {
123
29
            Diags.setEnableAllWarnings(true);
124
29
          } else {
125
16
            Diags.setEnableAllWarnings(false);
126
16
            Diags.setSeverityForAll(Flavor, diag::Severity::Ignored);
127
16
          }
128
45
        }
129
84
        continue;
130
84
      }
131
106k
132
106k
      // -Werror/-Wno-error is a special case, not controlled by the option
133
106k
      // table. It also has the "specifier" form of -Werror=foo and -Werror-foo.
134
106k
      if (Opt.startswith("error")) {
135
47.9k
        StringRef Specifier;
136
47.9k
        if (Opt.size() > 5) {  // Specifier must be present.
137
46.5k
          if ((Opt[5] != '=' && 
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
46.5k
          Specifier = Opt.substr(6);
144
46.5k
        }
145
47.9k
146
47.9k
        if (Specifier.empty()) {
147
1.38k
          if (SetDiagnostic)
148
796
            Diags.setWarningsAsErrors(isPositive);
149
1.38k
          continue;
150
1.38k
        }
151
46.5k
152
46.5k
        if (SetDiagnostic) {
153
24.5k
          // Set the warning as error flag for this specifier.
154
24.5k
          Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive);
155
24.5k
        } else 
if (22.0k
DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)22.0k
) {
156
0
          EmitUnknownDiagWarning(Diags, Flavor, "-Werror=", Specifier);
157
0
        }
158
46.5k
        continue;
159
46.5k
      }
160
58.2k
161
58.2k
      // -Wfatal-errors is yet another special case.
162
58.2k
      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
1
          // 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
58.2k
189
58.2k
      if (Report) {
190
27.6k
        if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
191
13
          EmitUnknownDiagWarning(Diags, Flavor, isPositive ? 
"-W"11
:
"-Wno-"2
,
192
13
                                 Opt);
193
30.5k
      } else {
194
30.5k
        Diags.setSeverityForGroup(Flavor, Opt, Mapping);
195
30.5k
      }
196
58.2k
    }
197
157k
198
158k
    for (unsigned i = 0, e = Opts.Remarks.size(); i != e; 
++i520
) {
199
520
      StringRef Opt = Opts.Remarks[i];
200
520
      const auto Flavor = diag::Flavor::Remark;
201
520
202
520
      // Check to see if this warning starts with "no-", if so, this is a
203
520
      // negative form of the option.
204
520
      bool IsPositive = !Opt.startswith("no-");
205
520
      if (!IsPositive) 
Opt = Opt.substr(3)17
;
206
520
207
520
      auto Severity = IsPositive ? 
diag::Severity::Remark503
208
520
                                 : 
diag::Severity::Ignored17
;
209
520
210
520
      // -Reverything sets the state of all remarks. Note that all remarks are
211
520
      // in remark groups, so we don't need a separate 'all remarks enabled'
212
520
      // flag.
213
520
      if (Opt == "everything") {
214
17
        if (SetDiagnostic)
215
9
          Diags.setSeverityForAll(Flavor, Severity);
216
17
        continue;
217
17
      }
218
503
219
503
      if (Report) {
220
205
        if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
221
3
          EmitUnknownDiagWarning(Diags, Flavor, IsPositive ? 
"-R"1
:
"-Rno-"2
,
222
3
                                 Opt);
223
298
      } else {
224
298
        Diags.setSeverityForGroup(Flavor, Opt,
225
298
                                  IsPositive ? 
diag::Severity::Remark295
226
298
                                             : 
diag::Severity::Ignored3
);
227
298
      }
228
503
    }
229
157k
  }
230
91.5k
}