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) {
|
|
|
|
if (!isa<NullStmt>(St)) {
|
|
|
|
S.Diag(A.getRange().getBegin(), diag::err_fallthrough_attr_wrong_target)
|
|
|
|
<< St->getLocStart();
|
|
|
|
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
|
|
|
}
|
|
|
|
if (S.getCurFunction()->SwitchStack.empty()) {
|
|
|
|
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
|
|
|
}
|
2014-01-16 21:03:14 +08:00
|
|
|
return ::new (S.Context) FallThroughAttr(A.getRange(), S.Context,
|
2014-06-07 04:31:48 +08:00
|
|
|
A.getAttributeSpellingListIndex());
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
IdentifierInfo *OptionInfo = OptionLoc->Ident;
|
|
|
|
IdentifierLoc *ValueLoc = A.getArgAsIdent(2);
|
|
|
|
IdentifierInfo *ValueInfo = ValueLoc ? ValueLoc->Ident : nullptr;
|
|
|
|
Expr *ValueExpr = A.getArgAsExpr(3);
|
|
|
|
|
|
|
|
assert(OptionInfo && "Attribute must have valid option info.");
|
|
|
|
|
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-22 02:08:34 +08:00
|
|
|
const char *Pragma = PragmaNameLoc->Ident->getName() == "unroll"
|
|
|
|
? "#pragma unroll"
|
|
|
|
: "#pragma clang loop";
|
|
|
|
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::OptionType Option;
|
|
|
|
LoopHintAttr::Spelling Spelling;
|
|
|
|
if (PragmaNameLoc->Ident->getName() == "unroll") {
|
|
|
|
Option = ValueLoc ? LoopHintAttr::UnrollCount : LoopHintAttr::Unroll;
|
|
|
|
Spelling = LoopHintAttr::Pragma_unroll;
|
|
|
|
} else {
|
|
|
|
Option = llvm::StringSwitch<LoopHintAttr::OptionType>(OptionInfo->getName())
|
|
|
|
.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);
|
|
|
|
Spelling = LoopHintAttr::Pragma_clang_loop;
|
|
|
|
}
|
2014-06-07 04:31:48 +08:00
|
|
|
|
|
|
|
int ValueInt;
|
2014-07-22 02:08:34 +08:00
|
|
|
if (Option == LoopHintAttr::Unroll &&
|
|
|
|
Spelling == LoopHintAttr::Pragma_unroll) {
|
|
|
|
ValueInt = 1;
|
|
|
|
} else if (Option == LoopHintAttr::Vectorize ||
|
|
|
|
Option == LoopHintAttr::Interleave ||
|
|
|
|
Option == LoopHintAttr::Unroll) {
|
2014-06-07 04:31:48 +08:00
|
|
|
if (!ValueInfo) {
|
2014-06-20 02:30:15 +08:00
|
|
|
S.Diag(ValueLoc->Loc, diag::err_pragma_loop_invalid_keyword);
|
2014-06-07 04:31:48 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (ValueInfo->isStr("disable"))
|
|
|
|
ValueInt = 0;
|
|
|
|
else if (ValueInfo->isStr("enable"))
|
|
|
|
ValueInt = 1;
|
|
|
|
else {
|
2014-06-20 02:30:15 +08:00
|
|
|
S.Diag(ValueLoc->Loc, diag::err_pragma_loop_invalid_keyword);
|
2014-06-07 04:31:48 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} else if (Option == LoopHintAttr::VectorizeWidth ||
|
2014-06-12 01:56:26 +08:00
|
|
|
Option == LoopHintAttr::InterleaveCount ||
|
|
|
|
Option == LoopHintAttr::UnrollCount) {
|
2014-06-07 04:31:48 +08:00
|
|
|
// FIXME: We should support template parameters for the loop hint value.
|
|
|
|
// See bug report #19610.
|
|
|
|
llvm::APSInt ValueAPS;
|
2014-06-20 02:30:15 +08:00
|
|
|
if (!ValueExpr || !ValueExpr->isIntegerConstantExpr(ValueAPS, S.Context) ||
|
|
|
|
(ValueInt = ValueAPS.getSExtValue()) < 1) {
|
|
|
|
S.Diag(ValueLoc->Loc, diag::err_pragma_loop_invalid_value);
|
2014-06-07 04:31:48 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
llvm_unreachable("Unknown loop hint option");
|
|
|
|
|
2014-07-22 02:08:34 +08:00
|
|
|
return LoopHintAttr::CreateImplicit(S.Context, Spelling, Option, ValueInt,
|
2014-06-07 04:31:48 +08:00
|
|
|
A.getRange());
|
|
|
|
}
|
|
|
|
|
2014-07-22 02:08:34 +08:00
|
|
|
static void CheckForIncompatibleAttributes(
|
|
|
|
Sema &S, const SmallVectorImpl<const Attr *> &Attrs) {
|
2014-06-12 01:56:26 +08:00
|
|
|
// There are 3 categories of loop hints: vectorize, interleave, and
|
|
|
|
// unroll. Each comes in two variants: an enable/disable form and a
|
|
|
|
// form which takes a numeric argument. For example:
|
|
|
|
// unroll(enable|disable) and unroll_count(N). The following array
|
|
|
|
// accumulate the hints encountered while iterating through the
|
|
|
|
// attributes to check for compatibility.
|
|
|
|
struct {
|
2014-07-22 02:08:34 +08:00
|
|
|
const LoopHintAttr *EnableAttr;
|
|
|
|
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;
|
|
|
|
|
|
|
|
int Option = LH->getOption();
|
2014-06-12 01:56:26 +08:00
|
|
|
int Category;
|
2014-06-07 04:31:48 +08:00
|
|
|
switch (Option) {
|
|
|
|
case LoopHintAttr::Vectorize:
|
|
|
|
case LoopHintAttr::VectorizeWidth:
|
2014-06-12 01:56:26 +08:00
|
|
|
Category = 0;
|
2014-06-07 04:31:48 +08:00
|
|
|
break;
|
|
|
|
case LoopHintAttr::Interleave:
|
|
|
|
case LoopHintAttr::InterleaveCount:
|
2014-06-12 01:56:26 +08:00
|
|
|
Category = 1;
|
2014-06-07 04:31:48 +08:00
|
|
|
break;
|
2014-06-12 01:56:26 +08:00
|
|
|
case LoopHintAttr::Unroll:
|
|
|
|
case LoopHintAttr::UnrollCount:
|
|
|
|
Category = 2;
|
|
|
|
break;
|
|
|
|
};
|
2014-06-07 04:31:48 +08:00
|
|
|
|
2014-07-22 02:08:34 +08:00
|
|
|
auto &CategoryState = HintAttrs[Category];
|
|
|
|
SourceLocation OptionLoc = LH->getRange().getBegin();
|
|
|
|
const LoopHintAttr *PrevAttr;
|
2014-06-12 01:56:26 +08:00
|
|
|
if (Option == LoopHintAttr::Vectorize ||
|
|
|
|
Option == LoopHintAttr::Interleave || Option == LoopHintAttr::Unroll) {
|
|
|
|
// Enable|disable hint. For example, vectorize(enable).
|
2014-07-22 02:08:34 +08:00
|
|
|
PrevAttr = CategoryState.EnableAttr;
|
|
|
|
CategoryState.EnableAttr = 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-22 02:08:34 +08:00
|
|
|
if (PrevAttr)
|
|
|
|
// Cannot specify same type of attribute twice.
|
|
|
|
S.Diag(OptionLoc, diag::err_pragma_loop_compatibility)
|
|
|
|
<< /*Duplicate=*/true << PrevAttr->getDiagnosticName()
|
|
|
|
<< LH->getDiagnosticName();
|
|
|
|
|
|
|
|
if (CategoryState.EnableAttr && !CategoryState.EnableAttr->getValue() &&
|
|
|
|
CategoryState.NumericAttr) {
|
2014-06-12 01:56:26 +08:00
|
|
|
// Disable hints are not compatible with numeric hints of the
|
|
|
|
// same category.
|
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-07-22 02:08:34 +08:00
|
|
|
<< CategoryState.EnableAttr->getDiagnosticName()
|
|
|
|
<< CategoryState.NumericAttr->getDiagnosticName();
|
2014-06-12 01:56:26 +08:00
|
|
|
}
|
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);
|
|
|
|
default:
|
|
|
|
// if we're here, then we parsed a known attribute, but didn't recognize
|
|
|
|
// it as a statement attribute => it is declaration attribute
|
2013-02-20 07:47:15 +08:00
|
|
|
S.Diag(A.getRange().getBegin(), diag::err_attribute_invalid_on_stmt)
|
|
|
|
<< 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);
|
|
|
|
}
|