Coverage Report

Created: 2019-07-24 05:18

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