clang  10.0.0svn
SemaStmtAttr.cpp
Go to the documentation of this file.
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 
14 #include "clang/AST/ASTContext.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  SourceRange Range) {
26  FallThroughAttr Attr(S.Context, A);
27  if (!isa<NullStmt>(St)) {
28  S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_wrong_target)
29  << Attr.getSpelling() << St->getBeginLoc();
30  if (isa<SwitchCase>(St)) {
32  S.Diag(L, diag::note_fallthrough_insert_semi_fixit)
33  << FixItHint::CreateInsertion(L, ";");
34  }
35  return nullptr;
36  }
37  auto *FnScope = S.getCurFunction();
38  if (FnScope->SwitchStack.empty()) {
39  S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_outside_switch);
40  return nullptr;
41  }
42 
43  // If this is spelled as the standard C++17 attribute, but not in C++17, warn
44  // about using it as an extension.
45  if (!S.getLangOpts().CPlusPlus17 && A.isCXX11Attribute() &&
46  !A.getScopeName())
47  S.Diag(A.getLoc(), diag::ext_cxx17_attr) << A;
48 
49  FnScope->setHasFallthroughStmt();
50  return ::new (S.Context) FallThroughAttr(S.Context, A);
51 }
52 
53 static Attr *handleSuppressAttr(Sema &S, Stmt *St, const ParsedAttr &A,
54  SourceRange Range) {
55  if (A.getNumArgs() < 1) {
56  S.Diag(A.getLoc(), diag::err_attribute_too_few_arguments) << A << 1;
57  return nullptr;
58  }
59 
60  std::vector<StringRef> DiagnosticIdentifiers;
61  for (unsigned I = 0, E = A.getNumArgs(); I != E; ++I) {
62  StringRef RuleName;
63 
64  if (!S.checkStringLiteralArgumentAttr(A, I, RuleName, nullptr))
65  return nullptr;
66 
67  // FIXME: Warn if the rule name is unknown. This is tricky because only
68  // clang-tidy knows about available rules.
69  DiagnosticIdentifiers.push_back(RuleName);
70  }
71 
72  return ::new (S.Context) SuppressAttr(
73  S.Context, A, DiagnosticIdentifiers.data(), DiagnosticIdentifiers.size());
74 }
75 
76 static Attr *handleLoopHintAttr(Sema &S, Stmt *St, const ParsedAttr &A,
77  SourceRange) {
78  IdentifierLoc *PragmaNameLoc = A.getArgAsIdent(0);
79  IdentifierLoc *OptionLoc = A.getArgAsIdent(1);
80  IdentifierLoc *StateLoc = A.getArgAsIdent(2);
81  Expr *ValueExpr = A.getArgAsExpr(3);
82 
83  StringRef PragmaName =
84  llvm::StringSwitch<StringRef>(PragmaNameLoc->Ident->getName())
85  .Cases("unroll", "nounroll", "unroll_and_jam", "nounroll_and_jam",
86  PragmaNameLoc->Ident->getName())
87  .Default("clang loop");
88 
89  if (St->getStmtClass() != Stmt::DoStmtClass &&
90  St->getStmtClass() != Stmt::ForStmtClass &&
91  St->getStmtClass() != Stmt::CXXForRangeStmtClass &&
92  St->getStmtClass() != Stmt::WhileStmtClass) {
93  std::string Pragma = "#pragma " + std::string(PragmaName);
94  S.Diag(St->getBeginLoc(), diag::err_pragma_loop_precedes_nonloop) << Pragma;
95  return nullptr;
96  }
97 
98  LoopHintAttr::OptionType Option;
99  LoopHintAttr::LoopHintState State;
100 
101  auto SetHints = [&Option, &State](LoopHintAttr::OptionType O,
102  LoopHintAttr::LoopHintState S) {
103  Option = O;
104  State = S;
105  };
106 
107  if (PragmaName == "nounroll") {
108  SetHints(LoopHintAttr::Unroll, LoopHintAttr::Disable);
109  } else if (PragmaName == "unroll") {
110  // #pragma unroll N
111  if (ValueExpr)
112  SetHints(LoopHintAttr::UnrollCount, LoopHintAttr::Numeric);
113  else
114  SetHints(LoopHintAttr::Unroll, LoopHintAttr::Enable);
115  } else if (PragmaName == "nounroll_and_jam") {
116  SetHints(LoopHintAttr::UnrollAndJam, LoopHintAttr::Disable);
117  } else if (PragmaName == "unroll_and_jam") {
118  // #pragma unroll_and_jam N
119  if (ValueExpr)
120  SetHints(LoopHintAttr::UnrollAndJamCount, LoopHintAttr::Numeric);
121  else
122  SetHints(LoopHintAttr::UnrollAndJam, LoopHintAttr::Enable);
123  } else {
124  // #pragma clang loop ...
125  assert(OptionLoc && OptionLoc->Ident &&
126  "Attribute must have valid option info.");
127  Option = llvm::StringSwitch<LoopHintAttr::OptionType>(
128  OptionLoc->Ident->getName())
129  .Case("vectorize", LoopHintAttr::Vectorize)
130  .Case("vectorize_width", LoopHintAttr::VectorizeWidth)
131  .Case("interleave", LoopHintAttr::Interleave)
132  .Case("vectorize_predicate", LoopHintAttr::VectorizePredicate)
133  .Case("interleave_count", LoopHintAttr::InterleaveCount)
134  .Case("unroll", LoopHintAttr::Unroll)
135  .Case("unroll_count", LoopHintAttr::UnrollCount)
136  .Case("pipeline", LoopHintAttr::PipelineDisabled)
137  .Case("pipeline_initiation_interval",
138  LoopHintAttr::PipelineInitiationInterval)
139  .Case("distribute", LoopHintAttr::Distribute)
140  .Default(LoopHintAttr::Vectorize);
141  if (Option == LoopHintAttr::VectorizeWidth ||
142  Option == LoopHintAttr::InterleaveCount ||
143  Option == LoopHintAttr::UnrollCount ||
144  Option == LoopHintAttr::PipelineInitiationInterval) {
145  assert(ValueExpr && "Attribute must have a valid value expression.");
146  if (S.CheckLoopHintExpr(ValueExpr, St->getBeginLoc()))
147  return nullptr;
148  State = LoopHintAttr::Numeric;
149  } else if (Option == LoopHintAttr::Vectorize ||
150  Option == LoopHintAttr::Interleave ||
151  Option == LoopHintAttr::VectorizePredicate ||
152  Option == LoopHintAttr::Unroll ||
153  Option == LoopHintAttr::Distribute ||
154  Option == LoopHintAttr::PipelineDisabled) {
155  assert(StateLoc && StateLoc->Ident && "Loop hint must have an argument");
156  if (StateLoc->Ident->isStr("disable"))
157  State = LoopHintAttr::Disable;
158  else if (StateLoc->Ident->isStr("assume_safety"))
159  State = LoopHintAttr::AssumeSafety;
160  else if (StateLoc->Ident->isStr("full"))
161  State = LoopHintAttr::Full;
162  else if (StateLoc->Ident->isStr("enable"))
163  State = LoopHintAttr::Enable;
164  else
165  llvm_unreachable("bad loop hint argument");
166  } else
167  llvm_unreachable("bad loop hint");
168  }
169 
170  return LoopHintAttr::CreateImplicit(S.Context, Option, State, ValueExpr, A);
171 }
172 
173 static void
175  const SmallVectorImpl<const Attr *> &Attrs) {
176  // There are 6 categories of loop hints attributes: vectorize, interleave,
177  // unroll, unroll_and_jam, pipeline and distribute. Except for distribute they
178  // come in two variants: a state form and a numeric form. The state form
179  // selectively defaults/enables/disables the transformation for the loop
180  // (for unroll, default indicates full unrolling rather than enabling the
181  // transformation). The numeric form form provides an integer hint (for
182  // example, unroll count) to the transformer. The following array accumulates
183  // the hints encountered while iterating through the attributes to check for
184  // compatibility.
185  struct {
186  const LoopHintAttr *StateAttr;
187  const LoopHintAttr *NumericAttr;
188  } HintAttrs[] = {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr},
189  {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr},
190  {nullptr, nullptr}};
191 
192  for (const auto *I : Attrs) {
193  const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I);
194 
195  // Skip non loop hint attributes
196  if (!LH)
197  continue;
198 
199  LoopHintAttr::OptionType Option = LH->getOption();
200  enum {
201  Vectorize,
202  Interleave,
203  Unroll,
204  UnrollAndJam,
205  Distribute,
206  Pipeline,
207  VectorizePredicate
208  } Category;
209  switch (Option) {
210  case LoopHintAttr::Vectorize:
211  case LoopHintAttr::VectorizeWidth:
212  Category = Vectorize;
213  break;
214  case LoopHintAttr::Interleave:
215  case LoopHintAttr::InterleaveCount:
216  Category = Interleave;
217  break;
218  case LoopHintAttr::Unroll:
219  case LoopHintAttr::UnrollCount:
220  Category = Unroll;
221  break;
222  case LoopHintAttr::UnrollAndJam:
223  case LoopHintAttr::UnrollAndJamCount:
224  Category = UnrollAndJam;
225  break;
226  case LoopHintAttr::Distribute:
227  // Perform the check for duplicated 'distribute' hints.
228  Category = Distribute;
229  break;
230  case LoopHintAttr::PipelineDisabled:
231  case LoopHintAttr::PipelineInitiationInterval:
232  Category = Pipeline;
233  break;
234  case LoopHintAttr::VectorizePredicate:
235  Category = VectorizePredicate;
236  break;
237  };
238 
239  assert(Category < sizeof(HintAttrs) / sizeof(HintAttrs[0]));
240  auto &CategoryState = HintAttrs[Category];
241  const LoopHintAttr *PrevAttr;
242  if (Option == LoopHintAttr::Vectorize ||
243  Option == LoopHintAttr::Interleave || Option == LoopHintAttr::Unroll ||
244  Option == LoopHintAttr::UnrollAndJam ||
245  Option == LoopHintAttr::VectorizePredicate ||
246  Option == LoopHintAttr::PipelineDisabled ||
247  Option == LoopHintAttr::Distribute) {
248  // Enable|Disable|AssumeSafety hint. For example, vectorize(enable).
249  PrevAttr = CategoryState.StateAttr;
250  CategoryState.StateAttr = LH;
251  } else {
252  // Numeric hint. For example, vectorize_width(8).
253  PrevAttr = CategoryState.NumericAttr;
254  CategoryState.NumericAttr = LH;
255  }
256 
257  PrintingPolicy Policy(S.Context.getLangOpts());
258  SourceLocation OptionLoc = LH->getRange().getBegin();
259  if (PrevAttr)
260  // Cannot specify same type of attribute twice.
261  S.Diag(OptionLoc, diag::err_pragma_loop_compatibility)
262  << /*Duplicate=*/true << PrevAttr->getDiagnosticName(Policy)
263  << LH->getDiagnosticName(Policy);
264 
265  if (CategoryState.StateAttr && CategoryState.NumericAttr &&
266  (Category == Unroll || Category == UnrollAndJam ||
267  CategoryState.StateAttr->getState() == LoopHintAttr::Disable)) {
268  // Disable hints are not compatible with numeric hints of the same
269  // category. As a special case, numeric unroll hints are also not
270  // compatible with enable or full form of the unroll pragma because these
271  // directives indicate full unrolling.
272  S.Diag(OptionLoc, diag::err_pragma_loop_compatibility)
273  << /*Duplicate=*/false
274  << CategoryState.StateAttr->getDiagnosticName(Policy)
275  << CategoryState.NumericAttr->getDiagnosticName(Policy);
276  }
277  }
278 }
279 
280 static Attr *handleOpenCLUnrollHint(Sema &S, Stmt *St, const ParsedAttr &A,
281  SourceRange Range) {
282  // Although the feature was introduced only in OpenCL C v2.0 s6.11.5, it's
283  // useful for OpenCL 1.x too and doesn't require HW support.
284  // opencl_unroll_hint can have 0 arguments (compiler
285  // determines unrolling factor) or 1 argument (the unroll factor provided
286  // by the user).
287 
288  unsigned NumArgs = A.getNumArgs();
289 
290  if (NumArgs > 1) {
291  S.Diag(A.getLoc(), diag::err_attribute_too_many_arguments) << A << 1;
292  return nullptr;
293  }
294 
295  unsigned UnrollFactor = 0;
296 
297  if (NumArgs == 1) {
298  Expr *E = A.getArgAsExpr(0);
299  llvm::APSInt ArgVal(32);
300 
301  if (!E->isIntegerConstantExpr(ArgVal, S.Context)) {
302  S.Diag(A.getLoc(), diag::err_attribute_argument_type)
304  return nullptr;
305  }
306 
307  int Val = ArgVal.getSExtValue();
308 
309  if (Val <= 0) {
310  S.Diag(A.getRange().getBegin(),
311  diag::err_attribute_requires_positive_integer)
312  << A << /* positive */ 0;
313  return nullptr;
314  }
315  UnrollFactor = Val;
316  }
317 
318  return OpenCLUnrollHintAttr::CreateImplicit(S.Context, UnrollFactor);
319 }
320 
321 static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const ParsedAttr &A,
322  SourceRange Range) {
323  switch (A.getKind()) {
325  S.Diag(A.getLoc(), A.isDeclspecAttribute()
326  ? (unsigned)diag::warn_unhandled_ms_attribute_ignored
327  : (unsigned)diag::warn_unknown_attribute_ignored)
328  << A;
329  return nullptr;
330  case ParsedAttr::AT_FallThrough:
331  return handleFallThroughAttr(S, St, A, Range);
332  case ParsedAttr::AT_LoopHint:
333  return handleLoopHintAttr(S, St, A, Range);
334  case ParsedAttr::AT_OpenCLUnrollHint:
335  return handleOpenCLUnrollHint(S, St, A, Range);
336  case ParsedAttr::AT_Suppress:
337  return handleSuppressAttr(S, St, A, Range);
338  default:
339  // if we're here, then we parsed a known attribute, but didn't recognize
340  // it as a statement attribute => it is declaration attribute
341  S.Diag(A.getRange().getBegin(), diag::err_decl_attribute_invalid_on_stmt)
342  << A << St->getBeginLoc();
343  return nullptr;
344  }
345 }
346 
348  const ParsedAttributesView &AttrList,
349  SourceRange Range) {
351  for (const ParsedAttr &AL : AttrList) {
352  if (Attr *a = ProcessStmtAttribute(*this, S, AL, Range))
353  Attrs.push_back(a);
354  }
355 
356  CheckForIncompatibleAttributes(*this, Attrs);
357 
358  if (Attrs.empty())
359  return S;
360 
361  return ActOnAttributedStmt(Range.getBegin(), Attrs, S);
362 }
Defines the clang::ASTContext interface.
bool CheckLoopHintExpr(Expr *E, SourceLocation Loc)
Definition: SemaExpr.cpp:3363
SourceLocation getLoc() const
static void CheckForIncompatibleAttributes(Sema &S, const SmallVectorImpl< const Attr *> &Attrs)
Stmt - This represents one statement.
Definition: Stmt.h:66
Defines the SourceManager interface.
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID)
Emit a diagnostic.
Definition: Sema.h:1375
IdentifierInfo * Ident
Definition: ParsedAttr.h:97
Expr * getArgAsExpr(unsigned Arg) const
Definition: ParsedAttr.h:383
SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset=0)
Calls Lexer::getLocForEndOfToken()
Definition: Sema.cpp:49
static Attr * handleFallThroughAttr(Sema &S, Stmt *St, const ParsedAttr &A, SourceRange Range)
Describes how types, statements, expressions, and declarations should be printed. ...
Definition: PrettyPrinter.h:37
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
LineState State
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Stmt.cpp:274
int Category
Definition: Format.cpp:1810
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this attribute.
Definition: ParsedAttr.h:371
AttributeCommonInfo::Kind getKind() const
Definition: ParsedAttr.h:537
const LangOptions & getLangOpts() const
Definition: Sema.h:1288
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:331
static Attr * handleSuppressAttr(Sema &S, Stmt *St, const ParsedAttr &A, SourceRange Range)
This represents one expression.
Definition: Expr.h:108
Defines the classes clang::DelayedDiagnostic and clang::AccessedEntity.
SourceLocation getEnd() const
Wraps an identifier and optional source location for the identifier.
Definition: ParsedAttr.h:95
static Attr * ProcessStmtAttribute(Sema &S, Stmt *St, const ParsedAttr &A, SourceRange Range)
ActionResult - This structure is used while parsing/acting on expressions, stmts, etc...
Definition: Ownership.h:153
Encodes a location in the source.
llvm::APSInt APSInt
ParsedAttr - Represents a syntactic attribute.
Definition: ParsedAttr.h:117
StringRef getName() const
Return the actual identifier string.
Dataflow Directional Tag Classes.
StmtClass getStmtClass() const
Definition: Stmt.h:1097
bool isIntegerConstantExpr(llvm::APSInt &Result, const ASTContext &Ctx, SourceLocation *Loc=nullptr, bool isEvaluated=true) const
isIntegerConstantExpr - Return true if this expression is a valid integer constant expression...
static FixItHint CreateInsertion(SourceLocation InsertionLoc, StringRef Code, bool BeforePreviousInsertions=false)
Create a code modification hint that inserts the given code string at a specific location.
Definition: Diagnostic.h:92
StmtResult ProcessStmtAttributes(Stmt *Stmt, const ParsedAttributesView &Attrs, SourceRange Range)
Stmt attributes - this routine is the top level dispatcher.
sema::FunctionScopeInfo * getCurFunction() const
Definition: Sema.h:1452
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition: Stmt.cpp:262
const IdentifierInfo * getScopeName() const
static Attr * handleOpenCLUnrollHint(Sema &S, Stmt *St, const ParsedAttr &A, SourceRange Range)
bool checkStringLiteralArgumentAttr(const ParsedAttr &Attr, unsigned ArgNum, StringRef &Str, SourceLocation *ArgLocation=nullptr)
Check if the argument ArgNum of Attr is a ASCII string literal.
A trivial tuple used to represent a source range.
ASTContext & Context
Definition: Sema.h:377
SourceLocation getBegin() const
const LangOptions & getLangOpts() const
Definition: ASTContext.h:723
IdentifierLoc * getArgAsIdent(unsigned Arg) const
Definition: ParsedAttr.h:391
static Attr * handleLoopHintAttr(Sema &S, Stmt *St, const ParsedAttr &A, SourceRange)
Attr - This represents one attribute.
Definition: Attr.h:45