xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/google/objc-avoid-throwing-exception.m (revision 6c2140943cbe257c85f7121349c5bca950a26e0d)
1*6c214094SStephane Moore// RUN: %check_clang_tidy %s google-objc-avoid-throwing-exception %t -- -- -I %S/Inputs/
2*6c214094SStephane Moore
389a1d03eSRichard@class NSString;
489a1d03eSRichard
589a1d03eSRichard@interface NSException
689a1d03eSRichard
789a1d03eSRichard+ (void)raise:(NSString *)name format:(NSString *)format;
889a1d03eSRichard+ (void)raise:(NSString *)name format:(NSString *)format arguments:(NSString *)args; // using NSString type since va_list cannot be recognized here
989a1d03eSRichard
1089a1d03eSRichard@end
1189a1d03eSRichard
1289a1d03eSRichard@interface NotException
1389a1d03eSRichard
1489a1d03eSRichard+ (void)raise:(NSString *)name format:(NSString *)format;
1589a1d03eSRichard
1689a1d03eSRichard@end
1789a1d03eSRichard
1889a1d03eSRichard@implementation Foo
1989a1d03eSRichard- (void)f {
2089a1d03eSRichard    NSString *foo = @"foo";
2189a1d03eSRichard    @throw foo;
2289a1d03eSRichard    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: pass in NSError ** instead of throwing exception to indicate Objective-C errors [google-objc-avoid-throwing-exception]
2389a1d03eSRichard}
2489a1d03eSRichard
25*6c214094SStephane Moore#include "system-header-throw.h"
26*6c214094SStephane Moore
27*6c214094SStephane Moore#define THROW(e) @throw e
28*6c214094SStephane Moore
29*6c214094SStephane Moore#define RAISE [NSException raise:@"example" format:@"fmt"]
30*6c214094SStephane Moore
3189a1d03eSRichard- (void)f2 {
3289a1d03eSRichard    [NSException raise:@"TestException" format:@"Test"];
3389a1d03eSRichard    // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: pass in NSError ** instead of throwing exception to indicate Objective-C errors [google-objc-avoid-throwing-exception]
3489a1d03eSRichard    [NSException raise:@"TestException" format:@"Test %@" arguments:@"bar"];
3589a1d03eSRichard    // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: pass in NSError ** instead of throwing exception to indicate Objective-C errors [google-objc-avoid-throwing-exception]
3689a1d03eSRichard    [NotException raise:@"NotException" format:@"Test"];
37*6c214094SStephane Moore
38*6c214094SStephane Moore    NSException *e;
39*6c214094SStephane Moore    SYS_THROW(e);
40*6c214094SStephane Moore
41*6c214094SStephane Moore    SYS_RAISE;
42*6c214094SStephane Moore
43*6c214094SStephane Moore    THROW(e);
44*6c214094SStephane Moore    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: pass in NSError ** instead of throwing exception to indicate Objective-C errors [google-objc-avoid-throwing-exception]
45*6c214094SStephane Moore
46*6c214094SStephane Moore    RAISE;
47*6c214094SStephane Moore    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: pass in NSError ** instead of throwing exception to indicate Objective-C errors [google-objc-avoid-throwing-exception]
4889a1d03eSRichard}
4989a1d03eSRichard@end
5089a1d03eSRichard
51