blob: 4e3999bf737e0732361ee423e0eea2d6103d0706 [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"
Aaron Ballmanb06b15a2014-06-06 12:40:24 +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,
Aaron Ballmanb06b15a2014-06-06 12:40:24 +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 =
65 llvm::StringSwitch<LoopHintAttr::OptionType>(OptionInfo->getNameStart())
66 .Case("vectorize", LoopHintAttr::Vectorize)
67 .Case("vectorize_width", LoopHintAttr::VectorizeWidth)
68 .Case("interleave", LoopHintAttr::Interleave)
69 .Case("interleave_count", LoopHintAttr::InterleaveCount)
70 .Default(LoopHintAttr::Vectorize);
71
72 int ValueInt;
73 if (Option == LoopHintAttr::Vectorize || Option == LoopHintAttr::Interleave) {
74 if (!ValueInfo) {
75 S.Diag(ValueLoc->Loc, diag::err_pragma_loop_invalid_keyword)
76 << /*MissingKeyword=*/true << "";
77 return nullptr;
78 }
79
80 if (ValueInfo->isStr("disable"))
81 ValueInt = 0;
82 else if (ValueInfo->isStr("enable"))
83 ValueInt = 1;
84 else {
85 S.Diag(ValueLoc->Loc, diag::err_pragma_loop_invalid_keyword)
86 << /*MissingKeyword=*/false << ValueInfo;
87 return nullptr;
88 }
89 } else if (Option == LoopHintAttr::VectorizeWidth ||
90 Option == LoopHintAttr::InterleaveCount) {
91 // FIXME: We should support template parameters for the loop hint value.
92 // See bug report #19610.
93 llvm::APSInt ValueAPS;
94 if (!ValueExpr || !ValueExpr->isIntegerConstantExpr(ValueAPS, S.Context)) {
95 S.Diag(ValueLoc->Loc, diag::err_pragma_loop_invalid_value)
96 << /*MissingValue=*/true << "";
97 return nullptr;
98 }
99
100 if ((ValueInt = ValueAPS.getSExtValue()) < 1) {
101 S.Diag(ValueLoc->Loc, diag::err_pragma_loop_invalid_value)
102 << /*MissingValue=*/false << ValueInt;
103 return nullptr;
104 }
105 }
106
107 return LoopHintAttr::CreateImplicit(S.Context, Option, ValueInt,
108 A.getRange());
109}
110
111static void
112CheckForIncompatibleAttributes(Sema &S, SmallVectorImpl<const Attr *> &Attrs) {
113 int PrevOptionValue[4] = {-1, -1, -1, -1};
114 int OptionId[4] = {LoopHintAttr::Vectorize, LoopHintAttr::VectorizeWidth,
115 LoopHintAttr::Interleave, LoopHintAttr::InterleaveCount};
116
117 for (const auto *I : Attrs) {
118 const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I);
119
120 // Skip non loop hint attributes
121 if (!LH)
122 continue;
123
124 int State, Value;
125 int Option = LH->getOption();
126 int ValueInt = LH->getValue();
127
128 switch (Option) {
129 case LoopHintAttr::Vectorize:
130 case LoopHintAttr::VectorizeWidth:
131 State = 0;
132 Value = 1;
133 break;
134 case LoopHintAttr::Interleave:
135 case LoopHintAttr::InterleaveCount:
136 State = 2;
137 Value = 3;
138 break;
139 }
140
141 SourceLocation ValueLoc = LH->getRange().getEnd();
142
143 // Compatibility testing is split into two cases.
144 // 1. if the current loop hint sets state (enable/disable) - check against
145 // previous state and value.
146 // 2. if the current loop hint sets a value - check against previous state
147 // and value.
148
149 if (Option == State) {
150 if (PrevOptionValue[State] != -1) {
151 // Cannot specify state twice.
152 int PrevValue = PrevOptionValue[State];
153 S.Diag(ValueLoc, diag::err_pragma_loop_compatibility)
154 << /*Duplicate=*/true << LoopHintAttr::getOptionName(Option)
155 << LoopHintAttr::getValueName(PrevValue)
156 << LoopHintAttr::getOptionName(Option)
157 << LoopHintAttr::getValueName(Value);
158 }
159
160 if (PrevOptionValue[Value] != -1) {
161 // Compare state with previous width/count.
162 int PrevOption = OptionId[Value];
163 int PrevValueInt = PrevOptionValue[Value];
164 if ((ValueInt == 0 && PrevValueInt > 1) ||
165 (ValueInt == 1 && PrevValueInt <= 1))
166 S.Diag(ValueLoc, diag::err_pragma_loop_compatibility)
167 << /*Duplicate=*/false << LoopHintAttr::getOptionName(PrevOption)
168 << PrevValueInt << LoopHintAttr::getOptionName(Option)
169 << LoopHintAttr::getValueName(ValueInt);
170 }
171 } else {
172 if (PrevOptionValue[State] != -1) {
173 // Compare width/count value with previous state.
174 int PrevOption = OptionId[State];
175 int PrevValueInt = PrevOptionValue[State];
176 if ((ValueInt > 1 && PrevValueInt == 0) ||
177 (ValueInt <= 1 && PrevValueInt == 1))
178 S.Diag(ValueLoc, diag::err_pragma_loop_compatibility)
179 << /*Duplicate=*/false << LoopHintAttr::getOptionName(PrevOption)
180 << LoopHintAttr::getValueName(PrevValueInt)
181 << LoopHintAttr::getOptionName(Option) << ValueInt;
182 }
183
184 if (PrevOptionValue[Value] != -1) {
185 // Cannot specify a width/count twice.
186 int PrevValueInt = PrevOptionValue[Value];
187 S.Diag(ValueLoc, diag::err_pragma_loop_compatibility)
188 << /*Duplicate=*/true << LoopHintAttr::getOptionName(Option)
189 << PrevValueInt << LoopHintAttr::getOptionName(Option) << ValueInt;
190 }
191 }
192
193 PrevOptionValue[Option] = ValueInt;
194 }
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();
Aaron Ballmanb06b15a2014-06-06 12:40:24 +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))
Aaron Ballmanb06b15a2014-06-06 12:40:24 +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}