blob: a32e0fbcb622934d59e5ebd3795d83a2dd871ac4 [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);
50 IdentifierInfo *OptionInfo = OptionLoc->Ident;
51 IdentifierLoc *ValueLoc = A.getArgAsIdent(2);
52 IdentifierInfo *ValueInfo = ValueLoc ? ValueLoc->Ident : nullptr;
53 Expr *ValueExpr = A.getArgAsExpr(3);
54
55 assert(OptionInfo && "Attribute must have valid option info.");
56
Eli Bendersky06a40422014-06-06 20:31:48 +000057 if (St->getStmtClass() != Stmt::DoStmtClass &&
58 St->getStmtClass() != Stmt::ForStmtClass &&
59 St->getStmtClass() != Stmt::CXXForRangeStmtClass &&
60 St->getStmtClass() != Stmt::WhileStmtClass) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +000061 const char *Pragma = PragmaNameLoc->Ident->getName() == "unroll"
62 ? "#pragma unroll"
63 : "#pragma clang loop";
64 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;
70 if (PragmaNameLoc->Ident->getName() == "unroll") {
71 Option = ValueLoc ? LoopHintAttr::UnrollCount : LoopHintAttr::Unroll;
72 Spelling = LoopHintAttr::Pragma_unroll;
73 } else {
74 Option = llvm::StringSwitch<LoopHintAttr::OptionType>(OptionInfo->getName())
75 .Case("vectorize", LoopHintAttr::Vectorize)
76 .Case("vectorize_width", LoopHintAttr::VectorizeWidth)
77 .Case("interleave", LoopHintAttr::Interleave)
78 .Case("interleave_count", LoopHintAttr::InterleaveCount)
79 .Case("unroll", LoopHintAttr::Unroll)
80 .Case("unroll_count", LoopHintAttr::UnrollCount)
81 .Default(LoopHintAttr::Vectorize);
82 Spelling = LoopHintAttr::Pragma_clang_loop;
83 }
Eli Bendersky06a40422014-06-06 20:31:48 +000084
85 int ValueInt;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +000086 if (Option == LoopHintAttr::Unroll &&
87 Spelling == LoopHintAttr::Pragma_unroll) {
88 ValueInt = 1;
89 } else if (Option == LoopHintAttr::Vectorize ||
90 Option == LoopHintAttr::Interleave ||
91 Option == LoopHintAttr::Unroll) {
Eli Bendersky06a40422014-06-06 20:31:48 +000092 if (!ValueInfo) {
Eli Benderskyf6377902014-06-19 18:30:15 +000093 S.Diag(ValueLoc->Loc, diag::err_pragma_loop_invalid_keyword);
Eli Bendersky06a40422014-06-06 20:31:48 +000094 return nullptr;
95 }
Eli Bendersky06a40422014-06-06 20:31:48 +000096 if (ValueInfo->isStr("disable"))
97 ValueInt = 0;
98 else if (ValueInfo->isStr("enable"))
99 ValueInt = 1;
100 else {
Eli Benderskyf6377902014-06-19 18:30:15 +0000101 S.Diag(ValueLoc->Loc, diag::err_pragma_loop_invalid_keyword);
Eli Bendersky06a40422014-06-06 20:31:48 +0000102 return nullptr;
103 }
104 } else if (Option == LoopHintAttr::VectorizeWidth ||
Eli Bendersky86483b32014-06-11 17:56:26 +0000105 Option == LoopHintAttr::InterleaveCount ||
106 Option == LoopHintAttr::UnrollCount) {
Eli Bendersky06a40422014-06-06 20:31:48 +0000107 // FIXME: We should support template parameters for the loop hint value.
108 // See bug report #19610.
109 llvm::APSInt ValueAPS;
Eli Benderskyf6377902014-06-19 18:30:15 +0000110 if (!ValueExpr || !ValueExpr->isIntegerConstantExpr(ValueAPS, S.Context) ||
111 (ValueInt = ValueAPS.getSExtValue()) < 1) {
112 S.Diag(ValueLoc->Loc, diag::err_pragma_loop_invalid_value);
Eli Bendersky06a40422014-06-06 20:31:48 +0000113 return nullptr;
114 }
115 } else
116 llvm_unreachable("Unknown loop hint option");
117
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000118 return LoopHintAttr::CreateImplicit(S.Context, Spelling, Option, ValueInt,
Eli Bendersky06a40422014-06-06 20:31:48 +0000119 A.getRange());
120}
121
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000122static void CheckForIncompatibleAttributes(
123 Sema &S, const SmallVectorImpl<const Attr *> &Attrs) {
Eli Bendersky86483b32014-06-11 17:56:26 +0000124 // There are 3 categories of loop hints: vectorize, interleave, and
125 // unroll. Each comes in two variants: an enable/disable form and a
126 // form which takes a numeric argument. For example:
127 // unroll(enable|disable) and unroll_count(N). The following array
128 // accumulate the hints encountered while iterating through the
129 // attributes to check for compatibility.
130 struct {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000131 const LoopHintAttr *EnableAttr;
132 const LoopHintAttr *NumericAttr;
133 } HintAttrs[] = {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}};
Eli Bendersky06a40422014-06-06 20:31:48 +0000134
135 for (const auto *I : Attrs) {
136 const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I);
137
138 // Skip non loop hint attributes
139 if (!LH)
140 continue;
141
Eli Bendersky06a40422014-06-06 20:31:48 +0000142 int Option = LH->getOption();
Eli Bendersky86483b32014-06-11 17:56:26 +0000143 int Category;
Eli Bendersky06a40422014-06-06 20:31:48 +0000144 switch (Option) {
145 case LoopHintAttr::Vectorize:
146 case LoopHintAttr::VectorizeWidth:
Eli Bendersky86483b32014-06-11 17:56:26 +0000147 Category = 0;
Eli Bendersky06a40422014-06-06 20:31:48 +0000148 break;
149 case LoopHintAttr::Interleave:
150 case LoopHintAttr::InterleaveCount:
Eli Bendersky86483b32014-06-11 17:56:26 +0000151 Category = 1;
Eli Bendersky06a40422014-06-06 20:31:48 +0000152 break;
Eli Bendersky86483b32014-06-11 17:56:26 +0000153 case LoopHintAttr::Unroll:
154 case LoopHintAttr::UnrollCount:
155 Category = 2;
156 break;
157 };
Eli Bendersky06a40422014-06-06 20:31:48 +0000158
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000159 auto &CategoryState = HintAttrs[Category];
160 SourceLocation OptionLoc = LH->getRange().getBegin();
161 const LoopHintAttr *PrevAttr;
Eli Bendersky86483b32014-06-11 17:56:26 +0000162 if (Option == LoopHintAttr::Vectorize ||
163 Option == LoopHintAttr::Interleave || Option == LoopHintAttr::Unroll) {
164 // Enable|disable hint. For example, vectorize(enable).
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000165 PrevAttr = CategoryState.EnableAttr;
166 CategoryState.EnableAttr = LH;
Eli Bendersky06a40422014-06-06 20:31:48 +0000167 } else {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000168 // Numeric hint. For example, vectorize_width(8).
169 PrevAttr = CategoryState.NumericAttr;
170 CategoryState.NumericAttr = LH;
Eli Bendersky06a40422014-06-06 20:31:48 +0000171 }
172
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)
176 << /*Duplicate=*/true << PrevAttr->getDiagnosticName()
177 << LH->getDiagnosticName();
178
179 if (CategoryState.EnableAttr && !CategoryState.EnableAttr->getValue() &&
180 CategoryState.NumericAttr) {
Eli Bendersky86483b32014-06-11 17:56:26 +0000181 // Disable hints are not compatible with numeric hints of the
182 // same category.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000183 S.Diag(OptionLoc, diag::err_pragma_loop_compatibility)
Eli Bendersky86483b32014-06-11 17:56:26 +0000184 << /*Duplicate=*/false
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000185 << CategoryState.EnableAttr->getDiagnosticName()
186 << CategoryState.NumericAttr->getDiagnosticName();
Eli Bendersky86483b32014-06-11 17:56:26 +0000187 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000188 }
189}
190
191static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const AttributeList &A,
192 SourceRange Range) {
193 switch (A.getKind()) {
Michael Han23214e52012-10-03 01:56:22 +0000194 case AttributeList::UnknownAttribute:
195 S.Diag(A.getLoc(), A.isDeclspecAttribute() ?
196 diag::warn_unhandled_ms_attribute_ignored :
197 diag::warn_unknown_attribute_ignored) << A.getName();
Eli Bendersky06a40422014-06-06 20:31:48 +0000198 return nullptr;
199 case AttributeList::AT_FallThrough:
200 return handleFallThroughAttr(S, St, A, Range);
201 case AttributeList::AT_LoopHint:
202 return handleLoopHintAttr(S, St, A, Range);
203 default:
204 // if we're here, then we parsed a known attribute, but didn't recognize
205 // it as a statement attribute => it is declaration attribute
Richard Smith4c96e992013-02-19 23:47:15 +0000206 S.Diag(A.getRange().getBegin(), diag::err_attribute_invalid_on_stmt)
207 << A.getName() << St->getLocStart();
Craig Topperc3ec1492014-05-26 06:22:03 +0000208 return nullptr;
Richard Smithc202b282012-04-14 00:33:13 +0000209 }
210}
211
212StmtResult Sema::ProcessStmtAttributes(Stmt *S, AttributeList *AttrList,
213 SourceRange Range) {
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000214 SmallVector<const Attr*, 8> Attrs;
Richard Smithc202b282012-04-14 00:33:13 +0000215 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Richard Smith84837d52012-05-03 18:27:39 +0000216 if (Attr *a = ProcessStmtAttribute(*this, S, *l, Range))
Eli Bendersky06a40422014-06-06 20:31:48 +0000217 Attrs.push_back(a);
218 }
219
220 CheckForIncompatibleAttributes(*this, Attrs);
221
222 if (Attrs.empty())
223 return S;
224
Richard Smithc202b282012-04-14 00:33:13 +0000225 return ActOnAttributedStmt(Range.getBegin(), Attrs, S);
226}