Coverage Report

Created: 2020-03-31 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Sema/SemaStmtAttr.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- SemaStmtAttr.cpp - Statement Attribute Handling ------------------===//
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
//  This file implements stmt-related attribute processing.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/Sema/SemaInternal.h"
14
#include "clang/AST/ASTContext.h"
15
#include "clang/Basic/SourceManager.h"
16
#include "clang/Sema/DelayedDiagnostic.h"
17
#include "clang/Sema/Lookup.h"
18
#include "clang/Sema/ScopeInfo.h"
19
#include "llvm/ADT/StringExtras.h"
20
21
using namespace clang;
22
using namespace sema;
23
24
static Attr *handleFallThroughAttr(Sema &S, Stmt *St, const ParsedAttr &A,
25
920
                                   SourceRange Range) {
26
920
  FallThroughAttr Attr(S.Context, A);
27
920
  if (!isa<NullStmt>(St)) {
28
5
    S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_wrong_target)
29
5
        << Attr.getSpelling() << St->getBeginLoc();
30
5
    if (isa<SwitchCase>(St)) {
31
1
      SourceLocation L = S.getLocForEndOfToken(Range.getEnd());
32
1
      S.Diag(L, diag::note_fallthrough_insert_semi_fixit)
33
1
          << FixItHint::CreateInsertion(L, ";");
34
1
    }
35
5
    return nullptr;
36
5
  }
37
915
  auto *FnScope = S.getCurFunction();
38
915
  if (FnScope->SwitchStack.empty()) {
39
1
    S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_outside_switch);
40
1
    return nullptr;
41
1
  }
42
914
43
914
  // If this is spelled as the standard C++17 attribute, but not in C++17, warn
44
914
  // about using it as an extension.
45
914
  if (!S.getLangOpts().CPlusPlus17 && 
A.isCXX11Attribute()884
&&
46
914
      
!A.getScopeName()865
)
47
7
    S.Diag(A.getLoc(), diag::ext_cxx17_attr) << A;
48
914
49
914
  FnScope->setHasFallthroughStmt();
50
914
  return ::new (S.Context) FallThroughAttr(S.Context, A);
51
914
}
52
53
static Attr *handleSuppressAttr(Sema &S, Stmt *St, const ParsedAttr &A,
54
4
                                SourceRange Range) {
55
4
  if (A.getNumArgs() < 1) {
56
0
    S.Diag(A.getLoc(), diag::err_attribute_too_few_arguments) << A << 1;
57
0
    return nullptr;
58
0
  }
59
4
60
4
  std::vector<StringRef> DiagnosticIdentifiers;
61
9
  for (unsigned I = 0, E = A.getNumArgs(); I != E; 
++I5
) {
62
5
    StringRef RuleName;
63
5
64
5
    if (!S.checkStringLiteralArgumentAttr(A, I, RuleName, nullptr))
65
0
      return nullptr;
66
5
67
5
    // FIXME: Warn if the rule name is unknown. This is tricky because only
68
5
    // clang-tidy knows about available rules.
69
5
    DiagnosticIdentifiers.push_back(RuleName);
70
5
  }
71
4
72
4
  return ::new (S.Context) SuppressAttr(
73
4
      S.Context, A, DiagnosticIdentifiers.data(), DiagnosticIdentifiers.size());
74
4
}
75
76
static Attr *handleLoopHintAttr(Sema &S, Stmt *St, const ParsedAttr &A,
77
275
                                SourceRange) {
78
275
  IdentifierLoc *PragmaNameLoc = A.getArgAsIdent(0);
79
275
  IdentifierLoc *OptionLoc = A.getArgAsIdent(1);
80
275
  IdentifierLoc *StateLoc = A.getArgAsIdent(2);
81
275
  Expr *ValueExpr = A.getArgAsExpr(3);
82
275
83
275
  StringRef PragmaName =
84
275
      llvm::StringSwitch<StringRef>(PragmaNameLoc->Ident->getName())
85
275
          .Cases("unroll", "nounroll", "unroll_and_jam", "nounroll_and_jam",
86
275
                 PragmaNameLoc->Ident->getName())
87
275
          .Default("clang loop");
88
275
89
275
  if (St->getStmtClass() != Stmt::DoStmtClass &&
90
275
      
St->getStmtClass() != Stmt::ForStmtClass269
&&
91
275
      
St->getStmtClass() != Stmt::CXXForRangeStmtClass134
&&
92
275
      
St->getStmtClass() != Stmt::WhileStmtClass125
) {
93
9
    std::string Pragma = "#pragma " + std::string(PragmaName);
94
9
    S.Diag(St->getBeginLoc(), diag::err_pragma_loop_precedes_nonloop) << Pragma;
95
9
    return nullptr;
96
9
  }
97
266
98
266
  LoopHintAttr::OptionType Option;
99
266
  LoopHintAttr::LoopHintState State;
100
266
101
266
  auto SetHints = [&Option, &State](LoopHintAttr::OptionType O,
102
266
                                    LoopHintAttr::LoopHintState S) {
103
51
    Option = O;
104
51
    State = S;
105
51
  };
106
266
107
266
  if (PragmaName == "nounroll") {
108
7
    SetHints(LoopHintAttr::Unroll, LoopHintAttr::Disable);
109
259
  } else if (PragmaName == "unroll") {
110
31
    // #pragma unroll N
111
31
    if (ValueExpr)
112
21
      SetHints(LoopHintAttr::UnrollCount, LoopHintAttr::Numeric);
113
10
    else
114
10
      SetHints(LoopHintAttr::Unroll, LoopHintAttr::Enable);
115
228
  } else if (PragmaName == "nounroll_and_jam") {
116
5
    SetHints(LoopHintAttr::UnrollAndJam, LoopHintAttr::Disable);
117
223
  } else if (PragmaName == "unroll_and_jam") {
118
8
    // #pragma unroll_and_jam N
119
8
    if (ValueExpr)
120
5
      SetHints(LoopHintAttr::UnrollAndJamCount, LoopHintAttr::Numeric);
121
3
    else
122
3
      SetHints(LoopHintAttr::UnrollAndJam, LoopHintAttr::Enable);
123
215
  } else {
124
215
    // #pragma clang loop ...
125
215
    assert(OptionLoc && OptionLoc->Ident &&
126
215
           "Attribute must have valid option info.");
127
215
    Option = llvm::StringSwitch<LoopHintAttr::OptionType>(
128
215
                 OptionLoc->Ident->getName())
129
215
                 .Case("vectorize", LoopHintAttr::Vectorize)
130
215
                 .Case("vectorize_width", LoopHintAttr::VectorizeWidth)
131
215
                 .Case("interleave", LoopHintAttr::Interleave)
132
215
                 .Case("vectorize_predicate", LoopHintAttr::VectorizePredicate)
133
215
                 .Case("interleave_count", LoopHintAttr::InterleaveCount)
134
215
                 .Case("unroll", LoopHintAttr::Unroll)
135
215
                 .Case("unroll_count", LoopHintAttr::UnrollCount)
136
215
                 .Case("pipeline", LoopHintAttr::PipelineDisabled)
137
215
                 .Case("pipeline_initiation_interval",
138
215
                       LoopHintAttr::PipelineInitiationInterval)
139
215
                 .Case("distribute", LoopHintAttr::Distribute)
140
215
                 .Default(LoopHintAttr::Vectorize);
141
215
    if (Option == LoopHintAttr::VectorizeWidth ||
142
215
        
Option == LoopHintAttr::InterleaveCount179
||
143
215
        
Option == LoopHintAttr::UnrollCount151
||
144
215
        
Option == LoopHintAttr::PipelineInitiationInterval133
) {
145
87
      assert(ValueExpr && "Attribute must have a valid value expression.");
146
87
      if (S.CheckLoopHintExpr(ValueExpr, St->getBeginLoc()))
147
0
        return nullptr;
148
87
      State = LoopHintAttr::Numeric;
149
128
    } else if (Option == LoopHintAttr::Vectorize ||
150
128
               
Option == LoopHintAttr::Interleave84
||
151
128
               
Option == LoopHintAttr::VectorizePredicate60
||
152
128
               
Option == LoopHintAttr::Unroll47
||
153
128
               
Option == LoopHintAttr::Distribute20
||
154
128
               
Option == LoopHintAttr::PipelineDisabled4
) {
155
128
      assert(StateLoc && StateLoc->Ident && "Loop hint must have an argument");
156
128
      if (StateLoc->Ident->isStr("disable"))
157
61
        State = LoopHintAttr::Disable;
158
67
      else if (StateLoc->Ident->isStr("assume_safety"))
159
19
        State = LoopHintAttr::AssumeSafety;
160
48
      else if (StateLoc->Ident->isStr("full"))
161
8
        State = LoopHintAttr::Full;
162
40
      else if (StateLoc->Ident->isStr("enable"))
163
40
        State = LoopHintAttr::Enable;
164
40
      else
165
40
        
llvm_unreachable0
("bad loop hint argument");
166
128
    } else
167
128
      
llvm_unreachable0
("bad loop hint");
168
215
  }
169
266
170
266
  return LoopHintAttr::CreateImplicit(S.Context, Option, State, ValueExpr, A);
171
266
}
172
173
static void
174
CheckForIncompatibleAttributes(Sema &S,
175
4.68k
                               const SmallVectorImpl<const Attr *> &Attrs) {
176
4.68k
  // There are 6 categories of loop hints attributes: vectorize, interleave,
177
4.68k
  // unroll, unroll_and_jam, pipeline and distribute. Except for distribute they
178
4.68k
  // come in two variants: a state form and a numeric form.  The state form
179
4.68k
  // selectively defaults/enables/disables the transformation for the loop
180
4.68k
  // (for unroll, default indicates full unrolling rather than enabling the
181
4.68k
  // transformation). The numeric form form provides an integer hint (for
182
4.68k
  // example, unroll count) to the transformer. The following array accumulates
183
4.68k
  // the hints encountered while iterating through the attributes to check for
184
4.68k
  // compatibility.
185
4.68k
  struct {
186
4.68k
    const LoopHintAttr *StateAttr;
187
4.68k
    const LoopHintAttr *NumericAttr;
188
4.68k
  } HintAttrs[] = {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr},
189
4.68k
                   {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr},
190
4.68k
                   {nullptr, nullptr}};
191
4.68k
192
4.68k
  for (const auto *I : Attrs) {
193
1.20k
    const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I);
194
1.20k
195
1.20k
    // Skip non loop hint attributes
196
1.20k
    if (!LH)
197
936
      continue;
198
266
199
266
    LoopHintAttr::OptionType Option = LH->getOption();
200
266
    enum {
201
266
      Vectorize,
202
266
      Interleave,
203
266
      Unroll,
204
266
      UnrollAndJam,
205
266
      Distribute,
206
266
      Pipeline,
207
266
      VectorizePredicate
208
266
    } Category;
209
266
    switch (Option) {
210
80
    case LoopHintAttr::Vectorize:
211
80
    case LoopHintAttr::VectorizeWidth:
212
80
      Category = Vectorize;
213
80
      break;
214
80
    case LoopHintAttr::Interleave:
215
52
    case LoopHintAttr::InterleaveCount:
216
52
      Category = Interleave;
217
52
      break;
218
83
    case LoopHintAttr::Unroll:
219
83
    case LoopHintAttr::UnrollCount:
220
83
      Category = Unroll;
221
83
      break;
222
83
    case LoopHintAttr::UnrollAndJam:
223
13
    case LoopHintAttr::UnrollAndJamCount:
224
13
      Category = UnrollAndJam;
225
13
      break;
226
16
    case LoopHintAttr::Distribute:
227
16
      // Perform the check for duplicated 'distribute' hints.
228
16
      Category = Distribute;
229
16
      break;
230
13
    case LoopHintAttr::PipelineDisabled:
231
9
    case LoopHintAttr::PipelineInitiationInterval:
232
9
      Category = Pipeline;
233
9
      break;
234
13
    case LoopHintAttr::VectorizePredicate:
235
13
      Category = VectorizePredicate;
236
13
      break;
237
266
    };
238
266
239
266
    assert(Category < sizeof(HintAttrs) / sizeof(HintAttrs[0]));
240
266
    auto &CategoryState = HintAttrs[Category];
241
266
    const LoopHintAttr *PrevAttr;
242
266
    if (Option == LoopHintAttr::Vectorize ||
243
266
        
Option == LoopHintAttr::Interleave222
||
Option == LoopHintAttr::Unroll198
||
244
266
        
Option == LoopHintAttr::UnrollAndJam154
||
245
266
        
Option == LoopHintAttr::VectorizePredicate146
||
246
266
        
Option == LoopHintAttr::PipelineDisabled133
||
247
266
        
Option == LoopHintAttr::Distribute129
) {
248
153
      // Enable|Disable|AssumeSafety hint.  For example, vectorize(enable).
249
153
      PrevAttr = CategoryState.StateAttr;
250
153
      CategoryState.StateAttr = LH;
251
153
    } else {
252
113
      // Numeric hint.  For example, vectorize_width(8).
253
113
      PrevAttr = CategoryState.NumericAttr;
254
113
      CategoryState.NumericAttr = LH;
255
113
    }
256
266
257
266
    PrintingPolicy Policy(S.Context.getLangOpts());
258
266
    SourceLocation OptionLoc = LH->getRange().getBegin();
259
266
    if (PrevAttr)
260
14
      // Cannot specify same type of attribute twice.
261
14
      S.Diag(OptionLoc, diag::err_pragma_loop_compatibility)
262
14
          << /*Duplicate=*/true << PrevAttr->getDiagnosticName(Policy)
263
14
          << LH->getDiagnosticName(Policy);
264
266
265
266
    if (CategoryState.StateAttr && 
CategoryState.NumericAttr162
&&
266
266
        
(18
Category == Unroll18
||
Category == UnrollAndJam10
||
267
18
         
CategoryState.StateAttr->getState() == LoopHintAttr::Disable9
)) {
268
14
      // Disable hints are not compatible with numeric hints of the same
269
14
      // category.  As a special case, numeric unroll hints are also not
270
14
      // compatible with enable or full form of the unroll pragma because these
271
14
      // directives indicate full unrolling.
272
14
      S.Diag(OptionLoc, diag::err_pragma_loop_compatibility)
273
14
          << /*Duplicate=*/false
274
14
          << CategoryState.StateAttr->getDiagnosticName(Policy)
275
14
          << CategoryState.NumericAttr->getDiagnosticName(Policy);
276
14
    }
277
266
  }
278
4.68k
}
279
280
static Attr *handleOpenCLUnrollHint(Sema &S, Stmt *St, const ParsedAttr &A,
281
21
                                    SourceRange Range) {
282
21
  // Although the feature was introduced only in OpenCL C v2.0 s6.11.5, it's
283
21
  // useful for OpenCL 1.x too and doesn't require HW support.
284
21
  // opencl_unroll_hint can have 0 arguments (compiler
285
21
  // determines unrolling factor) or 1 argument (the unroll factor provided
286
21
  // by the user).
287
21
288
21
  unsigned NumArgs = A.getNumArgs();
289
21
290
21
  if (NumArgs > 1) {
291
1
    S.Diag(A.getLoc(), diag::err_attribute_too_many_arguments) << A << 1;
292
1
    return nullptr;
293
1
  }
294
20
295
20
  unsigned UnrollFactor = 0;
296
20
297
20
  if (NumArgs == 1) {
298
14
    Expr *E = A.getArgAsExpr(0);
299
14
    llvm::APSInt ArgVal(32);
300
14
301
14
    if (!E->isIntegerConstantExpr(ArgVal, S.Context)) {
302
1
      S.Diag(A.getLoc(), diag::err_attribute_argument_type)
303
1
          << A << AANT_ArgumentIntegerConstant << E->getSourceRange();
304
1
      return nullptr;
305
1
    }
306
13
307
13
    int Val = ArgVal.getSExtValue();
308
13
309
13
    if (Val <= 0) {
310
1
      S.Diag(A.getRange().getBegin(),
311
1
             diag::err_attribute_requires_positive_integer)
312
1
          << A << /* positive */ 0;
313
1
      return nullptr;
314
1
    }
315
12
    UnrollFactor = Val;
316
12
  }
317
20
318
20
  
return OpenCLUnrollHintAttr::CreateImplicit(S.Context, UnrollFactor)18
;
319
20
}
320
321
static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const ParsedAttr &A,
322
1.27k
                                  SourceRange Range) {
323
1.27k
  switch (A.getKind()) {
324
29
  case ParsedAttr::UnknownAttribute:
325
29
    S.Diag(A.getLoc(), A.isDeclspecAttribute()
326
29
                           ? 
(unsigned)diag::warn_unhandled_ms_attribute_ignored0
327
29
                           : (unsigned)diag::warn_unknown_attribute_ignored)
328
29
        << A;
329
29
    return nullptr;
330
920
  case ParsedAttr::AT_FallThrough:
331
920
    return handleFallThroughAttr(S, St, A, Range);
332
275
  case ParsedAttr::AT_LoopHint:
333
275
    return handleLoopHintAttr(S, St, A, Range);
334
21
  case ParsedAttr::AT_OpenCLUnrollHint:
335
21
    return handleOpenCLUnrollHint(S, St, A, Range);
336
4
  case ParsedAttr::AT_Suppress:
337
4
    return handleSuppressAttr(S, St, A, Range);
338
23
  default:
339
23
    // if we're here, then we parsed a known attribute, but didn't recognize
340
23
    // it as a statement attribute => it is declaration attribute
341
23
    S.Diag(A.getRange().getBegin(), diag::err_decl_attribute_invalid_on_stmt)
342
23
        << A << St->getBeginLoc();
343
23
    return nullptr;
344
1.27k
  }
345
1.27k
}
346
347
StmtResult Sema::ProcessStmtAttributes(Stmt *S,
348
                                       const ParsedAttributesView &AttrList,
349
4.68k
                                       SourceRange Range) {
350
4.68k
  SmallVector<const Attr*, 8> Attrs;
351
4.68k
  for (const ParsedAttr &AL : AttrList) {
352
1.27k
    if (Attr *a = ProcessStmtAttribute(*this, S, AL, Range))
353
1.20k
      Attrs.push_back(a);
354
1.27k
  }
355
4.68k
356
4.68k
  CheckForIncompatibleAttributes(*this, Attrs);
357
4.68k
358
4.68k
  if (Attrs.empty())
359
3.61k
    return S;
360
1.06k
361
1.06k
  return ActOnAttributedStmt(Range.getBegin(), Attrs, S);
362
1.06k
}