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>(
84 .Cases(
"unroll",
"nounroll",
"unroll_and_jam",
"nounroll_and_jam",
86 .Default(
"clang loop");
91 if (!isa<DoStmt, ForStmt, CXXForRangeStmt, WhileStmt>(St)) {
92 std::string Pragma =
"#pragma " + std::string(PragmaName);
93 S.
Diag(St->
getBeginLoc(), diag::err_pragma_loop_precedes_nonloop) << Pragma;
97 LoopHintAttr::OptionType Option;
98 LoopHintAttr::LoopHintState State;
100 auto SetHints = [&Option, &State](LoopHintAttr::OptionType O,
101 LoopHintAttr::LoopHintState S) {
106 if (PragmaName ==
"nounroll") {
107 SetHints(LoopHintAttr::Unroll, LoopHintAttr::Disable);
108 }
else if (PragmaName ==
"unroll") {
114 SetHints(LoopHintAttr::Unroll, LoopHintAttr::Disable);
116 SetHints(LoopHintAttr::UnrollCount, LoopHintAttr::Numeric);
118 SetHints(LoopHintAttr::UnrollCount, LoopHintAttr::Numeric);
120 SetHints(LoopHintAttr::Unroll, LoopHintAttr::Enable);
121 }
else if (PragmaName ==
"nounroll_and_jam") {
122 SetHints(LoopHintAttr::UnrollAndJam, LoopHintAttr::Disable);
123 }
else if (PragmaName ==
"unroll_and_jam") {
126 SetHints(LoopHintAttr::UnrollAndJamCount, LoopHintAttr::Numeric);
128 SetHints(LoopHintAttr::UnrollAndJam, LoopHintAttr::Enable);
132 "Attribute must have valid option info.");
133 Option = llvm::StringSwitch<LoopHintAttr::OptionType>(
135 .Case(
"vectorize", LoopHintAttr::Vectorize)
136 .Case(
"vectorize_width", LoopHintAttr::VectorizeWidth)
137 .Case(
"interleave", LoopHintAttr::Interleave)
138 .Case(
"vectorize_predicate", LoopHintAttr::VectorizePredicate)
139 .Case(
"interleave_count", LoopHintAttr::InterleaveCount)
140 .Case(
"unroll", LoopHintAttr::Unroll)
141 .Case(
"unroll_count", LoopHintAttr::UnrollCount)
142 .Case(
"pipeline", LoopHintAttr::PipelineDisabled)
143 .Case(
"pipeline_initiation_interval",
144 LoopHintAttr::PipelineInitiationInterval)
145 .Case(
"distribute", LoopHintAttr::Distribute)
146 .Default(LoopHintAttr::Vectorize);
147 if (Option == LoopHintAttr::VectorizeWidth) {
149 "Attribute must have a valid value expression or argument.");
155 State = LoopHintAttr::ScalableWidth;
157 State = LoopHintAttr::FixedWidth;
158 }
else if (Option == LoopHintAttr::InterleaveCount ||
159 Option == LoopHintAttr::UnrollCount ||
160 Option == LoopHintAttr::PipelineInitiationInterval) {
161 assert(ValueExpr &&
"Attribute must have a valid value expression.");
165 State = LoopHintAttr::Numeric;
166 }
else if (Option == LoopHintAttr::Vectorize ||
167 Option == LoopHintAttr::Interleave ||
168 Option == LoopHintAttr::VectorizePredicate ||
169 Option == LoopHintAttr::Unroll ||
170 Option == LoopHintAttr::Distribute ||
171 Option == LoopHintAttr::PipelineDisabled) {
173 "Loop hint must have an argument");
175 State = LoopHintAttr::Disable;
177 State = LoopHintAttr::AssumeSafety;
179 State = LoopHintAttr::Full;
181 State = LoopHintAttr::Enable;
183 llvm_unreachable(
"bad loop hint argument");
185 llvm_unreachable(
"bad loop hint");
188 return LoopHintAttr::CreateImplicit(S.
Context, Option, State, ValueExpr, A);
193 bool FoundAsmStmt =
false;
194 std::vector<const CallExpr *> CallExprs;
199 CallExprFinder(
Sema &S,
const Stmt *St) : Inherited(S.Context) {
Visit(St); }
201 bool foundCallExpr() {
return !CallExprs.empty(); }
202 const std::vector<const CallExpr *> &getCallExprs() {
return CallExprs; }
204 bool foundAsmStmt() {
return FoundAsmStmt; }
208 void VisitAsmStmt(
const AsmStmt *S) { FoundAsmStmt =
true; }
220 CallExprFinder CEF(S, St);
222 if (!CEF.foundCallExpr() && !CEF.foundAsmStmt()) {
223 S.
Diag(St->
getBeginLoc(), diag::warn_attribute_ignored_no_calls_in_stmt)
233 CallExprFinder CEF(S, St);
235 if (!CEF.foundCallExpr() && !CEF.foundAsmStmt()) {
236 S.
Diag(St->
getBeginLoc(), diag::warn_attribute_ignored_no_calls_in_stmt)
244template <
typename OtherAttr,
int DiagIdx>
248 CallExprFinder OrigCEF(SemaRef, OrigSt);
249 CallExprFinder CEF(SemaRef, CurSt);
258 bool CanSuppressDiag =
259 OrigSt && CEF.getCallExprs().size() == OrigCEF.getCallExprs().size();
261 if (!CEF.foundCallExpr()) {
263 diag::warn_attribute_ignored_no_calls_in_stmt)
267 for (
const auto &Tup :
268 llvm::zip_longest(OrigCEF.getCallExprs(), CEF.getCallExprs())) {
272 if (!CanSuppressDiag || !(*std::get<0>(Tup))->getCalleeDecl()) {
273 const Decl *Callee = (*std::get<1>(Tup))->getCalleeDecl();
275 (Callee->hasAttr<OtherAttr>() || Callee->hasAttr<FlattenAttr>())) {
277 diag::warn_function_stmt_attribute_precedence)
278 << A << (Callee->hasAttr<OtherAttr>() ? DiagIdx : 1);
279 SemaRef.
Diag(Callee->getBeginLoc(), diag::note_conflicting_attribute);
289 return CheckStmtInlineAttr<AlwaysInlineAttr, 0>(*
this, OrigSt, CurSt, A);
294 return CheckStmtInlineAttr<NoInlineAttr, 2>(*
this, OrigSt, CurSt, A);
299 NoInlineAttr NIA(S.
Context, A);
300 if (!NIA.isStmtNoInline()) {
301 S.
Diag(St->
getBeginLoc(), diag::warn_function_attribute_ignored_in_stmt)
302 <<
"[[clang::noinline]]";
314 AlwaysInlineAttr AIA(S.
Context, A);
315 if (!AIA.isClangAlwaysInline()) {
316 S.
Diag(St->
getBeginLoc(), diag::warn_function_attribute_ignored_in_stmt)
317 <<
"[[clang::always_inline]]";
371 if (ArgVal < CodeAlignAttr::MinimumAlignment ||
372 ArgVal > CodeAlignAttr::MaximumAlignment || !ArgVal.isPowerOf2()) {
373 if (std::optional<int64_t>
Value = ArgVal.trySExtValue())
374 Diag(CI.
getLoc(), diag::err_attribute_power_of_two_in_range)
375 << CI << CodeAlignAttr::MinimumAlignment
376 << CodeAlignAttr::MaximumAlignment <<
Value.value();
378 Diag(CI.
getLoc(), diag::err_attribute_power_of_two_in_range)
379 << CI << CodeAlignAttr::MinimumAlignment
380 << CodeAlignAttr::MaximumAlignment <<
E;
395template <
typename LoopAttrT>
397 auto FindFunc = [](
const Attr *A) {
return isa<const LoopAttrT>(A); };
398 const auto *FirstItr = llvm::find_if(Attrs, FindFunc);
400 if (FirstItr == Attrs.end())
403 const auto *LastFoundItr = FirstItr;
404 std::optional<llvm::APSInt> FirstValue;
407 dyn_cast<ConstantExpr>(cast<LoopAttrT>(*FirstItr)->getAlignment());
413 while (Attrs.end() != (LastFoundItr = std::find_if(LastFoundItr + 1,
414 Attrs.end(), FindFunc))) {
416 dyn_cast<ConstantExpr>(cast<LoopAttrT>(*LastFoundItr)->getAlignment());
421 llvm::APSInt SecondValue = CASA->getResultAsAPSInt();
423 FirstValue = CAFA->getResultAsAPSInt();
425 if (FirstValue != SecondValue) {
426 S.
Diag((*LastFoundItr)->getLocation(), diag::err_loop_attr_conflict)
428 S.
Diag((*FirstItr)->getLocation(), diag::note_previous_attribute);
436 S.
Diag(A.
getLoc(), diag::warn_unknown_attribute_ignored)
443#define WANT_STMT_MERGE_LOGIC
444#include "clang/Sema/AttrParsedAttrImpl.inc"
445#undef WANT_STMT_MERGE_LOGIC
452 if (Attrs.size() < 2)
456 if (!DiagnoseMutualExclusions(S, Attrs))
483 const LoopHintAttr *StateAttr;
484 const LoopHintAttr *NumericAttr;
485 } HintAttrs[CategoryType::NumberOfCategories] = {};
487 for (
const auto *I : Attrs) {
488 const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I);
494 CategoryType
Category = CategoryType::NumberOfCategories;
495 LoopHintAttr::OptionType Option = LH->getOption();
497 case LoopHintAttr::Vectorize:
498 case LoopHintAttr::VectorizeWidth:
501 case LoopHintAttr::Interleave:
502 case LoopHintAttr::InterleaveCount:
505 case LoopHintAttr::Unroll:
506 case LoopHintAttr::UnrollCount:
509 case LoopHintAttr::UnrollAndJam:
510 case LoopHintAttr::UnrollAndJamCount:
513 case LoopHintAttr::Distribute:
517 case LoopHintAttr::PipelineDisabled:
518 case LoopHintAttr::PipelineInitiationInterval:
521 case LoopHintAttr::VectorizePredicate:
526 assert(
Category != NumberOfCategories &&
"Unhandled loop hint option");
527 auto &CategoryState = HintAttrs[
Category];
528 const LoopHintAttr *PrevAttr;
529 if (Option == LoopHintAttr::Vectorize ||
530 Option == LoopHintAttr::Interleave || Option == LoopHintAttr::Unroll ||
531 Option == LoopHintAttr::UnrollAndJam ||
532 Option == LoopHintAttr::VectorizePredicate ||
533 Option == LoopHintAttr::PipelineDisabled ||
534 Option == LoopHintAttr::Distribute) {
536 PrevAttr = CategoryState.StateAttr;
537 CategoryState.StateAttr = LH;
540 PrevAttr = CategoryState.NumericAttr;
541 CategoryState.NumericAttr = LH;
548 S.
Diag(OptionLoc, diag::err_pragma_loop_compatibility)
549 <<
true << PrevAttr->getDiagnosticName(Policy)
550 << LH->getDiagnosticName(Policy);
552 if (CategoryState.StateAttr && CategoryState.NumericAttr &&
554 CategoryState.StateAttr->getState() == LoopHintAttr::Disable)) {
559 S.
Diag(OptionLoc, diag::err_pragma_loop_compatibility)
561 << CategoryState.StateAttr->getDiagnosticName(Policy)
562 << CategoryState.NumericAttr->getDiagnosticName(Policy);
574 unsigned UnrollFactor = 0;
577 std::optional<llvm::APSInt> ArgVal;
580 S.
Diag(A.
getLoc(), diag::err_attribute_argument_type)
585 int Val = ArgVal->getSExtValue();
588 diag::err_attribute_requires_positive_integer)
592 UnrollFactor =
static_cast<unsigned>(Val);
595 return ::new (S.
Context) OpenCLUnrollHintAttr(S.
Context, A, UnrollFactor);
605 unsigned UnrollFactor = 0;
615 assert(ArgVal != std::nullopt &&
"ArgVal should be an integer constant.");
616 int Val = ArgVal->getSExtValue();
618 assert(Val > 0 &&
"Val should be a positive integer greater than zero.");
619 UnrollFactor =
static_cast<unsigned>(Val);
621 return ::new (S.
Context) HLSLLoopHintAttr(S.
Context, A, UnrollFactor);
636 for (
unsigned ArgIndex = 0; ArgIndex < AL.
getNumArgs(); ++ArgIndex) {
637 AtomicAttr::ConsumedOption Option;
638 StringRef OptionString;
643 diag::err_attribute_argument_type)
651 if (!AtomicAttr::ConvertStrToConsumedOption(OptionString, Option)) {
652 S.
Diag(
Loc, diag::err_attribute_invalid_atomic_argument) << OptionString;
655 Options.push_back(Option);
659 AtomicAttr(S.
Context, AL, Options.data(), Options.size());
677 ? diag::err_keyword_not_supported_on_target
678 : diag::warn_unhandled_ms_attribute_ignored)
690 case ParsedAttr::AT_AlwaysInline:
692 case ParsedAttr::AT_CXXAssume:
694 case ParsedAttr::AT_FallThrough:
696 case ParsedAttr::AT_LoopHint:
698 case ParsedAttr::AT_HLSLLoopHint:
700 case ParsedAttr::AT_HLSLControlFlowHint:
702 case ParsedAttr::AT_OpenCLUnrollHint:
704 case ParsedAttr::AT_Suppress:
706 case ParsedAttr::AT_NoMerge:
708 case ParsedAttr::AT_NoInline:
710 case ParsedAttr::AT_MustTail:
712 case ParsedAttr::AT_Likely:
714 case ParsedAttr::AT_Unlikely:
716 case ParsedAttr::AT_CodeAlign:
718 case ParsedAttr::AT_MSConstexpr:
720 case ParsedAttr::AT_NoConvergent:
722 case ParsedAttr::AT_Annotate:
724 case ParsedAttr::AT_Atomic:
727 if (
Attr *AT =
nullptr; A.
getInfo().handleStmtAttribute(S, St, A, AT) !=
744 OutAttrs.push_back(A);
748 CheckForDuplicateLoopAttrs<CodeAlignAttr>(*
this, OutAttrs);
752 CheckForDuplicateLoopAttrs<CodeAlignAttr>(*
this, Attrs);
759 Diag(A.
getLoc(), diag::err_attribute_wrong_number_arguments)
770 if (Assumption->getDependence() == ExprDependence::None) {
774 Assumption = Res.
get();
802 Assumption = Res.
get();
805 << 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 Attr * handleAtomicAttr(Sema &S, Stmt *St, const ParsedAttr &AL, SourceRange Range)
static void CheckForIncompatibleAttributes(Sema &S, const SmallVectorImpl< const Attr * > &Attrs)
static Attr * handleHLSLControlFlowHint(Sema &S, Stmt *St, const ParsedAttr &A, SourceRange Range)
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.
std::optional< llvm::APSInt > getIntegerConstantExpr(const ASTContext &Ctx) const
isIntegerConstantExpr - Return the value if this expression is a valid integer constant expression.
bool HasSideEffects(const ASTContext &Ctx, bool IncludePossibleEffects=true) const
HasSideEffects - This routine returns true for all those expressions which have any effect other than...
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.
A simple pair of identifier info and location.
SourceLocation getLoc() const
IdentifierInfo * getIdentifierInfo() const
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.
bool isArgIdent(unsigned Arg) const
Expr * getArgAsExpr(unsigned Arg) const
bool checkAtLeastNumArgs(class Sema &S, unsigned Num) const
Check if the attribute has at least as many args as Num.
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)
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)
void DiagnoseUnknownAttribute(const ParsedAttr &AL)
ExprResult ActOnCXXAssumeAttr(Stmt *St, const ParsedAttr &A, SourceRange Range)
ExprResult VerifyIntegerConstantExpression(Expr *E, llvm::APSInt *Result, VerifyICEDiagnoser &Diagnoser, AllowFoldKind CanFold=AllowFoldKind::No)
VerifyIntegerConstantExpression - Verifies that an expression is an ICE, and reports the appropriate ...
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 ActOnFinishFullExpr(Expr *Expr, bool DiscardedValue)
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
@ AANT_ArgumentIdentifier
Describes how types, statements, expressions, and declarations should be printed.