Author: erichkeane Date: Wed Jun 5 07:10:39 2019 New Revision: 362607 URL: http://llvm.org/viewvc/llvm-project?rev=362607&view=rev Log: Avoid using NoThrow Exception Specifier in non-C++ Modes.
As reported in https://bugs.llvm.org/show_bug.cgi?id=42113, there are a number of locations in Clang where it is assumed that exception specifications are only valid in C++ mode. Since the original justification for the NoThrow Exception Specifier Type was C++ related, this patch just makes C mode use the attribute-based nothrow handling. Additionally, I noticed that the handling of non-prototype functions regressed the behavior of the nothrow attribute, in part because it is was listed in the function type macro(which I did in the previous patch). In reality, it should only be doing so in a conditional nature, so this patch removes it there and puts it directly in the switch to be handled correctly. Added: cfe/trunk/test/Sema/attr-nothrow.c (with props) Modified: cfe/trunk/include/clang/Sema/ParsedAttr.h cfe/trunk/lib/Sema/SemaType.cpp Modified: cfe/trunk/include/clang/Sema/ParsedAttr.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/ParsedAttr.h?rev=362607&r1=362606&r2=362607&view=diff ============================================================================== --- cfe/trunk/include/clang/Sema/ParsedAttr.h (original) +++ cfe/trunk/include/clang/Sema/ParsedAttr.h Wed Jun 5 07:10:39 2019 @@ -440,7 +440,7 @@ public: } bool isUsedAsTypeAttr() const { return UsedAsTypeAttr; } - void setUsedAsTypeAttr() { UsedAsTypeAttr = true; } + void setUsedAsTypeAttr(bool Used = true) { UsedAsTypeAttr = Used; } /// True if the attribute is specified using '#pragma clang attribute'. bool isPragmaClangAttribute() const { return IsPragmaClangAttribute; } Modified: cfe/trunk/lib/Sema/SemaType.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaType.cpp?rev=362607&r1=362606&r2=362607&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaType.cpp (original) +++ cfe/trunk/lib/Sema/SemaType.cpp Wed Jun 5 07:10:39 2019 @@ -130,7 +130,6 @@ static void diagnoseBadTypeAttribute(Sem case ParsedAttr::AT_Regparm: \ case ParsedAttr::AT_AnyX86NoCallerSavedRegisters: \ case ParsedAttr::AT_AnyX86NoCfCheck: \ - case ParsedAttr::AT_NoThrow: \ CALLING_CONV_ATTRS_CASELIST // Microsoft-specific type qualifiers. @@ -6947,23 +6946,17 @@ static bool handleFunctionTypeAttr(TypeP } if (attr.getKind() == ParsedAttr::AT_NoThrow) { - if (S.CheckAttrNoArgs(attr)) - return true; - // Delay if this is not a function type. if (!unwrapped.isFunctionType()) return false; - // Otherwise we can process right away. - auto *Proto = unwrapped.get()->getAs<FunctionProtoType>(); - - // In the case where this is a FunctionNoProtoType instead of a - // FunctionProtoType, let the existing NoThrowAttr implementation do its - // thing. - if (!Proto) - return false; + if (S.CheckAttrNoArgs(attr)) { + attr.setInvalid(); + return true; + } - attr.setUsedAsTypeAttr(); + // Otherwise we can process right away. + auto *Proto = unwrapped.get()->castAs<FunctionProtoType>(); // MSVC ignores nothrow if it is in conflict with an explicit exception // specification. @@ -7668,6 +7661,12 @@ static void processTypeAttrs(TypeProcess attr.setInvalid(); break; + case ParsedAttr::AT_NoThrow: + // Exception Specifications aren't generally supported in C mode throughout + // clang, so revert to attribute-based handling for C. + if (!state.getSema().getLangOpts().CPlusPlus) + break; + LLVM_FALLTHROUGH; FUNCTION_TYPE_ATTRS_CASELIST: attr.setUsedAsTypeAttr(); Added: cfe/trunk/test/Sema/attr-nothrow.c URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/attr-nothrow.c?rev=362607&view=auto ============================================================================== --- cfe/trunk/test/Sema/attr-nothrow.c (added) +++ cfe/trunk/test/Sema/attr-nothrow.c Wed Jun 5 07:10:39 2019 @@ -0,0 +1,18 @@ +// RUN: %clang_cc1 %s -verify +// RUN: %clang_cc1 %s -ast-dump | FileCheck %s +// expected-no-diagnostics + +// PR42113: The following caused an assertion in mergeFunctionTypes +// because it causes one side to have an exception specification, which +// isn't typically supported in C. +void PR42113a(); +void PR42113a(void) __attribute__((nothrow)); +// CHECK: FunctionDecl {{.*}} PR42113a +// CHECK: FunctionDecl {{.*}} PR42113a +// CHECK: NoThrowAttr +void PR42113b() __attribute__((nothrow)); +// CHECK: FunctionDecl {{.*}} PR42113b +// CHECK: NoThrowAttr + __attribute__((nothrow)) void PR42113c(); +// CHECK: FunctionDecl {{.*}} PR42113c +// CHECK: NoThrowAttr Propchange: cfe/trunk/test/Sema/attr-nothrow.c ------------------------------------------------------------------------------ svn:eol-style = native Propchange: cfe/trunk/test/Sema/attr-nothrow.c ------------------------------------------------------------------------------ svn:keywords = Author Date Id Rev URL Propchange: cfe/trunk/test/Sema/attr-nothrow.c ------------------------------------------------------------------------------ svn:mime-type = text/plain _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits