22#include "llvm/ADT/StringExtras.h"
31 if (isa<SwitchCase>(St)) {
35 S.
Diag(L, diag::note_fallthrough_insert_semi_fixit)
40 if (FnScope->SwitchStack.empty()) {
51 FnScope->setHasFallthroughStmt();
60 S.
Diag(A.
getLoc(), diag::err_attribute_too_few_arguments) << A << 1;
64 std::vector<StringRef> DiagnosticIdentifiers;
71 DiagnosticIdentifiers.push_back(RuleName);
74 return ::new (S.
Context) SuppressAttr(
75 S.
Context, A, DiagnosticIdentifiers.data(), DiagnosticIdentifiers.size());
85 StringRef PragmaName =
86 llvm::StringSwitch<StringRef>(PragmaNameLoc->
Ident->
getName())
87 .Cases(
"unroll",
"nounroll",
"unroll_and_jam",
"nounroll_and_jam",
89 .Default(
"clang loop");
94 if (!isa<DoStmt, ForStmt, CXXForRangeStmt, WhileStmt>(St)) {
95 std::string Pragma =
"#pragma " + std::string(PragmaName);
96 S.
Diag(St->
getBeginLoc(), diag::err_pragma_loop_precedes_nonloop) << Pragma;
100 LoopHintAttr::OptionType Option;
101 LoopHintAttr::LoopHintState State;
103 auto SetHints = [&Option, &State](LoopHintAttr::OptionType O,
104 LoopHintAttr::LoopHintState S) {
109 if (PragmaName ==
"nounroll") {
110 SetHints(LoopHintAttr::Unroll, LoopHintAttr::Disable);
111 }
else if (PragmaName ==
"unroll") {
117 SetHints(LoopHintAttr::Unroll, LoopHintAttr::Disable);
119 SetHints(LoopHintAttr::UnrollCount, LoopHintAttr::Numeric);
121 SetHints(LoopHintAttr::UnrollCount, LoopHintAttr::Numeric);
123 SetHints(LoopHintAttr::Unroll, LoopHintAttr::Enable);
124 }
else if (PragmaName ==
"nounroll_and_jam") {
125 SetHints(LoopHintAttr::UnrollAndJam, LoopHintAttr::Disable);
126 }
else if (PragmaName ==
"unroll_and_jam") {
129 SetHints(LoopHintAttr::UnrollAndJamCount, LoopHintAttr::Numeric);
131 SetHints(LoopHintAttr::UnrollAndJam, LoopHintAttr::Enable);
134 assert(OptionLoc && OptionLoc->
Ident &&
135 "Attribute must have valid option info.");
136 Option = llvm::StringSwitch<LoopHintAttr::OptionType>(
138 .Case(
"vectorize", LoopHintAttr::Vectorize)
139 .Case(
"vectorize_width", LoopHintAttr::VectorizeWidth)
140 .Case(
"interleave", LoopHintAttr::Interleave)
141 .Case(
"vectorize_predicate", LoopHintAttr::VectorizePredicate)
142 .Case(
"interleave_count", LoopHintAttr::InterleaveCount)
143 .Case(
"unroll", LoopHintAttr::Unroll)
144 .Case(
"unroll_count", LoopHintAttr::UnrollCount)
145 .Case(
"pipeline", LoopHintAttr::PipelineDisabled)
146 .Case(
"pipeline_initiation_interval",
147 LoopHintAttr::PipelineInitiationInterval)
148 .Case(
"distribute", LoopHintAttr::Distribute)
149 .Default(LoopHintAttr::Vectorize);
150 if (Option == LoopHintAttr::VectorizeWidth) {
151 assert((ValueExpr || (StateLoc && StateLoc->
Ident)) &&
152 "Attribute must have a valid value expression or argument.");
157 State = LoopHintAttr::ScalableWidth;
159 State = LoopHintAttr::FixedWidth;
160 }
else if (Option == LoopHintAttr::InterleaveCount ||
161 Option == LoopHintAttr::UnrollCount ||
162 Option == LoopHintAttr::PipelineInitiationInterval) {
163 assert(ValueExpr &&
"Attribute must have a valid value expression.");
167 State = LoopHintAttr::Numeric;
168 }
else if (Option == LoopHintAttr::Vectorize ||
169 Option == LoopHintAttr::Interleave ||
170 Option == LoopHintAttr::VectorizePredicate ||
171 Option == LoopHintAttr::Unroll ||
172 Option == LoopHintAttr::Distribute ||
173 Option == LoopHintAttr::PipelineDisabled) {
174 assert(StateLoc && StateLoc->
Ident &&
"Loop hint must have an argument");
176 State = LoopHintAttr::Disable;
177 else if (StateLoc->
Ident->
isStr(
"assume_safety"))
178 State = LoopHintAttr::AssumeSafety;
180 State = LoopHintAttr::Full;
182 State = LoopHintAttr::Enable;
184 llvm_unreachable(
"bad loop hint argument");
186 llvm_unreachable(
"bad loop hint");
189 return LoopHintAttr::CreateImplicit(S.
Context, Option, State, ValueExpr, A);
194 bool FoundAsmStmt =
false;
195 std::vector<const CallExpr *> CallExprs;
200 CallExprFinder(
Sema &S,
const Stmt *St) : Inherited(S.Context) {
Visit(St); }
202 bool foundCallExpr() {
return !CallExprs.empty(); }
203 const std::vector<const CallExpr *> &getCallExprs() {
return CallExprs; }
205 bool foundAsmStmt() {
return FoundAsmStmt; }
209 void VisitAsmStmt(
const AsmStmt *S) { FoundAsmStmt =
true; }
221 CallExprFinder CEF(S, St);
223 if (!CEF.foundCallExpr() && !CEF.foundAsmStmt()) {
224 S.
Diag(St->
getBeginLoc(), diag::warn_attribute_ignored_no_calls_in_stmt)
234 CallExprFinder CEF(S, St);
236 if (!CEF.foundCallExpr() && !CEF.foundAsmStmt()) {
237 S.
Diag(St->
getBeginLoc(), diag::warn_attribute_ignored_no_calls_in_stmt)
245template <
typename OtherAttr,
int DiagIdx>
249 CallExprFinder OrigCEF(SemaRef, OrigSt);
250 CallExprFinder CEF(SemaRef, CurSt);
259 bool CanSuppressDiag =
260 OrigSt && CEF.getCallExprs().size() == OrigCEF.getCallExprs().size();
262 if (!CEF.foundCallExpr()) {
264 diag::warn_attribute_ignored_no_calls_in_stmt)
268 for (
const auto &Tup :
269 llvm::zip_longest(OrigCEF.getCallExprs(), CEF.getCallExprs())) {
273 if (!CanSuppressDiag || !(*std::get<0>(Tup))->getCalleeDecl()) {
274 const Decl *Callee = (*std::get<1>(Tup))->getCalleeDecl();
276 (Callee->hasAttr<OtherAttr>() || Callee->hasAttr<FlattenAttr>())) {
278 diag::warn_function_stmt_attribute_precedence)
279 << A << (Callee->hasAttr<OtherAttr>() ? DiagIdx : 1);
280 SemaRef.
Diag(Callee->getBeginLoc(), diag::note_conflicting_attribute);
290 return CheckStmtInlineAttr<AlwaysInlineAttr, 0>(*
this, OrigSt, CurSt, A);
295 return CheckStmtInlineAttr<NoInlineAttr, 2>(*
this, OrigSt, CurSt, A);
300 NoInlineAttr NIA(S.
Context, A);
301 if (!NIA.isStmtNoInline()) {
302 S.
Diag(St->
getBeginLoc(), diag::warn_function_attribute_ignored_in_stmt)
303 <<
"[[clang::noinline]]";
315 AlwaysInlineAttr AIA(S.
Context, A);
316 if (!AIA.isClangAlwaysInline()) {
317 S.
Diag(St->
getBeginLoc(), diag::warn_function_attribute_ignored_in_stmt)
318 <<
"[[clang::always_inline]]";
372 if (ArgVal < CodeAlignAttr::MinimumAlignment ||
373 ArgVal > CodeAlignAttr::MaximumAlignment || !ArgVal.isPowerOf2()) {
374 if (std::optional<int64_t>
Value = ArgVal.trySExtValue())
375 Diag(CI.
getLoc(), diag::err_attribute_power_of_two_in_range)
376 << CI << CodeAlignAttr::MinimumAlignment
377 << CodeAlignAttr::MaximumAlignment <<
Value.value();
379 Diag(CI.
getLoc(), diag::err_attribute_power_of_two_in_range)
380 << CI << CodeAlignAttr::MinimumAlignment
381 << CodeAlignAttr::MaximumAlignment <<
E;
396template <
typename LoopAttrT>
398 auto FindFunc = [](
const Attr *A) {
return isa<const LoopAttrT>(A); };
399 const auto *FirstItr = std::find_if(Attrs.begin(), Attrs.end(), FindFunc);
401 if (FirstItr == Attrs.end())
404 const auto *LastFoundItr = FirstItr;
405 std::optional<llvm::APSInt> FirstValue;
408 dyn_cast<ConstantExpr>(cast<LoopAttrT>(*FirstItr)->getAlignment());
414 while (Attrs.end() != (LastFoundItr = std::find_if(LastFoundItr + 1,
415 Attrs.end(), FindFunc))) {
417 dyn_cast<ConstantExpr>(cast<LoopAttrT>(*LastFoundItr)->getAlignment());
422 llvm::APSInt SecondValue = CASA->getResultAsAPSInt();
424 FirstValue = CAFA->getResultAsAPSInt();
426 if (FirstValue != SecondValue) {
427 S.
Diag((*LastFoundItr)->getLocation(), diag::err_loop_attr_conflict)
429 S.
Diag((*FirstItr)->getLocation(), diag::note_previous_attribute);
438 S.
Diag(A.
getLoc(), diag::warn_unknown_attribute_ignored)
445#define WANT_STMT_MERGE_LOGIC
446#include "clang/Sema/AttrParsedAttrImpl.inc"
447#undef WANT_STMT_MERGE_LOGIC
454 if (Attrs.size() < 2)
458 if (!DiagnoseMutualExclusions(S, Attrs))
485 const LoopHintAttr *StateAttr;
486 const LoopHintAttr *NumericAttr;
487 } HintAttrs[CategoryType::NumberOfCategories] = {};
489 for (
const auto *I : Attrs) {
490 const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I);
496 CategoryType
Category = CategoryType::NumberOfCategories;
497 LoopHintAttr::OptionType Option = LH->getOption();
499 case LoopHintAttr::Vectorize:
500 case LoopHintAttr::VectorizeWidth:
503 case LoopHintAttr::Interleave:
504 case LoopHintAttr::InterleaveCount:
507 case LoopHintAttr::Unroll:
508 case LoopHintAttr::UnrollCount:
511 case LoopHintAttr::UnrollAndJam:
512 case LoopHintAttr::UnrollAndJamCount:
515 case LoopHintAttr::Distribute:
519 case LoopHintAttr::PipelineDisabled:
520 case LoopHintAttr::PipelineInitiationInterval:
523 case LoopHintAttr::VectorizePredicate:
528 assert(
Category != NumberOfCategories &&
"Unhandled loop hint option");
529 auto &CategoryState = HintAttrs[
Category];
530 const LoopHintAttr *PrevAttr;
531 if (Option == LoopHintAttr::Vectorize ||
532 Option == LoopHintAttr::Interleave || Option == LoopHintAttr::Unroll ||
533 Option == LoopHintAttr::UnrollAndJam ||
534 Option == LoopHintAttr::VectorizePredicate ||
535 Option == LoopHintAttr::PipelineDisabled ||
536 Option == LoopHintAttr::Distribute) {
538 PrevAttr = CategoryState.StateAttr;
539 CategoryState.StateAttr = LH;
542 PrevAttr = CategoryState.NumericAttr;
543 CategoryState.NumericAttr = LH;
550 S.
Diag(OptionLoc, diag::err_pragma_loop_compatibility)
551 <<
true << PrevAttr->getDiagnosticName(Policy)
552 << LH->getDiagnosticName(Policy);
554 if (CategoryState.StateAttr && CategoryState.NumericAttr &&
556 CategoryState.StateAttr->getState() == LoopHintAttr::Disable)) {
561 S.
Diag(OptionLoc, diag::err_pragma_loop_compatibility)
563 << CategoryState.StateAttr->getDiagnosticName(Policy)
564 << CategoryState.NumericAttr->getDiagnosticName(Policy);
576 unsigned UnrollFactor = 0;
579 std::optional<llvm::APSInt> ArgVal;
582 S.
Diag(A.
getLoc(), diag::err_attribute_argument_type)
587 int Val = ArgVal->getSExtValue();
590 diag::err_attribute_requires_positive_integer)
594 UnrollFactor =
static_cast<unsigned>(Val);
597 return ::new (S.
Context) OpenCLUnrollHintAttr(S.
Context, A, UnrollFactor);
607 unsigned UnrollFactor = 0;
617 assert(ArgVal != std::nullopt &&
"ArgVal should be an integer constant.");
618 int Val = ArgVal->getSExtValue();
620 assert(Val > 0 &&
"Val should be a positive integer greater than zero.");
621 UnrollFactor =
static_cast<unsigned>(Val);
623 return ::new (S.
Context) HLSLLoopHintAttr(S.
Context, A, UnrollFactor);
640 ? (
unsigned)diag::err_keyword_not_supported_on_target
642 ? (
unsigned)diag::warn_unhandled_ms_attribute_ignored
643 : (
unsigned)diag::warn_unknown_attribute_ignored)
652 case ParsedAttr::AT_AlwaysInline:
654 case ParsedAttr::AT_CXXAssume:
656 case ParsedAttr::AT_FallThrough:
658 case ParsedAttr::AT_LoopHint:
660 case ParsedAttr::AT_HLSLLoopHint:
662 case ParsedAttr::AT_OpenCLUnrollHint:
664 case ParsedAttr::AT_Suppress:
666 case ParsedAttr::AT_NoMerge:
668 case ParsedAttr::AT_NoInline:
670 case ParsedAttr::AT_MustTail:
672 case ParsedAttr::AT_Likely:
674 case ParsedAttr::AT_Unlikely:
676 case ParsedAttr::AT_CodeAlign:
678 case ParsedAttr::AT_MSConstexpr:
680 case ParsedAttr::AT_NoConvergent:
696 OutAttrs.push_back(A);
700 CheckForDuplicateLoopAttrs<CodeAlignAttr>(*
this, OutAttrs);
704 CheckForDuplicateLoopAttrs<CodeAlignAttr>(*
this, Attrs);
711 Diag(A.
getLoc(), diag::err_attribute_wrong_number_arguments)
722 if (Assumption->getDependence() == ExprDependence::None) {
726 Assumption = Res.
get();
751 Assumption = Res.
get();
754 << 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)
Defines the SourceManager interface.
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
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)
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.