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 *handleSuppressAttr(Sema &S, Stmt *St, const AttributeList &A,
58 if (A.getNumArgs() < 1) {
59 S.Diag(A.getLoc(), diag::err_attribute_too_few_arguments)
64 std::vector<StringRef> DiagnosticIdentifiers;
65 for (unsigned I = 0, E = A.getNumArgs(); I != E; ++I) {
68 if (!S.checkStringLiteralArgumentAttr(A, I, RuleName, nullptr))
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);
76 return ::new (S.Context) SuppressAttr(
77 A.getRange(), S.Context, DiagnosticIdentifiers.data(),
78 DiagnosticIdentifiers.size(), A.getAttributeSpellingListIndex());
81 static Attr *handleLoopHintAttr(Sema &S, Stmt *St, const AttributeList &A,
83 IdentifierLoc *PragmaNameLoc = A.getArgAsIdent(0);
84 IdentifierLoc *OptionLoc = A.getArgAsIdent(1);
85 IdentifierLoc *StateLoc = A.getArgAsIdent(2);
86 Expr *ValueExpr = A.getArgAsExpr(3);
88 bool PragmaUnroll = PragmaNameLoc->Ident->getName() == "unroll";
89 bool PragmaNoUnroll = PragmaNameLoc->Ident->getName() == "nounroll";
90 if (St->getStmtClass() != Stmt::DoStmtClass &&
91 St->getStmtClass() != Stmt::ForStmtClass &&
92 St->getStmtClass() != Stmt::CXXForRangeStmtClass &&
93 St->getStmtClass() != Stmt::WhileStmtClass) {
95 llvm::StringSwitch<const char *>(PragmaNameLoc->Ident->getName())
96 .Case("unroll", "#pragma unroll")
97 .Case("nounroll", "#pragma nounroll")
98 .Default("#pragma clang loop");
99 S.Diag(St->getLocStart(), diag::err_pragma_loop_precedes_nonloop) << Pragma;
103 LoopHintAttr::Spelling Spelling;
104 LoopHintAttr::OptionType Option;
105 LoopHintAttr::LoopHintState State;
106 if (PragmaNoUnroll) {
108 Spelling = LoopHintAttr::Pragma_nounroll;
109 Option = LoopHintAttr::Unroll;
110 State = LoopHintAttr::Disable;
111 } else if (PragmaUnroll) {
112 Spelling = LoopHintAttr::Pragma_unroll;
115 Option = LoopHintAttr::UnrollCount;
116 State = LoopHintAttr::Numeric;
119 Option = LoopHintAttr::Unroll;
120 State = LoopHintAttr::Enable;
123 // #pragma clang loop ...
124 Spelling = LoopHintAttr::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("interleave_count", LoopHintAttr::InterleaveCount)
133 .Case("unroll", LoopHintAttr::Unroll)
134 .Case("unroll_count", LoopHintAttr::UnrollCount)
135 .Case("distribute", LoopHintAttr::Distribute)
136 .Default(LoopHintAttr::Vectorize);
137 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()))
143 State = LoopHintAttr::Numeric;
144 } else if (Option == LoopHintAttr::Vectorize ||
145 Option == LoopHintAttr::Interleave ||
146 Option == LoopHintAttr::Unroll ||
147 Option == LoopHintAttr::Distribute) {
148 assert(StateLoc && StateLoc->Ident && "Loop hint must have an argument");
149 if (StateLoc->Ident->isStr("disable"))
150 State = LoopHintAttr::Disable;
151 else if (StateLoc->Ident->isStr("assume_safety"))
152 State = LoopHintAttr::AssumeSafety;
153 else if (StateLoc->Ident->isStr("full"))
154 State = LoopHintAttr::Full;
155 else if (StateLoc->Ident->isStr("enable"))
156 State = LoopHintAttr::Enable;
158 llvm_unreachable("bad loop hint argument");
160 llvm_unreachable("bad loop hint");
163 return LoopHintAttr::CreateImplicit(S.Context, Spelling, Option, State,
164 ValueExpr, A.getRange());
168 CheckForIncompatibleAttributes(Sema &S,
169 const SmallVectorImpl<const Attr *> &Attrs) {
170 // 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.
179 const LoopHintAttr *StateAttr;
180 const LoopHintAttr *NumericAttr;
181 } HintAttrs[] = {{nullptr, nullptr},
186 for (const auto *I : Attrs) {
187 const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I);
189 // Skip non loop hint attributes
193 LoopHintAttr::OptionType Option = LH->getOption();
194 enum { Vectorize, Interleave, Unroll, Distribute } Category;
196 case LoopHintAttr::Vectorize:
197 case LoopHintAttr::VectorizeWidth:
198 Category = Vectorize;
200 case LoopHintAttr::Interleave:
201 case LoopHintAttr::InterleaveCount:
202 Category = Interleave;
204 case LoopHintAttr::Unroll:
205 case LoopHintAttr::UnrollCount:
208 case LoopHintAttr::Distribute:
209 // Perform the check for duplicated 'distribute' hints.
210 Category = Distribute;
214 auto &CategoryState = HintAttrs[Category];
215 const LoopHintAttr *PrevAttr;
216 if (Option == LoopHintAttr::Vectorize ||
217 Option == LoopHintAttr::Interleave || Option == LoopHintAttr::Unroll ||
218 Option == LoopHintAttr::Distribute) {
219 // Enable|Disable|AssumeSafety hint. For example, vectorize(enable).
220 PrevAttr = CategoryState.StateAttr;
221 CategoryState.StateAttr = LH;
223 // Numeric hint. For example, vectorize_width(8).
224 PrevAttr = CategoryState.NumericAttr;
225 CategoryState.NumericAttr = LH;
228 PrintingPolicy Policy(S.Context.getLangOpts());
229 SourceLocation OptionLoc = LH->getRange().getBegin();
231 // Cannot specify same type of attribute twice.
232 S.Diag(OptionLoc, diag::err_pragma_loop_compatibility)
233 << /*Duplicate=*/true << PrevAttr->getDiagnosticName(Policy)
234 << LH->getDiagnosticName(Policy);
236 if (CategoryState.StateAttr && CategoryState.NumericAttr &&
237 (Category == Unroll ||
238 CategoryState.StateAttr->getState() == LoopHintAttr::Disable)) {
239 // Disable hints are not compatible with numeric hints of the same
240 // category. As a special case, numeric unroll hints are also not
241 // compatible with enable or full form of the unroll pragma because these
242 // directives indicate full unrolling.
243 S.Diag(OptionLoc, diag::err_pragma_loop_compatibility)
244 << /*Duplicate=*/false
245 << CategoryState.StateAttr->getDiagnosticName(Policy)
246 << CategoryState.NumericAttr->getDiagnosticName(Policy);
251 static Attr *handleOpenCLUnrollHint(Sema &S, Stmt *St, const AttributeList &A,
253 // 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
256 // determines unrolling factor) or 1 argument (the unroll factor provided
259 unsigned NumArgs = A.getNumArgs();
262 S.Diag(A.getLoc(), diag::err_attribute_too_many_arguments) << A.getName()
267 unsigned UnrollFactor = 0;
270 Expr *E = A.getArgAsExpr(0);
271 llvm::APSInt ArgVal(32);
273 if (!E->isIntegerConstantExpr(ArgVal, S.Context)) {
274 S.Diag(A.getLoc(), diag::err_attribute_argument_type)
275 << A.getName() << AANT_ArgumentIntegerConstant << E->getSourceRange();
279 int Val = ArgVal.getSExtValue();
282 S.Diag(A.getRange().getBegin(),
283 diag::err_attribute_requires_positive_integer)
290 return OpenCLUnrollHintAttr::CreateImplicit(S.Context, UnrollFactor);
293 static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const AttributeList &A,
295 switch (A.getKind()) {
296 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();
301 case AttributeList::AT_FallThrough:
302 return handleFallThroughAttr(S, St, A, Range);
303 case AttributeList::AT_LoopHint:
304 return handleLoopHintAttr(S, St, A, Range);
305 case AttributeList::AT_OpenCLUnrollHint:
306 return handleOpenCLUnrollHint(S, St, A, Range);
307 case AttributeList::AT_Suppress:
308 return handleSuppressAttr(S, St, A, Range);
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
312 S.Diag(A.getRange().getBegin(), diag::err_decl_attribute_invalid_on_stmt)
313 << A.getName() << St->getLocStart();
318 StmtResult Sema::ProcessStmtAttributes(Stmt *S, AttributeList *AttrList,
320 SmallVector<const Attr*, 8> Attrs;
321 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
322 if (Attr *a = ProcessStmtAttribute(*this, S, *l, Range))
326 CheckForIncompatibleAttributes(*this, Attrs);
331 return ActOnAttributedStmt(Range.getBegin(), Attrs, S);