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();
56 std::vector<StringRef> DiagnosticIdentifiers;
57 for (
unsigned I = 0, E = A.
getNumArgs(); I != E; ++I) {
65 DiagnosticIdentifiers.push_back(RuleName);
68 return ::new (S.
Context) SuppressAttr(
69 S.
Context, A, DiagnosticIdentifiers.data(), DiagnosticIdentifiers.size());
79 StringRef PragmaName =
80 llvm::StringSwitch<StringRef>(PragmaNameLoc->
Ident->
getName())
81 .Cases(
"unroll",
"nounroll",
"unroll_and_jam",
"nounroll_and_jam",
83 .Default(
"clang loop");
88 if (!isa<DoStmt, ForStmt, CXXForRangeStmt, WhileStmt>(St)) {
89 std::string Pragma =
"#pragma " + std::string(PragmaName);
90 S.
Diag(St->
getBeginLoc(), diag::err_pragma_loop_precedes_nonloop) << Pragma;
94 LoopHintAttr::OptionType Option;
95 LoopHintAttr::LoopHintState State;
97 auto SetHints = [&Option, &State](LoopHintAttr::OptionType O,
98 LoopHintAttr::LoopHintState S) {
103 if (PragmaName ==
"nounroll") {
104 SetHints(LoopHintAttr::Unroll, LoopHintAttr::Disable);
105 }
else if (PragmaName ==
"unroll") {
108 SetHints(LoopHintAttr::UnrollCount, LoopHintAttr::Numeric);
110 SetHints(LoopHintAttr::Unroll, LoopHintAttr::Enable);
111 }
else if (PragmaName ==
"nounroll_and_jam") {
112 SetHints(LoopHintAttr::UnrollAndJam, LoopHintAttr::Disable);
113 }
else if (PragmaName ==
"unroll_and_jam") {
116 SetHints(LoopHintAttr::UnrollAndJamCount, LoopHintAttr::Numeric);
118 SetHints(LoopHintAttr::UnrollAndJam, LoopHintAttr::Enable);
121 assert(OptionLoc && OptionLoc->
Ident &&
122 "Attribute must have valid option info.");
123 Option = llvm::StringSwitch<LoopHintAttr::OptionType>(
125 .Case(
"vectorize", LoopHintAttr::Vectorize)
126 .Case(
"vectorize_width", LoopHintAttr::VectorizeWidth)
127 .Case(
"interleave", LoopHintAttr::Interleave)
128 .Case(
"vectorize_predicate", LoopHintAttr::VectorizePredicate)
129 .Case(
"interleave_count", LoopHintAttr::InterleaveCount)
130 .Case(
"unroll", LoopHintAttr::Unroll)
131 .Case(
"unroll_count", LoopHintAttr::UnrollCount)
132 .Case(
"pipeline", LoopHintAttr::PipelineDisabled)
133 .Case(
"pipeline_initiation_interval",
134 LoopHintAttr::PipelineInitiationInterval)
135 .Case(
"distribute", LoopHintAttr::Distribute)
136 .Default(LoopHintAttr::Vectorize);
137 if (Option == LoopHintAttr::VectorizeWidth) {
138 assert((ValueExpr || (StateLoc && StateLoc->
Ident)) &&
139 "Attribute must have a valid value expression or argument.");
143 State = LoopHintAttr::ScalableWidth;
145 State = LoopHintAttr::FixedWidth;
146 }
else if (Option == LoopHintAttr::InterleaveCount ||
147 Option == LoopHintAttr::UnrollCount ||
148 Option == LoopHintAttr::PipelineInitiationInterval) {
149 assert(ValueExpr &&
"Attribute must have a valid value expression.");
152 State = LoopHintAttr::Numeric;
153 }
else if (Option == LoopHintAttr::Vectorize ||
154 Option == LoopHintAttr::Interleave ||
155 Option == LoopHintAttr::VectorizePredicate ||
156 Option == LoopHintAttr::Unroll ||
157 Option == LoopHintAttr::Distribute ||
158 Option == LoopHintAttr::PipelineDisabled) {
159 assert(StateLoc && StateLoc->
Ident &&
"Loop hint must have an argument");
161 State = LoopHintAttr::Disable;
162 else if (StateLoc->
Ident->
isStr(
"assume_safety"))
163 State = LoopHintAttr::AssumeSafety;
165 State = LoopHintAttr::Full;
167 State = LoopHintAttr::Enable;
169 llvm_unreachable(
"bad loop hint argument");
171 llvm_unreachable(
"bad loop hint");
174 return LoopHintAttr::CreateImplicit(S.
Context, Option, State, ValueExpr, A);
179 bool FoundAsmStmt =
false;
180 std::vector<const CallExpr *> CallExprs;
185 CallExprFinder(
Sema &S,
const Stmt *St) : Inherited(S.Context) {
Visit(St); }
187 bool foundCallExpr() {
return !CallExprs.empty(); }
188 const std::vector<const CallExpr *> &getCallExprs() {
return CallExprs; }
190 bool foundAsmStmt() {
return FoundAsmStmt; }
194 void VisitAsmStmt(
const AsmStmt *S) { FoundAsmStmt =
true; }
207 CallExprFinder CEF(S, St);
209 if (!CEF.foundCallExpr() && !CEF.foundAsmStmt()) {
210 S.
Diag(St->
getBeginLoc(), diag::warn_attribute_ignored_no_calls_in_stmt)
218template <
typename OtherAttr,
int DiagIdx>
222 CallExprFinder OrigCEF(SemaRef, OrigSt);
223 CallExprFinder CEF(SemaRef, CurSt);
232 bool CanSuppressDiag =
233 OrigSt && CEF.getCallExprs().size() == OrigCEF.getCallExprs().size();
235 if (!CEF.foundCallExpr()) {
237 diag::warn_attribute_ignored_no_calls_in_stmt)
241 for (
const auto &Tup :
242 llvm::zip_longest(OrigCEF.getCallExprs(), CEF.getCallExprs())) {
246 if (!CanSuppressDiag || !(*std::get<0>(Tup))->getCalleeDecl()) {
247 const Decl *Callee = (*std::get<1>(Tup))->getCalleeDecl();
249 (Callee->hasAttr<OtherAttr>() || Callee->hasAttr<FlattenAttr>())) {
251 diag::warn_function_stmt_attribute_precedence)
252 << A << (Callee->hasAttr<OtherAttr>() ? DiagIdx : 1);
253 SemaRef.
Diag(Callee->getBeginLoc(), diag::note_conflicting_attribute);
263 return CheckStmtInlineAttr<AlwaysInlineAttr, 0>(*
this, OrigSt, CurSt, A);
268 return CheckStmtInlineAttr<NoInlineAttr, 2>(*
this, OrigSt, CurSt, A);
273 NoInlineAttr NIA(S.
Context, A);
274 if (!NIA.isClangNoInline()) {
275 S.
Diag(St->
getBeginLoc(), diag::warn_function_attribute_ignored_in_stmt)
276 <<
"[[clang::noinline]]";
288 AlwaysInlineAttr AIA(S.
Context, A);
289 if (!AIA.isClangAlwaysInline()) {
290 S.
Diag(St->
getBeginLoc(), diag::warn_function_attribute_ignored_in_stmt)
291 <<
"[[clang::always_inline]]";
311 S.
Diag(A.
getLoc(), diag::ext_cxx20_attr) << A << Range;
320 S.
Diag(A.
getLoc(), diag::ext_cxx20_attr) << A << Range;
325#define WANT_STMT_MERGE_LOGIC
326#include "clang/Sema/AttrParsedAttrImpl.inc"
327#undef WANT_STMT_MERGE_LOGIC
334 if (Attrs.size() < 2)
338 if (!DiagnoseMutualExclusions(S, Attrs))
365 const LoopHintAttr *StateAttr;
366 const LoopHintAttr *NumericAttr;
367 } HintAttrs[CategoryType::NumberOfCategories] = {};
369 for (
const auto *I : Attrs) {
370 const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I);
376 CategoryType
Category = CategoryType::NumberOfCategories;
377 LoopHintAttr::OptionType Option = LH->getOption();
379 case LoopHintAttr::Vectorize:
380 case LoopHintAttr::VectorizeWidth:
383 case LoopHintAttr::Interleave:
384 case LoopHintAttr::InterleaveCount:
387 case LoopHintAttr::Unroll:
388 case LoopHintAttr::UnrollCount:
391 case LoopHintAttr::UnrollAndJam:
392 case LoopHintAttr::UnrollAndJamCount:
395 case LoopHintAttr::Distribute:
399 case LoopHintAttr::PipelineDisabled:
400 case LoopHintAttr::PipelineInitiationInterval:
403 case LoopHintAttr::VectorizePredicate:
408 assert(
Category != NumberOfCategories &&
"Unhandled loop hint option");
409 auto &CategoryState = HintAttrs[
Category];
410 const LoopHintAttr *PrevAttr;
411 if (Option == LoopHintAttr::Vectorize ||
412 Option == LoopHintAttr::Interleave || Option == LoopHintAttr::Unroll ||
413 Option == LoopHintAttr::UnrollAndJam ||
414 Option == LoopHintAttr::VectorizePredicate ||
415 Option == LoopHintAttr::PipelineDisabled ||
416 Option == LoopHintAttr::Distribute) {
418 PrevAttr = CategoryState.StateAttr;
419 CategoryState.StateAttr = LH;
422 PrevAttr = CategoryState.NumericAttr;
423 CategoryState.NumericAttr = LH;
430 S.
Diag(OptionLoc, diag::err_pragma_loop_compatibility)
431 <<
true << PrevAttr->getDiagnosticName(Policy)
432 << LH->getDiagnosticName(Policy);
434 if (CategoryState.StateAttr && CategoryState.NumericAttr &&
436 CategoryState.StateAttr->getState() == LoopHintAttr::Disable)) {
441 S.
Diag(OptionLoc, diag::err_pragma_loop_compatibility)
443 << CategoryState.StateAttr->getDiagnosticName(Policy)
444 << CategoryState.NumericAttr->getDiagnosticName(Policy);
456 unsigned UnrollFactor = 0;
459 std::optional<llvm::APSInt> ArgVal;
462 S.
Diag(A.
getLoc(), diag::err_attribute_argument_type)
467 int Val = ArgVal->getSExtValue();
470 diag::err_attribute_requires_positive_integer)
474 UnrollFactor =
static_cast<unsigned>(Val);
477 return ::new (S.
Context) OpenCLUnrollHintAttr(S.
Context, A, UnrollFactor);
494 ? (
unsigned)diag::err_keyword_not_supported_on_target
496 ? (
unsigned)diag::warn_unhandled_ms_attribute_ignored
497 : (
unsigned)diag::warn_unknown_attribute_ignored)
506 case ParsedAttr::AT_AlwaysInline:
508 case ParsedAttr::AT_FallThrough:
510 case ParsedAttr::AT_LoopHint:
512 case ParsedAttr::AT_OpenCLUnrollHint:
514 case ParsedAttr::AT_Suppress:
516 case ParsedAttr::AT_NoMerge:
518 case ParsedAttr::AT_NoInline:
520 case ParsedAttr::AT_MustTail:
522 case ParsedAttr::AT_Likely:
524 case ParsedAttr::AT_Unlikely:
540 OutAttrs.push_back(A);
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 handleSuppressAttr(Sema &S, Decl *D, const ParsedAttr &AL)
static Attr * handleMustTailAttr(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 * 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
const IdentifierInfo * getScopeName() const
bool isRegularKeywordAttribute() const
SourceLocation getLoc() 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.
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.
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.
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.
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.
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID, bool DeferHint=false)
Emit a diagnostic.
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 ...
SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset=0)
Calls Lexer::getLocForEndOfToken()
const LangOptions & getLangOpts() const
sema::FunctionScopeInfo * getCurFunction() const
bool CheckNoInlineAttr(const Stmt *OrigSt, const Stmt *CurSt, const AttributeCommonInfo &A)
bool CheckAlwaysInlineAttr(const Stmt *OrigSt, const Stmt *CurSt, const AttributeCommonInfo &A)
bool CheckLoopHintExpr(Expr *E, SourceLocation Loc)
bool checkStringLiteralArgumentAttr(const AttributeCommonInfo &CI, const Expr *E, StringRef &Str, SourceLocation *ArgLocation=nullptr)
Check if the argument E is a ASCII string literal.
Encodes a location in the source.
A trivial tuple used to represent a source range.
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.
@ AANT_ArgumentIntegerConstant
Wraps an identifier and optional source location for the identifier.
Describes how types, statements, expressions, and declarations should be printed.