blob: 44169c2fdce88cc11020500879765a917d9612cf [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) {
48 if (St->getStmtClass() != Stmt::DoStmtClass &&
49 St->getStmtClass() != Stmt::ForStmtClass &&
50 St->getStmtClass() != Stmt::CXXForRangeStmtClass &&
51 St->getStmtClass() != Stmt::WhileStmtClass) {
52 S.Diag(St->getLocStart(), diag::err_pragma_loop_precedes_nonloop);
53 return nullptr;
54 }
55
56 IdentifierLoc *OptionLoc = A.getArgAsIdent(0);
57 IdentifierInfo *OptionInfo = OptionLoc->Ident;
58 IdentifierLoc *ValueLoc = A.getArgAsIdent(1);
59 IdentifierInfo *ValueInfo = ValueLoc->Ident;
60 Expr *ValueExpr = A.getArgAsExpr(2);
61
62 assert(OptionInfo && "Attribute must have valid option info.");
63
64 LoopHintAttr::OptionType Option =
Alp Toker541d5072014-06-07 23:30:53 +000065 llvm::StringSwitch<LoopHintAttr::OptionType>(OptionInfo->getName())
Eli Bendersky06a40422014-06-06 20:31:48 +000066 .Case("vectorize", LoopHintAttr::Vectorize)
67 .Case("vectorize_width", LoopHintAttr::VectorizeWidth)
68 .Case("interleave", LoopHintAttr::Interleave)
69 .Case("interleave_count", LoopHintAttr::InterleaveCount)
Eli Bendersky86483b32014-06-11 17:56:26 +000070 .Case("unroll", LoopHintAttr::Unroll)
71 .Case("unroll_count", LoopHintAttr::UnrollCount)
Eli Bendersky06a40422014-06-06 20:31:48 +000072 .Default(LoopHintAttr::Vectorize);
73
74 int ValueInt;
Eli Bendersky86483b32014-06-11 17:56:26 +000075 if (Option == LoopHintAttr::Vectorize || Option == LoopHintAttr::Interleave ||
76 Option == LoopHintAttr::Unroll) {
Eli Bendersky06a40422014-06-06 20:31:48 +000077 if (!ValueInfo) {
Eli Benderskyf6377902014-06-19 18:30:15 +000078 S.Diag(ValueLoc->Loc, diag::err_pragma_loop_invalid_keyword);
Eli Bendersky06a40422014-06-06 20:31:48 +000079 return nullptr;
80 }
Eli Bendersky06a40422014-06-06 20:31:48 +000081 if (ValueInfo->isStr("disable"))
82 ValueInt = 0;
83 else if (ValueInfo->isStr("enable"))
84 ValueInt = 1;
85 else {
Eli Benderskyf6377902014-06-19 18:30:15 +000086 S.Diag(ValueLoc->Loc, diag::err_pragma_loop_invalid_keyword);
Eli Bendersky06a40422014-06-06 20:31:48 +000087 return nullptr;
88 }
89 } else if (Option == LoopHintAttr::VectorizeWidth ||
Eli Bendersky86483b32014-06-11 17:56:26 +000090 Option == LoopHintAttr::InterleaveCount ||
91 Option == LoopHintAttr::UnrollCount) {
Eli Bendersky06a40422014-06-06 20:31:48 +000092 // FIXME: We should support template parameters for the loop hint value.
93 // See bug report #19610.
94 llvm::APSInt ValueAPS;
Eli Benderskyf6377902014-06-19 18:30:15 +000095 if (!ValueExpr || !ValueExpr->isIntegerConstantExpr(ValueAPS, S.Context) ||
96 (ValueInt = ValueAPS.getSExtValue()) < 1) {
97 S.Diag(ValueLoc->Loc, diag::err_pragma_loop_invalid_value);
Eli Bendersky06a40422014-06-06 20:31:48 +000098 return nullptr;
99 }
100 } else
101 llvm_unreachable("Unknown loop hint option");
102
103 return LoopHintAttr::CreateImplicit(S.Context, Option, ValueInt,
104 A.getRange());
105}
106
107static void
108CheckForIncompatibleAttributes(Sema &S, SmallVectorImpl<const Attr *> &Attrs) {
Eli Bendersky86483b32014-06-11 17:56:26 +0000109 // There are 3 categories of loop hints: vectorize, interleave, and
110 // unroll. Each comes in two variants: an enable/disable form and a
111 // form which takes a numeric argument. For example:
112 // unroll(enable|disable) and unroll_count(N). The following array
113 // accumulate the hints encountered while iterating through the
114 // attributes to check for compatibility.
115 struct {
116 int EnableOptionId;
117 int NumericOptionId;
118 bool EnabledIsSet;
119 bool ValueIsSet;
120 bool Enabled;
121 int Value;
Eli Bendersky96e1ee12014-06-12 15:47:57 +0000122 } Options[] = {{LoopHintAttr::Vectorize, LoopHintAttr::VectorizeWidth, false,
123 false, false, 0},
124 {LoopHintAttr::Interleave, LoopHintAttr::InterleaveCount,
125 false, false, false, 0},
126 {LoopHintAttr::Unroll, LoopHintAttr::UnrollCount, false, false,
127 false, 0}};
Eli Bendersky06a40422014-06-06 20:31:48 +0000128
129 for (const auto *I : Attrs) {
130 const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I);
131
132 // Skip non loop hint attributes
133 if (!LH)
134 continue;
135
Eli Bendersky06a40422014-06-06 20:31:48 +0000136 int Option = LH->getOption();
137 int ValueInt = LH->getValue();
138
Eli Bendersky86483b32014-06-11 17:56:26 +0000139 int Category;
Eli Bendersky06a40422014-06-06 20:31:48 +0000140 switch (Option) {
141 case LoopHintAttr::Vectorize:
142 case LoopHintAttr::VectorizeWidth:
Eli Bendersky86483b32014-06-11 17:56:26 +0000143 Category = 0;
Eli Bendersky06a40422014-06-06 20:31:48 +0000144 break;
145 case LoopHintAttr::Interleave:
146 case LoopHintAttr::InterleaveCount:
Eli Bendersky86483b32014-06-11 17:56:26 +0000147 Category = 1;
Eli Bendersky06a40422014-06-06 20:31:48 +0000148 break;
Eli Bendersky86483b32014-06-11 17:56:26 +0000149 case LoopHintAttr::Unroll:
150 case LoopHintAttr::UnrollCount:
151 Category = 2;
152 break;
153 };
Eli Bendersky06a40422014-06-06 20:31:48 +0000154
Eli Bendersky86483b32014-06-11 17:56:26 +0000155 auto &CategoryState = Options[Category];
Eli Bendersky06a40422014-06-06 20:31:48 +0000156 SourceLocation ValueLoc = LH->getRange().getEnd();
Eli Bendersky86483b32014-06-11 17:56:26 +0000157 if (Option == LoopHintAttr::Vectorize ||
158 Option == LoopHintAttr::Interleave || Option == LoopHintAttr::Unroll) {
159 // Enable|disable hint. For example, vectorize(enable).
160 if (CategoryState.EnabledIsSet) {
161 // Cannot specify enable/disable state twice.
Eli Bendersky06a40422014-06-06 20:31:48 +0000162 S.Diag(ValueLoc, diag::err_pragma_loop_compatibility)
163 << /*Duplicate=*/true << LoopHintAttr::getOptionName(Option)
Eli Bendersky86483b32014-06-11 17:56:26 +0000164 << LoopHintAttr::getValueName(CategoryState.Enabled)
Eli Bendersky06a40422014-06-06 20:31:48 +0000165 << LoopHintAttr::getOptionName(Option)
Eli Bendersky86483b32014-06-11 17:56:26 +0000166 << LoopHintAttr::getValueName(ValueInt);
Eli Bendersky06a40422014-06-06 20:31:48 +0000167 }
Eli Bendersky86483b32014-06-11 17:56:26 +0000168 CategoryState.EnabledIsSet = true;
169 CategoryState.Enabled = ValueInt;
Eli Bendersky06a40422014-06-06 20:31:48 +0000170 } else {
Eli Bendersky86483b32014-06-11 17:56:26 +0000171 // Numeric hint. For example, unroll_count(8).
172 if (CategoryState.ValueIsSet) {
173 // Cannot specify numeric hint twice.
Eli Bendersky06a40422014-06-06 20:31:48 +0000174 S.Diag(ValueLoc, diag::err_pragma_loop_compatibility)
175 << /*Duplicate=*/true << LoopHintAttr::getOptionName(Option)
Eli Bendersky86483b32014-06-11 17:56:26 +0000176 << CategoryState.Value << LoopHintAttr::getOptionName(Option)
177 << ValueInt;
Eli Bendersky06a40422014-06-06 20:31:48 +0000178 }
Eli Bendersky86483b32014-06-11 17:56:26 +0000179 CategoryState.ValueIsSet = true;
180 CategoryState.Value = ValueInt;
Eli Bendersky06a40422014-06-06 20:31:48 +0000181 }
182
Eli Bendersky86483b32014-06-11 17:56:26 +0000183 if (CategoryState.EnabledIsSet && !CategoryState.Enabled &&
184 CategoryState.ValueIsSet) {
185 // Disable hints are not compatible with numeric hints of the
186 // same category.
187 S.Diag(ValueLoc, diag::err_pragma_loop_compatibility)
188 << /*Duplicate=*/false
189 << LoopHintAttr::getOptionName(CategoryState.EnableOptionId)
190 << LoopHintAttr::getValueName(CategoryState.Enabled)
191 << LoopHintAttr::getOptionName(CategoryState.NumericOptionId)
192 << CategoryState.Value;
193 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000194 }
195}
196
197static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const AttributeList &A,
198 SourceRange Range) {
199 switch (A.getKind()) {
Michael Han23214e52012-10-03 01:56:22 +0000200 case AttributeList::UnknownAttribute:
201 S.Diag(A.getLoc(), A.isDeclspecAttribute() ?
202 diag::warn_unhandled_ms_attribute_ignored :
203 diag::warn_unknown_attribute_ignored) << A.getName();
Eli Bendersky06a40422014-06-06 20:31:48 +0000204 return nullptr;
205 case AttributeList::AT_FallThrough:
206 return handleFallThroughAttr(S, St, A, Range);
207 case AttributeList::AT_LoopHint:
208 return handleLoopHintAttr(S, St, A, Range);
209 default:
210 // if we're here, then we parsed a known attribute, but didn't recognize
211 // it as a statement attribute => it is declaration attribute
Richard Smith4c96e992013-02-19 23:47:15 +0000212 S.Diag(A.getRange().getBegin(), diag::err_attribute_invalid_on_stmt)
213 << A.getName() << St->getLocStart();
Craig Topperc3ec1492014-05-26 06:22:03 +0000214 return nullptr;
Richard Smithc202b282012-04-14 00:33:13 +0000215 }
216}
217
218StmtResult Sema::ProcessStmtAttributes(Stmt *S, AttributeList *AttrList,
219 SourceRange Range) {
Alexander Kornienko20f6fc62012-07-09 10:04:07 +0000220 SmallVector<const Attr*, 8> Attrs;
Richard Smithc202b282012-04-14 00:33:13 +0000221 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Richard Smith84837d52012-05-03 18:27:39 +0000222 if (Attr *a = ProcessStmtAttribute(*this, S, *l, Range))
Eli Bendersky06a40422014-06-06 20:31:48 +0000223 Attrs.push_back(a);
224 }
225
226 CheckForIncompatibleAttributes(*this, Attrs);
227
228 if (Attrs.empty())
229 return S;
230
Richard Smithc202b282012-04-14 00:33:13 +0000231 return ActOnAttributedStmt(Range.getBegin(), Attrs, S);
232}