21#include "llvm/ADT/StringExtras.h"
30 if (isa<SwitchCase>(St)) {
34 S.
Diag(L, diag::note_fallthrough_insert_semi_fixit)
39 if (FnScope->SwitchStack.empty()) {
50 FnScope->setHasFallthroughStmt();
59 S.
Diag(A.
getLoc(), diag::err_attribute_too_few_arguments) << A << 1;
63 std::vector<StringRef> DiagnosticIdentifiers;
64 for (
unsigned I = 0, E = A.
getNumArgs(); I != E; ++I) {
70 DiagnosticIdentifiers.push_back(RuleName);
73 return ::new (S.
Context) SuppressAttr(
74 S.
Context, A, DiagnosticIdentifiers.data(), DiagnosticIdentifiers.size());
84 StringRef PragmaName =
85 llvm::StringSwitch<StringRef>(PragmaNameLoc->
Ident->
getName())
86 .Cases(
"unroll",
"nounroll",
"unroll_and_jam",
"nounroll_and_jam",
88 .Default(
"clang loop");
93 if (!isa<DoStmt, ForStmt, CXXForRangeStmt, WhileStmt>(St)) {
94 std::string Pragma =
"#pragma " + std::string(PragmaName);
95 S.
Diag(St->
getBeginLoc(), diag::err_pragma_loop_precedes_nonloop) << Pragma;
99 LoopHintAttr::OptionType Option;
100 LoopHintAttr::LoopHintState State;
102 auto SetHints = [&Option, &State](LoopHintAttr::OptionType O,
103 LoopHintAttr::LoopHintState S) {
108 if (PragmaName ==
"nounroll") {
109 SetHints(LoopHintAttr::Unroll, LoopHintAttr::Disable);
110 }
else if (PragmaName ==
"unroll") {
116 SetHints(LoopHintAttr::Unroll, LoopHintAttr::Disable);
118 SetHints(LoopHintAttr::UnrollCount, LoopHintAttr::Numeric);
120 SetHints(LoopHintAttr::UnrollCount, LoopHintAttr::Numeric);
122 SetHints(LoopHintAttr::Unroll, LoopHintAttr::Enable);
123 }
else if (PragmaName ==
"nounroll_and_jam") {
124 SetHints(LoopHintAttr::UnrollAndJam, LoopHintAttr::Disable);
125 }
else if (PragmaName ==
"unroll_and_jam") {
128 SetHints(LoopHintAttr::UnrollAndJamCount, LoopHintAttr::Numeric);
130 SetHints(LoopHintAttr::UnrollAndJam, LoopHintAttr::Enable);
133 assert(OptionLoc && OptionLoc->
Ident &&
134 "Attribute must have valid option info.");
135 Option = llvm::StringSwitch<LoopHintAttr::OptionType>(
137 .Case(
"vectorize", LoopHintAttr::Vectorize)
138 .Case(
"vectorize_width", LoopHintAttr::VectorizeWidth)
139 .Case(
"interleave", LoopHintAttr::Interleave)
140 .Case(
"vectorize_predicate", LoopHintAttr::VectorizePredicate)
141 .Case(
"interleave_count", LoopHintAttr::InterleaveCount)
142 .Case(
"unroll", LoopHintAttr::Unroll)
143 .Case(
"unroll_count", LoopHintAttr::UnrollCount)
144 .Case(
"pipeline", LoopHintAttr::PipelineDisabled)
145 .Case(
"pipeline_initiation_interval",
146 LoopHintAttr::PipelineInitiationInterval)
147 .Case(
"distribute", LoopHintAttr::Distribute)
148 .Default(LoopHintAttr::Vectorize);
149 if (Option == LoopHintAttr::VectorizeWidth) {
150 assert((ValueExpr || (StateLoc && StateLoc->
Ident)) &&
151 "Attribute must have a valid value expression or argument.");
156 State = LoopHintAttr::ScalableWidth;
158 State = LoopHintAttr::FixedWidth;
159 }
else if (Option == LoopHintAttr::InterleaveCount ||
160 Option == LoopHintAttr::UnrollCount ||
161 Option == LoopHintAttr::PipelineInitiationInterval) {
162 assert(ValueExpr &&
"Attribute must have a valid value expression.");
166 State = LoopHintAttr::Numeric;
167 }
else if (Option == LoopHintAttr::Vectorize ||
168 Option == LoopHintAttr::Interleave ||
169 Option == LoopHintAttr::VectorizePredicate ||
170 Option == LoopHintAttr::Unroll ||
171 Option == LoopHintAttr::Distribute ||
172 Option == LoopHintAttr::PipelineDisabled) {
173 assert(StateLoc && StateLoc->
Ident &&
"Loop hint must have an argument");
175 State = LoopHintAttr::Disable;
176 else if (StateLoc->
Ident->
isStr(
"assume_safety"))
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; }
221 CallExprFinder CEF(S, St);
223 if (!CEF.foundCallExpr() && !CEF.foundAsmStmt()) {
224 S.
Diag(St->
getBeginLoc(), diag::warn_attribute_ignored_no_calls_in_stmt)
232template <
typename OtherAttr,
int DiagIdx>
236 CallExprFinder OrigCEF(SemaRef, OrigSt);
237 CallExprFinder CEF(SemaRef, CurSt);
246 bool CanSuppressDiag =
247 OrigSt && CEF.getCallExprs().size() == OrigCEF.getCallExprs().size();
249 if (!CEF.foundCallExpr()) {
251 diag::warn_attribute_ignored_no_calls_in_stmt)
255 for (
const auto &Tup :
256 llvm::zip_longest(OrigCEF.getCallExprs(), CEF.getCallExprs())) {
260 if (!CanSuppressDiag || !(*std::get<0>(Tup))->getCalleeDecl()) {
261 const Decl *Callee = (*std::get<1>(Tup))->getCalleeDecl();
263 (Callee->hasAttr<OtherAttr>() || Callee->hasAttr<FlattenAttr>())) {
265 diag::warn_function_stmt_attribute_precedence)
266 << A << (Callee->hasAttr<OtherAttr>() ? DiagIdx : 1);
267 SemaRef.
Diag(Callee->getBeginLoc(), diag::note_conflicting_attribute);
277 return CheckStmtInlineAttr<AlwaysInlineAttr, 0>(*
this, OrigSt, CurSt, A);
282 return CheckStmtInlineAttr<NoInlineAttr, 2>(*
this, OrigSt, CurSt, A);
287 NoInlineAttr NIA(S.
Context, A);
288 if (!NIA.isClangNoInline()) {
289 S.
Diag(St->
getBeginLoc(), diag::warn_function_attribute_ignored_in_stmt)
290 <<
"[[clang::noinline]]";
302 AlwaysInlineAttr AIA(S.
Context, A);
303 if (!AIA.isClangAlwaysInline()) {
304 S.
Diag(St->
getBeginLoc(), diag::warn_function_attribute_ignored_in_stmt)
305 <<
"[[clang::always_inline]]";
359 if (ArgVal < CodeAlignAttr::MinimumAlignment ||
360 ArgVal > CodeAlignAttr::MaximumAlignment || !ArgVal.isPowerOf2()) {
361 if (std::optional<int64_t>
Value = ArgVal.trySExtValue())
362 Diag(CI.
getLoc(), diag::err_attribute_power_of_two_in_range)
363 << CI << CodeAlignAttr::MinimumAlignment
364 << CodeAlignAttr::MaximumAlignment <<
Value.value();
366 Diag(CI.
getLoc(), diag::err_attribute_power_of_two_in_range)
367 << CI << CodeAlignAttr::MinimumAlignment
368 << CodeAlignAttr::MaximumAlignment << E;
383template <
typename LoopAttrT>
385 auto FindFunc = [](
const Attr *A) {
return isa<const LoopAttrT>(A); };
386 const auto *FirstItr = std::find_if(Attrs.begin(), Attrs.end(), FindFunc);
388 if (FirstItr == Attrs.end())
391 const auto *LastFoundItr = FirstItr;
392 std::optional<llvm::APSInt> FirstValue;
395 dyn_cast<ConstantExpr>(cast<LoopAttrT>(*FirstItr)->getAlignment());
401 while (Attrs.end() != (LastFoundItr = std::find_if(LastFoundItr + 1,
402 Attrs.end(), FindFunc))) {
404 dyn_cast<ConstantExpr>(cast<LoopAttrT>(*LastFoundItr)->getAlignment());
409 llvm::APSInt SecondValue = CASA->getResultAsAPSInt();
411 FirstValue = CAFA->getResultAsAPSInt();
413 if (FirstValue != SecondValue) {
414 S.
Diag((*LastFoundItr)->getLocation(), diag::err_loop_attr_conflict)
416 S.
Diag((*FirstItr)->getLocation(), diag::note_previous_attribute);
425 S.
Diag(A.
getLoc(), diag::warn_unknown_attribute_ignored)
432#define WANT_STMT_MERGE_LOGIC
433#include "clang/Sema/AttrParsedAttrImpl.inc"
434#undef WANT_STMT_MERGE_LOGIC
441 if (Attrs.size() < 2)
445 if (!DiagnoseMutualExclusions(S, Attrs))
472 const LoopHintAttr *StateAttr;
473 const LoopHintAttr *NumericAttr;
474 } HintAttrs[CategoryType::NumberOfCategories] = {};
476 for (
const auto *I : Attrs) {
477 const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I);
483 CategoryType
Category = CategoryType::NumberOfCategories;
484 LoopHintAttr::OptionType Option = LH->getOption();
486 case LoopHintAttr::Vectorize:
487 case LoopHintAttr::VectorizeWidth:
490 case LoopHintAttr::Interleave:
491 case LoopHintAttr::InterleaveCount:
494 case LoopHintAttr::Unroll:
495 case LoopHintAttr::UnrollCount:
498 case LoopHintAttr::UnrollAndJam:
499 case LoopHintAttr::UnrollAndJamCount:
502 case LoopHintAttr::Distribute:
506 case LoopHintAttr::PipelineDisabled:
507 case LoopHintAttr::PipelineInitiationInterval:
510 case LoopHintAttr::VectorizePredicate:
515 assert(
Category != NumberOfCategories &&
"Unhandled loop hint option");
516 auto &CategoryState = HintAttrs[
Category];
517 const LoopHintAttr *PrevAttr;
518 if (Option == LoopHintAttr::Vectorize ||
519 Option == LoopHintAttr::Interleave || Option == LoopHintAttr::Unroll ||
520 Option == LoopHintAttr::UnrollAndJam ||
521 Option == LoopHintAttr::VectorizePredicate ||
522 Option == LoopHintAttr::PipelineDisabled ||
523 Option == LoopHintAttr::Distribute) {
525 PrevAttr = CategoryState.StateAttr;
526 CategoryState.StateAttr = LH;
529 PrevAttr = CategoryState.NumericAttr;
530 CategoryState.NumericAttr = LH;
537 S.
Diag(OptionLoc, diag::err_pragma_loop_compatibility)
538 <<
true << PrevAttr->getDiagnosticName(Policy)
539 << LH->getDiagnosticName(Policy);
541 if (CategoryState.StateAttr && CategoryState.NumericAttr &&
543 CategoryState.StateAttr->getState() == LoopHintAttr::Disable)) {
548 S.
Diag(OptionLoc, diag::err_pragma_loop_compatibility)
550 << CategoryState.StateAttr->getDiagnosticName(Policy)
551 << CategoryState.NumericAttr->getDiagnosticName(Policy);
563 unsigned UnrollFactor = 0;
566 std::optional<llvm::APSInt> ArgVal;
569 S.
Diag(A.
getLoc(), diag::err_attribute_argument_type)
574 int Val = ArgVal->getSExtValue();
577 diag::err_attribute_requires_positive_integer)
581 UnrollFactor =
static_cast<unsigned>(Val);
584 return ::new (S.
Context) OpenCLUnrollHintAttr(S.
Context, A, UnrollFactor);
601 ? (
unsigned)diag::err_keyword_not_supported_on_target
603 ? (
unsigned)diag::warn_unhandled_ms_attribute_ignored
604 : (
unsigned)diag::warn_unknown_attribute_ignored)
613 case ParsedAttr::AT_AlwaysInline:
615 case ParsedAttr::AT_CXXAssume:
617 case ParsedAttr::AT_FallThrough:
619 case ParsedAttr::AT_LoopHint:
621 case ParsedAttr::AT_OpenCLUnrollHint:
623 case ParsedAttr::AT_Suppress:
625 case ParsedAttr::AT_NoMerge:
627 case ParsedAttr::AT_NoInline:
629 case ParsedAttr::AT_MustTail:
631 case ParsedAttr::AT_Likely:
633 case ParsedAttr::AT_Unlikely:
635 case ParsedAttr::AT_CodeAlign:
637 case ParsedAttr::AT_MSConstexpr:
653 OutAttrs.push_back(A);
657 CheckForDuplicateLoopAttrs<CodeAlignAttr>(*
this, OutAttrs);
661 CheckForDuplicateLoopAttrs<CodeAlignAttr>(*
this, Attrs);
678 if (Assumption->getDependence() == ExprDependence::None) {
682 Assumption = Res.
get();
706 Assumption = Res.
get();
709 << 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 * 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.
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
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.