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; }
222 CallExprFinder CEF(S, St);
224 if (!CEF.foundCallExpr() && !CEF.foundAsmStmt()) {
225 S.
Diag(St->
getBeginLoc(), diag::warn_attribute_ignored_no_calls_in_stmt)
233template <
typename OtherAttr,
int DiagIdx>
237 CallExprFinder OrigCEF(SemaRef, OrigSt);
238 CallExprFinder CEF(SemaRef, CurSt);
247 bool CanSuppressDiag =
248 OrigSt && CEF.getCallExprs().size() == OrigCEF.getCallExprs().size();
250 if (!CEF.foundCallExpr()) {
252 diag::warn_attribute_ignored_no_calls_in_stmt)
256 for (
const auto &Tup :
257 llvm::zip_longest(OrigCEF.getCallExprs(), CEF.getCallExprs())) {
261 if (!CanSuppressDiag || !(*std::get<0>(Tup))->getCalleeDecl()) {
262 const Decl *Callee = (*std::get<1>(Tup))->getCalleeDecl();
264 (Callee->hasAttr<OtherAttr>() || Callee->hasAttr<FlattenAttr>())) {
266 diag::warn_function_stmt_attribute_precedence)
267 << A << (Callee->hasAttr<OtherAttr>() ? DiagIdx : 1);
268 SemaRef.
Diag(Callee->getBeginLoc(), diag::note_conflicting_attribute);
278 return CheckStmtInlineAttr<AlwaysInlineAttr, 0>(*
this, OrigSt, CurSt, A);
283 return CheckStmtInlineAttr<NoInlineAttr, 2>(*
this, OrigSt, CurSt, A);
288 NoInlineAttr NIA(S.
Context, A);
289 if (!NIA.isStmtNoInline()) {
290 S.
Diag(St->
getBeginLoc(), diag::warn_function_attribute_ignored_in_stmt)
291 <<
"[[clang::noinline]]";
303 AlwaysInlineAttr AIA(S.
Context, A);
304 if (!AIA.isClangAlwaysInline()) {
305 S.
Diag(St->
getBeginLoc(), diag::warn_function_attribute_ignored_in_stmt)
306 <<
"[[clang::always_inline]]";
360 if (ArgVal < CodeAlignAttr::MinimumAlignment ||
361 ArgVal > CodeAlignAttr::MaximumAlignment || !ArgVal.isPowerOf2()) {
362 if (std::optional<int64_t>
Value = ArgVal.trySExtValue())
363 Diag(CI.
getLoc(), diag::err_attribute_power_of_two_in_range)
364 << CI << CodeAlignAttr::MinimumAlignment
365 << CodeAlignAttr::MaximumAlignment <<
Value.value();
367 Diag(CI.
getLoc(), diag::err_attribute_power_of_two_in_range)
368 << CI << CodeAlignAttr::MinimumAlignment
369 << CodeAlignAttr::MaximumAlignment <<
E;
384template <
typename LoopAttrT>
386 auto FindFunc = [](
const Attr *A) {
return isa<const LoopAttrT>(A); };
387 const auto *FirstItr = std::find_if(Attrs.begin(), Attrs.end(), FindFunc);
389 if (FirstItr == Attrs.end())
392 const auto *LastFoundItr = FirstItr;
393 std::optional<llvm::APSInt> FirstValue;
396 dyn_cast<ConstantExpr>(cast<LoopAttrT>(*FirstItr)->getAlignment());
402 while (Attrs.end() != (LastFoundItr = std::find_if(LastFoundItr + 1,
403 Attrs.end(), FindFunc))) {
405 dyn_cast<ConstantExpr>(cast<LoopAttrT>(*LastFoundItr)->getAlignment());
410 llvm::APSInt SecondValue = CASA->getResultAsAPSInt();
412 FirstValue = CAFA->getResultAsAPSInt();
414 if (FirstValue != SecondValue) {
415 S.
Diag((*LastFoundItr)->getLocation(), diag::err_loop_attr_conflict)
417 S.
Diag((*FirstItr)->getLocation(), diag::note_previous_attribute);
426 S.
Diag(A.
getLoc(), diag::warn_unknown_attribute_ignored)
433#define WANT_STMT_MERGE_LOGIC
434#include "clang/Sema/AttrParsedAttrImpl.inc"
435#undef WANT_STMT_MERGE_LOGIC
442 if (Attrs.size() < 2)
446 if (!DiagnoseMutualExclusions(S, Attrs))
473 const LoopHintAttr *StateAttr;
474 const LoopHintAttr *NumericAttr;
475 } HintAttrs[CategoryType::NumberOfCategories] = {};
477 for (
const auto *I : Attrs) {
478 const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I);
484 CategoryType
Category = CategoryType::NumberOfCategories;
485 LoopHintAttr::OptionType Option = LH->getOption();
487 case LoopHintAttr::Vectorize:
488 case LoopHintAttr::VectorizeWidth:
491 case LoopHintAttr::Interleave:
492 case LoopHintAttr::InterleaveCount:
495 case LoopHintAttr::Unroll:
496 case LoopHintAttr::UnrollCount:
499 case LoopHintAttr::UnrollAndJam:
500 case LoopHintAttr::UnrollAndJamCount:
503 case LoopHintAttr::Distribute:
507 case LoopHintAttr::PipelineDisabled:
508 case LoopHintAttr::PipelineInitiationInterval:
511 case LoopHintAttr::VectorizePredicate:
516 assert(
Category != NumberOfCategories &&
"Unhandled loop hint option");
517 auto &CategoryState = HintAttrs[
Category];
518 const LoopHintAttr *PrevAttr;
519 if (Option == LoopHintAttr::Vectorize ||
520 Option == LoopHintAttr::Interleave || Option == LoopHintAttr::Unroll ||
521 Option == LoopHintAttr::UnrollAndJam ||
522 Option == LoopHintAttr::VectorizePredicate ||
523 Option == LoopHintAttr::PipelineDisabled ||
524 Option == LoopHintAttr::Distribute) {
526 PrevAttr = CategoryState.StateAttr;
527 CategoryState.StateAttr = LH;
530 PrevAttr = CategoryState.NumericAttr;
531 CategoryState.NumericAttr = LH;
538 S.
Diag(OptionLoc, diag::err_pragma_loop_compatibility)
539 <<
true << PrevAttr->getDiagnosticName(Policy)
540 << LH->getDiagnosticName(Policy);
542 if (CategoryState.StateAttr && CategoryState.NumericAttr &&
544 CategoryState.StateAttr->getState() == LoopHintAttr::Disable)) {
549 S.
Diag(OptionLoc, diag::err_pragma_loop_compatibility)
551 << CategoryState.StateAttr->getDiagnosticName(Policy)
552 << CategoryState.NumericAttr->getDiagnosticName(Policy);
564 unsigned UnrollFactor = 0;
567 std::optional<llvm::APSInt> ArgVal;
570 S.
Diag(A.
getLoc(), diag::err_attribute_argument_type)
575 int Val = ArgVal->getSExtValue();
578 diag::err_attribute_requires_positive_integer)
582 UnrollFactor =
static_cast<unsigned>(Val);
585 return ::new (S.
Context) OpenCLUnrollHintAttr(S.
Context, A, UnrollFactor);
595 unsigned UnrollFactor = 0;
599 S.
Diag(A.
getLoc(), diag::err_attribute_argument_type)
612 assert(ArgVal != std::nullopt &&
"ArgVal should be an integer constant.");
613 int Val = ArgVal->getSExtValue();
615 assert(Val > 0 &&
"Val should be a positive integer greater than zero.");
616 UnrollFactor =
static_cast<unsigned>(Val);
618 return ::new (S.
Context) HLSLLoopHintAttr(S.
Context, A, UnrollFactor);
635 ? (
unsigned)diag::err_keyword_not_supported_on_target
637 ? (
unsigned)diag::warn_unhandled_ms_attribute_ignored
638 : (
unsigned)diag::warn_unknown_attribute_ignored)
647 case ParsedAttr::AT_AlwaysInline:
649 case ParsedAttr::AT_CXXAssume:
651 case ParsedAttr::AT_FallThrough:
653 case ParsedAttr::AT_LoopHint:
655 case ParsedAttr::AT_HLSLLoopHint:
657 case ParsedAttr::AT_OpenCLUnrollHint:
659 case ParsedAttr::AT_Suppress:
661 case ParsedAttr::AT_NoMerge:
663 case ParsedAttr::AT_NoInline:
665 case ParsedAttr::AT_MustTail:
667 case ParsedAttr::AT_Likely:
669 case ParsedAttr::AT_Unlikely:
671 case ParsedAttr::AT_CodeAlign:
673 case ParsedAttr::AT_MSConstexpr:
689 OutAttrs.push_back(A);
693 CheckForDuplicateLoopAttrs<CodeAlignAttr>(*
this, OutAttrs);
697 CheckForDuplicateLoopAttrs<CodeAlignAttr>(*
this, Attrs);
704 Diag(A.
getLoc(), diag::err_attribute_wrong_number_arguments)
715 if (Assumption->getDependence() == ExprDependence::None) {
719 Assumption = Res.
get();
744 Assumption = Res.
get();
747 << 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 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.
bool isArgIdent(unsigned Arg) const
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.