Skip to content

Commit ee76920

Browse files
committed
Add an error message when trying to remove valued enum attributes
The current version of LLVM crashes when attempting to remove valued enum attributes. The bug was discovered and fixed in LLVM 7 (see llvm-mirror/llvm@9bc0b10). The guard placed in `removeAttribute` should be removed once this version is released.
1 parent 7109545 commit ee76920

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

Sources/LLVM/Function+Attributes.swift

+11
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,17 @@ extension Function {
343343
/// - parameter index: The index representing the function, its return value
344344
/// or one of its parameters.
345345
public func removeAttribute(_ attrKind: AttributeKind, from index: AttributeIndex) {
346+
// FIXME: Remove when LLVM 7.0.0 is released.
347+
guard attrKind != .align
348+
&& attrKind != .alignstack
349+
&& attrKind != .allocsize
350+
&& attrKind != .dereferenceable
351+
&& attrKind != .dereferenceableOrNull else {
352+
fatalError(
353+
"Removing valued enum attributes crashes in LLVM <7.0.0 " +
354+
"and is currently disabled in LLVMSwift.")
355+
}
356+
346357
LLVMRemoveEnumAttributeAtIndex(llvm, index.rawValue, attrKind.id)
347358
}
348359

Tests/LLVMTests/IRAttributesSpec.swift

+3-6
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,7 @@ class IRAttributesSpec : XCTestCase {
131131
XCTAssertEqual(enumAttr.value, value)
132132
XCTAssert(fn.attributes(at: .function).contains { $0.asLLVM() == enumAttr.asLLVM() })
133133

134-
// LLVM issue?
135-
// Removing an integer attribute seems to always fail, because
136-
// `AttributeList::removeAttribute` (Attributes.cpp:1096) attempts to add the
137-
// attribute about to be removed without preserving its value.
134+
// FIXME: Remove when LLVM 7.0.0 is released.
138135
guard value == 0 else { continue }
139136

140137
fn.removeAttribute(enumAttr, from: .function)
@@ -198,7 +195,7 @@ class IRAttributesSpec : XCTestCase {
198195
XCTAssertEqual(enumAttr.value, value)
199196
XCTAssert(fn.attributes(at: .returnValue).contains { $0.asLLVM() == enumAttr.asLLVM() })
200197

201-
// LLVM issue?
198+
// FIXME: Remove when LLVM 7.0.0 is released.
202199
guard value == 0 else { continue }
203200

204201
fn.removeAttribute(enumAttr, from: .returnValue)
@@ -214,7 +211,7 @@ class IRAttributesSpec : XCTestCase {
214211
XCTAssertEqual(enumAttr.value, value)
215212
XCTAssert(fn.attributes(at: .argument(0)).contains { $0.asLLVM() == enumAttr.asLLVM() })
216213

217-
// LLVM issue?
214+
// FIXME: Remove when LLVM 7.0.0 is released.
218215
guard value == 0 else { continue }
219216

220217
fn.removeAttribute(enumAttr, from: .argument(0))

0 commit comments

Comments
 (0)