blob: 5120a8773c57ece9fa29bd76a87b105cae93ae2f [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
56static Attr *handleLoopHintAttr(Sema &S, Stmt *St, const AttributeList &A,
57 SourceRange) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +000058 IdentifierLoc *PragmaNameLoc = A.getArgAsIdent(0);
59 IdentifierLoc *OptionLoc = A.getArgAsIdent(1);
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +000060 IdentifierLoc *StateLoc = A.getArgAsIdent(2);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +000061 Expr *ValueExpr = A.getArgAsExpr(3);
62
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +000063 bool PragmaUnroll = PragmaNameLoc->Ident->getName() == "unroll";
64 bool PragmaNoUnroll = PragmaNameLoc->Ident->getName() == "nounroll";
Eli Bendersky06a40422014-06-06 20:31:48 +000065 if (St->getStmtClass() != Stmt::DoStmtClass &&
66 St->getStmtClass() != Stmt::ForStmtClass &&
67 St->getStmtClass() != Stmt::CXXForRangeStmtClass &&
68 St->getStmtClass() != Stmt::WhileStmtClass) {
Mark Heffernanc888e412014-07-24 18:09:38 +000069 const char *Pragma =
70 llvm::StringSwitch<const char *>(PragmaNameLoc->Ident->getName())
71 .Case("unroll", "#pragma unroll")
72 .Case("nounroll", "#pragma nounroll")
73 .Default("#pragma clang loop");
Mark Heffernanbd26f5e2014-07-21 18:08:34 +000074 S.Diag(St->getLocStart(), diag::err_pragma_loop_precedes_nonloop) << Pragma;
Eli Bendersky06a40422014-06-06 20:31:48 +000075 return nullptr;
76 }
77
Mark Heffernanbd26f5e2014-07-21 18:08:34 +000078 LoopHintAttr::Spelling Spelling;
Mark Heffernan397a98d2015-08-10 17:29:39 +000079 LoopHintAttr::OptionType Option;
80 LoopHintAttr::LoopHintState State;
81 if (PragmaNoUnroll) {
82 // #pragma nounroll
Mark Heffernanc888e412014-07-24 18:09:38 +000083 Spelling = LoopHintAttr::Pragma_nounroll;
Mark Heffernan397a98d2015-08-10 17:29:39 +000084 Option = LoopHintAttr::Unroll;
85 State = LoopHintAttr::Disable;
86 } else if (PragmaUnroll) {
87 Spelling = LoopHintAttr::Pragma_unroll;
88 if (ValueExpr) {
89 // #pragma unroll N
90 Option = LoopHintAttr::UnrollCount;
91 State = LoopHintAttr::Numeric;
92 } else {
93 // #pragma unroll
94 Option = LoopHintAttr::Unroll;
95 State = LoopHintAttr::Enable;
96 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +000097 } else {
Mark Heffernan397a98d2015-08-10 17:29:39 +000098 // #pragma clang loop ...
99 Spelling = LoopHintAttr::Pragma_clang_loop;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000100 assert(OptionLoc && OptionLoc->Ident &&
101 "Attribute must have valid option info.");
Mark Heffernan397a98d2015-08-10 17:29:39 +0000102 Option = llvm::StringSwitch<LoopHintAttr::OptionType>(
103 OptionLoc->Ident->getName())
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000104 .Case("vectorize", LoopHintAttr::Vectorize)
105 .Case("vectorize_width", LoopHintAttr::VectorizeWidth)
106 .Case("interleave", LoopHintAttr::Interleave)
107 .Case("interleave_count", LoopHintAttr::InterleaveCount)
108 .Case("unroll", LoopHintAttr::Unroll)
109 .Case("unroll_count", LoopHintAttr::UnrollCount)
110 .Default(LoopHintAttr::Vectorize);
Mark Heffernan397a98d2015-08-10 17:29:39 +0000111 if (Option == LoopHintAttr::VectorizeWidth ||
112 Option == LoopHintAttr::InterleaveCount ||
113 Option == LoopHintAttr::UnrollCount) {
114 assert(ValueExpr && "Attribute must have a valid value expression.");
115 if (S.CheckLoopHintExpr(ValueExpr, St->getLocStart()))
116 return nullptr;
117 State = LoopHintAttr::Numeric;
118 } else if (Option == LoopHintAttr::Vectorize ||
119 Option == LoopHintAttr::Interleave ||
120 Option == LoopHintAttr::Unroll) {
121 assert(StateLoc && StateLoc->Ident && "Loop hint must have an argument");
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000122 if (StateLoc->Ident->isStr("disable"))
123 State = LoopHintAttr::Disable;
Tyler Nowicki9d268e12015-06-11 23:23:17 +0000124 else if (StateLoc->Ident->isStr("assume_safety"))
125 State = LoopHintAttr::AssumeSafety;
Mark Heffernan397a98d2015-08-10 17:29:39 +0000126 else if (StateLoc->Ident->isStr("full"))
127 State = LoopHintAttr::Full;
128 else if (StateLoc->Ident->isStr("enable"))
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000129 State = LoopHintAttr::Enable;
Mark Heffernan397a98d2015-08-10 17:29:39 +0000130 else
131 llvm_unreachable("bad loop hint argument");
132 } else
133 llvm_unreachable("bad loop hint");
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000134 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000135
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000136 return LoopHintAttr::CreateImplicit(S.Context, Spelling, Option, State,
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000137 ValueExpr, A.getRange());
Eli Bendersky06a40422014-06-06 20:31:48 +0000138}
139
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000140static void
141CheckForIncompatibleAttributes(Sema &S,
142 const SmallVectorImpl<const Attr *> &Attrs) {
143 // There are 3 categories of loop hints attributes: vectorize, interleave,
144 // and unroll. Each comes in two variants: a state form and a numeric form.
145 // The state form selectively defaults/enables/disables the transformation
146 // for the loop (for unroll, default indicates full unrolling rather than
147 // enabling the transformation). The numeric form form provides an integer
148 // hint (for example, unroll count) to the transformer. The following array
149 // accumulates the hints encountered while iterating through the attributes
150 // to check for compatibility.
Eli Bendersky86483b32014-06-11 17:56:26 +0000151 struct {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000152 const LoopHintAttr *StateAttr;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000153 const LoopHintAttr *NumericAttr;
154 } HintAttrs[] = {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}};
Eli Bendersky06a40422014-06-06 20:31:48 +0000155
156 for (const auto *I : Attrs) {
157 const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I);
158
159 // Skip non loop hint attributes
160 if (!LH)
161 continue;
162
Craig Topperec9be542015-12-23 05:44:43 +0000163 LoopHintAttr::OptionType Option = LH->getOption();
164 enum { Vectorize, Interleave, Unroll } Category;
Eli Bendersky06a40422014-06-06 20:31:48 +0000165 switch (Option) {
166 case LoopHintAttr::Vectorize:
167 case LoopHintAttr::VectorizeWidth:
Mark Heffernan450c2382014-07-23 17:31:31 +0000168 Category = Vectorize;
Eli Bendersky06a40422014-06-06 20:31:48 +0000169 break;
170 case LoopHintAttr::Interleave:
171 case LoopHintAttr::InterleaveCount:
Mark Heffernan450c2382014-07-23 17:31:31 +0000172 Category = Interleave;
Eli Bendersky06a40422014-06-06 20:31:48 +0000173 break;
Eli Bendersky86483b32014-06-11 17:56:26 +0000174 case LoopHintAttr::Unroll:
175 case LoopHintAttr::UnrollCount:
Mark Heffernan450c2382014-07-23 17:31:31 +0000176 Category = Unroll;
Eli Bendersky86483b32014-06-11 17:56:26 +0000177 break;
178 };
Eli Bendersky06a40422014-06-06 20:31:48 +0000179
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000180 auto &CategoryState = HintAttrs[Category];
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000181 const LoopHintAttr *PrevAttr;
Eli Bendersky86483b32014-06-11 17:56:26 +0000182 if (Option == LoopHintAttr::Vectorize ||
183 Option == LoopHintAttr::Interleave || Option == LoopHintAttr::Unroll) {
Tyler Nowicki9d268e12015-06-11 23:23:17 +0000184 // Enable|Disable|AssumeSafety hint. For example, vectorize(enable).
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000185 PrevAttr = CategoryState.StateAttr;
186 CategoryState.StateAttr = LH;
Eli Bendersky06a40422014-06-06 20:31:48 +0000187 } else {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000188 // Numeric hint. For example, vectorize_width(8).
189 PrevAttr = CategoryState.NumericAttr;
190 CategoryState.NumericAttr = LH;
Eli Bendersky06a40422014-06-06 20:31:48 +0000191 }
192
Tyler Nowicki884fc612014-07-29 17:21:32 +0000193 PrintingPolicy Policy(S.Context.getLangOpts());
194 SourceLocation OptionLoc = LH->getRange().getBegin();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000195 if (PrevAttr)
196 // Cannot specify same type of attribute twice.
197 S.Diag(OptionLoc, diag::err_pragma_loop_compatibility)
Tyler Nowicki884fc612014-07-29 17:21:32 +0000198 << /*Duplicate=*/true << PrevAttr->getDiagnosticName(Policy)
199 << LH->getDiagnosticName(Policy);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000200
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000201 if (CategoryState.StateAttr && CategoryState.NumericAttr &&
202 (Category == Unroll ||
203 CategoryState.StateAttr->getState() == LoopHintAttr::Disable)) {
Mark Heffernan450c2382014-07-23 17:31:31 +0000204 // Disable hints are not compatible with numeric hints of the same
205 // category. As a special case, numeric unroll hints are also not
Mark Heffernan397a98d2015-08-10 17:29:39 +0000206 // compatible with enable or full form of the unroll pragma because these
207 // directives indicate full unrolling.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000208 S.Diag(OptionLoc, diag::err_pragma_loop_compatibility)
Eli Bendersky86483b32014-06-11 17:56:26 +0000209 << /*Duplicate=*/false
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000210 << CategoryState.StateAttr->getDiagnosticName(Policy)
Tyler Nowicki884fc612014-07-29 17:21:32 +0000211 << CategoryState.NumericAttr->getDiagnosticName(Policy);
Eli Bendersky86483b32014-06-11 17:56:26 +0000212 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000213 }
214}
215
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +0000216static Attr *handleOpenCLUnrollHint(Sema &S, Stmt *St, const AttributeList &A,
217 SourceRange Range) {
218 // OpenCL v2.0 s6.11.5 - opencl_unroll_hint can have 0 arguments (compiler
219 // determines unrolling factor) or 1 argument (the unroll factor provided
220 // by the user).
221
222 if (S.getLangOpts().OpenCLVersion < 200) {
223 S.Diag(A.getLoc(), diag::err_attribute_requires_opencl_version)
Anastasia Stulovac4bb5df2016-03-31 11:07:22 +0000224 << A.getName() << "2.0" << 1;
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +0000225 return nullptr;
226 }
227
228 unsigned NumArgs = A.getNumArgs();
229
230 if (NumArgs > 1) {
231 S.Diag(A.getLoc(), diag::err_attribute_too_many_arguments) << A.getName()
232 << 1;
233 return nullptr;
234 }
235
236 unsigned UnrollFactor = 0;
237
238 if (NumArgs == 1) {
239 Expr *E = A.getArgAsExpr(0);
240 llvm::APSInt ArgVal(32);
241
242 if (!E->isIntegerConstantExpr(ArgVal, S.Context)) {
243 S.Diag(A.getLoc(), diag::err_attribute_argument_type)
244 << A.getName() << AANT_ArgumentIntegerConstant << E->getSourceRange();
245 return nullptr;
246 }
247
248 int Val = ArgVal.getSExtValue();
249
250 if (Val <= 0) {
251 S.Diag(A.getRange().getBegin(),
252 diag::err_attribute_requires_positive_integer)
253 << A.getName();
254 return nullptr;
255 }
256 UnrollFactor = Val;
257 }
258
259 return OpenCLUnrollHintAttr::CreateImplicit(S.Context, UnrollFactor);
260}
261
Eli Bendersky06a40422014-06-06 20:31:48 +0000262static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const AttributeList &A,
263 SourceRange Range) {
264 switch (A.getKind()) {
Michael Han23214e52012-10-03 01:56:22 +0000265 case AttributeList::UnknownAttribute:
266 S.Diag(A.getLoc(), A.isDeclspecAttribute() ?
267 diag::warn_unhandled_ms_attribute_ignored :
268 diag::warn_unknown_attribute_ignored) << A.getName();
Eli Bendersky06a40422014-06-06 20:31:48 +0000269 return nullptr;
270 case AttributeList::AT_FallThrough:
271 return handleFallThroughAttr(S, St, A, Range);
272 case AttributeList::AT_LoopHint:
273 return handleLoopHintAttr(S, St, A, Range);
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +0000274 case AttributeList::AT_OpenCLUnrollHint:
275 return handleOpenCLUnrollHint(S, St, A, Range);
Eli Bendersky06a40422014-06-06 20:31:48 +0000276 default:
277 // if we're here, then we parsed a known attribute, but didn't recognize
278 // it as a statement attribute => it is declaration attribute
Richard Smith4f902c72016-03-08 00:32:55 +0000279 S.Diag(A.getRange().getBegin(), diag::err_decl_attribute_invalid_on_stmt)
Richard Smith4c96e992013-02-19 23:47:15 +0000280 << A.getName() << St->getLocStart();
Craig Topperc3ec1492014-05-26 06:22:03 +0000281 return nullptr;
Richard Smithc202b282012-04-14 00:33:13 +0000282 }
283}
284
285StmtResult Sema::ProcessStmtAttributes(Stmt *S, AttributeList *AttrList,
286 SourceRange Range) {
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000287 SmallVector<const Attr*, 8> Attrs;
Richard Smithc202b282012-04-14 00:33:13 +0000288 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Richard Smith84837d52012-05-03 18:27:39 +0000289 if (Attr *a = ProcessStmtAttribute(*this, S, *l, Range))
Eli Bendersky06a40422014-06-06 20:31:48 +0000290 Attrs.push_back(a);
291 }
292
293 CheckForIncompatibleAttributes(*this, Attrs);
294
295 if (Attrs.empty())
296 return S;
297
Richard Smithc202b282012-04-14 00:33:13 +0000298 return ActOnAttributedStmt(Range.getBegin(), Attrs, S);
299}