| 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); |
| 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 Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 57 | if (St->getStmtClass() != Stmt::DoStmtClass && |
| 58 | St->getStmtClass() != Stmt::ForStmtClass && |
| 59 | St->getStmtClass() != Stmt::CXXForRangeStmtClass && |
| 60 | St->getStmtClass() != Stmt::WhileStmtClass) { |
| Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame^] | 61 | 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 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; |
| 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 Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 84 | |
| 85 | int ValueInt; |
| Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame^] | 86 | 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 Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 92 | if (!ValueInfo) { |
| Eli Bendersky | f637790 | 2014-06-19 18:30:15 +0000 | [diff] [blame] | 93 | S.Diag(ValueLoc->Loc, diag::err_pragma_loop_invalid_keyword); |
| Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 94 | return nullptr; |
| 95 | } |
| Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 96 | if (ValueInfo->isStr("disable")) |
| 97 | ValueInt = 0; |
| 98 | else if (ValueInfo->isStr("enable")) |
| 99 | ValueInt = 1; |
| 100 | else { |
| Eli Bendersky | f637790 | 2014-06-19 18:30:15 +0000 | [diff] [blame] | 101 | S.Diag(ValueLoc->Loc, diag::err_pragma_loop_invalid_keyword); |
| Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 102 | return nullptr; |
| 103 | } |
| 104 | } else if (Option == LoopHintAttr::VectorizeWidth || |
| Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 105 | Option == LoopHintAttr::InterleaveCount || |
| 106 | Option == LoopHintAttr::UnrollCount) { |
| Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 107 | // FIXME: We should support template parameters for the loop hint value. |
| 108 | // See bug report #19610. |
| 109 | llvm::APSInt ValueAPS; |
| Eli Bendersky | f637790 | 2014-06-19 18:30:15 +0000 | [diff] [blame] | 110 | if (!ValueExpr || !ValueExpr->isIntegerConstantExpr(ValueAPS, S.Context) || |
| 111 | (ValueInt = ValueAPS.getSExtValue()) < 1) { |
| 112 | S.Diag(ValueLoc->Loc, diag::err_pragma_loop_invalid_value); |
| Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 113 | return nullptr; |
| 114 | } |
| 115 | } else |
| 116 | llvm_unreachable("Unknown loop hint option"); |
| 117 | |
| Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame^] | 118 | return LoopHintAttr::CreateImplicit(S.Context, Spelling, Option, ValueInt, |
| Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 119 | A.getRange()); |
| 120 | } |
| 121 | |
| Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame^] | 122 | static void CheckForIncompatibleAttributes( |
| 123 | Sema &S, const SmallVectorImpl<const Attr *> &Attrs) { |
| Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 124 | // 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 Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame^] | 131 | const LoopHintAttr *EnableAttr; |
| 132 | const LoopHintAttr *NumericAttr; |
| 133 | } HintAttrs[] = {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}}; |
| Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 134 | |
| 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 Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 142 | int Option = LH->getOption(); |
| Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 143 | int Category; |
| Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 144 | switch (Option) { |
| 145 | case LoopHintAttr::Vectorize: |
| 146 | case LoopHintAttr::VectorizeWidth: |
| Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 147 | Category = 0; |
| Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 148 | break; |
| 149 | case LoopHintAttr::Interleave: |
| 150 | case LoopHintAttr::InterleaveCount: |
| Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 151 | Category = 1; |
| Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 152 | break; |
| Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 153 | case LoopHintAttr::Unroll: |
| 154 | case LoopHintAttr::UnrollCount: |
| 155 | Category = 2; |
| 156 | break; |
| 157 | }; |
| Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 158 | |
| Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame^] | 159 | auto &CategoryState = HintAttrs[Category]; |
| 160 | SourceLocation OptionLoc = LH->getRange().getBegin(); |
| 161 | const LoopHintAttr *PrevAttr; |
| Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 162 | if (Option == LoopHintAttr::Vectorize || |
| 163 | Option == LoopHintAttr::Interleave || Option == LoopHintAttr::Unroll) { |
| 164 | // Enable|disable hint. For example, vectorize(enable). |
| Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame^] | 165 | PrevAttr = CategoryState.EnableAttr; |
| 166 | CategoryState.EnableAttr = LH; |
| Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 167 | } else { |
| Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame^] | 168 | // Numeric hint. For example, vectorize_width(8). |
| 169 | PrevAttr = CategoryState.NumericAttr; |
| 170 | CategoryState.NumericAttr = LH; |
| Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame^] | 173 | 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 Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 181 | // Disable hints are not compatible with numeric hints of the |
| 182 | // same category. |
| Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame^] | 183 | S.Diag(OptionLoc, diag::err_pragma_loop_compatibility) |
| Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 184 | << /*Duplicate=*/false |
| Mark Heffernan | bd26f5e | 2014-07-21 18:08:34 +0000 | [diff] [blame^] | 185 | << CategoryState.EnableAttr->getDiagnosticName() |
| 186 | << CategoryState.NumericAttr->getDiagnosticName(); |
| Eli Bendersky | 86483b3 | 2014-06-11 17:56:26 +0000 | [diff] [blame] | 187 | } |
| Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const AttributeList &A, |
| 192 | SourceRange Range) { |
| 193 | switch (A.getKind()) { |
| Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 194 | 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 Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 198 | 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 Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 206 | S.Diag(A.getRange().getBegin(), diag::err_attribute_invalid_on_stmt) |
| 207 | << A.getName() << St->getLocStart(); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 208 | return nullptr; |
| Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
| 212 | StmtResult Sema::ProcessStmtAttributes(Stmt *S, AttributeList *AttrList, |
| 213 | SourceRange Range) { |
| Alexander Kornienko | 20f6fc6 | 2012-07-09 10:04:07 +0000 | [diff] [blame] | 214 | SmallVector<const Attr*, 8> Attrs; |
| Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 215 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
| Richard Smith | 84837d5 | 2012-05-03 18:27:39 +0000 | [diff] [blame] | 216 | if (Attr *a = ProcessStmtAttribute(*this, S, *l, Range)) |
| Eli Bendersky | 06a4042 | 2014-06-06 20:31:48 +0000 | [diff] [blame] | 217 | Attrs.push_back(a); |
| 218 | } |
| 219 | |
| 220 | CheckForIncompatibleAttributes(*this, Attrs); |
| 221 | |
| 222 | if (Attrs.empty()) |
| 223 | return S; |
| 224 | |
| Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 225 | return ActOnAttributedStmt(Range.getBegin(), Attrs, S); |
| 226 | } |