Skip to content

Commit f4fe7f5

Browse files
xlaukolanza
authored andcommitted
[CIR] Simplify error emission to return failures directly (llvm#1634)
1 parent c763924 commit f4fe7f5

File tree

3 files changed

+142
-209
lines changed

3 files changed

+142
-209
lines changed

clang/lib/CIR/Dialect/IR/CIRAttrs.cpp

Lines changed: 88 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -137,33 +137,25 @@ static ParseResult parseRecordMembers(mlir::AsmParser &parser,
137137
return mlir::success();
138138
}
139139

140-
LogicalResult ConstRecordAttr::verify(
141-
::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError,
142-
mlir::Type type, ArrayAttr members) {
140+
LogicalResult
141+
ConstRecordAttr::verify(function_ref<InFlightDiagnostic()> emitError,
142+
mlir::Type type, ArrayAttr members) {
143143
auto sTy = mlir::dyn_cast_if_present<cir::RecordType>(type);
144-
if (!sTy) {
145-
emitError() << "expected !cir.record type";
146-
return failure();
147-
}
144+
if (!sTy)
145+
return emitError() << "expected !cir.record type";
148146

149-
if (sTy.getMembers().size() != members.size()) {
150-
emitError() << "number of elements must match";
151-
return failure();
152-
}
147+
if (sTy.getMembers().size() != members.size())
148+
return emitError() << "number of elements must match";
153149

154150
unsigned attrIdx = 0;
155151
for (auto &member : sTy.getMembers()) {
156152
auto m = mlir::dyn_cast_or_null<mlir::TypedAttr>(members[attrIdx]);
157-
if (!m) {
158-
emitError() << "expected mlir::TypedAttr attribute";
159-
return failure();
160-
}
161-
if (member != m.getType()) {
162-
emitError() << "element at index " << attrIdx << " has type "
163-
<< m.getType() << " but return type for this element is "
164-
<< member;
165-
return failure();
166-
}
153+
if (!m)
154+
return emitError() << "expected mlir::TypedAttr attribute";
155+
if (member != m.getType())
156+
return emitError() << "element at index " << attrIdx << " has type "
157+
<< m.getType()
158+
<< " but return type for this element is " << member;
167159
attrIdx++;
168160
}
169161

@@ -176,14 +168,12 @@ LogicalResult ConstRecordAttr::verify(
176168

177169
LogicalResult OptInfoAttr::verify(function_ref<InFlightDiagnostic()> emitError,
178170
unsigned level, unsigned size) {
179-
if (level > 3) {
180-
emitError() << "optimization level must be between 0 and 3 inclusive";
181-
return failure();
182-
}
183-
if (size > 2) {
184-
emitError() << "size optimization level must be between 0 and 2 inclusive";
185-
return failure();
186-
}
171+
if (level > 3)
172+
return emitError()
173+
<< "optimization level must be between 0 and 3 inclusive";
174+
if (size > 2)
175+
return emitError()
176+
<< "size optimization level must be between 0 and 2 inclusive";
187177
return success();
188178
}
189179

@@ -266,17 +256,13 @@ void IntAttr::print(AsmPrinter &printer) const {
266256

267257
LogicalResult IntAttr::verify(function_ref<InFlightDiagnostic()> emitError,
268258
Type type, APInt value) {
269-
if (!mlir::isa<IntType>(type)) {
270-
emitError() << "expected 'simple.int' type";
271-
return failure();
272-
}
259+
if (!mlir::isa<IntType>(type))
260+
return emitError() << "expected 'simple.int' type";
273261

274262
auto intType = mlir::cast<IntType>(type);
275-
if (value.getBitWidth() != intType.getWidth()) {
276-
emitError() << "type and value bitwidth mismatch: " << intType.getWidth()
277-
<< " != " << value.getBitWidth();
278-
return failure();
279-
}
263+
if (value.getBitWidth() != intType.getWidth())
264+
return emitError() << "type and value bitwidth mismatch: "
265+
<< intType.getWidth() << " != " << value.getBitWidth();
280266

281267
return success();
282268
}
@@ -310,10 +296,8 @@ cir::FPAttr cir::FPAttr::getZero(mlir::Type type) {
310296
LogicalResult FPAttr::verify(function_ref<InFlightDiagnostic()> emitError,
311297
CIRFPTypeInterface fpType, APFloat value) {
312298
if (APFloat::SemanticsToEnum(fpType.getFloatSemantics()) !=
313-
APFloat::SemanticsToEnum(value.getSemantics())) {
314-
emitError() << "floating-point semantics mismatch";
315-
return failure();
316-
}
299+
APFloat::SemanticsToEnum(value.getSemantics()))
300+
return emitError() << "floating-point semantics mismatch";
317301

318302
return success();
319303
}
@@ -326,14 +310,13 @@ LogicalResult ComplexAttr::verify(function_ref<InFlightDiagnostic()> emitError,
326310
cir::ComplexType type, mlir::TypedAttr real,
327311
mlir::TypedAttr imag) {
328312
auto elemType = type.getElementType();
329-
if (real.getType() != elemType) {
330-
emitError() << "type of the real part does not match the complex type";
331-
return failure();
332-
}
333-
if (imag.getType() != elemType) {
334-
emitError() << "type of the imaginary part does not match the complex type";
335-
return failure();
336-
}
313+
if (real.getType() != elemType)
314+
return emitError()
315+
<< "type of the real part does not match the complex type";
316+
317+
if (imag.getType() != elemType)
318+
return emitError()
319+
<< "type of the imaginary part does not match the complex type";
337320

338321
return success();
339322
}
@@ -378,14 +361,11 @@ CmpThreeWayInfoAttr::verify(function_ref<InFlightDiagnostic()> emitError,
378361
CmpOrdering ordering, int64_t lt, int64_t eq,
379362
int64_t gt, std::optional<int64_t> unordered) {
380363
// The presense of unordered must match the value of ordering.
381-
if (ordering == CmpOrdering::Strong && unordered) {
382-
emitError() << "strong ordering does not include unordered ordering";
383-
return failure();
384-
}
385-
if (ordering == CmpOrdering::Partial && !unordered) {
386-
emitError() << "partial ordering lacks unordered ordering";
387-
return failure();
388-
}
364+
if (ordering == CmpOrdering::Strong && unordered)
365+
return emitError() << "strong ordering does not include unordered ordering";
366+
367+
if (ordering == CmpOrdering::Partial && !unordered)
368+
return emitError() << "partial ordering lacks unordered ordering";
389369

390370
return success();
391371
}
@@ -404,25 +384,21 @@ DataMemberAttr::verify(function_ref<InFlightDiagnostic()> emitError,
404384
}
405385

406386
auto clsRecordTy = ty.getClsTy();
407-
if (clsRecordTy.isIncomplete()) {
408-
emitError() << "incomplete 'cir.record' cannot be used to build a non-null "
409-
"data member pointer";
410-
return failure();
411-
}
387+
if (clsRecordTy.isIncomplete())
388+
return emitError()
389+
<< "incomplete 'cir.record' cannot be used to build a non-null "
390+
"data member pointer";
412391

413392
auto memberIndexValue = memberIndex.value();
414-
if (memberIndexValue >= clsRecordTy.getNumElements()) {
415-
emitError()
416-
<< "member index of a #cir.data_member attribute is out of range";
417-
return failure();
418-
}
393+
if (memberIndexValue >= clsRecordTy.getNumElements())
394+
return emitError()
395+
<< "member index of a #cir.data_member attribute is out of range";
419396

420397
auto memberTy = clsRecordTy.getMembers()[memberIndexValue];
421-
if (memberTy != ty.getMemberTy()) {
422-
emitError() << "member type of a #cir.data_member attribute must match the "
423-
"attribute type";
424-
return failure();
425-
}
398+
if (memberTy != ty.getMemberTy())
399+
return emitError()
400+
<< "member type of a #cir.data_member attribute must match the "
401+
"attribute type";
426402

427403
return success();
428404
}
@@ -431,16 +407,14 @@ DataMemberAttr::verify(function_ref<InFlightDiagnostic()> emitError,
431407
// MethodAttr definitions
432408
//===----------------------------------------------------------------------===//
433409

434-
LogicalResult
435-
MethodAttr::verify(function_ref<::mlir::InFlightDiagnostic()> emitError,
436-
cir::MethodType type,
437-
std::optional<FlatSymbolRefAttr> symbol,
438-
std::optional<uint64_t> vtable_offset) {
439-
if (symbol.has_value() && vtable_offset.has_value()) {
440-
emitError() << "at most one of symbol and vtable_offset can be present "
441-
"in #cir.method";
442-
return failure();
443-
}
410+
LogicalResult MethodAttr::verify(function_ref<InFlightDiagnostic()> emitError,
411+
cir::MethodType type,
412+
std::optional<FlatSymbolRefAttr> symbol,
413+
std::optional<uint64_t> vtable_offset) {
414+
if (symbol.has_value() && vtable_offset.has_value())
415+
return emitError()
416+
<< "at most one of symbol and vtable_offset can be present "
417+
"in #cir.method";
444418

445419
return success();
446420
}
@@ -504,38 +478,35 @@ void MethodAttr::print(AsmPrinter &printer) const {
504478
// GlobalAnnotationValuesAttr definitions
505479
//===----------------------------------------------------------------------===//
506480

507-
LogicalResult GlobalAnnotationValuesAttr::verify(
508-
function_ref<::mlir::InFlightDiagnostic()> emitError,
509-
mlir::ArrayAttr annotations) {
510-
if (annotations.empty()) {
511-
emitError()
512-
<< "GlobalAnnotationValuesAttr should at least have one annotation";
513-
return failure();
514-
}
481+
LogicalResult
482+
GlobalAnnotationValuesAttr::verify(function_ref<InFlightDiagnostic()> emitError,
483+
mlir::ArrayAttr annotations) {
484+
if (annotations.empty())
485+
return emitError()
486+
<< "GlobalAnnotationValuesAttr should at least have one annotation";
487+
515488
for (auto &entry : annotations) {
516489
auto annoEntry = ::mlir::dyn_cast<mlir::ArrayAttr>(entry);
517-
if (!annoEntry) {
518-
emitError() << "Element of GlobalAnnotationValuesAttr annotations array"
519-
" must be an array";
520-
return failure();
521-
} else if (annoEntry.size() != 2) {
522-
emitError() << "Element of GlobalAnnotationValuesAttr annotations array"
523-
<< " must be a 2-element array and you have "
524-
<< annoEntry.size();
525-
return failure();
526-
} else if (!::mlir::isa<mlir::StringAttr>(annoEntry[0])) {
527-
emitError() << "Element of GlobalAnnotationValuesAttr annotations"
528-
"array must start with a string, which is the name of "
529-
"global op or func it annotates";
530-
return failure();
531-
}
532-
auto annoPart = ::mlir::dyn_cast<cir::AnnotationAttr>(annoEntry[1]);
533-
if (!annoPart) {
534-
emitError() << "The second element of GlobalAnnotationValuesAttr"
535-
"annotations array element must be of "
536-
"type AnnotationValueAttr";
537-
return failure();
538-
}
490+
if (!annoEntry)
491+
return emitError()
492+
<< "Element of GlobalAnnotationValuesAttr annotations array"
493+
" must be an array";
494+
495+
if (annoEntry.size() != 2)
496+
return emitError()
497+
<< "Element of GlobalAnnotationValuesAttr annotations array"
498+
<< " must be a 2-element array and you have " << annoEntry.size();
499+
500+
if (!mlir::isa<mlir::StringAttr>(annoEntry[0]))
501+
return emitError()
502+
<< "Element of GlobalAnnotationValuesAttr annotations"
503+
"array must start with a string, which is the name of "
504+
"global op or func it annotates";
505+
506+
if (!mlir::isa<cir::AnnotationAttr>(annoEntry[1]))
507+
return emitError() << "The second element of GlobalAnnotationValuesAttr"
508+
"annotations array element must be of "
509+
"type AnnotationValueAttr";
539510
}
540511
return success();
541512
}
@@ -574,15 +545,11 @@ LogicalResult DynamicCastInfoAttr::verify(
574545
return pointeeIntTy.isUnsigned() && pointeeIntTy.getWidth() == 8;
575546
};
576547

577-
if (!isRttiPtr(srcRtti.getType())) {
578-
emitError() << "srcRtti must be an RTTI pointer";
579-
return failure();
580-
}
548+
if (!isRttiPtr(srcRtti.getType()))
549+
return emitError() << "srcRtti must be an RTTI pointer";
581550

582-
if (!isRttiPtr(destRtti.getType())) {
583-
emitError() << "destRtti must be an RTTI pointer";
584-
return failure();
585-
}
551+
if (!isRttiPtr(destRtti.getType()))
552+
return emitError() << "destRtti must be an RTTI pointer";
586553

587554
return success();
588555
}

0 commit comments

Comments
 (0)