2012-04-14 08:33:13 +08:00
|
|
|
//===--- SemaStmtAttr.cpp - Statement Attribute Handling ------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-04-14 08:33:13 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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/ScopeInfo.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
|
2012-04-14 08:33:13 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace sema;
|
|
|
|
|
2018-07-13 23:07:47 +08:00
|
|
|
static Attr *handleFallThroughAttr(Sema &S, Stmt *St, const ParsedAttr &A,
|
2012-05-04 02:27:39 +08:00
|
|
|
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)
|
2018-08-10 05:08:08 +08:00
|
|
|
<< Attr.getSpelling() << St->getBeginLoc();
|
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
|
|
|
|
2017-12-05 04:27:34 +08:00
|
|
|
// If this is spelled as the standard C++17 attribute, but not in C++17, warn
|
2016-03-08 08:32:55 +08:00
|
|
|
// about using it as an extension.
|
2017-12-05 04:27:34 +08:00
|
|
|
if (!S.getLangOpts().CPlusPlus17 && A.isCXX11Attribute() &&
|
2016-03-08 08:32:55 +08:00
|
|
|
!A.getScopeName())
|
2017-08-14 07:37:29 +08:00
|
|
|
S.Diag(A.getLoc(), diag::ext_cxx17_attr) << A.getName();
|
2016-03-08 08:32:55 +08:00
|
|
|
|
|
|
|
FnScope->setHasFallthroughStmt();
|
|
|
|
return ::new (S.Context) auto(Attr);
|
2014-06-07 04:31:48 +08:00
|
|
|
}
|
|
|
|
|
2018-07-13 23:07:47 +08:00
|
|
|
static Attr *handleSuppressAttr(Sema &S, Stmt *St, const ParsedAttr &A,
|
Add [[clang::suppress(rule, ...)]] attribute
Summary:
This patch implements parsing of [[clang::suppress(rule, ...)]]
and [[gsl::suppress(rule, ...)]] attributes.
C++ Core Guidelines depend heavily on tool support for
rule enforcement. They also propose a way to suppress
warnings [1] which is by annotating any ancestor in AST
with the C++11 attribute [[gsl::suppress(rule1,...)]].
To have a mechanism to suppress non-C++ Core
Guidelines specific, an additional spelling of [[clang::suppress]]
is defined.
For example, to suppress the warning cppcoreguidelines-slicing,
one could do
```
[[clang::suppress("cppcoreguidelines-slicing")]]
void f() { ... code that does slicing ... }
```
or
```
void g() {
Derived b;
[[clang::suppress("cppcoreguidelines-slicing")]]
Base a{b};
[[clang::suppress("cppcoreguidelines-slicing")]] {
doSomething();
Base a2{b};
}
}
```
This parsing can then be used by clang-tidy, which includes multiple
C++ Core Guidelines rules, to suppress warnings (see
https://reviews.llvm.org/D24888).
For the exact naming of the rule in the attribute, there
are different possibilities, which will be defined in the
corresponding clang-tidy patch.
Currently, clang-tidy supports suppressing of warnings through "//
NOLINT" comments. There are some advantages that the attribute has:
- Suppressing specific warnings instead of all warnings
- Suppressing warnings in a block (namespace, function, compound
statement)
- Code formatting may split a statement into multiple lines,
thus a "// NOLINT" comment may be on the wrong line
I'm looking forward to your comments!
[1] https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#inforce-enforcement
Reviewers: alexfh, aaron.ballman, rsmith
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D24886
llvm-svn: 298880
2017-03-28 03:45:24 +08:00
|
|
|
SourceRange Range) {
|
|
|
|
if (A.getNumArgs() < 1) {
|
2018-08-09 21:21:32 +08:00
|
|
|
S.Diag(A.getLoc(), diag::err_attribute_too_few_arguments) << A << 1;
|
Add [[clang::suppress(rule, ...)]] attribute
Summary:
This patch implements parsing of [[clang::suppress(rule, ...)]]
and [[gsl::suppress(rule, ...)]] attributes.
C++ Core Guidelines depend heavily on tool support for
rule enforcement. They also propose a way to suppress
warnings [1] which is by annotating any ancestor in AST
with the C++11 attribute [[gsl::suppress(rule1,...)]].
To have a mechanism to suppress non-C++ Core
Guidelines specific, an additional spelling of [[clang::suppress]]
is defined.
For example, to suppress the warning cppcoreguidelines-slicing,
one could do
```
[[clang::suppress("cppcoreguidelines-slicing")]]
void f() { ... code that does slicing ... }
```
or
```
void g() {
Derived b;
[[clang::suppress("cppcoreguidelines-slicing")]]
Base a{b};
[[clang::suppress("cppcoreguidelines-slicing")]] {
doSomething();
Base a2{b};
}
}
```
This parsing can then be used by clang-tidy, which includes multiple
C++ Core Guidelines rules, to suppress warnings (see
https://reviews.llvm.org/D24888).
For the exact naming of the rule in the attribute, there
are different possibilities, which will be defined in the
corresponding clang-tidy patch.
Currently, clang-tidy supports suppressing of warnings through "//
NOLINT" comments. There are some advantages that the attribute has:
- Suppressing specific warnings instead of all warnings
- Suppressing warnings in a block (namespace, function, compound
statement)
- Code formatting may split a statement into multiple lines,
thus a "// NOLINT" comment may be on the wrong line
I'm looking forward to your comments!
[1] https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#inforce-enforcement
Reviewers: alexfh, aaron.ballman, rsmith
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D24886
llvm-svn: 298880
2017-03-28 03:45:24 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<StringRef> DiagnosticIdentifiers;
|
|
|
|
for (unsigned I = 0, E = A.getNumArgs(); I != E; ++I) {
|
|
|
|
StringRef RuleName;
|
|
|
|
|
|
|
|
if (!S.checkStringLiteralArgumentAttr(A, I, RuleName, nullptr))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// FIXME: Warn if the rule name is unknown. This is tricky because only
|
|
|
|
// clang-tidy knows about available rules.
|
|
|
|
DiagnosticIdentifiers.push_back(RuleName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ::new (S.Context) SuppressAttr(
|
|
|
|
A.getRange(), S.Context, DiagnosticIdentifiers.data(),
|
|
|
|
DiagnosticIdentifiers.size(), A.getAttributeSpellingListIndex());
|
|
|
|
}
|
|
|
|
|
2018-07-13 23:07:47 +08:00
|
|
|
static Attr *handleLoopHintAttr(Sema &S, Stmt *St, const ParsedAttr &A,
|
2014-06-07 04:31:48 +08:00
|
|
|
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";
|
2018-08-01 22:36:12 +08:00
|
|
|
bool PragmaUnrollAndJam = PragmaNameLoc->Ident->getName() == "unroll_and_jam";
|
|
|
|
bool PragmaNoUnrollAndJam =
|
|
|
|
PragmaNameLoc->Ident->getName() == "nounroll_and_jam";
|
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")
|
2018-08-01 22:36:12 +08:00
|
|
|
.Case("unroll_and_jam", "#pragma unroll_and_jam")
|
|
|
|
.Case("nounroll_and_jam", "#pragma nounroll_and_jam")
|
2014-07-25 02:09:38 +08:00
|
|
|
.Default("#pragma clang loop");
|
2018-08-10 05:08:08 +08:00
|
|
|
S.Diag(St->getBeginLoc(), diag::err_pragma_loop_precedes_nonloop) << Pragma;
|
2014-06-07 04:31:48 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-08-09 23:27:36 +08:00
|
|
|
LoopHintAttr::Spelling Spelling =
|
|
|
|
LoopHintAttr::Spelling(A.getAttributeSpellingListIndex());
|
2015-08-11 01:29:39 +08:00
|
|
|
LoopHintAttr::OptionType Option;
|
|
|
|
LoopHintAttr::LoopHintState State;
|
|
|
|
if (PragmaNoUnroll) {
|
|
|
|
// #pragma nounroll
|
|
|
|
Option = LoopHintAttr::Unroll;
|
|
|
|
State = LoopHintAttr::Disable;
|
|
|
|
} else if (PragmaUnroll) {
|
|
|
|
if (ValueExpr) {
|
|
|
|
// #pragma unroll N
|
|
|
|
Option = LoopHintAttr::UnrollCount;
|
|
|
|
State = LoopHintAttr::Numeric;
|
|
|
|
} else {
|
|
|
|
// #pragma unroll
|
|
|
|
Option = LoopHintAttr::Unroll;
|
|
|
|
State = LoopHintAttr::Enable;
|
|
|
|
}
|
2018-08-01 22:36:12 +08:00
|
|
|
} else if (PragmaNoUnrollAndJam) {
|
|
|
|
// #pragma nounroll_and_jam
|
|
|
|
Option = LoopHintAttr::UnrollAndJam;
|
|
|
|
State = LoopHintAttr::Disable;
|
|
|
|
} else if (PragmaUnrollAndJam) {
|
|
|
|
if (ValueExpr) {
|
|
|
|
// #pragma unroll_and_jam N
|
|
|
|
Option = LoopHintAttr::UnrollAndJamCount;
|
|
|
|
State = LoopHintAttr::Numeric;
|
|
|
|
} else {
|
|
|
|
// #pragma unroll_and_jam
|
|
|
|
Option = LoopHintAttr::UnrollAndJam;
|
|
|
|
State = LoopHintAttr::Enable;
|
|
|
|
}
|
2014-07-22 02:08:34 +08:00
|
|
|
} else {
|
2015-08-11 01:29:39 +08:00
|
|
|
// #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)
|
2019-01-05 01:20:00 +08:00
|
|
|
.Case("pipeline", LoopHintAttr::PipelineDisabled)
|
|
|
|
.Case("pipeline_initiation_interval",
|
|
|
|
LoopHintAttr::PipelineInitiationInterval)
|
2016-06-14 20:04:26 +08:00
|
|
|
.Case("distribute", LoopHintAttr::Distribute)
|
2014-07-22 02:08:34 +08:00
|
|
|
.Default(LoopHintAttr::Vectorize);
|
2015-08-11 01:29:39 +08:00
|
|
|
if (Option == LoopHintAttr::VectorizeWidth ||
|
|
|
|
Option == LoopHintAttr::InterleaveCount ||
|
2019-01-05 01:20:00 +08:00
|
|
|
Option == LoopHintAttr::UnrollCount ||
|
|
|
|
Option == LoopHintAttr::PipelineInitiationInterval) {
|
2015-08-11 01:29:39 +08:00
|
|
|
assert(ValueExpr && "Attribute must have a valid value expression.");
|
2018-08-10 05:08:08 +08:00
|
|
|
if (S.CheckLoopHintExpr(ValueExpr, St->getBeginLoc()))
|
2015-08-11 01:29:39 +08:00
|
|
|
return nullptr;
|
|
|
|
State = LoopHintAttr::Numeric;
|
|
|
|
} else if (Option == LoopHintAttr::Vectorize ||
|
|
|
|
Option == LoopHintAttr::Interleave ||
|
2016-06-14 20:04:26 +08:00
|
|
|
Option == LoopHintAttr::Unroll ||
|
2019-01-05 01:20:00 +08:00
|
|
|
Option == LoopHintAttr::Distribute ||
|
|
|
|
Option == LoopHintAttr::PipelineDisabled) {
|
2015-08-11 01:29:39 +08:00
|
|
|
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) {
|
2019-01-05 01:20:00 +08:00
|
|
|
// There are 6 categories of loop hints attributes: vectorize, interleave,
|
|
|
|
// unroll, unroll_and_jam, pipeline and distribute. Except for distribute they
|
|
|
|
// come in two variants: a state form and a numeric form. The state form
|
2018-08-01 22:36:12 +08:00
|
|
|
// 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;
|
2019-01-05 01:20:00 +08:00
|
|
|
} HintAttrs[] = {{nullptr, nullptr}, {nullptr, nullptr}, {nullptr, nullptr},
|
|
|
|
{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();
|
2019-01-05 01:20:00 +08:00
|
|
|
enum {
|
|
|
|
Vectorize,
|
|
|
|
Interleave,
|
|
|
|
Unroll,
|
|
|
|
UnrollAndJam,
|
|
|
|
Distribute,
|
|
|
|
Pipeline
|
|
|
|
} 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;
|
2018-08-01 22:36:12 +08:00
|
|
|
case LoopHintAttr::UnrollAndJam:
|
|
|
|
case LoopHintAttr::UnrollAndJamCount:
|
|
|
|
Category = UnrollAndJam;
|
|
|
|
break;
|
2016-06-14 20:04:26 +08:00
|
|
|
case LoopHintAttr::Distribute:
|
|
|
|
// Perform the check for duplicated 'distribute' hints.
|
|
|
|
Category = Distribute;
|
|
|
|
break;
|
2019-01-05 01:20:00 +08:00
|
|
|
case LoopHintAttr::PipelineDisabled:
|
|
|
|
case LoopHintAttr::PipelineInitiationInterval:
|
|
|
|
Category = Pipeline;
|
|
|
|
break;
|
2014-06-12 01:56:26 +08:00
|
|
|
};
|
2014-06-07 04:31:48 +08:00
|
|
|
|
2018-08-01 22:36:12 +08:00
|
|
|
assert(Category < sizeof(HintAttrs) / sizeof(HintAttrs[0]));
|
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 ||
|
2016-06-14 20:04:26 +08:00
|
|
|
Option == LoopHintAttr::Interleave || Option == LoopHintAttr::Unroll ||
|
2018-08-01 22:36:12 +08:00
|
|
|
Option == LoopHintAttr::UnrollAndJam ||
|
2019-01-05 01:20:00 +08:00
|
|
|
Option == LoopHintAttr::PipelineDisabled ||
|
2016-06-14 20:04:26 +08:00
|
|
|
Option == LoopHintAttr::Distribute) {
|
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 &&
|
2018-08-01 22:36:12 +08:00
|
|
|
(Category == Unroll || Category == UnrollAndJam ||
|
2014-08-01 04:15:14 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-13 23:07:47 +08:00
|
|
|
static Attr *handleOpenCLUnrollHint(Sema &S, Stmt *St, const ParsedAttr &A,
|
2016-02-20 02:30:11 +08:00
|
|
|
SourceRange Range) {
|
2016-12-13 22:02:35 +08:00
|
|
|
// Although the feature was introduced only in OpenCL C v2.0 s6.11.5, it's
|
|
|
|
// useful for OpenCL 1.x too and doesn't require HW support.
|
|
|
|
// opencl_unroll_hint can have 0 arguments (compiler
|
2016-02-20 02:30:11 +08:00
|
|
|
// determines unrolling factor) or 1 argument (the unroll factor provided
|
|
|
|
// by the user).
|
|
|
|
|
|
|
|
unsigned NumArgs = A.getNumArgs();
|
|
|
|
|
|
|
|
if (NumArgs > 1) {
|
2018-08-09 21:21:32 +08:00
|
|
|
S.Diag(A.getLoc(), diag::err_attribute_too_many_arguments) << A << 1;
|
2016-02-20 02:30:11 +08:00
|
|
|
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)
|
2018-08-09 21:21:32 +08:00
|
|
|
<< A << AANT_ArgumentIntegerConstant << E->getSourceRange();
|
2016-02-20 02:30:11 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Val = ArgVal.getSExtValue();
|
|
|
|
|
|
|
|
if (Val <= 0) {
|
|
|
|
S.Diag(A.getRange().getBegin(),
|
|
|
|
diag::err_attribute_requires_positive_integer)
|
2018-09-17 18:39:46 +08:00
|
|
|
<< A << /* positive */ 0;
|
2016-02-20 02:30:11 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
UnrollFactor = Val;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OpenCLUnrollHintAttr::CreateImplicit(S.Context, UnrollFactor);
|
|
|
|
}
|
|
|
|
|
2018-07-13 23:07:47 +08:00
|
|
|
static Attr *ProcessStmtAttribute(Sema &S, Stmt *St, const ParsedAttr &A,
|
2014-06-07 04:31:48 +08:00
|
|
|
SourceRange Range) {
|
|
|
|
switch (A.getKind()) {
|
2018-07-13 23:07:47 +08:00
|
|
|
case ParsedAttr::UnknownAttribute:
|
2018-12-17 20:25:42 +08:00
|
|
|
S.Diag(A.getLoc(), A.isDeclspecAttribute()
|
|
|
|
? (unsigned)diag::warn_unhandled_ms_attribute_ignored
|
|
|
|
: (unsigned)diag::warn_unknown_attribute_ignored)
|
|
|
|
<< A.getName();
|
2014-06-07 04:31:48 +08:00
|
|
|
return nullptr;
|
2018-07-13 23:07:47 +08:00
|
|
|
case ParsedAttr::AT_FallThrough:
|
2014-06-07 04:31:48 +08:00
|
|
|
return handleFallThroughAttr(S, St, A, Range);
|
2018-07-13 23:07:47 +08:00
|
|
|
case ParsedAttr::AT_LoopHint:
|
2014-06-07 04:31:48 +08:00
|
|
|
return handleLoopHintAttr(S, St, A, Range);
|
2018-07-13 23:07:47 +08:00
|
|
|
case ParsedAttr::AT_OpenCLUnrollHint:
|
2016-02-20 02:30:11 +08:00
|
|
|
return handleOpenCLUnrollHint(S, St, A, Range);
|
2018-07-13 23:07:47 +08:00
|
|
|
case ParsedAttr::AT_Suppress:
|
Add [[clang::suppress(rule, ...)]] attribute
Summary:
This patch implements parsing of [[clang::suppress(rule, ...)]]
and [[gsl::suppress(rule, ...)]] attributes.
C++ Core Guidelines depend heavily on tool support for
rule enforcement. They also propose a way to suppress
warnings [1] which is by annotating any ancestor in AST
with the C++11 attribute [[gsl::suppress(rule1,...)]].
To have a mechanism to suppress non-C++ Core
Guidelines specific, an additional spelling of [[clang::suppress]]
is defined.
For example, to suppress the warning cppcoreguidelines-slicing,
one could do
```
[[clang::suppress("cppcoreguidelines-slicing")]]
void f() { ... code that does slicing ... }
```
or
```
void g() {
Derived b;
[[clang::suppress("cppcoreguidelines-slicing")]]
Base a{b};
[[clang::suppress("cppcoreguidelines-slicing")]] {
doSomething();
Base a2{b};
}
}
```
This parsing can then be used by clang-tidy, which includes multiple
C++ Core Guidelines rules, to suppress warnings (see
https://reviews.llvm.org/D24888).
For the exact naming of the rule in the attribute, there
are different possibilities, which will be defined in the
corresponding clang-tidy patch.
Currently, clang-tidy supports suppressing of warnings through "//
NOLINT" comments. There are some advantages that the attribute has:
- Suppressing specific warnings instead of all warnings
- Suppressing warnings in a block (namespace, function, compound
statement)
- Code formatting may split a statement into multiple lines,
thus a "// NOLINT" comment may be on the wrong line
I'm looking forward to your comments!
[1] https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#inforce-enforcement
Reviewers: alexfh, aaron.ballman, rsmith
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D24886
llvm-svn: 298880
2017-03-28 03:45:24 +08:00
|
|
|
return handleSuppressAttr(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)
|
2018-08-10 05:08:08 +08:00
|
|
|
<< A.getName() << St->getBeginLoc();
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2012-04-14 08:33:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-13 05:09:05 +08:00
|
|
|
StmtResult Sema::ProcessStmtAttributes(Stmt *S,
|
|
|
|
const ParsedAttributesView &AttrList,
|
2012-04-14 08:33:13 +08:00
|
|
|
SourceRange Range) {
|
2012-07-09 18:04:07 +08:00
|
|
|
SmallVector<const Attr*, 8> Attrs;
|
2018-07-13 23:07:47 +08:00
|
|
|
for (const ParsedAttr &AL : AttrList) {
|
2018-07-13 05:09:05 +08:00
|
|
|
if (Attr *a = ProcessStmtAttribute(*this, S, AL, 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);
|
|
|
|
}
|