Merge pull request #22 from rdmarsh2/rdmarsh/cpp/use-in-own-initializer-macro

C++: handle more macros in UseInOwnInitializer
This commit is contained in:
Jonas Jensen
2018-08-07 20:03:01 +02:00
committed by GitHub
2 changed files with 20 additions and 11 deletions

View File

@@ -27,4 +27,7 @@ where init.getDeclaration() = v
va = mi.getExpr()
)
)
and not (
va.getEnclosingStmt().isInMacroExpansion()
)
select va, v.getName() + " is used in its own initializer."

View File

@@ -1,23 +1,23 @@
typedef long size_t;
void test1() {
int x = x;
int x = x; // BAD
}
void test2() {
int x = x = 2;
int x = x = 2; // BAD
}
void test3() {
int x = 2;
int x = 2; // GOOD
}
void test4() {
void* x = (void *)&x;
void* x = (void *)&x; // GOOD
}
void test5() {
size_t x = sizeof(x);
size_t x = sizeof(x); // GOOD
}
typedef void *voidptr;
@@ -28,7 +28,7 @@ voidptr address_of(voidptr &var) {
}
void test6() {
voidptr x = address_of(x); // initialize to it's own address
voidptr x = address_of(x); // GOOD (implicit conversion to reference)
}
@@ -38,25 +38,31 @@ struct MyString {
};
void test7() {
MyString ms = {ms.array, {0}}; // initialize to ""
MyString ms = {ms.array, {0}}; // GOOD (implicit conversion to pointer)
}
#define uninitialized(x) x
void test8() {
int x = uninitialized(x);
int x = uninitialized(x); // GOOD (rval is a macro)
}
#define uninitialized2(x) x = x
void test9() {
int uninitialized2(x);
int uninitialized2(x); // GOOD (initializer is a macro)
}
void test10() {
int x = x + 1;
int x = x + 1; // BAD: x is evaluated on the right hand side
}
void test11() {
int x = uninitialized(x) + 1;
int x = uninitialized(x) + 1; // BAD: x is evaluated on the right hand side
}
#define self_initialize(t, x) t x = x
void test12() {
self_initialize(int, x); // GOOD (statement is from a macro)
}