1 //===--- SemaStmtAttr.cpp - Statement Attribute Handling ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements stmt-related attribute processing.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Sema/SemaInternal.h"
15 #include "clang/AST/ASTContext.h"
16 #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"
23 using namespace clang;
26 static Attr *handleFallThroughAttr(Sema &S, Stmt *St, const AttributeList &A,
28 FallThroughAttr Attr(A.getRange(), S.Context,
29 A.getAttributeSpellingListIndex());
30 if (!isa<NullStmt>(St)) {
31 S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_wrong_target)
32 << Attr.getSpelling() << St->getLocStart();
33 if (isa<SwitchCase>(St)) {
34 SourceLocation L = S.getLocForEndOfToken(Range.getEnd());
35 S.Diag(L, diag::note_fallthrough_insert_semi_fixit)
36 << FixItHint::CreateInsertion(L, ";");
40 auto *FnScope = S.getCurFunction();
41 if (FnScope->SwitchStack.empty()) {
42 S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_outside_switch);
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() &&
50 S.Diag(A.getLoc(), diag::ext_cxx1z_attr) << A.getName();
52 FnScope->setHasFallthroughStmt();
53 return ::new (S.Context) auto(Attr);
56 static Attr *handleLoopHintAttr(Sema &S, Stmt *St, const AttributeList &A,
58 IdentifierLoc *PragmaNameLoc = A.getArgAsIdent(0);
59 IdentifierLoc *OptionLoc = A.getArgAsIdent(1);
60 IdentifierLoc *StateLoc = A.getArgAsIdent(2);
61 Expr *ValueExpr = A.getArgAsExpr(3);
63 bool PragmaUnroll = PragmaNameLoc->Ident->getName() == "unroll";
64 bool PragmaNoUnroll = PragmaNameLoc->Ident->getName() == "nounroll";
65 if (St->getStmtClass() != Stmt::DoStmtClass &&
66 St->getStmtClass() != Stmt::ForStmtClass &&
67 St->getStmtClass() != Stmt::CXXForRangeStmtClass &&
68 St->getStmtClass() != Stmt::WhileStmtClass) {
70 llvm::StringSwitch<const char *>(PragmaNameLoc->Ident->getName())
71 .Case("unroll", "#pragma unroll")
72 .Case("nounroll", "#pragma nounroll")
73 .Default("#pragma clang loop");
74 S.Diag(St->getLocStart(), diag::err_pragma_loop_precedes_nonloop) << Pragma;
78 LoopHintAttr::Spelling Spelling;
79 LoopHintAttr::OptionType Option;
80 LoopHintAttr::LoopHintState State;
83 Spelling = LoopHintAttr::Pragma_nounroll;
84 Option = LoopHintAttr::Unroll;
85 State = LoopHintAttr::Disable;
86 } else if (PragmaUnroll) {
87 Spelling = LoopHintAttr::Pragma_unroll;
90 Option = LoopHintAttr::UnrollCount;
91 State = LoopHintAttr::Numeric;
94 Option = LoopHintAttr::Unroll;
95 State = LoopHintAttr::Enable;
98 // #pragma clang loop ...
99 Spelling = LoopHintAttr::Pragma_clang_loop;
100 assert(OptionLoc && OptionLoc->Ident &&
101 "Attribute must have valid option info.");
102 Option = llvm::StringSwitch<LoopHintAttr::OptionType>(
103 OptionLoc->Ident->getName())
104 .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 .Case("distribute", LoopHintAttr::Distribute)
111 .Default(LoopHintAttr::Vectorize);
112 if (Option == LoopHintAttr::VectorizeWidth ||
113 Option == LoopHintAttr::InterleaveCount ||
114 Option == LoopHintAttr::UnrollCount) {
115 assert(ValueExpr && "Attribute must have a valid value expression.");
116 if (S.CheckLoopHintExpr(ValueExpr, St->getLocStart()))
118 State = LoopHintAttr::Numeric;
119 } else if (Option == LoopHintAttr::Vectorize ||
120 Option == LoopHintAttr::Interleave ||
121 Option == LoopHintAttr::Unroll ||
122 Option == LoopHintAttr::Distribute) {
123 assert(StateLoc && StateLoc->Ident && "Loop hint must have an argument");
124 if (StateLoc->Ident->isStr("disable"))
125 State = LoopHintAttr::Disable;
126 else if (StateLoc->Ident->isStr("assume_safety"))
127 State = LoopHintAttr::AssumeSafety;
128 else if (StateLoc->Ident->isStr("full"))
129 State = LoopHintAttr::Full;
130 else if (StateLoc->Ident->isStr("enable"))
131 State = LoopHintAttr::Enable;
133 llvm_unreachable("bad loop hint argument");
135 llvm_unreachable("bad loop hint");
138 return LoopHintAttr::CreateImplicit(S.Context, Spelling, Option, State,
139 ValueExpr, A.getRange());
143 CheckForIncompatibleAttributes(Sema &S,
144 const SmallVectorImpl<const Attr *> &Attrs) {
145 // There are 4 categories of loop hints attributes: vectorize, interleave,
146 // unroll and distribute. Except for distribute they come in two variants: a
147 // state form and a numeric form. The state form selectively
148 // defaults/enables/disables the transformation for the loop (for unroll,
149 // default indicates full unrolling rather than enabling the transformation).
150 // The numeric form form provides an integer hint (for example, unroll count)
151 // to the transformer. The following array accumulates the hints encountered
152 // while iterating through the attributes to check for compatibility.
154 const LoopHintAttr *StateAttr;
155 const LoopHintAttr *NumericAttr;
156 } HintAttrs[] = {{nullptr, nullptr},
161 for (const auto *I : Attrs) {
162 const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I);
164 // Skip non loop hint attributes
168 LoopHintAttr::OptionType Option = LH->getOption();
169 enum { Vectorize, Interleave, Unroll, Distribute } Category;
171 case LoopHintAttr::Vectorize:
172 case LoopHintAttr::VectorizeWidth:
173 Category = Vectorize;
175 case LoopHintAttr::Interleave:
176 case LoopHintAttr::InterleaveCount:
177 Category = Interleave;
179 case LoopHintAttr::Unroll:
180 case LoopHintAttr::UnrollCount:
183 case LoopHintAttr::Distribute:
184 // Perform the check for duplicated 'distribute' hints.
185 Category = Distribute;
189 auto &CategoryState = HintAttrs[Category];
190 const LoopHintAttr *PrevAttr;
191 if (Option == LoopHintAttr::Vectorize ||
192 Option == LoopHintAttr::Interleave || Option == LoopHintAttr::Unroll ||
193 Option == LoopHintAttr::Distribute) {
194 // Enable|Disable|AssumeSafety hint. For example, vectorize(enable).
195 PrevAttr = CategoryState.StateAttr;
196 CategoryState.StateAttr = LH;
198 // Numeric hint. For example, vectorize_width(8).
199 PrevAttr = CategoryState.NumericAttr;
200 CategoryState.NumericAttr = LH;
203 PrintingPolicy Policy(S.Context.getLangOpts());
204 SourceLocation OptionLoc = LH->getRange().getBegin();
206 // Cannot specify same type of attribute twice.
207 S.Diag(OptionLoc, diag::err_pragma_loop_compatibility)
208 << /*Duplicate=*/true << PrevAttr->getDiagnosticName(Policy)
209 << LH->getDiagnosticName(Policy);
211 if (CategoryState.StateAttr && CategoryState.NumericAttr &&
212 (Category == Unroll ||
213 CategoryState.StateAttr->getState() == LoopHintAttr::Disable)) {
214 // Disable hints are not compatible with numeric hints of the same
215 // category. As a special case, numeric unroll hints are also not
216 // compatible with enable or full form of the unroll pragma because these
217 // directives indicate full unrolling.
218 S.Diag(OptionLoc, diag::err_pragma_loop_compatibility)
219 << /*Duplicate=*/false
220 << CategoryState.StateAttr->getDiagnosticName(Policy)
221 << CategoryState.NumericAttr->getDiagnosticName(Policy);
226 static Attr *handleOpenCLUnrollHint(Sema &S, Stmt *St, const AttributeList &A,
228 // Although the feature was introduced only in OpenCL C v2.0 s6.11.5, it's
229 // useful for OpenCL 1.x too and doesn't require HW support.
230 // opencl_unroll_hint can have 0 arguments (compiler
231 // determines unrolling factor) or 1 argument (the unroll factor provided
234 unsigned NumArgs = A.getNumArgs();
237 S.Diag(A.getLoc(), diag::err_attribute_too_many_arguments) << A.getName()
242 unsigned UnrollFactor = 0;
245 Expr *E = A.getArgAsExpr(0);
246 llvm::APSInt ArgVal(32);
248 if (!E->isIntegerConstantExpr(ArgVal, S.Context)) {
249 S.Diag(A.getLoc(), diag::err_attribute_argument_type)
250 << A.getName() << AANT_ArgumentIntegerConstant << E->getSourceRange();
254 int Val = ArgVal.getSExtValue();
257 S.Diag(A.getRange().getBegin(),
258 diag::err_attribute_requires_positive_integer)
265 return OpenCLUnrollHintAttr::CreateImplicit(S.Context, UnrollFactor);
268 static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const AttributeList &A,
270 switch (A.getKind()) {
271 case AttributeList::UnknownAttribute:
272 S.Diag(A.getLoc(), A.isDeclspecAttribute() ?
273 diag::warn_unhandled_ms_attribute_ignored :
274 diag::warn_unknown_attribute_ignored) << A.getName();
276 case AttributeList::AT_FallThrough:
277 return handleFallThroughAttr(S, St, A, Range);
278 case AttributeList::AT_LoopHint:
279 return handleLoopHintAttr(S, St, A, Range);
280 case AttributeList::AT_OpenCLUnrollHint:
281 return handleOpenCLUnrollHint(S, St, A, Range);
283 // if we're here, then we parsed a known attribute, but didn't recognize
284 // it as a statement attribute => it is declaration attribute
285 S.Diag(A.getRange().getBegin(), diag::err_decl_attribute_invalid_on_stmt)
286 << A.getName() << St->getLocStart();
291 StmtResult Sema::ProcessStmtAttributes(Stmt *S, AttributeList *AttrList,
293 SmallVector<const Attr*, 8> Attrs;
294 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
295 if (Attr *a = ProcessStmtAttribute(*this, S, *l, Range))
299 CheckForIncompatibleAttributes(*this, Attrs);
304 return ActOnAttributedStmt(Range.getBegin(), Attrs, S);