blob: 4ee3412170a85cb641cc6f3109d4a4de5cf95437 [file] [log] [blame]
Richard Smithc202b282012-04-14 00:33:13 +00001//===--- SemaStmtAttr.cpp - Statement Attribute Handling ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements stmt-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Sema/SemaInternal.h"
Richard Smithc202b282012-04-14 00:33:13 +000015#include "clang/AST/ASTContext.h"
Eli Bendersky06a40422014-06-06 20:31:48 +000016#include "clang/Basic/SourceManager.h"
17#include "clang/Sema/DelayedDiagnostic.h"
18#include "clang/Sema/Lookup.h"
19#include "clang/Sema/LoopHint.h"
20#include "clang/Sema/ScopeInfo.h"
21#include "llvm/ADT/StringExtras.h"
22
Richard Smithc202b282012-04-14 00:33:13 +000023using namespace clang;
24using namespace sema;
25
Richard Smith84837d52012-05-03 18:27:39 +000026static Attr *handleFallThroughAttr(Sema &S, Stmt *St, const AttributeList &A,
27 SourceRange Range) {
Richard Smith4f902c72016-03-08 00:32:55 +000028 FallThroughAttr Attr(A.getRange(), S.Context,
29 A.getAttributeSpellingListIndex());
Richard Smith84837d52012-05-03 18:27:39 +000030 if (!isa<NullStmt>(St)) {
31 S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_wrong_target)
Richard Smith4f902c72016-03-08 00:32:55 +000032 << Attr.getSpelling() << St->getLocStart();
Richard Smith84837d52012-05-03 18:27:39 +000033 if (isa<SwitchCase>(St)) {
Alp Tokerb6cc5922014-05-03 03:45:55 +000034 SourceLocation L = S.getLocForEndOfToken(Range.getEnd());
Richard Smith84837d52012-05-03 18:27:39 +000035 S.Diag(L, diag::note_fallthrough_insert_semi_fixit)
36 << FixItHint::CreateInsertion(L, ";");
37 }
Craig Topperc3ec1492014-05-26 06:22:03 +000038 return nullptr;
Richard Smith84837d52012-05-03 18:27:39 +000039 }
Richard Smith4f902c72016-03-08 00:32:55 +000040 auto *FnScope = S.getCurFunction();
41 if (FnScope->SwitchStack.empty()) {
Richard Smith84837d52012-05-03 18:27:39 +000042 S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_outside_switch);
Craig Topperc3ec1492014-05-26 06:22:03 +000043 return nullptr;
Richard Smith84837d52012-05-03 18:27:39 +000044 }
Richard Smith4f902c72016-03-08 00:32:55 +000045
46 // If this is spelled as the standard C++1z attribute, but not in C++1z, warn
47 // about using it as an extension.
48 if (!S.getLangOpts().CPlusPlus1z && A.isCXX11Attribute() &&
49 !A.getScopeName())
50 S.Diag(A.getLoc(), diag::ext_cxx1z_attr) << A.getName();
51
52 FnScope->setHasFallthroughStmt();
53 return ::new (S.Context) auto(Attr);
Eli Bendersky06a40422014-06-06 20:31:48 +000054}
55
Matthias Gehre01a63382017-03-27 19:45:24 +000056static Attr *handleSuppressAttr(Sema &S, Stmt *St, const AttributeList &A,
57 SourceRange Range) {
58 if (A.getNumArgs() < 1) {
59 S.Diag(A.getLoc(), diag::err_attribute_too_few_arguments)
60 << A.getName() << 1;
61 return nullptr;
62 }
63
64 std::vector<StringRef> DiagnosticIdentifiers;
65 for (unsigned I = 0, E = A.getNumArgs(); I != E; ++I) {
66 StringRef RuleName;
67
68 if (!S.checkStringLiteralArgumentAttr(A, I, RuleName, nullptr))
69 return nullptr;
70
71 // FIXME: Warn if the rule name is unknown. This is tricky because only
72 // clang-tidy knows about available rules.
73 DiagnosticIdentifiers.push_back(RuleName);
74 }
75
76 return ::new (S.Context) SuppressAttr(
77 A.getRange(), S.Context, DiagnosticIdentifiers.data(),
78 DiagnosticIdentifiers.size(), A.getAttributeSpellingListIndex());
79}
80
Eli Bendersky06a40422014-06-06 20:31:48 +000081static Attr *handleLoopHintAttr(Sema &S, Stmt *St, const AttributeList &A,
82 SourceRange) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +000083 IdentifierLoc *PragmaNameLoc = A.getArgAsIdent(0);
84 IdentifierLoc *OptionLoc = A.getArgAsIdent(1);
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +000085 IdentifierLoc *StateLoc = A.getArgAsIdent(2);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +000086 Expr *ValueExpr = A.getArgAsExpr(3);
87
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +000088 bool PragmaUnroll = PragmaNameLoc->Ident->getName() == "unroll";
89 bool PragmaNoUnroll = PragmaNameLoc->Ident->getName() == "nounroll";
Eli Bendersky06a40422014-06-06 20:31:48 +000090 if (St->getStmtClass() != Stmt::DoStmtClass &&
91 St->getStmtClass() != Stmt::ForStmtClass &&
92 St->getStmtClass() != Stmt::CXXForRangeStmtClass &&
93 St->getStmtClass() != Stmt::WhileStmtClass) {
Mark Heffernanc888e412014-07-24 18:09:38 +000094 const char *Pragma =
95 llvm::StringSwitch<const char *>(PragmaNameLoc->Ident->getName())
96 .Case("unroll", "#pragma unroll")
97 .Case("nounroll", "#pragma nounroll")
98 .Default("#pragma clang loop");
Mark Heffernanbd26f5e2014-07-21 18:08:34 +000099 S.Diag(St->getLocStart(), diag::err_pragma_loop_precedes_nonloop) << Pragma;
Eli Bendersky06a40422014-06-06 20:31:48 +0000100 return nullptr;
101 }
102
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000103 LoopHintAttr::Spelling Spelling;
Mark Heffernan397a98d2015-08-10 17:29:39 +0000104 LoopHintAttr::OptionType Option;
105 LoopHintAttr::LoopHintState State;
106 if (PragmaNoUnroll) {
107 // #pragma nounroll
Mark Heffernanc888e412014-07-24 18:09:38 +0000108 Spelling = LoopHintAttr::Pragma_nounroll;
Mark Heffernan397a98d2015-08-10 17:29:39 +0000109 Option = LoopHintAttr::Unroll;
110 State = LoopHintAttr::Disable;
111 } else if (PragmaUnroll) {
112 Spelling = LoopHintAttr::Pragma_unroll;
113 if (ValueExpr) {
114 // #pragma unroll N
115 Option = LoopHintAttr::UnrollCount;
116 State = LoopHintAttr::Numeric;
117 } else {
118 // #pragma unroll
119 Option = LoopHintAttr::Unroll;
120 State = LoopHintAttr::Enable;
121 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000122 } else {
Mark Heffernan397a98d2015-08-10 17:29:39 +0000123 // #pragma clang loop ...
124 Spelling = LoopHintAttr::Pragma_clang_loop;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000125 assert(OptionLoc && OptionLoc->Ident &&
126 "Attribute must have valid option info.");
Mark Heffernan397a98d2015-08-10 17:29:39 +0000127 Option = llvm::StringSwitch<LoopHintAttr::OptionType>(
128 OptionLoc->Ident->getName())
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000129 .Case("vectorize", LoopHintAttr::Vectorize)
130 .Case("vectorize_width", LoopHintAttr::VectorizeWidth)
131 .Case("interleave", LoopHintAttr::Interleave)
132 .Case("interleave_count", LoopHintAttr::InterleaveCount)
133 .Case("unroll", LoopHintAttr::Unroll)
134 .Case("unroll_count", LoopHintAttr::UnrollCount)
Adam Nemet2de463e2016-06-14 12:04:26 +0000135 .Case("distribute", LoopHintAttr::Distribute)
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000136 .Default(LoopHintAttr::Vectorize);
Mark Heffernan397a98d2015-08-10 17:29:39 +0000137 if (Option == LoopHintAttr::VectorizeWidth ||
138 Option == LoopHintAttr::InterleaveCount ||
139 Option == LoopHintAttr::UnrollCount) {
140 assert(ValueExpr && "Attribute must have a valid value expression.");
141 if (S.CheckLoopHintExpr(ValueExpr, St->getLocStart()))
142 return nullptr;
143 State = LoopHintAttr::Numeric;
144 } else if (Option == LoopHintAttr::Vectorize ||
145 Option == LoopHintAttr::Interleave ||
Adam Nemet2de463e2016-06-14 12:04:26 +0000146 Option == LoopHintAttr::Unroll ||
147 Option == LoopHintAttr::Distribute) {
Mark Heffernan397a98d2015-08-10 17:29:39 +0000148 assert(StateLoc && StateLoc->Ident && "Loop hint must have an argument");
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000149 if (StateLoc->Ident->isStr("disable"))
150 State = LoopHintAttr::Disable;
Tyler Nowicki9d268e12015-06-11 23:23:17 +0000151 else if (StateLoc->Ident->isStr("assume_safety"))
152 State = LoopHintAttr::AssumeSafety;
Mark Heffernan397a98d2015-08-10 17:29:39 +0000153 else if (StateLoc->Ident->isStr("full"))
154 State = LoopHintAttr::Full;
155 else if (StateLoc->Ident->isStr("enable"))
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000156 State = LoopHintAttr::Enable;
Mark Heffernan397a98d2015-08-10 17:29:39 +0000157 else
158 llvm_unreachable("bad loop hint argument");
159 } else
160 llvm_unreachable("bad loop hint");
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000161 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000162
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000163 return LoopHintAttr::CreateImplicit(S.Context, Spelling, Option, State,
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000164 ValueExpr, A.getRange());
Eli Bendersky06a40422014-06-06 20:31:48 +0000165}
166
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000167static void
168CheckForIncompatibleAttributes(Sema &S,
169 const SmallVectorImpl<const Attr *> &Attrs) {
Adam Nemet2de463e2016-06-14 12:04:26 +0000170 // There are 4 categories of loop hints attributes: vectorize, interleave,
171 // unroll and distribute. Except for distribute they come in two variants: a
172 // state form and a numeric form. The state form selectively
173 // defaults/enables/disables the transformation for the loop (for unroll,
174 // default indicates full unrolling rather than enabling the transformation).
175 // The numeric form form provides an integer hint (for example, unroll count)
176 // to the transformer. The following array accumulates the hints encountered
177 // while iterating through the attributes to check for compatibility.
Eli Bendersky86483b32014-06-11 17:56:26 +0000178 struct {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000179 const LoopHintAttr *StateAttr;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000180 const LoopHintAttr *NumericAttr;
Adam Nemet2de463e2016-06-14 12:04:26 +0000181 } HintAttrs[] = {{nullptr, nullptr},
182 {nullptr, nullptr},
183 {nullptr, nullptr},
184 {nullptr, nullptr}};
Eli Bendersky06a40422014-06-06 20:31:48 +0000185
186 for (const auto *I : Attrs) {
187 const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I);
188
189 // Skip non loop hint attributes
190 if (!LH)
191 continue;
192
Craig Topperec9be542015-12-23 05:44:43 +0000193 LoopHintAttr::OptionType Option = LH->getOption();
Adam Nemet2de463e2016-06-14 12:04:26 +0000194 enum { Vectorize, Interleave, Unroll, Distribute } Category;
Eli Bendersky06a40422014-06-06 20:31:48 +0000195 switch (Option) {
196 case LoopHintAttr::Vectorize:
197 case LoopHintAttr::VectorizeWidth:
Mark Heffernan450c2382014-07-23 17:31:31 +0000198 Category = Vectorize;
Eli Bendersky06a40422014-06-06 20:31:48 +0000199 break;
200 case LoopHintAttr::Interleave:
201 case LoopHintAttr::InterleaveCount:
Mark Heffernan450c2382014-07-23 17:31:31 +0000202 Category = Interleave;
Eli Bendersky06a40422014-06-06 20:31:48 +0000203 break;
Eli Bendersky86483b32014-06-11 17:56:26 +0000204 case LoopHintAttr::Unroll:
205 case LoopHintAttr::UnrollCount:
Mark Heffernan450c2382014-07-23 17:31:31 +0000206 Category = Unroll;
Eli Bendersky86483b32014-06-11 17:56:26 +0000207 break;
Adam Nemet2de463e2016-06-14 12:04:26 +0000208 case LoopHintAttr::Distribute:
209 // Perform the check for duplicated 'distribute' hints.
210 Category = Distribute;
211 break;
Eli Bendersky86483b32014-06-11 17:56:26 +0000212 };
Eli Bendersky06a40422014-06-06 20:31:48 +0000213
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000214 auto &CategoryState = HintAttrs[Category];
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000215 const LoopHintAttr *PrevAttr;
Eli Bendersky86483b32014-06-11 17:56:26 +0000216 if (Option == LoopHintAttr::Vectorize ||
Adam Nemet2de463e2016-06-14 12:04:26 +0000217 Option == LoopHintAttr::Interleave || Option == LoopHintAttr::Unroll ||
218 Option == LoopHintAttr::Distribute) {
Tyler Nowicki9d268e12015-06-11 23:23:17 +0000219 // Enable|Disable|AssumeSafety hint. For example, vectorize(enable).
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000220 PrevAttr = CategoryState.StateAttr;
221 CategoryState.StateAttr = LH;
Eli Bendersky06a40422014-06-06 20:31:48 +0000222 } else {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000223 // Numeric hint. For example, vectorize_width(8).
224 PrevAttr = CategoryState.NumericAttr;
225 CategoryState.NumericAttr = LH;
Eli Bendersky06a40422014-06-06 20:31:48 +0000226 }
227
Tyler Nowicki884fc612014-07-29 17:21:32 +0000228 PrintingPolicy Policy(S.Context.getLangOpts());
229 SourceLocation OptionLoc = LH->getRange().getBegin();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000230 if (PrevAttr)
231 // Cannot specify same type of attribute twice.
232 S.Diag(OptionLoc, diag::err_pragma_loop_compatibility)
Tyler Nowicki884fc612014-07-29 17:21:32 +0000233 << /*Duplicate=*/true << PrevAttr->getDiagnosticName(Policy)
234 << LH->getDiagnosticName(Policy);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000235
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000236 if (CategoryState.StateAttr && CategoryState.NumericAttr &&
237 (Category == Unroll ||
238 CategoryState.StateAttr->getState() == LoopHintAttr::Disable)) {
Mark Heffernan450c2382014-07-23 17:31:31 +0000239 // Disable hints are not compatible with numeric hints of the same
240 // category. As a special case, numeric unroll hints are also not
Mark Heffernan397a98d2015-08-10 17:29:39 +0000241 // compatible with enable or full form of the unroll pragma because these
242 // directives indicate full unrolling.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000243 S.Diag(OptionLoc, diag::err_pragma_loop_compatibility)
Eli Bendersky86483b32014-06-11 17:56:26 +0000244 << /*Duplicate=*/false
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000245 << CategoryState.StateAttr->getDiagnosticName(Policy)
Tyler Nowicki884fc612014-07-29 17:21:32 +0000246 << CategoryState.NumericAttr->getDiagnosticName(Policy);
Eli Bendersky86483b32014-06-11 17:56:26 +0000247 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000248 }
249}
250
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +0000251static Attr *handleOpenCLUnrollHint(Sema &S, Stmt *St, const AttributeList &A,
252 SourceRange Range) {
Egor Churaev24939d42016-12-13 14:02:35 +0000253 // Although the feature was introduced only in OpenCL C v2.0 s6.11.5, it's
254 // useful for OpenCL 1.x too and doesn't require HW support.
255 // opencl_unroll_hint can have 0 arguments (compiler
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +0000256 // determines unrolling factor) or 1 argument (the unroll factor provided
257 // by the user).
258
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +0000259 unsigned NumArgs = A.getNumArgs();
260
261 if (NumArgs > 1) {
262 S.Diag(A.getLoc(), diag::err_attribute_too_many_arguments) << A.getName()
263 << 1;
264 return nullptr;
265 }
266
267 unsigned UnrollFactor = 0;
268
269 if (NumArgs == 1) {
270 Expr *E = A.getArgAsExpr(0);
271 llvm::APSInt ArgVal(32);
272
273 if (!E->isIntegerConstantExpr(ArgVal, S.Context)) {
274 S.Diag(A.getLoc(), diag::err_attribute_argument_type)
275 << A.getName() << AANT_ArgumentIntegerConstant << E->getSourceRange();
276 return nullptr;
277 }
278
279 int Val = ArgVal.getSExtValue();
280
281 if (Val <= 0) {
282 S.Diag(A.getRange().getBegin(),
283 diag::err_attribute_requires_positive_integer)
284 << A.getName();
285 return nullptr;
286 }
287 UnrollFactor = Val;
288 }
289
290 return OpenCLUnrollHintAttr::CreateImplicit(S.Context, UnrollFactor);
291}
292
Eli Bendersky06a40422014-06-06 20:31:48 +0000293static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const AttributeList &A,
294 SourceRange Range) {
295 switch (A.getKind()) {
Michael Han23214e52012-10-03 01:56:22 +0000296 case AttributeList::UnknownAttribute:
297 S.Diag(A.getLoc(), A.isDeclspecAttribute() ?
298 diag::warn_unhandled_ms_attribute_ignored :
299 diag::warn_unknown_attribute_ignored) << A.getName();
Eli Bendersky06a40422014-06-06 20:31:48 +0000300 return nullptr;
301 case AttributeList::AT_FallThrough:
302 return handleFallThroughAttr(S, St, A, Range);
303 case AttributeList::AT_LoopHint:
304 return handleLoopHintAttr(S, St, A, Range);
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +0000305 case AttributeList::AT_OpenCLUnrollHint:
306 return handleOpenCLUnrollHint(S, St, A, Range);
Matthias Gehre01a63382017-03-27 19:45:24 +0000307 case AttributeList::AT_Suppress:
308 return handleSuppressAttr(S, St, A, Range);
Eli Bendersky06a40422014-06-06 20:31:48 +0000309 default:
310 // if we're here, then we parsed a known attribute, but didn't recognize
311 // it as a statement attribute => it is declaration attribute
Richard Smith4f902c72016-03-08 00:32:55 +0000312 S.Diag(A.getRange().getBegin(), diag::err_decl_attribute_invalid_on_stmt)
Richard Smith4c96e992013-02-19 23:47:15 +0000313 << A.getName() << St->getLocStart();
Craig Topperc3ec1492014-05-26 06:22:03 +0000314 return nullptr;
Richard Smithc202b282012-04-14 00:33:13 +0000315 }
316}
317
318StmtResult Sema::ProcessStmtAttributes(Stmt *S, AttributeList *AttrList,
319 SourceRange Range) {
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000320 SmallVector<const Attr*, 8> Attrs;
Richard Smithc202b282012-04-14 00:33:13 +0000321 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Richard Smith84837d52012-05-03 18:27:39 +0000322 if (Attr *a = ProcessStmtAttribute(*this, S, *l, Range))
Eli Bendersky06a40422014-06-06 20:31:48 +0000323 Attrs.push_back(a);
324 }
325
326 CheckForIncompatibleAttributes(*this, Attrs);
327
328 if (Attrs.empty())
329 return S;
330
Richard Smithc202b282012-04-14 00:33:13 +0000331 return ActOnAttributedStmt(Range.getBegin(), Attrs, S);
332}