[PATCH] D38205: [Sema] Warn on attribute nothrow conflicting with language specifiers
Erich Keane via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 25 05:04:12 PDT 2017
erichkeane created this revision.
I discovered it was possible to create a 'nothrow' noexcept(false)
function, which is both non-sensical as well as seemingly breaking.
This patch warns if attribute nothrow is used with anything besides "noexcept".
"noexcept(true)" isn't possible, because the noexcept decl isn't parsed until
later.
https://reviews.llvm.org/D38205
Files:
include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaDeclAttr.cpp
test/SemaCXX/warn-conflicting-nothrow-attr-exception-spec.cpp
Index: test/SemaCXX/warn-conflicting-nothrow-attr-exception-spec.cpp
===================================================================
--- /dev/null
+++ test/SemaCXX/warn-conflicting-nothrow-attr-exception-spec.cpp
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 %s -fcxx-exceptions -fsyntax-only -Wexceptions -verify -std=c++14
+
+struct S {
+ __attribute__((nothrow)) S() noexcept(true);
+ __attribute__((nothrow)) void Func1() noexcept(false);
+ __attribute__((nothrow)) void Func2() throws();
+ __attribute__((nothrow)) void Func3() noexcept;
+};
Index: lib/Sema/SemaDeclAttr.cpp
===================================================================
--- lib/Sema/SemaDeclAttr.cpp
+++ lib/Sema/SemaDeclAttr.cpp
@@ -1969,6 +1969,25 @@
attr.getRange(), S.Context, attr.getAttributeSpellingListIndex()));
}
+static void handleNoThrowAttr(Sema &S, Decl *D, const AttributeList &attr) {
+ assert(isa<FunctionDecl>(D) && "attribute nothrow only valid on functions");
+
+ auto *FD = cast<FunctionDecl>(D);
+ const auto *FPT = FD->getType()->getAs<FunctionProtoType>();
+
+ if (FPT && FPT->hasExceptionSpec() &&
+ FPT->getExceptionSpecType() != EST_BasicNoexcept) {
+ S.Diag(attr.getLoc(),
+ diag::warn_nothrow_attr_disagrees_with_exception_specification);
+ S.Diag(FD->getExceptionSpecSourceRange().getBegin(),
+ diag::note_previous_decl)
+ << "exception specification";
+ }
+
+ D->addAttr(::new (S.Context) NoThrowAttr(
+ attr.getRange(), S.Context, attr.getAttributeSpellingListIndex()));
+}
+
static void handleNoCallerSavedRegsAttr(Sema &S, Decl *D,
const AttributeList &Attr) {
if (S.CheckNoCallerSavedRegsAttr(Attr))
@@ -6192,7 +6211,7 @@
handleNoReturnAttr(S, D, Attr);
break;
case AttributeList::AT_NoThrow:
- handleSimpleAttribute<NoThrowAttr>(S, D, Attr);
+ handleNoThrowAttr(S, D, Attr);
break;
case AttributeList::AT_CUDAShared:
handleSharedAttr(S, D, Attr);
Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td
+++ include/clang/Basic/DiagnosticSemaKinds.td
@@ -1407,6 +1407,10 @@
"argument to noexcept specifier must be a constant expression">;
def err_exception_spec_not_parsed : Error<
"exception specification is not available until end of class definition">;
+def warn_nothrow_attr_disagrees_with_exception_specification
+ : ExtWarn<"Attribute nothrow ignored, it disagrees with language specified "
+ "exception specification">,
+ InGroup<IgnoredAttributes>;
// C++ access checking
def err_class_redeclared_with_different_access : Error<
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38205.116428.patch
Type: text/x-patch
Size: 2726 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170925/49ddebf9/attachment-0001.bin>
More information about the cfe-commits
mailing list