This repository has been archived by the owner on Dec 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add information for checking MISRA C++:2008 rules 4-10-1, 5-0-1, 5-0-…
…4 and 5-0-6
- Loading branch information
1 parent
f2d41c7
commit 0149d47
Showing
5 changed files
with
127 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// RUN: %clang -fsyntax-only -Wnull-conversion -ferror-limit=0 -Xclang -verify %s | ||
|
||
#include <cstddef> | ||
|
||
void f1(int i); | ||
void f2(int *p); | ||
|
||
class T { | ||
public: | ||
void f(int p); | ||
}; | ||
|
||
template <typename T> | ||
void tf(T t); | ||
|
||
class C { | ||
void f(); | ||
}; | ||
|
||
void test_function() { | ||
f1(0); | ||
f1(NULL); // expected-warning {{implicit conversion of NULL constant to 'int'}} | ||
|
||
f2(0); | ||
f2(NULL); | ||
|
||
T t; | ||
t.f(0); | ||
t.f(NULL); // expected-warning {{implicit conversion of NULL constant to 'int'}} | ||
|
||
tf<int>(0); | ||
tf<int>(NULL); // expected-warning {{implicit conversion of NULL constant to 'int'}} | ||
|
||
size_t c = 0; | ||
bool d = c == 0; | ||
int a = NULL; // expected-warning {{implicit conversion of NULL constant to 'int'}} | ||
bool b = a == NULL; // expected-warning {{comparison between NULL and non-pointer}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// RUN: %clang -fsyntax-only -Wunsequenced -Xclang -verify %s | ||
|
||
void incrementOperator(int i) { | ||
int b[] = {10, 20, 30}; | ||
int x = b[i] + i++; // expected-warning {{unsequenced modification and access to 'i'}} | ||
|
||
i++; | ||
x = b[i] + i; // Compliant | ||
} | ||
|
||
int f(int i, int j); | ||
void functionArguments(int i) { | ||
int x = f(i++, i); // expected-warning {{unsequenced modification and access to 'i'}} | ||
|
||
x = f(i + 1, i); // Compliant | ||
i++; | ||
} | ||
|
||
struct S { | ||
void method(S *); | ||
}; | ||
|
||
void callByPointer(S pointer1[], S pointer2[]) { | ||
pointer1->method(pointer1++); // expected-warning {{unsequenced modification and access to 'pointer1'}} | ||
pointer1->method(pointer2++); // Compliant | ||
} | ||
|
||
void nestedAssignements(int &argToModify) { | ||
argToModify = argToModify++; // expected-warning {{multiple unsequenced modifications to 'argToModify'}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// RUN: %clang -fsyntax-only -Wsign-conversion -Xclang -verify %s | ||
|
||
signed int s = 0; | ||
unsigned int u = 0U; | ||
|
||
signed int s1 = s; | ||
unsigned int u1 = u; | ||
|
||
signed int s2 = u; // expected-warning {{implicit conversion changes signedness: 'unsigned int' to 'int'}} | ||
unsigned int u2 = s; // expected-warning {{implicit conversion changes signedness: 'int' to 'unsigned int'}} | ||
|
||
signed int s3 = static_cast<signed int>(u); | ||
signed int s4 = static_cast<unsigned int>(u); // expected-warning {{implicit conversion changes signedness: 'unsigned int' to 'int'}} | ||
|
||
unsigned int u3 = static_cast<unsigned int>(s); | ||
unsigned int u4 = static_cast<signed int>(s); // expected-warning {{implicit conversion changes signedness: 'int' to 'unsigned int'}} | ||
|
||
unsigned int funMixedIntegerWidths(unsigned int a, unsigned short b) { | ||
return a + b; | ||
} | ||
|
||
enum UnsignedEnum { | ||
MY_UNSIGNED_ENUM_ENTRY_1, | ||
MY_UNSIGNED_ENUM_ENTRY_2, | ||
}; | ||
|
||
void funUnsignedEnum(UnsignedEnum se) { | ||
bool aa = se == MY_UNSIGNED_ENUM_ENTRY_1; | ||
bool bb = se != MY_UNSIGNED_ENUM_ENTRY_1; | ||
bool ff = se != MY_UNSIGNED_ENUM_ENTRY_1; | ||
} | ||
|
||
enum SignedEnum { | ||
MY_SIGNED_ENUM_ENTRY_1 = -1, | ||
MY_SIGNED_ENUM_ENTRY_2, | ||
}; | ||
|
||
void funSigned(SignedEnum ue) { | ||
bool aa = ue == MY_SIGNED_ENUM_ENTRY_1; | ||
bool bb = ue != MY_SIGNED_ENUM_ENTRY_1; | ||
bool ff = ue != MY_SIGNED_ENUM_ENTRY_1; | ||
} | ||
|
||
bool funMixed(SignedEnum se, UnsignedEnum ue) { | ||
return (int)se == (int)ue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// RUN: %clang -fsyntax-only -Wno-missing-prototypes -Wconversion -Xclang -verify %s | ||
|
||
short truncateImplicitly(int i) { | ||
return i; // expected-warning {{implicit conversion loses integer precision: 'int' to 'short'}} | ||
} | ||
|
||
short truncateExplicitly(int i) { | ||
return static_cast<short>(i); | ||
} |