Skip to content
This repository has been archived by the owner on Dec 31, 2020. It is now read-only.

Commit

Permalink
Add information for checking MISRA C++:2008 rules 4-10-1, 5-0-1, 5-0-…
Browse files Browse the repository at this point in the history
…4 and 5-0-6
  • Loading branch information
rettichschnidi committed Apr 17, 2016
1 parent f2d41c7 commit 0149d47
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 4 deletions.
8 changes: 4 additions & 4 deletions doc/cpp2008.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,19 @@ Pointer conversions
===================
| Rule | Supported by | Remarks |
| ----- | -------------------- | --------------------------------------------- |
|4-10-1 | TBD | |
|4-10-1 | -Wnull-conversion | |
|4-10-2 | TBD | |

Expressions
===========
| Rule | Supported by | Remarks |
| ----- | -------------------- | --------------------------------------------- |
|5-0-1 | TBD | |
|5-0-1 | -Wunsequenced | |
|5-0-2 | TBD | |
|5-0-3 | TBD | |
|5-0-4 | TBD | |
|5-0-4 | -Wsign-conversion | |
|5-0-5 | TBD | |
|5-0-6 | TBD | |
|5-0-6 | -Wconversion | |
|5-0-7 | TBD | |
|5-0-8 | TBD | |
|5-0-9 | TBD | |
Expand Down
38 changes: 38 additions & 0 deletions test/cpp2008/4-10-1.cpp
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}}
}
30 changes: 30 additions & 0 deletions test/cpp2008/5-0-1.cpp
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'}}
}
46 changes: 46 additions & 0 deletions test/cpp2008/5-0-4.cpp
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;
}
9 changes: 9 additions & 0 deletions test/cpp2008/5-0-6.cpp
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);
}

0 comments on commit 0149d47

Please sign in to comment.