1f4a2713aSLionel Sambuc //===--- SemaStmtAttr.cpp - Statement Attribute Handling ------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements stmt-related attribute processing.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc
14f4a2713aSLionel Sambuc #include "clang/Sema/SemaInternal.h"
15f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
16f4a2713aSLionel Sambuc #include "clang/Basic/SourceManager.h"
17f4a2713aSLionel Sambuc #include "clang/Sema/DelayedDiagnostic.h"
18f4a2713aSLionel Sambuc #include "clang/Sema/Lookup.h"
19*0a6a1f1dSLionel Sambuc #include "clang/Sema/LoopHint.h"
20f4a2713aSLionel Sambuc #include "clang/Sema/ScopeInfo.h"
21f4a2713aSLionel Sambuc #include "llvm/ADT/StringExtras.h"
22f4a2713aSLionel Sambuc
23f4a2713aSLionel Sambuc using namespace clang;
24f4a2713aSLionel Sambuc using namespace sema;
25f4a2713aSLionel Sambuc
handleFallThroughAttr(Sema & S,Stmt * St,const AttributeList & A,SourceRange Range)26f4a2713aSLionel Sambuc static Attr *handleFallThroughAttr(Sema &S, Stmt *St, const AttributeList &A,
27f4a2713aSLionel Sambuc SourceRange Range) {
28f4a2713aSLionel Sambuc if (!isa<NullStmt>(St)) {
29f4a2713aSLionel Sambuc S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_wrong_target)
30f4a2713aSLionel Sambuc << St->getLocStart();
31f4a2713aSLionel Sambuc if (isa<SwitchCase>(St)) {
32*0a6a1f1dSLionel Sambuc SourceLocation L = S.getLocForEndOfToken(Range.getEnd());
33f4a2713aSLionel Sambuc S.Diag(L, diag::note_fallthrough_insert_semi_fixit)
34f4a2713aSLionel Sambuc << FixItHint::CreateInsertion(L, ";");
35f4a2713aSLionel Sambuc }
36*0a6a1f1dSLionel Sambuc return nullptr;
37f4a2713aSLionel Sambuc }
38f4a2713aSLionel Sambuc if (S.getCurFunction()->SwitchStack.empty()) {
39f4a2713aSLionel Sambuc S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_outside_switch);
40*0a6a1f1dSLionel Sambuc return nullptr;
41f4a2713aSLionel Sambuc }
42*0a6a1f1dSLionel Sambuc return ::new (S.Context) FallThroughAttr(A.getRange(), S.Context,
43*0a6a1f1dSLionel Sambuc A.getAttributeSpellingListIndex());
44f4a2713aSLionel Sambuc }
45f4a2713aSLionel Sambuc
handleLoopHintAttr(Sema & S,Stmt * St,const AttributeList & A,SourceRange)46*0a6a1f1dSLionel Sambuc static Attr *handleLoopHintAttr(Sema &S, Stmt *St, const AttributeList &A,
47*0a6a1f1dSLionel Sambuc SourceRange) {
48*0a6a1f1dSLionel Sambuc IdentifierLoc *PragmaNameLoc = A.getArgAsIdent(0);
49*0a6a1f1dSLionel Sambuc IdentifierLoc *OptionLoc = A.getArgAsIdent(1);
50*0a6a1f1dSLionel Sambuc IdentifierLoc *StateLoc = A.getArgAsIdent(2);
51*0a6a1f1dSLionel Sambuc Expr *ValueExpr = A.getArgAsExpr(3);
52*0a6a1f1dSLionel Sambuc
53*0a6a1f1dSLionel Sambuc bool PragmaUnroll = PragmaNameLoc->Ident->getName() == "unroll";
54*0a6a1f1dSLionel Sambuc bool PragmaNoUnroll = PragmaNameLoc->Ident->getName() == "nounroll";
55*0a6a1f1dSLionel Sambuc if (St->getStmtClass() != Stmt::DoStmtClass &&
56*0a6a1f1dSLionel Sambuc St->getStmtClass() != Stmt::ForStmtClass &&
57*0a6a1f1dSLionel Sambuc St->getStmtClass() != Stmt::CXXForRangeStmtClass &&
58*0a6a1f1dSLionel Sambuc St->getStmtClass() != Stmt::WhileStmtClass) {
59*0a6a1f1dSLionel Sambuc const char *Pragma =
60*0a6a1f1dSLionel Sambuc llvm::StringSwitch<const char *>(PragmaNameLoc->Ident->getName())
61*0a6a1f1dSLionel Sambuc .Case("unroll", "#pragma unroll")
62*0a6a1f1dSLionel Sambuc .Case("nounroll", "#pragma nounroll")
63*0a6a1f1dSLionel Sambuc .Default("#pragma clang loop");
64*0a6a1f1dSLionel Sambuc S.Diag(St->getLocStart(), diag::err_pragma_loop_precedes_nonloop) << Pragma;
65*0a6a1f1dSLionel Sambuc return nullptr;
66*0a6a1f1dSLionel Sambuc }
67*0a6a1f1dSLionel Sambuc
68*0a6a1f1dSLionel Sambuc LoopHintAttr::OptionType Option;
69*0a6a1f1dSLionel Sambuc LoopHintAttr::Spelling Spelling;
70*0a6a1f1dSLionel Sambuc if (PragmaUnroll) {
71*0a6a1f1dSLionel Sambuc Option = ValueExpr ? LoopHintAttr::UnrollCount : LoopHintAttr::Unroll;
72*0a6a1f1dSLionel Sambuc Spelling = LoopHintAttr::Pragma_unroll;
73*0a6a1f1dSLionel Sambuc } else if (PragmaNoUnroll) {
74*0a6a1f1dSLionel Sambuc Option = LoopHintAttr::Unroll;
75*0a6a1f1dSLionel Sambuc Spelling = LoopHintAttr::Pragma_nounroll;
76*0a6a1f1dSLionel Sambuc } else {
77*0a6a1f1dSLionel Sambuc assert(OptionLoc && OptionLoc->Ident &&
78*0a6a1f1dSLionel Sambuc "Attribute must have valid option info.");
79*0a6a1f1dSLionel Sambuc IdentifierInfo *OptionInfo = OptionLoc->Ident;
80*0a6a1f1dSLionel Sambuc Option = llvm::StringSwitch<LoopHintAttr::OptionType>(OptionInfo->getName())
81*0a6a1f1dSLionel Sambuc .Case("vectorize", LoopHintAttr::Vectorize)
82*0a6a1f1dSLionel Sambuc .Case("vectorize_width", LoopHintAttr::VectorizeWidth)
83*0a6a1f1dSLionel Sambuc .Case("interleave", LoopHintAttr::Interleave)
84*0a6a1f1dSLionel Sambuc .Case("interleave_count", LoopHintAttr::InterleaveCount)
85*0a6a1f1dSLionel Sambuc .Case("unroll", LoopHintAttr::Unroll)
86*0a6a1f1dSLionel Sambuc .Case("unroll_count", LoopHintAttr::UnrollCount)
87*0a6a1f1dSLionel Sambuc .Default(LoopHintAttr::Vectorize);
88*0a6a1f1dSLionel Sambuc Spelling = LoopHintAttr::Pragma_clang_loop;
89*0a6a1f1dSLionel Sambuc }
90*0a6a1f1dSLionel Sambuc
91*0a6a1f1dSLionel Sambuc LoopHintAttr::LoopHintState State = LoopHintAttr::Default;
92*0a6a1f1dSLionel Sambuc if (PragmaNoUnroll) {
93*0a6a1f1dSLionel Sambuc State = LoopHintAttr::Disable;
94*0a6a1f1dSLionel Sambuc } else if (Option == LoopHintAttr::VectorizeWidth ||
95*0a6a1f1dSLionel Sambuc Option == LoopHintAttr::InterleaveCount ||
96*0a6a1f1dSLionel Sambuc Option == LoopHintAttr::UnrollCount) {
97*0a6a1f1dSLionel Sambuc assert(ValueExpr && "Attribute must have a valid value expression.");
98*0a6a1f1dSLionel Sambuc if (S.CheckLoopHintExpr(ValueExpr, St->getLocStart()))
99*0a6a1f1dSLionel Sambuc return nullptr;
100*0a6a1f1dSLionel Sambuc } else if (Option == LoopHintAttr::Vectorize ||
101*0a6a1f1dSLionel Sambuc Option == LoopHintAttr::Interleave ||
102*0a6a1f1dSLionel Sambuc Option == LoopHintAttr::Unroll) {
103*0a6a1f1dSLionel Sambuc // Default state is assumed if StateLoc is not specified, such as with
104*0a6a1f1dSLionel Sambuc // '#pragma unroll'.
105*0a6a1f1dSLionel Sambuc if (StateLoc && StateLoc->Ident) {
106*0a6a1f1dSLionel Sambuc if (StateLoc->Ident->isStr("disable"))
107*0a6a1f1dSLionel Sambuc State = LoopHintAttr::Disable;
108*0a6a1f1dSLionel Sambuc else
109*0a6a1f1dSLionel Sambuc State = LoopHintAttr::Enable;
110*0a6a1f1dSLionel Sambuc }
111*0a6a1f1dSLionel Sambuc }
112*0a6a1f1dSLionel Sambuc
113*0a6a1f1dSLionel Sambuc return LoopHintAttr::CreateImplicit(S.Context, Spelling, Option, State,
114*0a6a1f1dSLionel Sambuc ValueExpr, A.getRange());
115*0a6a1f1dSLionel Sambuc }
116*0a6a1f1dSLionel Sambuc
117*0a6a1f1dSLionel Sambuc static void
CheckForIncompatibleAttributes(Sema & S,const SmallVectorImpl<const Attr * > & Attrs)118*0a6a1f1dSLionel Sambuc CheckForIncompatibleAttributes(Sema &S,
119*0a6a1f1dSLionel Sambuc const SmallVectorImpl<const Attr *> &Attrs) {
120*0a6a1f1dSLionel Sambuc // There are 3 categories of loop hints attributes: vectorize, interleave,
121*0a6a1f1dSLionel Sambuc // and unroll. Each comes in two variants: a state form and a numeric form.
122*0a6a1f1dSLionel Sambuc // The state form selectively defaults/enables/disables the transformation
123*0a6a1f1dSLionel Sambuc // for the loop (for unroll, default indicates full unrolling rather than
124*0a6a1f1dSLionel Sambuc // enabling the transformation). The numeric form form provides an integer
125*0a6a1f1dSLionel Sambuc // hint (for example, unroll count) to the transformer. The following array
126*0a6a1f1dSLionel Sambuc // accumulates the hints encountered while iterating through the attributes
127*0a6a1f1dSLionel Sambuc // to check for compatibility.
128*0a6a1f1dSLionel Sambuc struct {
129*0a6a1f1dSLionel Sambuc const LoopHintAttr *StateAttr;
130*0a6a1f1dSLionel Sambuc const LoopHintAttr *NumericAttr;
131*0a6a1f1dSLionel Sambuc } HintAttrs[] = {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}};
132*0a6a1f1dSLionel Sambuc
133*0a6a1f1dSLionel Sambuc for (const auto *I : Attrs) {
134*0a6a1f1dSLionel Sambuc const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I);
135*0a6a1f1dSLionel Sambuc
136*0a6a1f1dSLionel Sambuc // Skip non loop hint attributes
137*0a6a1f1dSLionel Sambuc if (!LH)
138*0a6a1f1dSLionel Sambuc continue;
139*0a6a1f1dSLionel Sambuc
140*0a6a1f1dSLionel Sambuc int Option = LH->getOption();
141*0a6a1f1dSLionel Sambuc int Category;
142*0a6a1f1dSLionel Sambuc enum { Vectorize, Interleave, Unroll };
143*0a6a1f1dSLionel Sambuc switch (Option) {
144*0a6a1f1dSLionel Sambuc case LoopHintAttr::Vectorize:
145*0a6a1f1dSLionel Sambuc case LoopHintAttr::VectorizeWidth:
146*0a6a1f1dSLionel Sambuc Category = Vectorize;
147*0a6a1f1dSLionel Sambuc break;
148*0a6a1f1dSLionel Sambuc case LoopHintAttr::Interleave:
149*0a6a1f1dSLionel Sambuc case LoopHintAttr::InterleaveCount:
150*0a6a1f1dSLionel Sambuc Category = Interleave;
151*0a6a1f1dSLionel Sambuc break;
152*0a6a1f1dSLionel Sambuc case LoopHintAttr::Unroll:
153*0a6a1f1dSLionel Sambuc case LoopHintAttr::UnrollCount:
154*0a6a1f1dSLionel Sambuc Category = Unroll;
155*0a6a1f1dSLionel Sambuc break;
156*0a6a1f1dSLionel Sambuc };
157*0a6a1f1dSLionel Sambuc
158*0a6a1f1dSLionel Sambuc auto &CategoryState = HintAttrs[Category];
159*0a6a1f1dSLionel Sambuc const LoopHintAttr *PrevAttr;
160*0a6a1f1dSLionel Sambuc if (Option == LoopHintAttr::Vectorize ||
161*0a6a1f1dSLionel Sambuc Option == LoopHintAttr::Interleave || Option == LoopHintAttr::Unroll) {
162*0a6a1f1dSLionel Sambuc // Enable|disable hint. For example, vectorize(enable).
163*0a6a1f1dSLionel Sambuc PrevAttr = CategoryState.StateAttr;
164*0a6a1f1dSLionel Sambuc CategoryState.StateAttr = LH;
165*0a6a1f1dSLionel Sambuc } else {
166*0a6a1f1dSLionel Sambuc // Numeric hint. For example, vectorize_width(8).
167*0a6a1f1dSLionel Sambuc PrevAttr = CategoryState.NumericAttr;
168*0a6a1f1dSLionel Sambuc CategoryState.NumericAttr = LH;
169*0a6a1f1dSLionel Sambuc }
170*0a6a1f1dSLionel Sambuc
171*0a6a1f1dSLionel Sambuc PrintingPolicy Policy(S.Context.getLangOpts());
172*0a6a1f1dSLionel Sambuc SourceLocation OptionLoc = LH->getRange().getBegin();
173*0a6a1f1dSLionel Sambuc if (PrevAttr)
174*0a6a1f1dSLionel Sambuc // Cannot specify same type of attribute twice.
175*0a6a1f1dSLionel Sambuc S.Diag(OptionLoc, diag::err_pragma_loop_compatibility)
176*0a6a1f1dSLionel Sambuc << /*Duplicate=*/true << PrevAttr->getDiagnosticName(Policy)
177*0a6a1f1dSLionel Sambuc << LH->getDiagnosticName(Policy);
178*0a6a1f1dSLionel Sambuc
179*0a6a1f1dSLionel Sambuc if (CategoryState.StateAttr && CategoryState.NumericAttr &&
180*0a6a1f1dSLionel Sambuc (Category == Unroll ||
181*0a6a1f1dSLionel Sambuc CategoryState.StateAttr->getState() == LoopHintAttr::Disable)) {
182*0a6a1f1dSLionel Sambuc // Disable hints are not compatible with numeric hints of the same
183*0a6a1f1dSLionel Sambuc // category. As a special case, numeric unroll hints are also not
184*0a6a1f1dSLionel Sambuc // compatible with "enable" form of the unroll pragma, unroll(full).
185*0a6a1f1dSLionel Sambuc S.Diag(OptionLoc, diag::err_pragma_loop_compatibility)
186*0a6a1f1dSLionel Sambuc << /*Duplicate=*/false
187*0a6a1f1dSLionel Sambuc << CategoryState.StateAttr->getDiagnosticName(Policy)
188*0a6a1f1dSLionel Sambuc << CategoryState.NumericAttr->getDiagnosticName(Policy);
189*0a6a1f1dSLionel Sambuc }
190*0a6a1f1dSLionel Sambuc }
191*0a6a1f1dSLionel Sambuc }
192f4a2713aSLionel Sambuc
ProcessStmtAttribute(Sema & S,Stmt * St,const AttributeList & A,SourceRange Range)193f4a2713aSLionel Sambuc static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const AttributeList &A,
194f4a2713aSLionel Sambuc SourceRange Range) {
195f4a2713aSLionel Sambuc switch (A.getKind()) {
196f4a2713aSLionel Sambuc case AttributeList::UnknownAttribute:
197f4a2713aSLionel Sambuc S.Diag(A.getLoc(), A.isDeclspecAttribute() ?
198f4a2713aSLionel Sambuc diag::warn_unhandled_ms_attribute_ignored :
199f4a2713aSLionel Sambuc diag::warn_unknown_attribute_ignored) << A.getName();
200*0a6a1f1dSLionel Sambuc return nullptr;
201f4a2713aSLionel Sambuc case AttributeList::AT_FallThrough:
202f4a2713aSLionel Sambuc return handleFallThroughAttr(S, St, A, Range);
203*0a6a1f1dSLionel Sambuc case AttributeList::AT_LoopHint:
204*0a6a1f1dSLionel Sambuc return handleLoopHintAttr(S, St, A, Range);
205f4a2713aSLionel Sambuc default:
206f4a2713aSLionel Sambuc // if we're here, then we parsed a known attribute, but didn't recognize
207f4a2713aSLionel Sambuc // it as a statement attribute => it is declaration attribute
208f4a2713aSLionel Sambuc S.Diag(A.getRange().getBegin(), diag::err_attribute_invalid_on_stmt)
209f4a2713aSLionel Sambuc << A.getName() << St->getLocStart();
210*0a6a1f1dSLionel Sambuc return nullptr;
211f4a2713aSLionel Sambuc }
212f4a2713aSLionel Sambuc }
213f4a2713aSLionel Sambuc
ProcessStmtAttributes(Stmt * S,AttributeList * AttrList,SourceRange Range)214f4a2713aSLionel Sambuc StmtResult Sema::ProcessStmtAttributes(Stmt *S, AttributeList *AttrList,
215f4a2713aSLionel Sambuc SourceRange Range) {
216f4a2713aSLionel Sambuc SmallVector<const Attr*, 8> Attrs;
217f4a2713aSLionel Sambuc for (const AttributeList* l = AttrList; l; l = l->getNext()) {
218f4a2713aSLionel Sambuc if (Attr *a = ProcessStmtAttribute(*this, S, *l, Range))
219f4a2713aSLionel Sambuc Attrs.push_back(a);
220f4a2713aSLionel Sambuc }
221f4a2713aSLionel Sambuc
222*0a6a1f1dSLionel Sambuc CheckForIncompatibleAttributes(*this, Attrs);
223*0a6a1f1dSLionel Sambuc
224f4a2713aSLionel Sambuc if (Attrs.empty())
225f4a2713aSLionel Sambuc return S;
226f4a2713aSLionel Sambuc
227f4a2713aSLionel Sambuc return ActOnAttributedStmt(Range.getBegin(), Attrs, S);
228f4a2713aSLionel Sambuc }
229