Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1 | //===--- 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 Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 16 | #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 Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | using namespace sema; |
| 25 | |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 26 | static Attr *handleFallThroughAttr(Sema &S, Stmt *St, const AttributeList &A, |
| 27 | SourceRange Range) { |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 28 | FallThroughAttr Attr(A.getRange(), S.Context, |
| 29 | A.getAttributeSpellingListIndex()); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 30 | if (!isa<NullStmt>(St)) { |
| 31 | S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_wrong_target) |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 32 | << Attr.getSpelling() << St->getLocStart(); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 33 | if (isa<SwitchCase>(St)) { |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 34 | SourceLocation L = S.getLocForEndOfToken(Range.getEnd()); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 35 | S.Diag(L, diag::note_fallthrough_insert_semi_fixit) |
| 36 | << FixItHint::CreateInsertion(L, ";"); |
| 37 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 38 | return nullptr; |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 39 | } |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 40 | auto *FnScope = S.getCurFunction(); |
| 41 | if (FnScope->SwitchStack.empty()) { |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 42 | S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_outside_switch); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 43 | return nullptr; |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 44 | } |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 45 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 46 | // If this is spelled as the standard C++17 attribute, but not in C++17, warn |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 47 | // about using it as an extension. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 48 | if (!S.getLangOpts().CPlusPlus17 && A.isCXX11Attribute() && |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 49 | !A.getScopeName()) |
Richard Smith | b115e5d | 2017-08-13 23:37:29 +0000 | [diff] [blame] | 50 | S.Diag(A.getLoc(), diag::ext_cxx17_attr) << A.getName(); |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 51 | |
| 52 | FnScope->setHasFallthroughStmt(); |
| 53 | return ::new (S.Context) auto(Attr); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 56 | static Attr *handleSuppressAttr(Sema &S, Stmt *St, const AttributeList &A, |
| 57 | SourceRange Range) { |
| 58 | if (A.getNumArgs() < 1) { |
| 59 | S.Diag(A.getLoc(), diag::err_attribute_too_few_arguments) |
| 60 | << A.getName() << 1; |
| 61 | return nullptr; |
| 62 | } |
| 63 | |
| 64 | std::vector<StringRef> DiagnosticIdentifiers; |
| 65 | for (unsigned I = 0, E = A.getNumArgs(); I != E; ++I) { |
| 66 | StringRef RuleName; |
| 67 | |
| 68 | if (!S.checkStringLiteralArgumentAttr(A, I, RuleName, nullptr)) |
| 69 | return nullptr; |
| 70 | |
| 71 | // FIXME: Warn if the rule name is unknown. This is tricky because only |
| 72 | // clang-tidy knows about available rules. |
| 73 | DiagnosticIdentifiers.push_back(RuleName); |
| 74 | } |
| 75 | |
| 76 | return ::new (S.Context) SuppressAttr( |
| 77 | A.getRange(), S.Context, DiagnosticIdentifiers.data(), |
| 78 | DiagnosticIdentifiers.size(), A.getAttributeSpellingListIndex()); |
| 79 | } |
| 80 | |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 81 | static Attr *handleLoopHintAttr(Sema &S, Stmt *St, const AttributeList &A, |
| 82 | SourceRange) { |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 83 | IdentifierLoc *PragmaNameLoc = A.getArgAsIdent(0); |
| 84 | IdentifierLoc *OptionLoc = A.getArgAsIdent(1); |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 85 | IdentifierLoc *StateLoc = A.getArgAsIdent(2); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 86 | Expr *ValueExpr = A.getArgAsExpr(3); |
| 87 | |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 88 | bool PragmaUnroll = PragmaNameLoc->Ident->getName() == "unroll"; |
| 89 | bool PragmaNoUnroll = PragmaNameLoc->Ident->getName() == "nounroll"; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 90 | if (St->getStmtClass() != Stmt::DoStmtClass && |
| 91 | St->getStmtClass() != Stmt::ForStmtClass && |
| 92 | St->getStmtClass() != Stmt::CXXForRangeStmtClass && |
| 93 | St->getStmtClass() != Stmt::WhileStmtClass) { |
Mark Heffernan | c888e41 | 2014-07-24 18:09:38 +0000 | [diff] [blame] | 94 | const char *Pragma = |
| 95 | llvm::StringSwitch<const char *>(PragmaNameLoc->Ident->getName()) |
| 96 | .Case("unroll", "#pragma unroll") |
| 97 | .Case("nounroll", "#pragma nounroll") |
| 98 | .Default("#pragma clang loop"); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 99 | S.Diag(St->getLocStart(), diag::err_pragma_loop_precedes_nonloop) << Pragma; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 100 | return nullptr; |
| 101 | } |
| 102 | |
Erich Keane | be89f4c5 | 2017-08-09 15:27:36 +0000 | [diff] [blame] | 103 | LoopHintAttr::Spelling Spelling = |
| 104 | LoopHintAttr::Spelling(A.getAttributeSpellingListIndex()); |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 105 | LoopHintAttr::OptionType Option; |
| 106 | LoopHintAttr::LoopHintState State; |
| 107 | if (PragmaNoUnroll) { |
| 108 | // #pragma nounroll |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 109 | Option = LoopHintAttr::Unroll; |
| 110 | State = LoopHintAttr::Disable; |
| 111 | } else if (PragmaUnroll) { |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 112 | if (ValueExpr) { |
| 113 | // #pragma unroll N |
| 114 | Option = LoopHintAttr::UnrollCount; |
| 115 | State = LoopHintAttr::Numeric; |
| 116 | } else { |
| 117 | // #pragma unroll |
| 118 | Option = LoopHintAttr::Unroll; |
| 119 | State = LoopHintAttr::Enable; |
| 120 | } |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 121 | } else { |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 122 | // #pragma clang loop ... |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 123 | assert(OptionLoc && OptionLoc->Ident && |
| 124 | "Attribute must have valid option info."); |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 125 | Option = llvm::StringSwitch<LoopHintAttr::OptionType>( |
| 126 | OptionLoc->Ident->getName()) |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 127 | .Case("vectorize", LoopHintAttr::Vectorize) |
| 128 | .Case("vectorize_width", LoopHintAttr::VectorizeWidth) |
| 129 | .Case("interleave", LoopHintAttr::Interleave) |
| 130 | .Case("interleave_count", LoopHintAttr::InterleaveCount) |
| 131 | .Case("unroll", LoopHintAttr::Unroll) |
| 132 | .Case("unroll_count", LoopHintAttr::UnrollCount) |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 133 | .Case("distribute", LoopHintAttr::Distribute) |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 134 | .Default(LoopHintAttr::Vectorize); |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 135 | if (Option == LoopHintAttr::VectorizeWidth || |
| 136 | Option == LoopHintAttr::InterleaveCount || |
| 137 | Option == LoopHintAttr::UnrollCount) { |
| 138 | assert(ValueExpr && "Attribute must have a valid value expression."); |
| 139 | if (S.CheckLoopHintExpr(ValueExpr, St->getLocStart())) |
| 140 | return nullptr; |
| 141 | State = LoopHintAttr::Numeric; |
| 142 | } else if (Option == LoopHintAttr::Vectorize || |
| 143 | Option == LoopHintAttr::Interleave || |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 144 | Option == LoopHintAttr::Unroll || |
| 145 | Option == LoopHintAttr::Distribute) { |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 146 | assert(StateLoc && StateLoc->Ident && "Loop hint must have an argument"); |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 147 | if (StateLoc->Ident->isStr("disable")) |
| 148 | State = LoopHintAttr::Disable; |
Tyler Nowicki | 9d268e1 | 2015-06-11 23:23:17 +0000 | [diff] [blame] | 149 | else if (StateLoc->Ident->isStr("assume_safety")) |
| 150 | State = LoopHintAttr::AssumeSafety; |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 151 | else if (StateLoc->Ident->isStr("full")) |
| 152 | State = LoopHintAttr::Full; |
| 153 | else if (StateLoc->Ident->isStr("enable")) |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 154 | State = LoopHintAttr::Enable; |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 155 | else |
| 156 | llvm_unreachable("bad loop hint argument"); |
| 157 | } else |
| 158 | llvm_unreachable("bad loop hint"); |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 159 | } |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 160 | |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 161 | return LoopHintAttr::CreateImplicit(S.Context, Spelling, Option, State, |
Tyler Nowicki | c724a83e | 2014-10-12 20:46:07 +0000 | [diff] [blame] | 162 | ValueExpr, A.getRange()); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 165 | static void |
| 166 | CheckForIncompatibleAttributes(Sema &S, |
| 167 | const SmallVectorImpl<const Attr *> &Attrs) { |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 168 | // There are 4 categories of loop hints attributes: vectorize, interleave, |
| 169 | // unroll and distribute. Except for distribute they come in two variants: a |
| 170 | // state form and a numeric form. The state form selectively |
| 171 | // defaults/enables/disables the transformation for the loop (for unroll, |
| 172 | // default indicates full unrolling rather than enabling the transformation). |
| 173 | // The numeric form form provides an integer hint (for example, unroll count) |
| 174 | // to the transformer. The following array accumulates the hints encountered |
| 175 | // while iterating through the attributes to check for compatibility. |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 176 | struct { |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 177 | const LoopHintAttr *StateAttr; |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 178 | const LoopHintAttr *NumericAttr; |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 179 | } HintAttrs[] = {{nullptr, nullptr}, |
| 180 | {nullptr, nullptr}, |
| 181 | {nullptr, nullptr}, |
| 182 | {nullptr, nullptr}}; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 183 | |
| 184 | for (const auto *I : Attrs) { |
| 185 | const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I); |
| 186 | |
| 187 | // Skip non loop hint attributes |
| 188 | if (!LH) |
| 189 | continue; |
| 190 | |
Craig Topper | ec9be54 | 2015-12-23 05:44:43 +0000 | [diff] [blame] | 191 | LoopHintAttr::OptionType Option = LH->getOption(); |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 192 | enum { Vectorize, Interleave, Unroll, Distribute } Category; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 193 | switch (Option) { |
| 194 | case LoopHintAttr::Vectorize: |
| 195 | case LoopHintAttr::VectorizeWidth: |
Mark Heffernan | 450c238 | 2014-07-23 17:31:31 +0000 | [diff] [blame] | 196 | Category = Vectorize; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 197 | break; |
| 198 | case LoopHintAttr::Interleave: |
| 199 | case LoopHintAttr::InterleaveCount: |
Mark Heffernan | 450c238 | 2014-07-23 17:31:31 +0000 | [diff] [blame] | 200 | Category = Interleave; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 201 | break; |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 202 | case LoopHintAttr::Unroll: |
| 203 | case LoopHintAttr::UnrollCount: |
Mark Heffernan | 450c238 | 2014-07-23 17:31:31 +0000 | [diff] [blame] | 204 | Category = Unroll; |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 205 | break; |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 206 | case LoopHintAttr::Distribute: |
| 207 | // Perform the check for duplicated 'distribute' hints. |
| 208 | Category = Distribute; |
| 209 | break; |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 210 | }; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 211 | |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 212 | auto &CategoryState = HintAttrs[Category]; |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 213 | const LoopHintAttr *PrevAttr; |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 214 | if (Option == LoopHintAttr::Vectorize || |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 215 | Option == LoopHintAttr::Interleave || Option == LoopHintAttr::Unroll || |
| 216 | Option == LoopHintAttr::Distribute) { |
Tyler Nowicki | 9d268e1 | 2015-06-11 23:23:17 +0000 | [diff] [blame] | 217 | // Enable|Disable|AssumeSafety hint. For example, vectorize(enable). |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 218 | PrevAttr = CategoryState.StateAttr; |
| 219 | CategoryState.StateAttr = LH; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 220 | } else { |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 221 | // Numeric hint. For example, vectorize_width(8). |
| 222 | PrevAttr = CategoryState.NumericAttr; |
| 223 | CategoryState.NumericAttr = LH; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Tyler Nowicki | 884fc61 | 2014-07-29 17:21:32 +0000 | [diff] [blame] | 226 | PrintingPolicy Policy(S.Context.getLangOpts()); |
| 227 | SourceLocation OptionLoc = LH->getRange().getBegin(); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 228 | if (PrevAttr) |
| 229 | // Cannot specify same type of attribute twice. |
| 230 | S.Diag(OptionLoc, diag::err_pragma_loop_compatibility) |
Tyler Nowicki | 884fc61 | 2014-07-29 17:21:32 +0000 | [diff] [blame] | 231 | << /*Duplicate=*/true << PrevAttr->getDiagnosticName(Policy) |
| 232 | << LH->getDiagnosticName(Policy); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 233 | |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 234 | if (CategoryState.StateAttr && CategoryState.NumericAttr && |
| 235 | (Category == Unroll || |
| 236 | CategoryState.StateAttr->getState() == LoopHintAttr::Disable)) { |
Mark Heffernan | 450c238 | 2014-07-23 17:31:31 +0000 | [diff] [blame] | 237 | // Disable hints are not compatible with numeric hints of the same |
| 238 | // category. As a special case, numeric unroll hints are also not |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 239 | // compatible with enable or full form of the unroll pragma because these |
| 240 | // directives indicate full unrolling. |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 241 | S.Diag(OptionLoc, diag::err_pragma_loop_compatibility) |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 242 | << /*Duplicate=*/false |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 243 | << CategoryState.StateAttr->getDiagnosticName(Policy) |
Tyler Nowicki | 884fc61 | 2014-07-29 17:21:32 +0000 | [diff] [blame] | 244 | << CategoryState.NumericAttr->getDiagnosticName(Policy); |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 245 | } |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
Anastasia Stulova | 6bdbcbb | 2016-02-19 18:30:11 +0000 | [diff] [blame] | 249 | static Attr *handleOpenCLUnrollHint(Sema &S, Stmt *St, const AttributeList &A, |
| 250 | SourceRange Range) { |
Egor Churaev | 24939d4 | 2016-12-13 14:02:35 +0000 | [diff] [blame] | 251 | // Although the feature was introduced only in OpenCL C v2.0 s6.11.5, it's |
| 252 | // useful for OpenCL 1.x too and doesn't require HW support. |
| 253 | // opencl_unroll_hint can have 0 arguments (compiler |
Anastasia Stulova | 6bdbcbb | 2016-02-19 18:30:11 +0000 | [diff] [blame] | 254 | // determines unrolling factor) or 1 argument (the unroll factor provided |
| 255 | // by the user). |
| 256 | |
Anastasia Stulova | 6bdbcbb | 2016-02-19 18:30:11 +0000 | [diff] [blame] | 257 | unsigned NumArgs = A.getNumArgs(); |
| 258 | |
| 259 | if (NumArgs > 1) { |
| 260 | S.Diag(A.getLoc(), diag::err_attribute_too_many_arguments) << A.getName() |
| 261 | << 1; |
| 262 | return nullptr; |
| 263 | } |
| 264 | |
| 265 | unsigned UnrollFactor = 0; |
| 266 | |
| 267 | if (NumArgs == 1) { |
| 268 | Expr *E = A.getArgAsExpr(0); |
| 269 | llvm::APSInt ArgVal(32); |
| 270 | |
| 271 | if (!E->isIntegerConstantExpr(ArgVal, S.Context)) { |
| 272 | S.Diag(A.getLoc(), diag::err_attribute_argument_type) |
| 273 | << A.getName() << AANT_ArgumentIntegerConstant << E->getSourceRange(); |
| 274 | return nullptr; |
| 275 | } |
| 276 | |
| 277 | int Val = ArgVal.getSExtValue(); |
| 278 | |
| 279 | if (Val <= 0) { |
| 280 | S.Diag(A.getRange().getBegin(), |
| 281 | diag::err_attribute_requires_positive_integer) |
| 282 | << A.getName(); |
| 283 | return nullptr; |
| 284 | } |
| 285 | UnrollFactor = Val; |
| 286 | } |
| 287 | |
| 288 | return OpenCLUnrollHintAttr::CreateImplicit(S.Context, UnrollFactor); |
| 289 | } |
| 290 | |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 291 | static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const AttributeList &A, |
| 292 | SourceRange Range) { |
| 293 | switch (A.getKind()) { |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 294 | case AttributeList::UnknownAttribute: |
| 295 | S.Diag(A.getLoc(), A.isDeclspecAttribute() ? |
| 296 | diag::warn_unhandled_ms_attribute_ignored : |
| 297 | diag::warn_unknown_attribute_ignored) << A.getName(); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 298 | return nullptr; |
| 299 | case AttributeList::AT_FallThrough: |
| 300 | return handleFallThroughAttr(S, St, A, Range); |
| 301 | case AttributeList::AT_LoopHint: |
| 302 | return handleLoopHintAttr(S, St, A, Range); |
Anastasia Stulova | 6bdbcbb | 2016-02-19 18:30:11 +0000 | [diff] [blame] | 303 | case AttributeList::AT_OpenCLUnrollHint: |
| 304 | return handleOpenCLUnrollHint(S, St, A, Range); |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 305 | case AttributeList::AT_Suppress: |
| 306 | return handleSuppressAttr(S, St, A, Range); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 307 | default: |
| 308 | // if we're here, then we parsed a known attribute, but didn't recognize |
| 309 | // it as a statement attribute => it is declaration attribute |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 310 | S.Diag(A.getRange().getBegin(), diag::err_decl_attribute_invalid_on_stmt) |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 311 | << A.getName() << St->getLocStart(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 312 | return nullptr; |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
| 316 | StmtResult Sema::ProcessStmtAttributes(Stmt *S, AttributeList *AttrList, |
| 317 | SourceRange Range) { |
Alexander Kornienko | 20f6fc6 | 2012-07-09 10:04:07 +0000 | [diff] [blame] | 318 | SmallVector<const Attr*, 8> Attrs; |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 319 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 320 | if (Attr *a = ProcessStmtAttribute(*this, S, *l, Range)) |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 321 | Attrs.push_back(a); |
| 322 | } |
| 323 | |
| 324 | CheckForIncompatibleAttributes(*this, Attrs); |
| 325 | |
| 326 | if (Attrs.empty()) |
| 327 | return S; |
| 328 | |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 329 | return ActOnAttributedStmt(Range.getBegin(), Attrs, S); |
| 330 | } |