xref: /freebsd/contrib/llvm-project/clang/lib/Sema/SemaStmtAttr.cpp (revision 77013d11e6483b970af25e13c9b892075742f7e5)
1 //===--- SemaStmtAttr.cpp - Statement Attribute Handling ------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file implements stmt-related attribute processing.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/AST/ASTContext.h"
14 #include "clang/AST/EvaluatedExprVisitor.h"
15 #include "clang/Basic/SourceManager.h"
16 #include "clang/Basic/TargetInfo.h"
17 #include "clang/Sema/DelayedDiagnostic.h"
18 #include "clang/Sema/Lookup.h"
19 #include "clang/Sema/ScopeInfo.h"
20 #include "clang/Sema/SemaInternal.h"
21 #include "llvm/ADT/StringExtras.h"
22 
23 using namespace clang;
24 using namespace sema;
25 
26 static Attr *handleFallThroughAttr(Sema &S, Stmt *St, const ParsedAttr &A,
27                                    SourceRange Range) {
28   FallThroughAttr Attr(S.Context, A);
29   if (!isa<NullStmt>(St)) {
30     S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_wrong_target)
31         << Attr.getSpelling() << St->getBeginLoc();
32     if (isa<SwitchCase>(St)) {
33       SourceLocation L = S.getLocForEndOfToken(Range.getEnd());
34       S.Diag(L, diag::note_fallthrough_insert_semi_fixit)
35           << FixItHint::CreateInsertion(L, ";");
36     }
37     return nullptr;
38   }
39   auto *FnScope = S.getCurFunction();
40   if (FnScope->SwitchStack.empty()) {
41     S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_outside_switch);
42     return nullptr;
43   }
44 
45   // If this is spelled as the standard C++17 attribute, but not in C++17, warn
46   // about using it as an extension.
47   if (!S.getLangOpts().CPlusPlus17 && A.isCXX11Attribute() &&
48       !A.getScopeName())
49     S.Diag(A.getLoc(), diag::ext_cxx17_attr) << A;
50 
51   FnScope->setHasFallthroughStmt();
52   return ::new (S.Context) FallThroughAttr(S.Context, A);
53 }
54 
55 static Attr *handleSuppressAttr(Sema &S, Stmt *St, const ParsedAttr &A,
56                                 SourceRange Range) {
57   if (A.getNumArgs() < 1) {
58     S.Diag(A.getLoc(), diag::err_attribute_too_few_arguments) << A << 1;
59     return nullptr;
60   }
61 
62   std::vector<StringRef> DiagnosticIdentifiers;
63   for (unsigned I = 0, E = A.getNumArgs(); I != E; ++I) {
64     StringRef RuleName;
65 
66     if (!S.checkStringLiteralArgumentAttr(A, I, RuleName, nullptr))
67       return nullptr;
68 
69     // FIXME: Warn if the rule name is unknown. This is tricky because only
70     // clang-tidy knows about available rules.
71     DiagnosticIdentifiers.push_back(RuleName);
72   }
73 
74   return ::new (S.Context) SuppressAttr(
75       S.Context, A, DiagnosticIdentifiers.data(), DiagnosticIdentifiers.size());
76 }
77 
78 static Attr *handleLoopHintAttr(Sema &S, Stmt *St, const ParsedAttr &A,
79                                 SourceRange) {
80   IdentifierLoc *PragmaNameLoc = A.getArgAsIdent(0);
81   IdentifierLoc *OptionLoc = A.getArgAsIdent(1);
82   IdentifierLoc *StateLoc = A.getArgAsIdent(2);
83   Expr *ValueExpr = A.getArgAsExpr(3);
84 
85   StringRef PragmaName =
86       llvm::StringSwitch<StringRef>(PragmaNameLoc->Ident->getName())
87           .Cases("unroll", "nounroll", "unroll_and_jam", "nounroll_and_jam",
88                  PragmaNameLoc->Ident->getName())
89           .Default("clang loop");
90 
91   if (St->getStmtClass() != Stmt::DoStmtClass &&
92       St->getStmtClass() != Stmt::ForStmtClass &&
93       St->getStmtClass() != Stmt::CXXForRangeStmtClass &&
94       St->getStmtClass() != Stmt::WhileStmtClass) {
95     std::string Pragma = "#pragma " + std::string(PragmaName);
96     S.Diag(St->getBeginLoc(), diag::err_pragma_loop_precedes_nonloop) << Pragma;
97     return nullptr;
98   }
99 
100   LoopHintAttr::OptionType Option;
101   LoopHintAttr::LoopHintState State;
102 
103   auto SetHints = [&Option, &State](LoopHintAttr::OptionType O,
104                                     LoopHintAttr::LoopHintState S) {
105     Option = O;
106     State = S;
107   };
108 
109   if (PragmaName == "nounroll") {
110     SetHints(LoopHintAttr::Unroll, LoopHintAttr::Disable);
111   } else if (PragmaName == "unroll") {
112     // #pragma unroll N
113     if (ValueExpr)
114       SetHints(LoopHintAttr::UnrollCount, LoopHintAttr::Numeric);
115     else
116       SetHints(LoopHintAttr::Unroll, LoopHintAttr::Enable);
117   } else if (PragmaName == "nounroll_and_jam") {
118     SetHints(LoopHintAttr::UnrollAndJam, LoopHintAttr::Disable);
119   } else if (PragmaName == "unroll_and_jam") {
120     // #pragma unroll_and_jam N
121     if (ValueExpr)
122       SetHints(LoopHintAttr::UnrollAndJamCount, LoopHintAttr::Numeric);
123     else
124       SetHints(LoopHintAttr::UnrollAndJam, LoopHintAttr::Enable);
125   } else {
126     // #pragma clang loop ...
127     assert(OptionLoc && OptionLoc->Ident &&
128            "Attribute must have valid option info.");
129     Option = llvm::StringSwitch<LoopHintAttr::OptionType>(
130                  OptionLoc->Ident->getName())
131                  .Case("vectorize", LoopHintAttr::Vectorize)
132                  .Case("vectorize_width", LoopHintAttr::VectorizeWidth)
133                  .Case("interleave", LoopHintAttr::Interleave)
134                  .Case("vectorize_predicate", LoopHintAttr::VectorizePredicate)
135                  .Case("interleave_count", LoopHintAttr::InterleaveCount)
136                  .Case("unroll", LoopHintAttr::Unroll)
137                  .Case("unroll_count", LoopHintAttr::UnrollCount)
138                  .Case("pipeline", LoopHintAttr::PipelineDisabled)
139                  .Case("pipeline_initiation_interval",
140                        LoopHintAttr::PipelineInitiationInterval)
141                  .Case("distribute", LoopHintAttr::Distribute)
142                  .Default(LoopHintAttr::Vectorize);
143     if (Option == LoopHintAttr::VectorizeWidth) {
144       assert((ValueExpr || (StateLoc && StateLoc->Ident)) &&
145              "Attribute must have a valid value expression or argument.");
146       if (ValueExpr && S.CheckLoopHintExpr(ValueExpr, St->getBeginLoc()))
147         return nullptr;
148       if (StateLoc && StateLoc->Ident && StateLoc->Ident->isStr("scalable"))
149         State = LoopHintAttr::ScalableWidth;
150       else
151         State = LoopHintAttr::FixedWidth;
152     } else if (Option == LoopHintAttr::InterleaveCount ||
153                Option == LoopHintAttr::UnrollCount ||
154                Option == LoopHintAttr::PipelineInitiationInterval) {
155       assert(ValueExpr && "Attribute must have a valid value expression.");
156       if (S.CheckLoopHintExpr(ValueExpr, St->getBeginLoc()))
157         return nullptr;
158       State = LoopHintAttr::Numeric;
159     } else if (Option == LoopHintAttr::Vectorize ||
160                Option == LoopHintAttr::Interleave ||
161                Option == LoopHintAttr::VectorizePredicate ||
162                Option == LoopHintAttr::Unroll ||
163                Option == LoopHintAttr::Distribute ||
164                Option == LoopHintAttr::PipelineDisabled) {
165       assert(StateLoc && StateLoc->Ident && "Loop hint must have an argument");
166       if (StateLoc->Ident->isStr("disable"))
167         State = LoopHintAttr::Disable;
168       else if (StateLoc->Ident->isStr("assume_safety"))
169         State = LoopHintAttr::AssumeSafety;
170       else if (StateLoc->Ident->isStr("full"))
171         State = LoopHintAttr::Full;
172       else if (StateLoc->Ident->isStr("enable"))
173         State = LoopHintAttr::Enable;
174       else
175         llvm_unreachable("bad loop hint argument");
176     } else
177       llvm_unreachable("bad loop hint");
178   }
179 
180   return LoopHintAttr::CreateImplicit(S.Context, Option, State, ValueExpr, A);
181 }
182 
183 namespace {
184 class CallExprFinder : public ConstEvaluatedExprVisitor<CallExprFinder> {
185   bool FoundCallExpr = false;
186 
187 public:
188   typedef ConstEvaluatedExprVisitor<CallExprFinder> Inherited;
189 
190   CallExprFinder(Sema &S, const Stmt *St) : Inherited(S.Context) { Visit(St); }
191 
192   bool foundCallExpr() { return FoundCallExpr; }
193 
194   void VisitCallExpr(const CallExpr *E) { FoundCallExpr = true; }
195   void VisitAsmStmt(const AsmStmt *S) { FoundCallExpr = true; }
196 
197   void Visit(const Stmt *St) {
198     if (!St)
199       return;
200     ConstEvaluatedExprVisitor<CallExprFinder>::Visit(St);
201   }
202 };
203 } // namespace
204 
205 static Attr *handleNoMergeAttr(Sema &S, Stmt *St, const ParsedAttr &A,
206                                SourceRange Range) {
207   NoMergeAttr NMA(S.Context, A);
208   if (S.CheckAttrNoArgs(A))
209     return nullptr;
210 
211   CallExprFinder CEF(S, St);
212 
213   if (!CEF.foundCallExpr()) {
214     S.Diag(St->getBeginLoc(), diag::warn_nomerge_attribute_ignored_in_stmt)
215         << NMA.getSpelling();
216     return nullptr;
217   }
218 
219   return ::new (S.Context) NoMergeAttr(S.Context, A);
220 }
221 
222 static Attr *handleLikely(Sema &S, Stmt *St, const ParsedAttr &A,
223                           SourceRange Range) {
224 
225   if (!S.getLangOpts().CPlusPlus20 && A.isCXX11Attribute() && !A.getScopeName())
226     S.Diag(A.getLoc(), diag::ext_cxx20_attr) << A << Range;
227 
228   return ::new (S.Context) LikelyAttr(S.Context, A);
229 }
230 
231 static Attr *handleUnlikely(Sema &S, Stmt *St, const ParsedAttr &A,
232                             SourceRange Range) {
233 
234   if (!S.getLangOpts().CPlusPlus20 && A.isCXX11Attribute() && !A.getScopeName())
235     S.Diag(A.getLoc(), diag::ext_cxx20_attr) << A << Range;
236 
237   return ::new (S.Context) UnlikelyAttr(S.Context, A);
238 }
239 
240 static void
241 CheckForIncompatibleAttributes(Sema &S,
242                                const SmallVectorImpl<const Attr *> &Attrs) {
243   // There are 6 categories of loop hints attributes: vectorize, interleave,
244   // unroll, unroll_and_jam, pipeline and distribute. Except for distribute they
245   // come in two variants: a state form and a numeric form.  The state form
246   // selectively defaults/enables/disables the transformation for the loop
247   // (for unroll, default indicates full unrolling rather than enabling the
248   // transformation). The numeric form form provides an integer hint (for
249   // example, unroll count) to the transformer. The following array accumulates
250   // the hints encountered while iterating through the attributes to check for
251   // compatibility.
252   struct {
253     const LoopHintAttr *StateAttr;
254     const LoopHintAttr *NumericAttr;
255   } HintAttrs[] = {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr},
256                    {nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr},
257                    {nullptr, nullptr}};
258 
259   for (const auto *I : Attrs) {
260     const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I);
261 
262     // Skip non loop hint attributes
263     if (!LH)
264       continue;
265 
266     LoopHintAttr::OptionType Option = LH->getOption();
267     enum {
268       Vectorize,
269       Interleave,
270       Unroll,
271       UnrollAndJam,
272       Distribute,
273       Pipeline,
274       VectorizePredicate
275     } Category;
276     switch (Option) {
277     case LoopHintAttr::Vectorize:
278     case LoopHintAttr::VectorizeWidth:
279       Category = Vectorize;
280       break;
281     case LoopHintAttr::Interleave:
282     case LoopHintAttr::InterleaveCount:
283       Category = Interleave;
284       break;
285     case LoopHintAttr::Unroll:
286     case LoopHintAttr::UnrollCount:
287       Category = Unroll;
288       break;
289     case LoopHintAttr::UnrollAndJam:
290     case LoopHintAttr::UnrollAndJamCount:
291       Category = UnrollAndJam;
292       break;
293     case LoopHintAttr::Distribute:
294       // Perform the check for duplicated 'distribute' hints.
295       Category = Distribute;
296       break;
297     case LoopHintAttr::PipelineDisabled:
298     case LoopHintAttr::PipelineInitiationInterval:
299       Category = Pipeline;
300       break;
301     case LoopHintAttr::VectorizePredicate:
302       Category = VectorizePredicate;
303       break;
304     };
305 
306     assert(Category < sizeof(HintAttrs) / sizeof(HintAttrs[0]));
307     auto &CategoryState = HintAttrs[Category];
308     const LoopHintAttr *PrevAttr;
309     if (Option == LoopHintAttr::Vectorize ||
310         Option == LoopHintAttr::Interleave || Option == LoopHintAttr::Unroll ||
311         Option == LoopHintAttr::UnrollAndJam ||
312         Option == LoopHintAttr::VectorizePredicate ||
313         Option == LoopHintAttr::PipelineDisabled ||
314         Option == LoopHintAttr::Distribute) {
315       // Enable|Disable|AssumeSafety hint.  For example, vectorize(enable).
316       PrevAttr = CategoryState.StateAttr;
317       CategoryState.StateAttr = LH;
318     } else {
319       // Numeric hint.  For example, vectorize_width(8).
320       PrevAttr = CategoryState.NumericAttr;
321       CategoryState.NumericAttr = LH;
322     }
323 
324     PrintingPolicy Policy(S.Context.getLangOpts());
325     SourceLocation OptionLoc = LH->getRange().getBegin();
326     if (PrevAttr)
327       // Cannot specify same type of attribute twice.
328       S.Diag(OptionLoc, diag::err_pragma_loop_compatibility)
329           << /*Duplicate=*/true << PrevAttr->getDiagnosticName(Policy)
330           << LH->getDiagnosticName(Policy);
331 
332     if (CategoryState.StateAttr && CategoryState.NumericAttr &&
333         (Category == Unroll || Category == UnrollAndJam ||
334          CategoryState.StateAttr->getState() == LoopHintAttr::Disable)) {
335       // Disable hints are not compatible with numeric hints of the same
336       // category.  As a special case, numeric unroll hints are also not
337       // compatible with enable or full form of the unroll pragma because these
338       // directives indicate full unrolling.
339       S.Diag(OptionLoc, diag::err_pragma_loop_compatibility)
340           << /*Duplicate=*/false
341           << CategoryState.StateAttr->getDiagnosticName(Policy)
342           << CategoryState.NumericAttr->getDiagnosticName(Policy);
343     }
344   }
345 
346   // C++20 [dcl.attr.likelihood]p1 The attribute-token likely shall not appear
347   // in an attribute-specifier-seq that contains the attribute-token unlikely.
348   const LikelyAttr *Likely = nullptr;
349   const UnlikelyAttr *Unlikely = nullptr;
350   for (const auto *I : Attrs) {
351     if (const auto *Attr = dyn_cast<LikelyAttr>(I)) {
352       if (Unlikely) {
353         S.Diag(Attr->getLocation(), diag::err_attributes_are_not_compatible)
354             << Attr << Unlikely << Attr->getRange();
355         S.Diag(Unlikely->getLocation(), diag::note_conflicting_attribute)
356             << Unlikely->getRange();
357         return;
358       }
359       Likely = Attr;
360     } else if (const auto *Attr = dyn_cast<UnlikelyAttr>(I)) {
361       if (Likely) {
362         S.Diag(Attr->getLocation(), diag::err_attributes_are_not_compatible)
363             << Attr << Likely << Attr->getRange();
364         S.Diag(Likely->getLocation(), diag::note_conflicting_attribute)
365             << Likely->getRange();
366         return;
367       }
368       Unlikely = Attr;
369     }
370   }
371 }
372 
373 static Attr *handleOpenCLUnrollHint(Sema &S, Stmt *St, const ParsedAttr &A,
374                                     SourceRange Range) {
375   // Although the feature was introduced only in OpenCL C v2.0 s6.11.5, it's
376   // useful for OpenCL 1.x too and doesn't require HW support.
377   // opencl_unroll_hint can have 0 arguments (compiler
378   // determines unrolling factor) or 1 argument (the unroll factor provided
379   // by the user).
380 
381   unsigned NumArgs = A.getNumArgs();
382 
383   if (NumArgs > 1) {
384     S.Diag(A.getLoc(), diag::err_attribute_too_many_arguments) << A << 1;
385     return nullptr;
386   }
387 
388   unsigned UnrollFactor = 0;
389 
390   if (NumArgs == 1) {
391     Expr *E = A.getArgAsExpr(0);
392     Optional<llvm::APSInt> ArgVal;
393 
394     if (!(ArgVal = E->getIntegerConstantExpr(S.Context))) {
395       S.Diag(A.getLoc(), diag::err_attribute_argument_type)
396           << A << AANT_ArgumentIntegerConstant << E->getSourceRange();
397       return nullptr;
398     }
399 
400     int Val = ArgVal->getSExtValue();
401 
402     if (Val <= 0) {
403       S.Diag(A.getRange().getBegin(),
404              diag::err_attribute_requires_positive_integer)
405           << A << /* positive */ 0;
406       return nullptr;
407     }
408     UnrollFactor = Val;
409   }
410 
411   return OpenCLUnrollHintAttr::CreateImplicit(S.Context, UnrollFactor);
412 }
413 
414 static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const ParsedAttr &A,
415                                   SourceRange Range) {
416   switch (A.getKind()) {
417   case ParsedAttr::UnknownAttribute:
418     S.Diag(A.getLoc(), A.isDeclspecAttribute()
419                            ? (unsigned)diag::warn_unhandled_ms_attribute_ignored
420                            : (unsigned)diag::warn_unknown_attribute_ignored)
421         << A << A.getRange();
422     return nullptr;
423   case ParsedAttr::AT_FallThrough:
424     return handleFallThroughAttr(S, St, A, Range);
425   case ParsedAttr::AT_LoopHint:
426     return handleLoopHintAttr(S, St, A, Range);
427   case ParsedAttr::AT_OpenCLUnrollHint:
428     return handleOpenCLUnrollHint(S, St, A, Range);
429   case ParsedAttr::AT_Suppress:
430     return handleSuppressAttr(S, St, A, Range);
431   case ParsedAttr::AT_NoMerge:
432     return handleNoMergeAttr(S, St, A, Range);
433   case ParsedAttr::AT_Likely:
434     return handleLikely(S, St, A, Range);
435   case ParsedAttr::AT_Unlikely:
436     return handleUnlikely(S, St, A, Range);
437   default:
438     // if we're here, then we parsed a known attribute, but didn't recognize
439     // it as a statement attribute => it is declaration attribute
440     S.Diag(A.getRange().getBegin(), diag::err_decl_attribute_invalid_on_stmt)
441         << A << St->getBeginLoc();
442     return nullptr;
443   }
444 }
445 
446 StmtResult Sema::ProcessStmtAttributes(Stmt *S,
447                                        const ParsedAttributesView &AttrList,
448                                        SourceRange Range) {
449   SmallVector<const Attr*, 8> Attrs;
450   for (const ParsedAttr &AL : AttrList) {
451     if (Attr *a = ProcessStmtAttribute(*this, S, AL, Range))
452       Attrs.push_back(a);
453   }
454 
455   CheckForIncompatibleAttributes(*this, Attrs);
456 
457   if (Attrs.empty())
458     return S;
459 
460   return ActOnAttributedStmt(Range.getBegin(), Attrs, S);
461 }
462