27 if (isa<SwitchCase>(St)) {
31 S.
Diag(L, diag::note_fallthrough_insert_semi_fixit)
36 if (FnScope->SwitchStack.empty()) {
47 FnScope->setHasFallthroughStmt();
56 S.
Diag(A.
getLoc(), diag::err_attribute_too_few_arguments) << A << 1;
60 std::vector<StringRef> DiagnosticIdentifiers;
67 DiagnosticIdentifiers.push_back(RuleName);
70 return ::new (S.
Context) SuppressAttr(
71 S.
Context, A, DiagnosticIdentifiers.data(), DiagnosticIdentifiers.size());
81 StringRef PragmaName =
82 llvm::StringSwitch<StringRef>(PragmaNameLoc->
Ident->
getName())
83 .Cases(
"unroll",
"nounroll",
"unroll_and_jam",
"nounroll_and_jam",
85 .Default(
"clang loop");
90 if (!isa<DoStmt, ForStmt, CXXForRangeStmt, WhileStmt>(St)) {
91 std::string Pragma =
"#pragma " + std::string(PragmaName);
92 S.
Diag(St->
getBeginLoc(), diag::err_pragma_loop_precedes_nonloop) << Pragma;
96 LoopHintAttr::OptionType Option;
97 LoopHintAttr::LoopHintState State;
99 auto SetHints = [&Option, &State](LoopHintAttr::OptionType O,
100 LoopHintAttr::LoopHintState S) {
105 if (PragmaName ==
"nounroll") {
106 SetHints(LoopHintAttr::Unroll, LoopHintAttr::Disable);
107 }
else if (PragmaName ==
"unroll") {
113 SetHints(LoopHintAttr::Unroll, LoopHintAttr::Disable);
115 SetHints(LoopHintAttr::UnrollCount, LoopHintAttr::Numeric);
117 SetHints(LoopHintAttr::UnrollCount, LoopHintAttr::Numeric);
119 SetHints(LoopHintAttr::Unroll, LoopHintAttr::Enable);
120 }
else if (PragmaName ==
"nounroll_and_jam") {
121 SetHints(LoopHintAttr::UnrollAndJam, LoopHintAttr::Disable);
122 }
else if (PragmaName ==
"unroll_and_jam") {
125 SetHints(LoopHintAttr::UnrollAndJamCount, LoopHintAttr::Numeric);
127 SetHints(LoopHintAttr::UnrollAndJam, LoopHintAttr::Enable);
130 assert(OptionLoc && OptionLoc->
Ident &&
131 "Attribute must have valid option info.");
132 Option = llvm::StringSwitch<LoopHintAttr::OptionType>(
134 .Case(
"vectorize", LoopHintAttr::Vectorize)
135 .Case(
"vectorize_width", LoopHintAttr::VectorizeWidth)
136 .Case(
"interleave", LoopHintAttr::Interleave)
137 .Case(
"vectorize_predicate", LoopHintAttr::VectorizePredicate)
138 .Case(
"interleave_count", LoopHintAttr::InterleaveCount)
139 .Case(
"unroll", LoopHintAttr::Unroll)
140 .Case(
"unroll_count", LoopHintAttr::UnrollCount)
141 .Case(
"pipeline", LoopHintAttr::PipelineDisabled)
142 .Case(
"pipeline_initiation_interval",
143 LoopHintAttr::PipelineInitiationInterval)
144 .Case(
"distribute", LoopHintAttr::Distribute)
145 .Default(LoopHintAttr::Vectorize);
146 if (Option == LoopHintAttr::VectorizeWidth) {
147 assert((ValueExpr || (StateLoc && StateLoc->
Ident)) &&
148 "Attribute must have a valid value expression or argument.");
153 State = LoopHintAttr::ScalableWidth;
155 State = LoopHintAttr::FixedWidth;
156 }
else if (Option == LoopHintAttr::InterleaveCount ||
157 Option == LoopHintAttr::UnrollCount ||
158 Option == LoopHintAttr::PipelineInitiationInterval) {
159 assert(ValueExpr &&
"Attribute must have a valid value expression.");
163 State = LoopHintAttr::Numeric;
164 }
else if (Option == LoopHintAttr::Vectorize ||
165 Option == LoopHintAttr::Interleave ||
166 Option == LoopHintAttr::VectorizePredicate ||
167 Option == LoopHintAttr::Unroll ||
168 Option == LoopHintAttr::Distribute ||
169 Option == LoopHintAttr::PipelineDisabled) {
170 assert(StateLoc && StateLoc->
Ident &&
"Loop hint must have an argument");
172 State = LoopHintAttr::Disable;
173 else if (StateLoc->
Ident->
isStr(
"assume_safety"))
174 State = LoopHintAttr::AssumeSafety;
176 State = LoopHintAttr::Full;
178 State = LoopHintAttr::Enable;
180 llvm_unreachable(
"bad loop hint argument");
182 llvm_unreachable(
"bad loop hint");
185 return LoopHintAttr::CreateImplicit(S.
Context, Option, State, ValueExpr, A);
190 bool FoundAsmStmt =
false;
191 std::vector<const CallExpr *> CallExprs;
196 CallExprFinder(
Sema &S,
const Stmt *St) : Inherited(S.Context) {
Visit(St); }
198 bool foundCallExpr() {
return !CallExprs.empty(); }
199 const std::vector<const CallExpr *> &getCallExprs() {
return CallExprs; }
201 bool foundAsmStmt() {
return FoundAsmStmt; }
205 void VisitAsmStmt(
const AsmStmt *S) { FoundAsmStmt =
true; }
217 CallExprFinder CEF(S, St);
219 if (!CEF.foundCallExpr() && !CEF.foundAsmStmt()) {
220 S.
Diag(St->
getBeginLoc(), diag::warn_attribute_ignored_no_calls_in_stmt)
230 CallExprFinder CEF(S, St);
232 if (!CEF.foundCallExpr() && !CEF.foundAsmStmt()) {
233 S.
Diag(St->
getBeginLoc(), diag::warn_attribute_ignored_no_calls_in_stmt)
241template <
typename OtherAttr,
int DiagIdx>
245 CallExprFinder OrigCEF(SemaRef, OrigSt);
246 CallExprFinder CEF(SemaRef, CurSt);
255 bool CanSuppressDiag =
256 OrigSt && CEF.getCallExprs().size() == OrigCEF.getCallExprs().size();
258 if (!CEF.foundCallExpr()) {
260 diag::warn_attribute_ignored_no_calls_in_stmt)
264 for (
const auto &Tup :
265 llvm::zip_longest(OrigCEF.getCallExprs(), CEF.getCallExprs())) {
269 if (!CanSuppressDiag || !(*std::get<0>(Tup))->getCalleeDecl()) {
270 const Decl *Callee = (*std::get<1>(Tup))->getCalleeDecl();
272 (Callee->hasAttr<OtherAttr>() || Callee->hasAttr<FlattenAttr>())) {
274 diag::warn_function_stmt_attribute_precedence)
275 << A << (Callee->hasAttr<OtherAttr>() ? DiagIdx : 1);
276 SemaRef.
Diag(Callee->getBeginLoc(), diag::note_conflicting_attribute);
286 return CheckStmtInlineAttr<AlwaysInlineAttr, 0>(*
this, OrigSt, CurSt, A);
291 return CheckStmtInlineAttr<NoInlineAttr, 2>(*
this, OrigSt, CurSt, A);
296 NoInlineAttr NIA(S.
Context, A);
297 if (!NIA.isStmtNoInline()) {
298 S.
Diag(St->
getBeginLoc(), diag::warn_function_attribute_ignored_in_stmt)
299 <<
"[[clang::noinline]]";
311 AlwaysInlineAttr AIA(S.
Context, A);
312 if (!AIA.isClangAlwaysInline()) {
313 S.
Diag(St->
getBeginLoc(), diag::warn_function_attribute_ignored_in_stmt)
314 <<
"[[clang::always_inline]]";
368 if (ArgVal < CodeAlignAttr::MinimumAlignment ||
369 ArgVal > CodeAlignAttr::MaximumAlignment || !ArgVal.isPowerOf2()) {
370 if (std::optional<int64_t>
Value = ArgVal.trySExtValue())
371 Diag(CI.
getLoc(), diag::err_attribute_power_of_two_in_range)
372 << CI << CodeAlignAttr::MinimumAlignment
373 << CodeAlignAttr::MaximumAlignment <<
Value.value();
375 Diag(CI.
getLoc(), diag::err_attribute_power_of_two_in_range)
376 << CI << CodeAlignAttr::MinimumAlignment
377 << CodeAlignAttr::MaximumAlignment <<
E;
392template <
typename LoopAttrT>
394 auto FindFunc = [](
const Attr *A) {
return isa<const LoopAttrT>(A); };
395 const auto *FirstItr = std::find_if(Attrs.begin(), Attrs.end(), FindFunc);
397 if (FirstItr == Attrs.end())
400 const auto *LastFoundItr = FirstItr;
401 std::optional<llvm::APSInt> FirstValue;
404 dyn_cast<ConstantExpr>(cast<LoopAttrT>(*FirstItr)->getAlignment());
410 while (Attrs.end() != (LastFoundItr = std::find_if(LastFoundItr + 1,
411 Attrs.end(), FindFunc))) {
413 dyn_cast<ConstantExpr>(cast<LoopAttrT>(*LastFoundItr)->getAlignment());
418 llvm::APSInt SecondValue = CASA->getResultAsAPSInt();
420 FirstValue = CAFA->getResultAsAPSInt();
422 if (FirstValue != SecondValue) {
423 S.
Diag((*LastFoundItr)->getLocation(), diag::err_loop_attr_conflict)
425 S.
Diag((*FirstItr)->getLocation(), diag::note_previous_attribute);
434 S.
Diag(A.
getLoc(), diag::warn_unknown_attribute_ignored)
441#define WANT_STMT_MERGE_LOGIC
442#include "clang/Sema/AttrParsedAttrImpl.inc"
443#undef WANT_STMT_MERGE_LOGIC
450 if (Attrs.size() < 2)
454 if (!DiagnoseMutualExclusions(S, Attrs))
481 const LoopHintAttr *StateAttr;
482 const LoopHintAttr *NumericAttr;
483 } HintAttrs[CategoryType::NumberOfCategories] = {};
485 for (
const auto *I : Attrs) {
486 const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I);
492 CategoryType
Category = CategoryType::NumberOfCategories;
493 LoopHintAttr::OptionType Option = LH->getOption();
495 case LoopHintAttr::Vectorize:
496 case LoopHintAttr::VectorizeWidth:
499 case LoopHintAttr::Interleave:
500 case LoopHintAttr::InterleaveCount:
503 case LoopHintAttr::Unroll:
504 case LoopHintAttr::UnrollCount:
507 case LoopHintAttr::UnrollAndJam:
508 case LoopHintAttr::UnrollAndJamCount:
511 case LoopHintAttr::Distribute:
515 case LoopHintAttr::PipelineDisabled:
516 case LoopHintAttr::PipelineInitiationInterval:
519 case LoopHintAttr::VectorizePredicate:
524 assert(
Category != NumberOfCategories &&
"Unhandled loop hint option");
525 auto &CategoryState = HintAttrs[
Category];
526 const LoopHintAttr *PrevAttr;
527 if (Option == LoopHintAttr::Vectorize ||
528 Option == LoopHintAttr::Interleave || Option == LoopHintAttr::Unroll ||
529 Option == LoopHintAttr::UnrollAndJam ||
530 Option == LoopHintAttr::VectorizePredicate ||
531 Option == LoopHintAttr::PipelineDisabled ||
532 Option == LoopHintAttr::Distribute) {
534 PrevAttr = CategoryState.StateAttr;
535 CategoryState.StateAttr = LH;
538 PrevAttr = CategoryState.NumericAttr;
539 CategoryState.NumericAttr = LH;
546 S.
Diag(OptionLoc, diag::err_pragma_loop_compatibility)
547 <<
true << PrevAttr->getDiagnosticName(Policy)
548 << LH->getDiagnosticName(Policy);
550 if (CategoryState.StateAttr && CategoryState.NumericAttr &&
552 CategoryState.StateAttr->getState() == LoopHintAttr::Disable)) {
557 S.
Diag(OptionLoc, diag::err_pragma_loop_compatibility)
559 << CategoryState.StateAttr->getDiagnosticName(Policy)
560 << CategoryState.NumericAttr->getDiagnosticName(Policy);
572 unsigned UnrollFactor = 0;
575 std::optional<llvm::APSInt> ArgVal;
578 S.
Diag(A.
getLoc(), diag::err_attribute_argument_type)
583 int Val = ArgVal->getSExtValue();
586 diag::err_attribute_requires_positive_integer)
590 UnrollFactor =
static_cast<unsigned>(Val);
593 return ::new (S.
Context) OpenCLUnrollHintAttr(S.
Context, A, UnrollFactor);
603 unsigned UnrollFactor = 0;
613 assert(ArgVal != std::nullopt &&
"ArgVal should be an integer constant.");
614 int Val = ArgVal->getSExtValue();
616 assert(Val > 0 &&
"Val should be a positive integer greater than zero.");
617 UnrollFactor =
static_cast<unsigned>(Val);
619 return ::new (S.
Context) HLSLLoopHintAttr(S.
Context, A, UnrollFactor);
636 ? (
unsigned)diag::err_keyword_not_supported_on_target
638 ? (
unsigned)diag::warn_unhandled_ms_attribute_ignored
639 : (
unsigned)diag::warn_unknown_attribute_ignored)
648 case ParsedAttr::AT_AlwaysInline:
650 case ParsedAttr::AT_CXXAssume:
652 case ParsedAttr::AT_FallThrough:
654 case ParsedAttr::AT_LoopHint:
656 case ParsedAttr::AT_HLSLLoopHint:
658 case ParsedAttr::AT_OpenCLUnrollHint:
660 case ParsedAttr::AT_Suppress:
662 case ParsedAttr::AT_NoMerge:
664 case ParsedAttr::AT_NoInline:
666 case ParsedAttr::AT_MustTail:
668 case ParsedAttr::AT_Likely:
670 case ParsedAttr::AT_Unlikely:
672 case ParsedAttr::AT_CodeAlign:
674 case ParsedAttr::AT_MSConstexpr:
676 case ParsedAttr::AT_NoConvergent:
678 case ParsedAttr::AT_Annotate:
681 if (
Attr *AT =
nullptr; A.
getInfo().handleStmtAttribute(S, St, A, AT) !=
698 OutAttrs.push_back(A);
702 CheckForDuplicateLoopAttrs<CodeAlignAttr>(*
this, OutAttrs);
706 CheckForDuplicateLoopAttrs<CodeAlignAttr>(*
this, Attrs);
713 Diag(A.
getLoc(), diag::err_attribute_wrong_number_arguments)
724 if (Assumption->getDependence() == ExprDependence::None) {
728 Assumption = Res.
get();
753 Assumption = Res.
get();
756 << AttrName <<
Range;
Defines the clang::ASTContext interface.
Defines the classes clang::DelayedDiagnostic and clang::AccessedEntity.
static void handleNoMergeAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleAlwaysInlineAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleMSConstexprAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static void handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static Attr * handleNoConvergentAttr(Sema &S, Stmt *St, const ParsedAttr &A, SourceRange Range)
static void CheckForDuplicateLoopAttrs(Sema &S, ArrayRef< const Attr * > Attrs)
static Attr * handleMustTailAttr(Sema &S, Stmt *St, const ParsedAttr &A, SourceRange Range)
static Attr * handleCXXAssumeAttr(Sema &S, Stmt *St, const ParsedAttr &A, SourceRange Range)
static Attr * ProcessStmtAttribute(Sema &S, Stmt *St, const ParsedAttr &A, SourceRange Range)
static Attr * handleLikely(Sema &S, Stmt *St, const ParsedAttr &A, SourceRange Range)
static Attr * handleNoMergeAttr(Sema &S, Stmt *St, const ParsedAttr &A, SourceRange Range)
static Attr * handleOpenCLUnrollHint(Sema &S, Stmt *St, const ParsedAttr &A, SourceRange Range)
static Attr * handleLoopHintAttr(Sema &S, Stmt *St, const ParsedAttr &A, SourceRange)
static void CheckForIncompatibleAttributes(Sema &S, const SmallVectorImpl< const Attr * > &Attrs)
static bool CheckStmtInlineAttr(Sema &SemaRef, const Stmt *OrigSt, const Stmt *CurSt, const AttributeCommonInfo &A)
static Attr * handleHLSLLoopHintAttr(Sema &S, Stmt *St, const ParsedAttr &A, SourceRange Range)
static Attr * handleCodeAlignAttr(Sema &S, Stmt *St, const ParsedAttr &A)
static Attr * handleFallThroughAttr(Sema &S, Stmt *St, const ParsedAttr &A, SourceRange Range)
static Attr * handleSuppressAttr(Sema &S, Stmt *St, const ParsedAttr &A, SourceRange Range)
static Attr * handleUnlikely(Sema &S, Stmt *St, const ParsedAttr &A, SourceRange Range)
static Attr * handleNoInlineAttr(Sema &S, Stmt *St, const ParsedAttr &A, SourceRange Range)
const LangOptions & getLangOpts() const
const TargetInfo * getAuxTargetInfo() const
const TargetInfo & getTargetInfo() const
AsmStmt is the base class for GCCAsmStmt and MSAsmStmt.
Attr - This represents one attribute.
bool isCXX11Attribute() const
bool isDeclspecAttribute() const
SourceRange getRange() const
unsigned getAttributeSpellingListIndex() const
const IdentifierInfo * getScopeName() const
bool isRegularKeywordAttribute() const
SourceLocation getLoc() const
const IdentifierInfo * getAttrName() const
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
ConstEvaluatedExprVisitor - This class visits 'const Expr *'s.
Decl - This represents one declaration (or definition), e.g.
void VisitCallExpr(PTR(CallExpr) CE)
This represents one expression.
bool isValueDependent() const
Determines whether the value of this expression depends on.
llvm::APSInt EvaluateKnownConstInt(const ASTContext &Ctx, SmallVectorImpl< PartialDiagnosticAt > *Diag=nullptr) const
EvaluateKnownConstInt - Call EvaluateAsRValue and return the folded integer.
bool HasSideEffects(const ASTContext &Ctx, bool IncludePossibleEffects=true) const
HasSideEffects - This routine returns true for all those expressions which have any effect other than...
std::optional< llvm::APSInt > getIntegerConstantExpr(const ASTContext &Ctx, SourceLocation *Loc=nullptr) const
isIntegerConstantExpr - Return the value if this expression is a valid integer constant expression.
static FixItHint CreateInsertion(SourceLocation InsertionLoc, StringRef Code, bool BeforePreviousInsertions=false)
Create a code modification hint that inserts the given code string at a specific location.
One of these records is kept for each identifier that is lexed.
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
StringRef getName() const
Return the actual identifier string.
bool isCompatibleWithMSVC(MSVCMajorVersion MajorVersion) const
ParsedAttr - Represents a syntactic attribute.
unsigned getSemanticSpelling() const
If the parsed attribute has a semantic equivalent, and it would have a semantic Spelling enumeration ...
bool existsInTarget(const TargetInfo &Target) const
IdentifierLoc * getArgAsIdent(unsigned Arg) const
const ParsedAttrInfo & getInfo() const
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this attribute.
Expr * getArgAsExpr(unsigned Arg) const
AttributeCommonInfo::Kind getKind() const
bool checkAtMostNumArgs(class Sema &S, unsigned Num) const
Check if the attribute has at most as many args as Num.
ParsedAttributes - A collection of parsed attributes.
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID, bool DeferHint=false)
Emit a diagnostic.
Sema - This implements semantic analysis and AST building for C.
void ProcessStmtAttributes(Stmt *Stmt, const ParsedAttributes &InAttrs, SmallVectorImpl< const Attr * > &OutAttrs)
Process the attributes before creating an attributed statement.
bool checkCommonAttributeFeatures(const Decl *D, const ParsedAttr &A, bool SkipArgCountCheck=false)
Handles semantic checking for features that are common to all attributes, such as checking whether a ...
ExprResult BuildCXXAssumeExpr(Expr *Assumption, const IdentifierInfo *AttrName, SourceRange Range)
ExprResult VerifyIntegerConstantExpression(Expr *E, llvm::APSInt *Result, VerifyICEDiagnoser &Diagnoser, AllowFoldKind CanFold=NoFold)
VerifyIntegerConstantExpression - Verifies that an expression is an ICE, and reports the appropriate ...
ASTContext & getASTContext() const
SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset=0)
Calls Lexer::getLocForEndOfToken()
const LangOptions & getLangOpts() const
bool DiagnoseUnexpandedParameterPack(SourceLocation Loc, TypeSourceInfo *T, UnexpandedParameterPackContext UPPC)
If the given type contains an unexpanded parameter pack, diagnose the error.
sema::FunctionScopeInfo * getCurFunction() const
bool CheckLoopHintExpr(Expr *E, SourceLocation Loc, bool AllowZero)
ExprResult PerformContextuallyConvertToBool(Expr *From)
PerformContextuallyConvertToBool - Perform a contextual conversion of the expression From to bool (C+...
ExprResult CheckPlaceholderExpr(Expr *E)
Check for operands with placeholder types and complain if found.
bool CheckRebuiltStmtAttributes(ArrayRef< const Attr * > Attrs)
bool CheckNoInlineAttr(const Stmt *OrigSt, const Stmt *CurSt, const AttributeCommonInfo &A)
bool CheckAlwaysInlineAttr(const Stmt *OrigSt, const Stmt *CurSt, const AttributeCommonInfo &A)
ExprResult ActOnCXXAssumeAttr(Stmt *St, const ParsedAttr &A, SourceRange Range)
Attr * CreateAnnotationAttr(const AttributeCommonInfo &CI, StringRef Annot, MutableArrayRef< Expr * > Args)
CreateAnnotationAttr - Creates an annotation Annot with Args arguments.
CodeAlignAttr * BuildCodeAlignAttr(const AttributeCommonInfo &CI, Expr *E)
bool checkStringLiteralArgumentAttr(const AttributeCommonInfo &CI, const Expr *E, StringRef &Str, SourceLocation *ArgLocation=nullptr)
Check if the argument E is a ASCII string literal.
ExprResult CorrectDelayedTyposInExpr(Expr *E, VarDecl *InitDecl=nullptr, bool RecoverUncorrectedTypos=false, llvm::function_ref< ExprResult(Expr *)> Filter=[](Expr *E) -> ExprResult { return E;})
Process any TypoExprs in the given Expr and its children, generating diagnostics as appropriate and r...
Encodes a location in the source.
A trivial tuple used to represent a source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
RetTy Visit(PTR(Stmt) S, ParamTys... P)
Stmt - This represents one statement.
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
SourceLocation getBeginLoc() const LLVM_READONLY
Exposes information about the current target.
Defines the clang::TargetInfo interface.
The JSON file list parser is used to communicate input to InstallAPI.
@ AANT_ArgumentIntegerConstant
Wraps an identifier and optional source location for the identifier.
Describes how types, statements, expressions, and declarations should be printed.