Coverage Report

Created: 2021-09-21 08:58

/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
18
                                   StringRef Opt) {
37
18
  StringRef Suggestion = DiagnosticIDs::getNearestOption(Flavor, Opt);
38
18
  Diags.Report(diag::warn_unknown_diag_option)
39
18
      << (Flavor == diag::Flavor::WarningOrError ? 
015
:
13
)
40
18
      << (Prefix.str() += std::string(Opt)) << !Suggestion.empty()
41
18
      << (Prefix.str() += std::string(Suggestion));
42
18
}
43
44
void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
45
                                  const DiagnosticOptions &Opts,
46
153k
                                  bool ReportDiags) {
47
153k
  Diags.setSuppressSystemWarnings(true);  // Default to -Wno-system-headers
48
153k
  Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
49
153k
  Diags.setShowOverloads(Opts.getShowOverloads());
50
51
153k
  Diags.setElideType(Opts.ElideType);
52
153k
  Diags.setPrintTemplateTree(Opts.ShowTemplateTree);
53
153k
  Diags.setShowColors(Opts.ShowColors);
54
55
  // Handle -ferror-limit
56
153k
  if (Opts.ErrorLimit)
57
41.4k
    Diags.setErrorLimit(Opts.ErrorLimit);
58
153k
  if (Opts.TemplateBacktraceLimit)
59
153k
    Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
60
153k
  if (Opts.ConstexprBacktraceLimit)
61
153k
    Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit);
62
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
153k
  if (Opts.PedanticErrors)
67
237
    Diags.setExtensionHandlingBehavior(diag::Severity::Error);
68
153k
  else if (Opts.Pedantic)
69
622
    Diags.setExtensionHandlingBehavior(diag::Severity::Warning);
70
152k
  else
71
152k
    Diags.setExtensionHandlingBehavior(diag::Severity::Ignored);
72
73
153k
  SmallVector<diag::kind, 10> _Diags;
74
153k
  const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs =
75
153k
    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
435k
  for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; 
++Report281k
) {
81
307k
    bool SetDiagnostic = (Report == 0);
82
83
    // If we've set the diagnostic state and are not reporting diagnostics then
84
    // we're done.
85
307k
    if (!SetDiagnostic && 
!ReportDiags153k
)
86
26.1k
      break;
87
88
664k
    
for (unsigned i = 0, e = Opts.Warnings.size(); 281k
i != e;
++i382k
) {
89
382k
      const auto Flavor = diag::Flavor::WarningOrError;
90
382k
      StringRef Opt = Opts.Warnings[i];
91
382k
      StringRef OrigOpt = Opts.Warnings[i];
92
93
      // Treat -Wformat=0 as an alias for -Wno-format.
94
382k
      if (Opt == "format=0")
95
2
        Opt = "no-format";
96
97
      // Check to see if this warning starts with "no-", if so, this is a
98
      // negative form of the option.
99
382k
      bool isPositive = true;
100
382k
      if (Opt.startswith("no-")) {
101
13.0k
        isPositive = false;
102
13.0k
        Opt = Opt.substr(3);
103
13.0k
      }
104
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
382k
      diag::Severity Mapping =
108
382k
          isPositive ? 
diag::Severity::Warning369k
:
diag::Severity::Ignored13.0k
;
109
110
      // -Wsystem-headers is a special case, not driven by the option table.  It
111
      // cannot be controlled with -Werror.
112
382k
      if (Opt == "system-headers") {
113
47
        if (SetDiagnostic)
114
26
          Diags.setSuppressSystemWarnings(!isPositive);
115
47
        continue;
116
47
      }
117
118
      // -Weverything is a special case as well.  It implicitly enables all
119
      // warnings, including ones not explicitly in a warning group.
120
382k
      if (Opt == "everything") {
121
93
        if (SetDiagnostic) {
122
50
          if (isPositive) {
123
34
            Diags.setEnableAllWarnings(true);
124
34
          } else {
125
16
            Diags.setEnableAllWarnings(false);
126
16
            Diags.setSeverityForAll(Flavor, diag::Severity::Ignored);
127
16
          }
128
50
        }
129
93
        continue;
130
93
      }
131
132
      // -Werror/-Wno-error is a special case, not controlled by the option
133
      // table. It also has the "specifier" form of -Werror=foo. GCC supports
134
      // the deprecated -Werror-implicit-function-declaration which is used by
135
      // a few projects.
136
382k
      if (Opt.startswith("error")) {
137
270k
        StringRef Specifier;
138
270k
        if (Opt.size() > 5) {  // Specifier must be present.
139
264k
          if (Opt[5] != '=' &&
140
264k
              
Opt.substr(5) != "-implicit-function-declaration"4
) {
141
2
            if (Report)
142
1
              Diags.Report(diag::warn_unknown_warning_specifier)
143
1
                << "-Werror" << ("-W" + OrigOpt.str());
144
2
            continue;
145
2
          }
146
264k
          Specifier = Opt.substr(6);
147
264k
        }
148
149
270k
        if (Specifier.empty()) {
150
5.92k
          if (SetDiagnostic)
151
3.10k
            Diags.setWarningsAsErrors(isPositive);
152
5.92k
          continue;
153
5.92k
        }
154
155
264k
        if (SetDiagnostic) {
156
          // Set the warning as error flag for this specifier.
157
133k
          Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive);
158
133k
        } else 
if (130k
DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)130k
) {
159
0
          EmitUnknownDiagWarning(Diags, Flavor, "-Werror=", Specifier);
160
0
        }
161
264k
        continue;
162
270k
      }
163
164
      // -Wfatal-errors is yet another special case.
165
112k
      if (Opt.startswith("fatal-errors")) {
166
6
        StringRef Specifier;
167
6
        if (Opt.size() != 12) {
168
2
          if ((Opt[12] != '=' && 
Opt[12] != '-'0
) || Opt.size() == 13) {
169
0
            if (Report)
170
0
              Diags.Report(diag::warn_unknown_warning_specifier)
171
0
                << "-Wfatal-errors" << ("-W" + OrigOpt.str());
172
0
            continue;
173
0
          }
174
2
          Specifier = Opt.substr(13);
175
2
        }
176
177
6
        if (Specifier.empty()) {
178
4
          if (SetDiagnostic)
179
2
            Diags.setErrorsAsFatal(isPositive);
180
4
          continue;
181
4
        }
182
183
2
        if (SetDiagnostic) {
184
          // Set the error as fatal flag for this specifier.
185
1
          Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive);
186
1
        } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
187
0
          EmitUnknownDiagWarning(Diags, Flavor, "-Wfatal-errors=", Specifier);
188
0
        }
189
2
        continue;
190
6
      }
191
192
112k
      if (Report) {
193
54.2k
        if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
194
15
          EmitUnknownDiagWarning(Diags, Flavor, isPositive ? 
"-W"13
:
"-Wno-"2
,
195
15
                                 Opt);
196
57.7k
      } else {
197
57.7k
        Diags.setSeverityForGroup(Flavor, Opt, Mapping);
198
57.7k
      }
199
112k
    }
200
201
282k
    for (unsigned i = 0, e = Opts.Remarks.size(); i != e; 
++i735
) {
202
735
      StringRef Opt = Opts.Remarks[i];
203
735
      const auto Flavor = diag::Flavor::Remark;
204
205
      // Check to see if this warning starts with "no-", if so, this is a
206
      // negative form of the option.
207
735
      bool IsPositive = !Opt.startswith("no-");
208
735
      if (!IsPositive) 
Opt = Opt.substr(3)17
;
209
210
735
      auto Severity = IsPositive ? 
diag::Severity::Remark718
211
735
                                 : 
diag::Severity::Ignored17
;
212
213
      // -Reverything sets the state of all remarks. Note that all remarks are
214
      // in remark groups, so we don't need a separate 'all remarks enabled'
215
      // flag.
216
735
      if (Opt == "everything") {
217
17
        if (SetDiagnostic)
218
9
          Diags.setSeverityForAll(Flavor, Severity);
219
17
        continue;
220
17
      }
221
222
718
      if (Report) {
223
288
        if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
224
3
          EmitUnknownDiagWarning(Diags, Flavor, IsPositive ? 
"-R"1
:
"-Rno-"2
,
225
3
                                 Opt);
226
430
      } else {
227
430
        Diags.setSeverityForGroup(Flavor, Opt,
228
430
                                  IsPositive ? 
diag::Severity::Remark427
229
430
                                             : 
diag::Severity::Ignored3
);
230
430
      }
231
718
    }
232
281k
  }
233
153k
}