blob: 984bd078fa036fbd00bb2cd55d9e96290f56a1ed [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::Spelling Spelling;
Mark Heffernan397a98d2015-08-10 17:29:39 +000069 LoopHintAttr::OptionType Option;
70 LoopHintAttr::LoopHintState State;
71 if (PragmaNoUnroll) {
72 // #pragma nounroll
Mark Heffernanc888e412014-07-24 18:09:38 +000073 Spelling = LoopHintAttr::Pragma_nounroll;
Mark Heffernan397a98d2015-08-10 17:29:39 +000074 Option = LoopHintAttr::Unroll;
75 State = LoopHintAttr::Disable;
76 } else if (PragmaUnroll) {
77 Spelling = LoopHintAttr::Pragma_unroll;
78 if (ValueExpr) {
79 // #pragma unroll N
80 Option = LoopHintAttr::UnrollCount;
81 State = LoopHintAttr::Numeric;
82 } else {
83 // #pragma unroll
84 Option = LoopHintAttr::Unroll;
85 State = LoopHintAttr::Enable;
86 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +000087 } else {
Mark Heffernan397a98d2015-08-10 17:29:39 +000088 // #pragma clang loop ...
89 Spelling = LoopHintAttr::Pragma_clang_loop;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +000090 assert(OptionLoc && OptionLoc->Ident &&
91 "Attribute must have valid option info.");
Mark Heffernan397a98d2015-08-10 17:29:39 +000092 Option = llvm::StringSwitch<LoopHintAttr::OptionType>(
93 OptionLoc->Ident->getName())
Mark Heffernanbd26f5e2014-07-21 18:08:34 +000094 .Case("vectorize", LoopHintAttr::Vectorize)
95 .Case("vectorize_width", LoopHintAttr::VectorizeWidth)
96 .Case("interleave", LoopHintAttr::Interleave)
97 .Case("interleave_count", LoopHintAttr::InterleaveCount)
98 .Case("unroll", LoopHintAttr::Unroll)
99 .Case("unroll_count", LoopHintAttr::UnrollCount)
100 .Default(LoopHintAttr::Vectorize);
Mark Heffernan397a98d2015-08-10 17:29:39 +0000101 if (Option == LoopHintAttr::VectorizeWidth ||
102 Option == LoopHintAttr::InterleaveCount ||
103 Option == LoopHintAttr::UnrollCount) {
104 assert(ValueExpr && "Attribute must have a valid value expression.");
105 if (S.CheckLoopHintExpr(ValueExpr, St->getLocStart()))
106 return nullptr;
107 State = LoopHintAttr::Numeric;
108 } else if (Option == LoopHintAttr::Vectorize ||
109 Option == LoopHintAttr::Interleave ||
110 Option == LoopHintAttr::Unroll) {
111 assert(StateLoc && StateLoc->Ident && "Loop hint must have an argument");
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000112 if (StateLoc->Ident->isStr("disable"))
113 State = LoopHintAttr::Disable;
Tyler Nowicki9d268e12015-06-11 23:23:17 +0000114 else if (StateLoc->Ident->isStr("assume_safety"))
115 State = LoopHintAttr::AssumeSafety;
Mark Heffernan397a98d2015-08-10 17:29:39 +0000116 else if (StateLoc->Ident->isStr("full"))
117 State = LoopHintAttr::Full;
118 else if (StateLoc->Ident->isStr("enable"))
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000119 State = LoopHintAttr::Enable;
Mark Heffernan397a98d2015-08-10 17:29:39 +0000120 else
121 llvm_unreachable("bad loop hint argument");
122 } else
123 llvm_unreachable("bad loop hint");
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000124 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000125
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000126 return LoopHintAttr::CreateImplicit(S.Context, Spelling, Option, State,
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000127 ValueExpr, A.getRange());
Eli Bendersky06a40422014-06-06 20:31:48 +0000128}
129
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000130static void
131CheckForIncompatibleAttributes(Sema &S,
132 const SmallVectorImpl<const Attr *> &Attrs) {
133 // There are 3 categories of loop hints attributes: vectorize, interleave,
134 // and unroll. Each comes in two variants: a state form and a numeric form.
135 // The state form selectively defaults/enables/disables the transformation
136 // for the loop (for unroll, default indicates full unrolling rather than
137 // enabling the transformation). The numeric form form provides an integer
138 // hint (for example, unroll count) to the transformer. The following array
139 // accumulates the hints encountered while iterating through the attributes
140 // to check for compatibility.
Eli Bendersky86483b32014-06-11 17:56:26 +0000141 struct {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000142 const LoopHintAttr *StateAttr;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000143 const LoopHintAttr *NumericAttr;
144 } HintAttrs[] = {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}};
Eli Bendersky06a40422014-06-06 20:31:48 +0000145
146 for (const auto *I : Attrs) {
147 const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I);
148
149 // Skip non loop hint attributes
150 if (!LH)
151 continue;
152
Craig Topperec9be542015-12-23 05:44:43 +0000153 LoopHintAttr::OptionType Option = LH->getOption();
154 enum { Vectorize, Interleave, Unroll } Category;
Eli Bendersky06a40422014-06-06 20:31:48 +0000155 switch (Option) {
156 case LoopHintAttr::Vectorize:
157 case LoopHintAttr::VectorizeWidth:
Mark Heffernan450c2382014-07-23 17:31:31 +0000158 Category = Vectorize;
Eli Bendersky06a40422014-06-06 20:31:48 +0000159 break;
160 case LoopHintAttr::Interleave:
161 case LoopHintAttr::InterleaveCount:
Mark Heffernan450c2382014-07-23 17:31:31 +0000162 Category = Interleave;
Eli Bendersky06a40422014-06-06 20:31:48 +0000163 break;
Eli Bendersky86483b32014-06-11 17:56:26 +0000164 case LoopHintAttr::Unroll:
165 case LoopHintAttr::UnrollCount:
Mark Heffernan450c2382014-07-23 17:31:31 +0000166 Category = Unroll;
Eli Bendersky86483b32014-06-11 17:56:26 +0000167 break;
168 };
Eli Bendersky06a40422014-06-06 20:31:48 +0000169
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000170 auto &CategoryState = HintAttrs[Category];
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000171 const LoopHintAttr *PrevAttr;
Eli Bendersky86483b32014-06-11 17:56:26 +0000172 if (Option == LoopHintAttr::Vectorize ||
173 Option == LoopHintAttr::Interleave || Option == LoopHintAttr::Unroll) {
Tyler Nowicki9d268e12015-06-11 23:23:17 +0000174 // Enable|Disable|AssumeSafety hint. For example, vectorize(enable).
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000175 PrevAttr = CategoryState.StateAttr;
176 CategoryState.StateAttr = LH;
Eli Bendersky06a40422014-06-06 20:31:48 +0000177 } else {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000178 // Numeric hint. For example, vectorize_width(8).
179 PrevAttr = CategoryState.NumericAttr;
180 CategoryState.NumericAttr = LH;
Eli Bendersky06a40422014-06-06 20:31:48 +0000181 }
182
Tyler Nowicki884fc612014-07-29 17:21:32 +0000183 PrintingPolicy Policy(S.Context.getLangOpts());
184 SourceLocation OptionLoc = LH->getRange().getBegin();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000185 if (PrevAttr)
186 // Cannot specify same type of attribute twice.
187 S.Diag(OptionLoc, diag::err_pragma_loop_compatibility)
Tyler Nowicki884fc612014-07-29 17:21:32 +0000188 << /*Duplicate=*/true << PrevAttr->getDiagnosticName(Policy)
189 << LH->getDiagnosticName(Policy);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000190
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000191 if (CategoryState.StateAttr && CategoryState.NumericAttr &&
192 (Category == Unroll ||
193 CategoryState.StateAttr->getState() == LoopHintAttr::Disable)) {
Mark Heffernan450c2382014-07-23 17:31:31 +0000194 // Disable hints are not compatible with numeric hints of the same
195 // category. As a special case, numeric unroll hints are also not
Mark Heffernan397a98d2015-08-10 17:29:39 +0000196 // compatible with enable or full form of the unroll pragma because these
197 // directives indicate full unrolling.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000198 S.Diag(OptionLoc, diag::err_pragma_loop_compatibility)
Eli Bendersky86483b32014-06-11 17:56:26 +0000199 << /*Duplicate=*/false
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000200 << CategoryState.StateAttr->getDiagnosticName(Policy)
Tyler Nowicki884fc612014-07-29 17:21:32 +0000201 << CategoryState.NumericAttr->getDiagnosticName(Policy);
Eli Bendersky86483b32014-06-11 17:56:26 +0000202 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000203 }
204}
205
206static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const AttributeList &A,
207 SourceRange Range) {
208 switch (A.getKind()) {
Michael Han23214e52012-10-03 01:56:22 +0000209 case AttributeList::UnknownAttribute:
210 S.Diag(A.getLoc(), A.isDeclspecAttribute() ?
211 diag::warn_unhandled_ms_attribute_ignored :
212 diag::warn_unknown_attribute_ignored) << A.getName();
Eli Bendersky06a40422014-06-06 20:31:48 +0000213 return nullptr;
214 case AttributeList::AT_FallThrough:
215 return handleFallThroughAttr(S, St, A, Range);
216 case AttributeList::AT_LoopHint:
217 return handleLoopHintAttr(S, St, A, Range);
218 default:
219 // if we're here, then we parsed a known attribute, but didn't recognize
220 // it as a statement attribute => it is declaration attribute
Richard Smith4c96e992013-02-19 23:47:15 +0000221 S.Diag(A.getRange().getBegin(), diag::err_attribute_invalid_on_stmt)
222 << A.getName() << St->getLocStart();
Craig Topperc3ec1492014-05-26 06:22:03 +0000223 return nullptr;
Richard Smithc202b282012-04-14 00:33:13 +0000224 }
225}
226
227StmtResult Sema::ProcessStmtAttributes(Stmt *S, AttributeList *AttrList,
228 SourceRange Range) {
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000229 SmallVector<const Attr*, 8> Attrs;
Richard Smithc202b282012-04-14 00:33:13 +0000230 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Richard Smith84837d52012-05-03 18:27:39 +0000231 if (Attr *a = ProcessStmtAttribute(*this, S, *l, Range))
Eli Bendersky06a40422014-06-06 20:31:48 +0000232 Attrs.push_back(a);
233 }
234
235 CheckForIncompatibleAttributes(*this, Attrs);
236
237 if (Attrs.empty())
238 return S;
239
Richard Smithc202b282012-04-14 00:33:13 +0000240 return ActOnAttributedStmt(Range.getBegin(), Attrs, S);
241}