blob: 19e2c8ed652fd47f8b20b7af7ce3e54a1b0035d8 [file] [log] [blame]
Richard Smithc202b282012-04-14 00:33:13 +00001//===--- 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"
Richard Smithc202b282012-04-14 00:33:13 +000015#include "clang/AST/ASTContext.h"
Eli Bendersky06a40422014-06-06 20:31:48 +000016#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
Richard Smithc202b282012-04-14 00:33:13 +000023using namespace clang;
24using namespace sema;
25
Richard Smith84837d52012-05-03 18:27:39 +000026static Attr *handleFallThroughAttr(Sema &S, Stmt *St, const AttributeList &A,
27 SourceRange Range) {
28 if (!isa<NullStmt>(St)) {
29 S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_wrong_target)
30 << St->getLocStart();
31 if (isa<SwitchCase>(St)) {
Alp Tokerb6cc5922014-05-03 03:45:55 +000032 SourceLocation L = S.getLocForEndOfToken(Range.getEnd());
Richard Smith84837d52012-05-03 18:27:39 +000033 S.Diag(L, diag::note_fallthrough_insert_semi_fixit)
34 << FixItHint::CreateInsertion(L, ";");
35 }
Craig Topperc3ec1492014-05-26 06:22:03 +000036 return nullptr;
Richard Smith84837d52012-05-03 18:27:39 +000037 }
38 if (S.getCurFunction()->SwitchStack.empty()) {
39 S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_outside_switch);
Craig Topperc3ec1492014-05-26 06:22:03 +000040 return nullptr;
Richard Smith84837d52012-05-03 18:27:39 +000041 }
Aaron Ballman36a53502014-01-16 13:03:14 +000042 return ::new (S.Context) FallThroughAttr(A.getRange(), S.Context,
Eli Bendersky06a40422014-06-06 20:31:48 +000043 A.getAttributeSpellingListIndex());
44}
45
46static Attr *handleLoopHintAttr(Sema &S, Stmt *St, const AttributeList &A,
47 SourceRange) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +000048 IdentifierLoc *PragmaNameLoc = A.getArgAsIdent(0);
49 IdentifierLoc *OptionLoc = A.getArgAsIdent(1);
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +000050 IdentifierLoc *StateLoc = A.getArgAsIdent(2);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +000051 Expr *ValueExpr = A.getArgAsExpr(3);
52
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +000053 bool PragmaUnroll = PragmaNameLoc->Ident->getName() == "unroll";
54 bool PragmaNoUnroll = PragmaNameLoc->Ident->getName() == "nounroll";
Eli Bendersky06a40422014-06-06 20:31:48 +000055 if (St->getStmtClass() != Stmt::DoStmtClass &&
56 St->getStmtClass() != Stmt::ForStmtClass &&
57 St->getStmtClass() != Stmt::CXXForRangeStmtClass &&
58 St->getStmtClass() != Stmt::WhileStmtClass) {
Mark Heffernanc888e412014-07-24 18:09:38 +000059 const char *Pragma =
60 llvm::StringSwitch<const char *>(PragmaNameLoc->Ident->getName())
61 .Case("unroll", "#pragma unroll")
62 .Case("nounroll", "#pragma nounroll")
63 .Default("#pragma clang loop");
Mark Heffernanbd26f5e2014-07-21 18:08:34 +000064 S.Diag(St->getLocStart(), diag::err_pragma_loop_precedes_nonloop) << Pragma;
Eli Bendersky06a40422014-06-06 20:31:48 +000065 return nullptr;
66 }
67
Mark Heffernanbd26f5e2014-07-21 18:08:34 +000068 LoopHintAttr::OptionType Option;
69 LoopHintAttr::Spelling Spelling;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +000070 if (PragmaUnroll) {
71 Option = ValueExpr ? LoopHintAttr::UnrollCount : LoopHintAttr::Unroll;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +000072 Spelling = LoopHintAttr::Pragma_unroll;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +000073 } else if (PragmaNoUnroll) {
Mark Heffernanc888e412014-07-24 18:09:38 +000074 Option = LoopHintAttr::Unroll;
75 Spelling = LoopHintAttr::Pragma_nounroll;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +000076 } else {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +000077 assert(OptionLoc && OptionLoc->Ident &&
78 "Attribute must have valid option info.");
79 IdentifierInfo *OptionInfo = OptionLoc->Ident;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +000080 Option = llvm::StringSwitch<LoopHintAttr::OptionType>(OptionInfo->getName())
81 .Case("vectorize", LoopHintAttr::Vectorize)
82 .Case("vectorize_width", LoopHintAttr::VectorizeWidth)
83 .Case("interleave", LoopHintAttr::Interleave)
84 .Case("interleave_count", LoopHintAttr::InterleaveCount)
85 .Case("unroll", LoopHintAttr::Unroll)
86 .Case("unroll_count", LoopHintAttr::UnrollCount)
87 .Default(LoopHintAttr::Vectorize);
88 Spelling = LoopHintAttr::Pragma_clang_loop;
89 }
Eli Bendersky06a40422014-06-06 20:31:48 +000090
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +000091 LoopHintAttr::LoopHintState State = LoopHintAttr::Default;
92 if (PragmaNoUnroll) {
93 State = LoopHintAttr::Disable;
Eli Bendersky06a40422014-06-06 20:31:48 +000094 } else if (Option == LoopHintAttr::VectorizeWidth ||
Eli Bendersky86483b32014-06-11 17:56:26 +000095 Option == LoopHintAttr::InterleaveCount ||
96 Option == LoopHintAttr::UnrollCount) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +000097 assert(ValueExpr && "Attribute must have a valid value expression.");
98 if (S.CheckLoopHintExpr(ValueExpr, St->getLocStart()))
Eli Bendersky06a40422014-06-06 20:31:48 +000099 return nullptr;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000100 } else if (Option == LoopHintAttr::Vectorize ||
101 Option == LoopHintAttr::Interleave ||
102 Option == LoopHintAttr::Unroll) {
103 // Default state is assumed if StateLoc is not specified, such as with
104 // '#pragma unroll'.
105 if (StateLoc && StateLoc->Ident) {
106 if (StateLoc->Ident->isStr("disable"))
107 State = LoopHintAttr::Disable;
108 else
109 State = LoopHintAttr::Enable;
110 }
111 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000112
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000113 return LoopHintAttr::CreateImplicit(S.Context, Spelling, Option, State,
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000114 ValueExpr, A.getRange());
Eli Bendersky06a40422014-06-06 20:31:48 +0000115}
116
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000117static void
118CheckForIncompatibleAttributes(Sema &S,
119 const SmallVectorImpl<const Attr *> &Attrs) {
120 // There are 3 categories of loop hints attributes: vectorize, interleave,
121 // and unroll. Each comes in two variants: a state form and a numeric form.
122 // The state form selectively defaults/enables/disables the transformation
123 // for the loop (for unroll, default indicates full unrolling rather than
124 // enabling the transformation). The numeric form form provides an integer
125 // hint (for example, unroll count) to the transformer. The following array
126 // accumulates the hints encountered while iterating through the attributes
127 // to check for compatibility.
Eli Bendersky86483b32014-06-11 17:56:26 +0000128 struct {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000129 const LoopHintAttr *StateAttr;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000130 const LoopHintAttr *NumericAttr;
131 } HintAttrs[] = {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}};
Eli Bendersky06a40422014-06-06 20:31:48 +0000132
133 for (const auto *I : Attrs) {
134 const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I);
135
136 // Skip non loop hint attributes
137 if (!LH)
138 continue;
139
Eli Bendersky06a40422014-06-06 20:31:48 +0000140 int Option = LH->getOption();
Eli Bendersky86483b32014-06-11 17:56:26 +0000141 int Category;
Mark Heffernan450c2382014-07-23 17:31:31 +0000142 enum { Vectorize, Interleave, Unroll };
Eli Bendersky06a40422014-06-06 20:31:48 +0000143 switch (Option) {
144 case LoopHintAttr::Vectorize:
145 case LoopHintAttr::VectorizeWidth:
Mark Heffernan450c2382014-07-23 17:31:31 +0000146 Category = Vectorize;
Eli Bendersky06a40422014-06-06 20:31:48 +0000147 break;
148 case LoopHintAttr::Interleave:
149 case LoopHintAttr::InterleaveCount:
Mark Heffernan450c2382014-07-23 17:31:31 +0000150 Category = Interleave;
Eli Bendersky06a40422014-06-06 20:31:48 +0000151 break;
Eli Bendersky86483b32014-06-11 17:56:26 +0000152 case LoopHintAttr::Unroll:
153 case LoopHintAttr::UnrollCount:
Mark Heffernan450c2382014-07-23 17:31:31 +0000154 Category = Unroll;
Eli Bendersky86483b32014-06-11 17:56:26 +0000155 break;
156 };
Eli Bendersky06a40422014-06-06 20:31:48 +0000157
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000158 auto &CategoryState = HintAttrs[Category];
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000159 const LoopHintAttr *PrevAttr;
Eli Bendersky86483b32014-06-11 17:56:26 +0000160 if (Option == LoopHintAttr::Vectorize ||
161 Option == LoopHintAttr::Interleave || Option == LoopHintAttr::Unroll) {
162 // Enable|disable hint. For example, vectorize(enable).
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000163 PrevAttr = CategoryState.StateAttr;
164 CategoryState.StateAttr = LH;
Eli Bendersky06a40422014-06-06 20:31:48 +0000165 } else {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000166 // Numeric hint. For example, vectorize_width(8).
167 PrevAttr = CategoryState.NumericAttr;
168 CategoryState.NumericAttr = LH;
Eli Bendersky06a40422014-06-06 20:31:48 +0000169 }
170
Tyler Nowicki884fc612014-07-29 17:21:32 +0000171 PrintingPolicy Policy(S.Context.getLangOpts());
172 SourceLocation OptionLoc = LH->getRange().getBegin();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000173 if (PrevAttr)
174 // Cannot specify same type of attribute twice.
175 S.Diag(OptionLoc, diag::err_pragma_loop_compatibility)
Tyler Nowicki884fc612014-07-29 17:21:32 +0000176 << /*Duplicate=*/true << PrevAttr->getDiagnosticName(Policy)
177 << LH->getDiagnosticName(Policy);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000178
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000179 if (CategoryState.StateAttr && CategoryState.NumericAttr &&
180 (Category == Unroll ||
181 CategoryState.StateAttr->getState() == LoopHintAttr::Disable)) {
Mark Heffernan450c2382014-07-23 17:31:31 +0000182 // Disable hints are not compatible with numeric hints of the same
183 // category. As a special case, numeric unroll hints are also not
184 // compatible with "enable" form of the unroll pragma, unroll(full).
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000185 S.Diag(OptionLoc, diag::err_pragma_loop_compatibility)
Eli Bendersky86483b32014-06-11 17:56:26 +0000186 << /*Duplicate=*/false
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000187 << CategoryState.StateAttr->getDiagnosticName(Policy)
Tyler Nowicki884fc612014-07-29 17:21:32 +0000188 << CategoryState.NumericAttr->getDiagnosticName(Policy);
Eli Bendersky86483b32014-06-11 17:56:26 +0000189 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000190 }
191}
192
193static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const AttributeList &A,
194 SourceRange Range) {
195 switch (A.getKind()) {
Michael Han23214e52012-10-03 01:56:22 +0000196 case AttributeList::UnknownAttribute:
197 S.Diag(A.getLoc(), A.isDeclspecAttribute() ?
198 diag::warn_unhandled_ms_attribute_ignored :
199 diag::warn_unknown_attribute_ignored) << A.getName();
Eli Bendersky06a40422014-06-06 20:31:48 +0000200 return nullptr;
201 case AttributeList::AT_FallThrough:
202 return handleFallThroughAttr(S, St, A, Range);
203 case AttributeList::AT_LoopHint:
204 return handleLoopHintAttr(S, St, A, Range);
205 default:
206 // if we're here, then we parsed a known attribute, but didn't recognize
207 // it as a statement attribute => it is declaration attribute
Richard Smith4c96e992013-02-19 23:47:15 +0000208 S.Diag(A.getRange().getBegin(), diag::err_attribute_invalid_on_stmt)
209 << A.getName() << St->getLocStart();
Craig Topperc3ec1492014-05-26 06:22:03 +0000210 return nullptr;
Richard Smithc202b282012-04-14 00:33:13 +0000211 }
212}
213
214StmtResult Sema::ProcessStmtAttributes(Stmt *S, AttributeList *AttrList,
215 SourceRange Range) {
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000216 SmallVector<const Attr*, 8> Attrs;
Richard Smithc202b282012-04-14 00:33:13 +0000217 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Richard Smith84837d52012-05-03 18:27:39 +0000218 if (Attr *a = ProcessStmtAttribute(*this, S, *l, Range))
Eli Bendersky06a40422014-06-06 20:31:48 +0000219 Attrs.push_back(a);
220 }
221
222 CheckForIncompatibleAttributes(*this, Attrs);
223
224 if (Attrs.empty())
225 return S;
226
Richard Smithc202b282012-04-14 00:33:13 +0000227 return ActOnAttributedStmt(Range.getBegin(), Attrs, S);
228}