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