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) { |
| 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 Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 32 | SourceLocation L = S.getLocForEndOfToken(Range.getEnd()); |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 33 | S.Diag(L, diag::note_fallthrough_insert_semi_fixit) |
| 34 | << FixItHint::CreateInsertion(L, ";"); |
| 35 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 36 | return nullptr; |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 37 | } |
| 38 | if (S.getCurFunction()->SwitchStack.empty()) { |
| 39 | S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_outside_switch); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 40 | return nullptr; |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 41 | } |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 42 | return ::new (S.Context) FallThroughAttr(A.getRange(), S.Context, |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 43 | A.getAttributeSpellingListIndex()); |
| 44 | } |
| 45 | |
| 46 | static Attr *handleLoopHintAttr(Sema &S, Stmt *St, const AttributeList &A, |
| 47 | SourceRange) { |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 48 | IdentifierLoc *PragmaNameLoc = A.getArgAsIdent(0); |
| 49 | IdentifierLoc *OptionLoc = A.getArgAsIdent(1); |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 50 | IdentifierLoc *StateLoc = A.getArgAsIdent(2); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 51 | Expr *ValueExpr = A.getArgAsExpr(3); |
| 52 | |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 53 | bool PragmaUnroll = PragmaNameLoc->Ident->getName() == "unroll"; |
| 54 | bool PragmaNoUnroll = PragmaNameLoc->Ident->getName() == "nounroll"; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 55 | if (St->getStmtClass() != Stmt::DoStmtClass && |
| 56 | St->getStmtClass() != Stmt::ForStmtClass && |
| 57 | St->getStmtClass() != Stmt::CXXForRangeStmtClass && |
| 58 | St->getStmtClass() != Stmt::WhileStmtClass) { |
Mark Heffernan | c888e41 | 2014-07-24 18:09:38 +0000 | [diff] [blame] | 59 | 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 Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 64 | S.Diag(St->getLocStart(), diag::err_pragma_loop_precedes_nonloop) << Pragma; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 65 | return nullptr; |
| 66 | } |
| 67 | |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 68 | LoopHintAttr::OptionType Option; |
| 69 | LoopHintAttr::Spelling Spelling; |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 70 | if (PragmaUnroll) { |
| 71 | Option = ValueExpr ? LoopHintAttr::UnrollCount : LoopHintAttr::Unroll; |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 72 | Spelling = LoopHintAttr::Pragma_unroll; |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 73 | } else if (PragmaNoUnroll) { |
Mark Heffernan | c888e41 | 2014-07-24 18:09:38 +0000 | [diff] [blame] | 74 | Option = LoopHintAttr::Unroll; |
| 75 | Spelling = LoopHintAttr::Pragma_nounroll; |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 76 | } else { |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 77 | assert(OptionLoc && OptionLoc->Ident && |
| 78 | "Attribute must have valid option info."); |
| 79 | IdentifierInfo *OptionInfo = OptionLoc->Ident; |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 80 | Option = llvm::StringSwitch<LoopHintAttr::OptionType>(OptionInfo->getName()) |
| 81 | .Case("vectorize", LoopHintAttr::Vectorize) |
| 82 | .Case("vectorize_width", LoopHintAttr::VectorizeWidth) |
| 83 | .Case("interleave", LoopHintAttr::Interleave) |
| 84 | .Case("interleave_count", LoopHintAttr::InterleaveCount) |
| 85 | .Case("unroll", LoopHintAttr::Unroll) |
| 86 | .Case("unroll_count", LoopHintAttr::UnrollCount) |
| 87 | .Default(LoopHintAttr::Vectorize); |
| 88 | Spelling = LoopHintAttr::Pragma_clang_loop; |
| 89 | } |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 90 | |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 91 | int ValueInt = 1; |
| 92 | LoopHintAttr::LoopHintState State = LoopHintAttr::Default; |
| 93 | if (PragmaNoUnroll) { |
| 94 | State = LoopHintAttr::Disable; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 95 | } else if (Option == LoopHintAttr::VectorizeWidth || |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 96 | Option == LoopHintAttr::InterleaveCount || |
| 97 | Option == LoopHintAttr::UnrollCount) { |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 98 | // FIXME: We should support template parameters for the loop hint value. |
| 99 | // See bug report #19610. |
| 100 | llvm::APSInt ValueAPS; |
Eli Bendersky | f637790 | 2014-06-19 18:30:15 +0000 | [diff] [blame] | 101 | if (!ValueExpr || !ValueExpr->isIntegerConstantExpr(ValueAPS, S.Context) || |
| 102 | (ValueInt = ValueAPS.getSExtValue()) < 1) { |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 103 | S.Diag(A.getLoc(), diag::err_pragma_loop_invalid_value); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 104 | return nullptr; |
| 105 | } |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 106 | } else if (Option == LoopHintAttr::Vectorize || |
| 107 | Option == LoopHintAttr::Interleave || |
| 108 | Option == LoopHintAttr::Unroll) { |
| 109 | // Default state is assumed if StateLoc is not specified, such as with |
| 110 | // '#pragma unroll'. |
| 111 | if (StateLoc && StateLoc->Ident) { |
| 112 | if (StateLoc->Ident->isStr("disable")) |
| 113 | State = LoopHintAttr::Disable; |
| 114 | else |
| 115 | State = LoopHintAttr::Enable; |
| 116 | } |
| 117 | } |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 118 | |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 119 | return LoopHintAttr::CreateImplicit(S.Context, Spelling, Option, State, |
| 120 | ValueInt, A.getRange()); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 123 | static void |
| 124 | CheckForIncompatibleAttributes(Sema &S, |
| 125 | const SmallVectorImpl<const Attr *> &Attrs) { |
| 126 | // There are 3 categories of loop hints attributes: vectorize, interleave, |
| 127 | // and unroll. Each comes in two variants: a state form and a numeric form. |
| 128 | // The state form selectively defaults/enables/disables the transformation |
| 129 | // for the loop (for unroll, default indicates full unrolling rather than |
| 130 | // enabling the transformation). The numeric form form provides an integer |
| 131 | // hint (for example, unroll count) to the transformer. The following array |
| 132 | // accumulates the hints encountered while iterating through the attributes |
| 133 | // to check for compatibility. |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 134 | struct { |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 135 | const LoopHintAttr *StateAttr; |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 136 | const LoopHintAttr *NumericAttr; |
| 137 | } HintAttrs[] = {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 138 | |
| 139 | for (const auto *I : Attrs) { |
| 140 | const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I); |
| 141 | |
| 142 | // Skip non loop hint attributes |
| 143 | if (!LH) |
| 144 | continue; |
| 145 | |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 146 | int Option = LH->getOption(); |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 147 | int Category; |
Mark Heffernan | 450c238 | 2014-07-23 17:31:31 +0000 | [diff] [blame] | 148 | enum { Vectorize, Interleave, Unroll }; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 149 | switch (Option) { |
| 150 | case LoopHintAttr::Vectorize: |
| 151 | case LoopHintAttr::VectorizeWidth: |
Mark Heffernan | 450c238 | 2014-07-23 17:31:31 +0000 | [diff] [blame] | 152 | Category = Vectorize; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 153 | break; |
| 154 | case LoopHintAttr::Interleave: |
| 155 | case LoopHintAttr::InterleaveCount: |
Mark Heffernan | 450c238 | 2014-07-23 17:31:31 +0000 | [diff] [blame] | 156 | Category = Interleave; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 157 | break; |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 158 | case LoopHintAttr::Unroll: |
| 159 | case LoopHintAttr::UnrollCount: |
Mark Heffernan | 450c238 | 2014-07-23 17:31:31 +0000 | [diff] [blame] | 160 | Category = Unroll; |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 161 | break; |
| 162 | }; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 163 | |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 164 | auto &CategoryState = HintAttrs[Category]; |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 165 | const LoopHintAttr *PrevAttr; |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 166 | if (Option == LoopHintAttr::Vectorize || |
| 167 | Option == LoopHintAttr::Interleave || Option == LoopHintAttr::Unroll) { |
| 168 | // Enable|disable hint. For example, vectorize(enable). |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 169 | PrevAttr = CategoryState.StateAttr; |
| 170 | CategoryState.StateAttr = LH; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 171 | } else { |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 172 | // Numeric hint. For example, vectorize_width(8). |
| 173 | PrevAttr = CategoryState.NumericAttr; |
| 174 | CategoryState.NumericAttr = LH; |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Tyler Nowicki | 884fc61 | 2014-07-29 17:21:32 +0000 | [diff] [blame] | 177 | PrintingPolicy Policy(S.Context.getLangOpts()); |
| 178 | SourceLocation OptionLoc = LH->getRange().getBegin(); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 179 | if (PrevAttr) |
| 180 | // Cannot specify same type of attribute twice. |
| 181 | S.Diag(OptionLoc, diag::err_pragma_loop_compatibility) |
Tyler Nowicki | 884fc61 | 2014-07-29 17:21:32 +0000 | [diff] [blame] | 182 | << /*Duplicate=*/true << PrevAttr->getDiagnosticName(Policy) |
| 183 | << LH->getDiagnosticName(Policy); |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 184 | |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 185 | if (CategoryState.StateAttr && CategoryState.NumericAttr && |
| 186 | (Category == Unroll || |
| 187 | CategoryState.StateAttr->getState() == LoopHintAttr::Disable)) { |
Mark Heffernan | 450c238 | 2014-07-23 17:31:31 +0000 | [diff] [blame] | 188 | // Disable hints are not compatible with numeric hints of the same |
| 189 | // category. As a special case, numeric unroll hints are also not |
| 190 | // compatible with "enable" form of the unroll pragma, unroll(full). |
Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame] | 191 | S.Diag(OptionLoc, diag::err_pragma_loop_compatibility) |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 192 | << /*Duplicate=*/false |
Tyler Nowicki | 0c9b34b | 2014-07-31 20:15:14 +0000 | [diff] [blame] | 193 | << CategoryState.StateAttr->getDiagnosticName(Policy) |
Tyler Nowicki | 884fc61 | 2014-07-29 17:21:32 +0000 | [diff] [blame] | 194 | << CategoryState.NumericAttr->getDiagnosticName(Policy); |
Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 195 | } |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const AttributeList &A, |
| 200 | SourceRange Range) { |
| 201 | switch (A.getKind()) { |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 202 | case AttributeList::UnknownAttribute: |
| 203 | S.Diag(A.getLoc(), A.isDeclspecAttribute() ? |
| 204 | diag::warn_unhandled_ms_attribute_ignored : |
| 205 | diag::warn_unknown_attribute_ignored) << A.getName(); |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 206 | return nullptr; |
| 207 | case AttributeList::AT_FallThrough: |
| 208 | return handleFallThroughAttr(S, St, A, Range); |
| 209 | case AttributeList::AT_LoopHint: |
| 210 | return handleLoopHintAttr(S, St, A, Range); |
| 211 | default: |
| 212 | // if we're here, then we parsed a known attribute, but didn't recognize |
| 213 | // it as a statement attribute => it is declaration attribute |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 214 | S.Diag(A.getRange().getBegin(), diag::err_attribute_invalid_on_stmt) |
| 215 | << A.getName() << St->getLocStart(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 216 | return nullptr; |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | StmtResult Sema::ProcessStmtAttributes(Stmt *S, AttributeList *AttrList, |
| 221 | SourceRange Range) { |
Alexander Kornienko | 20f6fc6 | 2012-07-09 10:04:07 +0000 | [diff] [blame] | 222 | SmallVector<const Attr*, 8> Attrs; |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 223 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 224 | if (Attr *a = ProcessStmtAttribute(*this, S, *l, Range)) |
Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 225 | Attrs.push_back(a); |
| 226 | } |
| 227 | |
| 228 | CheckForIncompatibleAttributes(*this, Attrs); |
| 229 | |
| 230 | if (Attrs.empty()) |
| 231 | return S; |
| 232 | |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 233 | return ActOnAttributedStmt(Range.getBegin(), Attrs, S); |
| 234 | } |