1 //===--- SemaStmtAttr.cpp - Statement Attribute Handling ------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements stmt-related attribute processing.
11 //===----------------------------------------------------------------------===//
13 #include "clang/Sema/SemaInternal.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/Basic/SourceManager.h"
16 #include "clang/Sema/DelayedDiagnostic.h"
17 #include "clang/Sema/Lookup.h"
18 #include "clang/Sema/ScopeInfo.h"
19 #include "llvm/ADT/StringExtras.h"
21 using namespace clang;
24 static Attr *handleFallThroughAttr(Sema &S, Stmt *St, const ParsedAttr &A,
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)) {
31 SourceLocation L = S.getLocForEndOfToken(Range.getEnd());
32 S.Diag(L, diag::note_fallthrough_insert_semi_fixit)
33 << FixItHint::CreateInsertion(L, ";");
37 auto *FnScope = S.getCurFunction();
38 if (FnScope->SwitchStack.empty()) {
39 S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_outside_switch);
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() &&
47 S.Diag(A.getLoc(), diag::ext_cxx17_attr) << A;
49 FnScope->setHasFallthroughStmt();
50 return ::new (S.Context) FallThroughAttr(S.Context, A);
53 static Attr *handleSuppressAttr(Sema &S, Stmt *St, const ParsedAttr &A,
55 if (A.getNumArgs() < 1) {
56 S.Diag(A.getLoc(), diag::err_attribute_too_few_arguments) << A << 1;
60 std::vector<StringRef> DiagnosticIdentifiers;
61 for (unsigned I = 0, E = A.getNumArgs(); I != E; ++I) {
64 if (!S.checkStringLiteralArgumentAttr(A, I, RuleName, nullptr))
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);
72 return ::new (S.Context) SuppressAttr(
73 S.Context, A, DiagnosticIdentifiers.data(), DiagnosticIdentifiers.size());
76 static Attr *handleLoopHintAttr(Sema &S, Stmt *St, const ParsedAttr &A,
78 IdentifierLoc *PragmaNameLoc = A.getArgAsIdent(0);
79 IdentifierLoc *OptionLoc = A.getArgAsIdent(1);
80 IdentifierLoc *StateLoc = A.getArgAsIdent(2);
81 Expr *ValueExpr = A.getArgAsExpr(3);
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");
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;
98 LoopHintAttr::OptionType Option;
99 LoopHintAttr::LoopHintState State;
101 auto SetHints = [&Option, &State](LoopHintAttr::OptionType O,
102 LoopHintAttr::LoopHintState S) {
107 if (PragmaName == "nounroll") {
108 SetHints(LoopHintAttr::Unroll, LoopHintAttr::Disable);
109 } else if (PragmaName == "unroll") {
112 SetHints(LoopHintAttr::UnrollCount, LoopHintAttr::Numeric);
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
120 SetHints(LoopHintAttr::UnrollAndJamCount, LoopHintAttr::Numeric);
122 SetHints(LoopHintAttr::UnrollAndJam, LoopHintAttr::Enable);
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()))
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;
165 llvm_unreachable("bad loop hint argument");
167 llvm_unreachable("bad loop hint");
170 return LoopHintAttr::CreateImplicit(S.Context, Option, State, ValueExpr, A);
174 CheckForIncompatibleAttributes(Sema &S,
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
186 const LoopHintAttr *StateAttr;
187 const LoopHintAttr *NumericAttr;
188 } HintAttrs[] = {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr},
189 {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr},
192 for (const auto *I : Attrs) {
193 const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I);
195 // Skip non loop hint attributes
199 LoopHintAttr::OptionType Option = LH->getOption();
210 case LoopHintAttr::Vectorize:
211 case LoopHintAttr::VectorizeWidth:
212 Category = Vectorize;
214 case LoopHintAttr::Interleave:
215 case LoopHintAttr::InterleaveCount:
216 Category = Interleave;
218 case LoopHintAttr::Unroll:
219 case LoopHintAttr::UnrollCount:
222 case LoopHintAttr::UnrollAndJam:
223 case LoopHintAttr::UnrollAndJamCount:
224 Category = UnrollAndJam;
226 case LoopHintAttr::Distribute:
227 // Perform the check for duplicated 'distribute' hints.
228 Category = Distribute;
230 case LoopHintAttr::PipelineDisabled:
231 case LoopHintAttr::PipelineInitiationInterval:
234 case LoopHintAttr::VectorizePredicate:
235 Category = VectorizePredicate;
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;
252 // Numeric hint. For example, vectorize_width(8).
253 PrevAttr = CategoryState.NumericAttr;
254 CategoryState.NumericAttr = LH;
257 PrintingPolicy Policy(S.Context.getLangOpts());
258 SourceLocation OptionLoc = LH->getRange().getBegin();
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);
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);
280 static Attr *handleOpenCLUnrollHint(Sema &S, Stmt *St, const ParsedAttr &A,
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
288 unsigned NumArgs = A.getNumArgs();
291 S.Diag(A.getLoc(), diag::err_attribute_too_many_arguments) << A << 1;
295 unsigned UnrollFactor = 0;
298 Expr *E = A.getArgAsExpr(0);
299 llvm::APSInt ArgVal(32);
301 if (!E->isIntegerConstantExpr(ArgVal, S.Context)) {
302 S.Diag(A.getLoc(), diag::err_attribute_argument_type)
303 << A << AANT_ArgumentIntegerConstant << E->getSourceRange();
307 int Val = ArgVal.getSExtValue();
310 S.Diag(A.getRange().getBegin(),
311 diag::err_attribute_requires_positive_integer)
312 << A << /* positive */ 0;
318 return OpenCLUnrollHintAttr::CreateImplicit(S.Context, UnrollFactor);
321 static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const ParsedAttr &A,
323 switch (A.getKind()) {
324 case ParsedAttr::UnknownAttribute:
325 S.Diag(A.getLoc(), A.isDeclspecAttribute()
326 ? (unsigned)diag::warn_unhandled_ms_attribute_ignored
327 : (unsigned)diag::warn_unknown_attribute_ignored)
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);
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();
347 StmtResult Sema::ProcessStmtAttributes(Stmt *S,
348 const ParsedAttributesView &AttrList,
350 SmallVector<const Attr*, 8> Attrs;
351 for (const ParsedAttr &AL : AttrList) {
352 if (Attr *a = ProcessStmtAttribute(*this, S, AL, Range))
356 CheckForIncompatibleAttributes(*this, Attrs);
361 return ActOnAttributedStmt(Range.getBegin(), Attrs, S);