brintos

brintos / llvm-project-archived public Read only

0
0
Text · 8.9 KiB · 25d27b3 Raw
169 lines · c
1// RUN: %check_clang_tidy -std=c99,c11,c17 -check-suffixes=,BEFORE-23 %s bugprone-easily-swappable-parameters %t \2// RUN:   -config='{CheckOptions: { \3// RUN:     bugprone-easily-swappable-parameters.MinimumLength: 2, \4// RUN:     bugprone-easily-swappable-parameters.IgnoredParameterNames: "", \5// RUN:     bugprone-easily-swappable-parameters.IgnoredParameterTypeSuffixes: "bool;MyBool;struct U;MAKE_LOGICAL_TYPE(int)", \6// RUN:     bugprone-easily-swappable-parameters.QualifiersMix: 0, \7// RUN:     bugprone-easily-swappable-parameters.ModelImplicitConversions: 0, \8// RUN:     bugprone-easily-swappable-parameters.SuppressParametersUsedTogether: 0, \9// RUN:     bugprone-easily-swappable-parameters.NamePrefixSuffixSilenceDissimilarityThreshold: 0 \10// RUN:  }}' -- -Wno-strict-prototypes11//12// RUN: %check_clang_tidy -std=c23-or-later %s bugprone-easily-swappable-parameters %t \13// RUN:   -config='{CheckOptions: { \14// RUN:     bugprone-easily-swappable-parameters.MinimumLength: 2, \15// RUN:     bugprone-easily-swappable-parameters.IgnoredParameterNames: "", \16// RUN:     bugprone-easily-swappable-parameters.IgnoredParameterTypeSuffixes: "bool;MyBool;struct U;MAKE_LOGICAL_TYPE(int)", \17// RUN:     bugprone-easily-swappable-parameters.QualifiersMix: 0, \18// RUN:     bugprone-easily-swappable-parameters.ModelImplicitConversions: 0, \19// RUN:     bugprone-easily-swappable-parameters.SuppressParametersUsedTogether: 0, \20// RUN:     bugprone-easily-swappable-parameters.NamePrefixSuffixSilenceDissimilarityThreshold: 0 \21// RUN:  }}' -- -Wno-strict-prototypes22 23#define bool _Bool24#define true 125#define false 026 27typedef bool MyBool;28 29#define TheLogicalType bool30 31void declVoid(void);         // NO-WARN: Declaration only.32void decl();                 // NO-WARN: Declaration only.33void oneParam(int I) {}      // NO-WARN: 1 parameter.34void variadic(int I, ...) {} // NO-WARN: 1 visible parameter.35 36void trivial(int I, int J) {}37// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: 2 adjacent parameters of 'trivial' of similar type ('int') are easily swapped by mistake [bugprone-easily-swappable-parameters]38// CHECK-MESSAGES: :[[@LINE-2]]:18: note: the first parameter in the range is 'I'39// CHECK-MESSAGES: :[[@LINE-3]]:25: note: the last parameter in the range is 'J'40 41void qualifier(int I, const int CI) {} // NO-WARN: Distinct types.42 43void restrictQualifier(char *restrict CPR1, char *restrict CPR2) {}44// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: 2 adjacent parameters of 'restrictQualifier' of similar type ('char *restrict')45// CHECK-MESSAGES: :[[@LINE-2]]:39: note: the first parameter in the range is 'CPR1'46// CHECK-MESSAGES: :[[@LINE-3]]:60: note: the last parameter in the range is 'CPR2'47 48void pointer1(int *IP1, int *IP2) {}49// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: 2 adjacent parameters of 'pointer1' of similar type ('int *')50// CHECK-MESSAGES: :[[@LINE-2]]:20: note: the first parameter in the range is 'IP1'51// CHECK-MESSAGES: :[[@LINE-3]]:30: note: the last parameter in the range is 'IP2'52 53void pointerConversion(int *IP, long *LP) {}54// NO-WARN: Even though C can convert any T* to U* back and forth, compiler55// warnings already exist for this.56 57void testVariadicsCall() {58  int IVal = 1;59 60#if __STDC_VERSION__ < 202311L61  decl(IVal); // NO-WARN: Particular calls to "variadics" are like template62              // instantiations, and we do not model them.63#endif64 65  variadic(IVal);          // NO-WARN.66  variadic(IVal, 2, 3, 4); // NO-WARN.67}68 69struct S {};70struct T {};71 72void taggedTypes1(struct S SVar, struct T TVar) {} // NO-WARN: Distinct types.73 74void taggedTypes2(struct S SVar1, struct S SVar2) {}75// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: 2 adjacent parameters of 'taggedTypes2' of similar type ('struct S')76// CHECK-MESSAGES: :[[@LINE-2]]:28: note: the first parameter in the range is 'SVar1'77// CHECK-MESSAGES: :[[@LINE-3]]:44: note: the last parameter in the range is 'SVar2'78 79void wrappers(struct { int I; } I1, struct { int I; } I2) {} // NO-WARN: Distinct anonymous types.80 81#if __STDC_VERSION__ < 202311L82void knr(I, J)83  int I;84  int J;85{}86// CHECK-MESSAGES-BEFORE-23: :[[@LINE-3]]:3: warning: 2 adjacent parameters of 'knr' of similar type ('int')87// CHECK-MESSAGES-BEFORE-23: :[[@LINE-4]]:7: note: the first parameter in the range is 'I'88// CHECK-MESSAGES-BEFORE-23: :[[@LINE-4]]:7: note: the last parameter in the range is 'J'89#endif90 91void boolAsWritten(bool B1, bool B2) {} // NO-WARN: The type name is ignored.92// Note that "bool" is a macro that expands to "_Bool" internally, but it is93// only "bool" that is ignored from the two.94 95void underscoreBoolAsWritten(_Bool B1, _Bool B2) {}96// Even though it is "_Bool" that is written in the code, the diagnostic message97// respects the printing policy as defined by the compilation commands. Clang's98// default in C mode seems to say that the type itself is "bool", not "_Bool".99// CHECK-MESSAGES: :[[@LINE-4]]:30: warning: 2 adjacent parameters of 'underscoreBoolAsWritten' of similar type ('bool')100// CHECK-MESSAGES: :[[@LINE-5]]:36: note: the first parameter in the range is 'B1'101// CHECK-MESSAGES: :[[@LINE-6]]:46: note: the last parameter in the range is 'B2'102 103void typedefdBoolAsWritten(MyBool MB1, MyBool MB2) {} // NO-WARN: "MyBool" as written type name ignored.104 105void otherBoolMacroAsWritten(TheLogicalType TLT1, TheLogicalType TLT2) {}106// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: 2 adjacent parameters of 'otherBoolMacroAsWritten' of similar type ('bool')107// CHECK-MESSAGES: :[[@LINE-2]]:45: note: the first parameter in the range is 'TLT1'108// CHECK-MESSAGES: :[[@LINE-3]]:66: note: the last parameter in the range is 'TLT2'109 110struct U {};111typedef struct U U;112 113void typedefStruct(U X, U Y) {}114// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 2 adjacent parameters of 'typedefStruct' of similar type ('U')115// CHECK-MESSAGES: :[[@LINE-2]]:22: note: the first parameter in the range is 'X'116// CHECK-MESSAGES: :[[@LINE-3]]:27: note: the last parameter in the range is 'Y'117 118void ignoredStructU(struct U X, struct U Y) {} // NO-WARN: "struct U" ignored.119 120#define TYPE_TAG_TO_USE struct // We are in C!121#define MAKE_TYPE_NAME(T) TYPE_TAG_TO_USE T122 123void macroMagic1(TYPE_TAG_TO_USE T X, TYPE_TAG_TO_USE T Y) {}124// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 2 adjacent parameters of 'macroMagic1' of similar type ('struct T')125// CHECK-MESSAGES: :[[@LINE-5]]:25: note: expanded from macro 'TYPE_TAG_TO_USE'126// CHECK-MESSAGES: :[[@LINE-3]]:36: note: the first parameter in the range is 'X'127// CHECK-MESSAGES: :[[@LINE-4]]:57: note: the last parameter in the range is 'Y'128 129void macroMagic2(TYPE_TAG_TO_USE U X, TYPE_TAG_TO_USE U Y) {}130// "struct U" is ignored, but that is not what is written here!131// CHECK-MESSAGES: :[[@LINE-2]]:18: warning: 2 adjacent parameters of 'macroMagic2' of similar type ('struct U')132// CHECK-MESSAGES: :[[@LINE-12]]:25: note: expanded from macro 'TYPE_TAG_TO_USE'133// CHECK-MESSAGES: :[[@LINE-4]]:36: note: the first parameter in the range is 'X'134// CHECK-MESSAGES: :[[@LINE-5]]:57: note: the last parameter in the range is 'Y'135 136void evenMoreMacroMagic1(MAKE_TYPE_NAME(T) X, MAKE_TYPE_NAME(T) Y) {}137// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: 2 adjacent parameters of 'evenMoreMacroMagic1' of similar type ('struct T')138// CHECK-MESSAGES: :[[@LINE-17]]:27: note: expanded from macro 'MAKE_TYPE_NAME'139// CHECK-MESSAGES: :[[@LINE-19]]:25: note: expanded from macro 'TYPE_TAG_TO_USE'140// CHECK-MESSAGES: :[[@LINE-4]]:44: note: the first parameter in the range is 'X'141// CHECK-MESSAGES: :[[@LINE-5]]:65: note: the last parameter in the range is 'Y'142 143void evenMoreMacroMagic2(MAKE_TYPE_NAME(U) X, MAKE_TYPE_NAME(U) Y) {}144// "struct U" is ignored, but that is not what is written here!145// CHECK-MESSAGES: :[[@LINE-2]]:26: warning: 2 adjacent parameters of 'evenMoreMacroMagic2' of similar type ('struct U')146// CHECK-MESSAGES: :[[@LINE-25]]:27: note: expanded from macro 'MAKE_TYPE_NAME'147// CHECK-MESSAGES: :[[@LINE-27]]:25: note: expanded from macro 'TYPE_TAG_TO_USE'148// CHECK-MESSAGES: :[[@LINE-5]]:44: note: the first parameter in the range is 'X'149// CHECK-MESSAGES: :[[@LINE-6]]:65: note: the last parameter in the range is 'Y'150 151#define MAKE_PRIMITIVE_WRAPPER(WRAPPED_TYPE) \152  MAKE_TYPE_NAME() {                         \153    WRAPPED_TYPE Member;                     \154  }155 156void thisIsGettingRidiculous(MAKE_PRIMITIVE_WRAPPER(int) I1,157                             MAKE_PRIMITIVE_WRAPPER(int) I2) {} // NO-WARN: Distinct anonymous types.158 159#define MAKE_LOGICAL_TYPE(X) bool160 161void macroMagic3(MAKE_LOGICAL_TYPE(char) B1, MAKE_LOGICAL_TYPE(long) B2) {}162// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 2 adjacent parameters of 'macroMagic3' of similar type ('bool')163// CHECK-MESSAGES: :[[@LINE-4]]:30: note: expanded from macro 'MAKE_LOGICAL_TYPE'164// CHECK-MESSAGES: :[[@LINE-141]]:14: note: expanded from macro 'bool'165// CHECK-MESSAGES: :[[@LINE-4]]:42: note: the first parameter in the range is 'B1'166// CHECK-MESSAGES: :[[@LINE-5]]:70: note: the last parameter in the range is 'B2'167 168void macroMagic4(MAKE_LOGICAL_TYPE(int) B1, MAKE_LOGICAL_TYPE(int) B2) {} // NO-WARN: "Type name" ignored.169