2012-04-14 08:33:13 +08:00
|
|
|
//===--- SemaStmtAttr.cpp - Statement Attribute Handling ------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements stmt-related attribute processing.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Sema/SemaInternal.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
2014-06-07 04:31:48 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Sema/DelayedDiagnostic.h"
|
|
|
|
#include "clang/Sema/Lookup.h"
|
|
|
|
#include "clang/Sema/LoopHint.h"
|
|
|
|
#include "clang/Sema/ScopeInfo.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
|
2012-04-14 08:33:13 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace sema;
|
|
|
|
|
2012-05-04 02:27:39 +08:00
|
|
|
static Attr *handleFallThroughAttr(Sema &S, Stmt *St, const AttributeList &A,
|
|
|
|
SourceRange Range) {
|
2016-03-08 08:32:55 +08:00
|
|
|
FallThroughAttr Attr(A.getRange(), S.Context,
|
|
|
|
A.getAttributeSpellingListIndex());
|
2012-05-04 02:27:39 +08:00
|
|
|
if (!isa<NullStmt>(St)) {
|
|
|
|
S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_wrong_target)
|
2016-03-08 08:32:55 +08:00
|
|
|
<< Attr.getSpelling() << St->getLocStart();
|
2012-05-04 02:27:39 +08:00
|
|
|
if (isa<SwitchCase>(St)) {
|
2014-05-03 11:45:55 +08:00
|
|
|
SourceLocation L = S.getLocForEndOfToken(Range.getEnd());
|
2012-05-04 02:27:39 +08:00
|
|
|
S.Diag(L, diag::note_fallthrough_insert_semi_fixit)
|
|
|
|
<< FixItHint::CreateInsertion(L, ";");
|
|
|
|
}
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2012-05-04 02:27:39 +08:00
|
|
|
}
|
2016-03-08 08:32:55 +08:00
|
|
|
auto *FnScope = S.getCurFunction();
|
|
|
|
if (FnScope->SwitchStack.empty()) {
|
2012-05-04 02:27:39 +08:00
|
|
|
S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_outside_switch);
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2012-05-04 02:27:39 +08:00
|
|
|
}
|
2016-03-08 08:32:55 +08:00
|
|
|
|
|
|
|
// If this is spelled as the standard C++1z attribute, but not in C++1z, warn
|
|
|
|
// about using it as an extension.
|
|
|
|
if (!S.getLangOpts().CPlusPlus1z && A.isCXX11Attribute() &&
|
|
|
|
!A.getScopeName())
|
|
|
|
S.Diag(A.getLoc(), diag::ext_cxx1z_attr) << A.getName();
|
|
|
|
|
|
|
|
FnScope->setHasFallthroughStmt();
|
|
|
|
return ::new (S.Context) auto(Attr);
|
2014-06-07 04:31:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static Attr *handleLoopHintAttr(Sema &S, Stmt *St, const AttributeList &A,
|
|
|
|
SourceRange) {
|
2014-07-22 02:08:34 +08:00
|
|
|
IdentifierLoc *PragmaNameLoc = A.getArgAsIdent(0);
|
|
|
|
IdentifierLoc *OptionLoc = A.getArgAsIdent(1);
|
2014-08-01 04:15:14 +08:00
|
|
|
IdentifierLoc *StateLoc = A.getArgAsIdent(2);
|
2014-07-22 02:08:34 +08:00
|
|
|
Expr *ValueExpr = A.getArgAsExpr(3);
|
|
|
|
|
2014-08-01 04:15:14 +08:00
|
|
|
bool PragmaUnroll = PragmaNameLoc->Ident->getName() == "unroll";
|
|
|
|
bool PragmaNoUnroll = PragmaNameLoc->Ident->getName() == "nounroll";
|
2014-06-07 04:31:48 +08:00
|
|
|
if (St->getStmtClass() != Stmt::DoStmtClass &&
|
|
|
|
St->getStmtClass() != Stmt::ForStmtClass &&
|
|
|
|
St->getStmtClass() != Stmt::CXXForRangeStmtClass &&
|
|
|
|
St->getStmtClass() != Stmt::WhileStmtClass) {
|
2014-07-25 02:09:38 +08:00
|
|
|
const char *Pragma =
|
|
|
|
llvm::StringSwitch<const char *>(PragmaNameLoc->Ident->getName())
|
|
|
|
.Case("unroll", "#pragma unroll")
|
|
|
|
.Case("nounroll", "#pragma nounroll")
|
|
|
|
.Default("#pragma clang loop");
|
2014-07-22 02:08:34 +08:00
|
|
|
S.Diag(St->getLocStart(), diag::err_pragma_loop_precedes_nonloop) << Pragma;
|
2014-06-07 04:31:48 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-22 02:08:34 +08:00
|
|
|
LoopHintAttr::Spelling Spelling;
|
2015-08-11 01:29:39 +08:00
|
|
|
LoopHintAttr::OptionType Option;
|
|
|
|
LoopHintAttr::LoopHintState State;
|
|
|
|
if (PragmaNoUnroll) {
|
|
|
|
// #pragma nounroll
|
2014-07-25 02:09:38 +08:00
|
|
|
Spelling = LoopHintAttr::Pragma_nounroll;
|
2015-08-11 01:29:39 +08:00
|
|
|
Option = LoopHintAttr::Unroll;
|
|
|
|
State = LoopHintAttr::Disable;
|
|
|
|
} else if (PragmaUnroll) {
|
|
|
|
Spelling = LoopHintAttr::Pragma_unroll;
|
|
|
|
if (ValueExpr) {
|
|
|
|
// #pragma unroll N
|
|
|
|
Option = LoopHintAttr::UnrollCount;
|
|
|
|
State = LoopHintAttr::Numeric;
|
|
|
|
} else {
|
|
|
|
// #pragma unroll
|
|
|
|
Option = LoopHintAttr::Unroll;
|
|
|
|
State = LoopHintAttr::Enable;
|
|
|
|
}
|
2014-07-22 02:08:34 +08:00
|
|
|
} else {
|
2015-08-11 01:29:39 +08:00
|
|
|
// #pragma clang loop ...
|
|
|
|
Spelling = LoopHintAttr::Pragma_clang_loop;
|
2014-08-01 04:15:14 +08:00
|
|
|
assert(OptionLoc && OptionLoc->Ident &&
|
|
|
|
"Attribute must have valid option info.");
|
2015-08-11 01:29:39 +08:00
|
|
|
Option = llvm::StringSwitch<LoopHintAttr::OptionType>(
|
|
|
|
OptionLoc->Ident->getName())
|
2014-07-22 02:08:34 +08:00
|
|
|
.Case("vectorize", LoopHintAttr::Vectorize)
|
|
|
|
.Case("vectorize_width", LoopHintAttr::VectorizeWidth)
|
|
|
|
.Case("interleave", LoopHintAttr::Interleave)
|
|
|
|
.Case("interleave_count", LoopHintAttr::InterleaveCount)
|
|
|
|
.Case("unroll", LoopHintAttr::Unroll)
|
|
|
|
.Case("unroll_count", LoopHintAttr::UnrollCount)
|
|
|
|
.Default(LoopHintAttr::Vectorize);
|
2015-08-11 01:29:39 +08:00
|
|
|
if (Option == LoopHintAttr::VectorizeWidth ||
|
|
|
|
Option == LoopHintAttr::InterleaveCount ||
|
|
|
|
Option == LoopHintAttr::UnrollCount) {
|
|
|
|
assert(ValueExpr && "Attribute must have a valid value expression.");
|
|
|
|
if (S.CheckLoopHintExpr(ValueExpr, St->getLocStart()))
|
|
|
|
return nullptr;
|
|
|
|
State = LoopHintAttr::Numeric;
|
|
|
|
} else if (Option == LoopHintAttr::Vectorize ||
|
|
|
|
Option == LoopHintAttr::Interleave ||
|
|
|
|
Option == LoopHintAttr::Unroll) {
|
|
|
|
assert(StateLoc && StateLoc->Ident && "Loop hint must have an argument");
|
2014-08-01 04:15:14 +08:00
|
|
|
if (StateLoc->Ident->isStr("disable"))
|
|
|
|
State = LoopHintAttr::Disable;
|
2015-06-12 07:23:17 +08:00
|
|
|
else if (StateLoc->Ident->isStr("assume_safety"))
|
|
|
|
State = LoopHintAttr::AssumeSafety;
|
2015-08-11 01:29:39 +08:00
|
|
|
else if (StateLoc->Ident->isStr("full"))
|
|
|
|
State = LoopHintAttr::Full;
|
|
|
|
else if (StateLoc->Ident->isStr("enable"))
|
2014-08-01 04:15:14 +08:00
|
|
|
State = LoopHintAttr::Enable;
|
2015-08-11 01:29:39 +08:00
|
|
|
else
|
|
|
|
llvm_unreachable("bad loop hint argument");
|
|
|
|
} else
|
|
|
|
llvm_unreachable("bad loop hint");
|
2014-08-01 04:15:14 +08:00
|
|
|
}
|
2014-06-07 04:31:48 +08:00
|
|
|
|
2014-08-01 04:15:14 +08:00
|
|
|
return LoopHintAttr::CreateImplicit(S.Context, Spelling, Option, State,
|
2014-10-13 04:46:07 +08:00
|
|
|
ValueExpr, A.getRange());
|
2014-06-07 04:31:48 +08:00
|
|
|
}
|
|
|
|
|
2014-08-01 04:15:14 +08:00
|
|
|
static void
|
|
|
|
CheckForIncompatibleAttributes(Sema &S,
|
|
|
|
const SmallVectorImpl<const Attr *> &Attrs) {
|
|
|
|
// There are 3 categories of loop hints attributes: vectorize, interleave,
|
|
|
|
// and unroll. Each comes in two variants: a state form and a numeric form.
|
|
|
|
// The state form selectively defaults/enables/disables the transformation
|
|
|
|
// for the loop (for unroll, default indicates full unrolling rather than
|
|
|
|
// enabling the transformation). The numeric form form provides an integer
|
|
|
|
// hint (for example, unroll count) to the transformer. The following array
|
|
|
|
// accumulates the hints encountered while iterating through the attributes
|
|
|
|
// to check for compatibility.
|
2014-06-12 01:56:26 +08:00
|
|
|
struct {
|
2014-08-01 04:15:14 +08:00
|
|
|
const LoopHintAttr *StateAttr;
|
2014-07-22 02:08:34 +08:00
|
|
|
const LoopHintAttr *NumericAttr;
|
|
|
|
} HintAttrs[] = {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr}};
|
2014-06-07 04:31:48 +08:00
|
|
|
|
|
|
|
for (const auto *I : Attrs) {
|
|
|
|
const LoopHintAttr *LH = dyn_cast<LoopHintAttr>(I);
|
|
|
|
|
|
|
|
// Skip non loop hint attributes
|
|
|
|
if (!LH)
|
|
|
|
continue;
|
|
|
|
|
2015-12-23 13:44:43 +08:00
|
|
|
LoopHintAttr::OptionType Option = LH->getOption();
|
|
|
|
enum { Vectorize, Interleave, Unroll } Category;
|
2014-06-07 04:31:48 +08:00
|
|
|
switch (Option) {
|
|
|
|
case LoopHintAttr::Vectorize:
|
|
|
|
case LoopHintAttr::VectorizeWidth:
|
2014-07-24 01:31:31 +08:00
|
|
|
Category = Vectorize;
|
2014-06-07 04:31:48 +08:00
|
|
|
break;
|
|
|
|
case LoopHintAttr::Interleave:
|
|
|
|
case LoopHintAttr::InterleaveCount:
|
2014-07-24 01:31:31 +08:00
|
|
|
Category = Interleave;
|
2014-06-07 04:31:48 +08:00
|
|
|
break;
|
2014-06-12 01:56:26 +08:00
|
|
|
case LoopHintAttr::Unroll:
|
|
|
|
case LoopHintAttr::UnrollCount:
|
2014-07-24 01:31:31 +08:00
|
|
|
Category = Unroll;
|
2014-06-12 01:56:26 +08:00
|
|
|
break;
|
|
|
|
};
|
2014-06-07 04:31:48 +08:00
|
|
|
|
2014-07-22 02:08:34 +08:00
|
|
|
auto &CategoryState = HintAttrs[Category];
|
|
|
|
const LoopHintAttr *PrevAttr;
|
2014-06-12 01:56:26 +08:00
|
|
|
if (Option == LoopHintAttr::Vectorize ||
|
|
|
|
Option == LoopHintAttr::Interleave || Option == LoopHintAttr::Unroll) {
|
2015-06-12 07:23:17 +08:00
|
|
|
// Enable|Disable|AssumeSafety hint. For example, vectorize(enable).
|
2014-08-01 04:15:14 +08:00
|
|
|
PrevAttr = CategoryState.StateAttr;
|
|
|
|
CategoryState.StateAttr = LH;
|
2014-06-07 04:31:48 +08:00
|
|
|
} else {
|
2014-07-22 02:08:34 +08:00
|
|
|
// Numeric hint. For example, vectorize_width(8).
|
|
|
|
PrevAttr = CategoryState.NumericAttr;
|
|
|
|
CategoryState.NumericAttr = LH;
|
2014-06-07 04:31:48 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 01:21:32 +08:00
|
|
|
PrintingPolicy Policy(S.Context.getLangOpts());
|
|
|
|
SourceLocation OptionLoc = LH->getRange().getBegin();
|
2014-07-22 02:08:34 +08:00
|
|
|
if (PrevAttr)
|
|
|
|
// Cannot specify same type of attribute twice.
|
|
|
|
S.Diag(OptionLoc, diag::err_pragma_loop_compatibility)
|
2014-07-30 01:21:32 +08:00
|
|
|
<< /*Duplicate=*/true << PrevAttr->getDiagnosticName(Policy)
|
|
|
|
<< LH->getDiagnosticName(Policy);
|
2014-07-22 02:08:34 +08:00
|
|
|
|
2014-08-01 04:15:14 +08:00
|
|
|
if (CategoryState.StateAttr && CategoryState.NumericAttr &&
|
|
|
|
(Category == Unroll ||
|
|
|
|
CategoryState.StateAttr->getState() == LoopHintAttr::Disable)) {
|
2014-07-24 01:31:31 +08:00
|
|
|
// Disable hints are not compatible with numeric hints of the same
|
|
|
|
// category. As a special case, numeric unroll hints are also not
|
2015-08-11 01:29:39 +08:00
|
|
|
// compatible with enable or full form of the unroll pragma because these
|
|
|
|
// directives indicate full unrolling.
|
2014-07-22 02:08:34 +08:00
|
|
|
S.Diag(OptionLoc, diag::err_pragma_loop_compatibility)
|
2014-06-12 01:56:26 +08:00
|
|
|
<< /*Duplicate=*/false
|
2014-08-01 04:15:14 +08:00
|
|
|
<< CategoryState.StateAttr->getDiagnosticName(Policy)
|
2014-07-30 01:21:32 +08:00
|
|
|
<< CategoryState.NumericAttr->getDiagnosticName(Policy);
|
2014-06-12 01:56:26 +08:00
|
|
|
}
|
2014-06-07 04:31:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-20 02:30:11 +08:00
|
|
|
static Attr *handleOpenCLUnrollHint(Sema &S, Stmt *St, const AttributeList &A,
|
|
|
|
SourceRange Range) {
|
|
|
|
// OpenCL v2.0 s6.11.5 - opencl_unroll_hint can have 0 arguments (compiler
|
|
|
|
// determines unrolling factor) or 1 argument (the unroll factor provided
|
|
|
|
// by the user).
|
|
|
|
|
|
|
|
if (S.getLangOpts().OpenCLVersion < 200) {
|
|
|
|
S.Diag(A.getLoc(), diag::err_attribute_requires_opencl_version)
|
2016-03-31 19:07:22 +08:00
|
|
|
<< A.getName() << "2.0" << 1;
|
2016-02-20 02:30:11 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned NumArgs = A.getNumArgs();
|
|
|
|
|
|
|
|
if (NumArgs > 1) {
|
|
|
|
S.Diag(A.getLoc(), diag::err_attribute_too_many_arguments) << A.getName()
|
|
|
|
<< 1;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned UnrollFactor = 0;
|
|
|
|
|
|
|
|
if (NumArgs == 1) {
|
|
|
|
Expr *E = A.getArgAsExpr(0);
|
|
|
|
llvm::APSInt ArgVal(32);
|
|
|
|
|
|
|
|
if (!E->isIntegerConstantExpr(ArgVal, S.Context)) {
|
|
|
|
S.Diag(A.getLoc(), diag::err_attribute_argument_type)
|
|
|
|
<< A.getName() << AANT_ArgumentIntegerConstant << E->getSourceRange();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Val = ArgVal.getSExtValue();
|
|
|
|
|
|
|
|
if (Val <= 0) {
|
|
|
|
S.Diag(A.getRange().getBegin(),
|
|
|
|
diag::err_attribute_requires_positive_integer)
|
|
|
|
<< A.getName();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
UnrollFactor = Val;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OpenCLUnrollHintAttr::CreateImplicit(S.Context, UnrollFactor);
|
|
|
|
}
|
|
|
|
|
2014-06-07 04:31:48 +08:00
|
|
|
static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const AttributeList &A,
|
|
|
|
SourceRange Range) {
|
|
|
|
switch (A.getKind()) {
|
2012-10-03 09:56:22 +08:00
|
|
|
case AttributeList::UnknownAttribute:
|
|
|
|
S.Diag(A.getLoc(), A.isDeclspecAttribute() ?
|
|
|
|
diag::warn_unhandled_ms_attribute_ignored :
|
|
|
|
diag::warn_unknown_attribute_ignored) << A.getName();
|
2014-06-07 04:31:48 +08:00
|
|
|
return nullptr;
|
|
|
|
case AttributeList::AT_FallThrough:
|
|
|
|
return handleFallThroughAttr(S, St, A, Range);
|
|
|
|
case AttributeList::AT_LoopHint:
|
|
|
|
return handleLoopHintAttr(S, St, A, Range);
|
2016-02-20 02:30:11 +08:00
|
|
|
case AttributeList::AT_OpenCLUnrollHint:
|
|
|
|
return handleOpenCLUnrollHint(S, St, A, Range);
|
2014-06-07 04:31:48 +08:00
|
|
|
default:
|
|
|
|
// if we're here, then we parsed a known attribute, but didn't recognize
|
|
|
|
// it as a statement attribute => it is declaration attribute
|
2016-03-08 08:32:55 +08:00
|
|
|
S.Diag(A.getRange().getBegin(), diag::err_decl_attribute_invalid_on_stmt)
|
2013-02-20 07:47:15 +08:00
|
|
|
<< A.getName() << St->getLocStart();
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2012-04-14 08:33:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StmtResult Sema::ProcessStmtAttributes(Stmt *S, AttributeList *AttrList,
|
|
|
|
SourceRange Range) {
|
2012-07-09 18:04:07 +08:00
|
|
|
SmallVector<const Attr*, 8> Attrs;
|
2012-04-14 08:33:13 +08:00
|
|
|
for (const AttributeList* l = AttrList; l; l = l->getNext()) {
|
2012-05-04 02:27:39 +08:00
|
|
|
if (Attr *a = ProcessStmtAttribute(*this, S, *l, Range))
|
2014-06-07 04:31:48 +08:00
|
|
|
Attrs.push_back(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckForIncompatibleAttributes(*this, Attrs);
|
|
|
|
|
|
|
|
if (Attrs.empty())
|
|
|
|
return S;
|
|
|
|
|
2012-04-14 08:33:13 +08:00
|
|
|
return ActOnAttributedStmt(Range.getBegin(), Attrs, S);
|
|
|
|
}
|