]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Sema/SemaStmtAttr.cpp
Update clang to trunk r290819 and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Sema / SemaStmtAttr.cpp
1 //===--- 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"
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"
22
23 using namespace clang;
24 using namespace sema;
25
26 static Attr *handleFallThroughAttr(Sema &S, Stmt *St, const AttributeList &A,
27                                    SourceRange Range) {
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, ";");
37     }
38     return nullptr;
39   }
40   auto *FnScope = S.getCurFunction();
41   if (FnScope->SwitchStack.empty()) {
42     S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_outside_switch);
43     return nullptr;
44   }
45
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);
54 }
55
56 static Attr *handleLoopHintAttr(Sema &S, Stmt *St, const AttributeList &A,
57                                 SourceRange) {
58   IdentifierLoc *PragmaNameLoc = A.getArgAsIdent(0);
59   IdentifierLoc *OptionLoc = A.getArgAsIdent(1);
60   IdentifierLoc *StateLoc = A.getArgAsIdent(2);
61   Expr *ValueExpr = A.getArgAsExpr(3);
62
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) {
69     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");
74     S.Diag(St->getLocStart(), diag::err_pragma_loop_precedes_nonloop) << Pragma;
75     return nullptr;
76   }
77
78   LoopHintAttr::Spelling Spelling;
79   LoopHintAttr::OptionType Option;
80   LoopHintAttr::LoopHintState State;
81   if (PragmaNoUnroll) {
82     // #pragma nounroll
83     Spelling = LoopHintAttr::Pragma_nounroll;
84     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     }
97   } else {
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()))
117         return nullptr;
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;
132       else
133         llvm_unreachable("bad loop hint argument");
134     } else
135       llvm_unreachable("bad loop hint");
136   }
137
138   return LoopHintAttr::CreateImplicit(S.Context, Spelling, Option, State,
139                                       ValueExpr, A.getRange());
140 }
141
142 static void
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.
153   struct {
154     const LoopHintAttr *StateAttr;
155     const LoopHintAttr *NumericAttr;
156   } HintAttrs[] = {{nullptr, nullptr},
157                    {nullptr, nullptr},
158                    {nullptr, nullptr},
159                    {nullptr, nullptr}};
160
161   for (const auto *I : Attrs) {
162     const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I);
163
164     // Skip non loop hint attributes
165     if (!LH)
166       continue;
167
168     LoopHintAttr::OptionType Option = LH->getOption();
169     enum { Vectorize, Interleave, Unroll, Distribute } Category;
170     switch (Option) {
171     case LoopHintAttr::Vectorize:
172     case LoopHintAttr::VectorizeWidth:
173       Category = Vectorize;
174       break;
175     case LoopHintAttr::Interleave:
176     case LoopHintAttr::InterleaveCount:
177       Category = Interleave;
178       break;
179     case LoopHintAttr::Unroll:
180     case LoopHintAttr::UnrollCount:
181       Category = Unroll;
182       break;
183     case LoopHintAttr::Distribute:
184       // Perform the check for duplicated 'distribute' hints.
185       Category = Distribute;
186       break;
187     };
188
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;
197     } else {
198       // Numeric hint.  For example, vectorize_width(8).
199       PrevAttr = CategoryState.NumericAttr;
200       CategoryState.NumericAttr = LH;
201     }
202
203     PrintingPolicy Policy(S.Context.getLangOpts());
204     SourceLocation OptionLoc = LH->getRange().getBegin();
205     if (PrevAttr)
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);
210
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);
222     }
223   }
224 }
225
226 static Attr *handleOpenCLUnrollHint(Sema &S, Stmt *St, const AttributeList &A,
227                                     SourceRange Range) {
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
232   // by the user).
233
234   unsigned NumArgs = A.getNumArgs();
235
236   if (NumArgs > 1) {
237     S.Diag(A.getLoc(), diag::err_attribute_too_many_arguments) << A.getName()
238                                                                << 1;
239     return nullptr;
240   }
241
242   unsigned UnrollFactor = 0;
243
244   if (NumArgs == 1) {
245     Expr *E = A.getArgAsExpr(0);
246     llvm::APSInt ArgVal(32);
247
248     if (!E->isIntegerConstantExpr(ArgVal, S.Context)) {
249       S.Diag(A.getLoc(), diag::err_attribute_argument_type)
250           << A.getName() << AANT_ArgumentIntegerConstant << E->getSourceRange();
251       return nullptr;
252     }
253
254     int Val = ArgVal.getSExtValue();
255
256     if (Val <= 0) {
257       S.Diag(A.getRange().getBegin(),
258              diag::err_attribute_requires_positive_integer)
259           << A.getName();
260       return nullptr;
261     }
262     UnrollFactor = Val;
263   }
264
265   return OpenCLUnrollHintAttr::CreateImplicit(S.Context, UnrollFactor);
266 }
267
268 static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const AttributeList &A,
269                                   SourceRange Range) {
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();
275     return nullptr;
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);
282   default:
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();
287     return nullptr;
288   }
289 }
290
291 StmtResult Sema::ProcessStmtAttributes(Stmt *S, AttributeList *AttrList,
292                                        SourceRange Range) {
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))
296       Attrs.push_back(a);
297   }
298
299   CheckForIncompatibleAttributes(*this, Attrs);
300
301   if (Attrs.empty())
302     return S;
303
304   return ActOnAttributedStmt(Range.getBegin(), Attrs, S);
305 }