mirror of
https://github.com/github/codeql.git
synced 2025-12-16 08:43:11 +01:00
C++: Force LF for .c,.cpp,.h,.hpp
This commit is contained in:
@@ -1,2 +1,2 @@
|
||||
[*.{ql,qll,qlref,dbscheme,qhelp,html,js,mjs,ts,json,yml}]
|
||||
[*.{ql,qll,qlref,dbscheme,qhelp,html,js,mjs,ts,json,yml,c,cpp,h,hpp}]
|
||||
end_of_line = lf
|
||||
|
||||
4
.gitattributes
vendored
4
.gitattributes
vendored
@@ -21,3 +21,7 @@
|
||||
*.ts eol=lf
|
||||
*.json eol=lf
|
||||
*.yml eol=lf
|
||||
*.c eol=lf
|
||||
*.cpp eol=lf
|
||||
*.h eol=lf
|
||||
*.hpp eol=lf
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
// an include declaration just adds one source dependency, it does not automatically
|
||||
// add a dependency from this file to all the declarations in stdio.h
|
||||
#include <stdio.h>
|
||||
#include <myfile.h> // contains non-static global myfile_err
|
||||
|
||||
extern int myfile_err; // this external declaration adds a dependency on myfile.h
|
||||
|
||||
class C {
|
||||
public:
|
||||
C() {
|
||||
// one dependency for printf:
|
||||
printf("Hello world!");
|
||||
// one dependency for FILE type, and one for NULL macro:
|
||||
FILE fp = NULL;
|
||||
}
|
||||
};
|
||||
|
||||
// an include declaration just adds one source dependency, it does not automatically
|
||||
// add a dependency from this file to all the declarations in stdio.h
|
||||
#include <stdio.h>
|
||||
#include <myfile.h> // contains non-static global myfile_err
|
||||
|
||||
extern int myfile_err; // this external declaration adds a dependency on myfile.h
|
||||
|
||||
class C {
|
||||
public:
|
||||
C() {
|
||||
// one dependency for printf:
|
||||
printf("Hello world!");
|
||||
// one dependency for FILE type, and one for NULL macro:
|
||||
FILE fp = NULL;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
//This struct contains 30 fields.
|
||||
struct MyParticle {
|
||||
bool isActive;
|
||||
int priority;
|
||||
|
||||
float x, y, z;
|
||||
float dx, dy, dz;
|
||||
float ddx, ddy, ddz;
|
||||
bool isCollider;
|
||||
|
||||
int age, maxAge;
|
||||
float size1, size2;
|
||||
|
||||
bool hasColor;
|
||||
unsigned char r1, g1, b1, a1;
|
||||
unsigned char r2, g2, b2, a2;
|
||||
|
||||
class texture *tex;
|
||||
float u1, v1, u2, v2;
|
||||
};
|
||||
//This struct contains 30 fields.
|
||||
struct MyParticle {
|
||||
bool isActive;
|
||||
int priority;
|
||||
|
||||
float x, y, z;
|
||||
float dx, dy, dz;
|
||||
float ddx, ddy, ddz;
|
||||
bool isCollider;
|
||||
|
||||
int age, maxAge;
|
||||
float size1, size2;
|
||||
|
||||
bool hasColor;
|
||||
unsigned char r1, g1, b1, a1;
|
||||
unsigned char r2, g2, b2, a2;
|
||||
|
||||
class texture *tex;
|
||||
float u1, v1, u2, v2;
|
||||
};
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// this example has 15 parameters.
|
||||
void fillRect(int x, int y, int w, int h,
|
||||
int r1, int g1, int b1, int a1,
|
||||
int r2, int g2, int b2, int a2,
|
||||
gradient_type grad, unsigned int flags, bool border)
|
||||
{
|
||||
// ...
|
||||
}
|
||||
// this example has 15 parameters.
|
||||
void fillRect(int x, int y, int w, int h,
|
||||
int r1, int g1, int b1, int a1,
|
||||
int r2, int g2, int b2, int a2,
|
||||
gradient_type grad, unsigned int flags, bool border)
|
||||
{
|
||||
// ...
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
//This condition is too complex and can be improved by using local variables
|
||||
bool accept_message =
|
||||
(message_type == CONNECT && _state != CONNECTED) ||
|
||||
(message_type == DISCONNECT && _state == CONNECTED) ||
|
||||
(message_type == DATA && _state == CONNECTED);
|
||||
|
||||
//This condition is acceptable, as all the logical operators are of the same type (&&)
|
||||
bool valid_connect =
|
||||
message_type == CONNECT &&
|
||||
_state != CONNECTED &&
|
||||
time_since_prev_connect > MAX_CONNECT_INTERVAL &&
|
||||
message_length <= MAX_PACKET_SIZE &&
|
||||
//This condition is too complex and can be improved by using local variables
|
||||
bool accept_message =
|
||||
(message_type == CONNECT && _state != CONNECTED) ||
|
||||
(message_type == DISCONNECT && _state == CONNECTED) ||
|
||||
(message_type == DATA && _state == CONNECTED);
|
||||
|
||||
//This condition is acceptable, as all the logical operators are of the same type (&&)
|
||||
bool valid_connect =
|
||||
message_type == CONNECT &&
|
||||
_state != CONNECTED &&
|
||||
time_since_prev_connect > MAX_CONNECT_INTERVAL &&
|
||||
message_length <= MAX_PACKET_SIZE &&
|
||||
checksum(message) == get_checksum_field(message);
|
||||
@@ -1,6 +1,6 @@
|
||||
void f(int i) {
|
||||
for (int i = 0; i < 10; ++i) { //the loop variable hides the parameter to f()
|
||||
...
|
||||
}
|
||||
}
|
||||
|
||||
void f(int i) {
|
||||
for (int i = 0; i < 10; ++i) { //the loop variable hides the parameter to f()
|
||||
...
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
void f() {
|
||||
int i = 10;
|
||||
|
||||
for (int i = 0; i < 10; i++) { //the loop counter hides the variable
|
||||
...
|
||||
}
|
||||
|
||||
{
|
||||
int i = 12; //this variable hides the variable in the outer block
|
||||
...
|
||||
}
|
||||
}
|
||||
void f() {
|
||||
int i = 10;
|
||||
|
||||
for (int i = 0; i < 10; i++) { //the loop counter hides the variable
|
||||
...
|
||||
}
|
||||
|
||||
{
|
||||
int i = 12; //this variable hides the variable in the outer block
|
||||
...
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
int i = 10;
|
||||
|
||||
void f() {
|
||||
for (int i = 0; i < 10; i++) { //the loop counter hides the global variable i
|
||||
...
|
||||
}
|
||||
|
||||
{
|
||||
int i = 12; //this variable hides the global variable i
|
||||
...
|
||||
}
|
||||
}
|
||||
int i = 10;
|
||||
|
||||
void f() {
|
||||
for (int i = 0; i < 10; i++) { //the loop counter hides the global variable i
|
||||
...
|
||||
}
|
||||
|
||||
{
|
||||
int i = 12; //this variable hides the global variable i
|
||||
...
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
void f(int i) {
|
||||
if (i == 10); //empty then block
|
||||
... //won't be part of the if statement
|
||||
|
||||
if (i == 12) {
|
||||
...
|
||||
} else { //empty else block, most likely a mistake
|
||||
}
|
||||
}
|
||||
void f(int i) {
|
||||
if (i == 10); //empty then block
|
||||
... //won't be part of the if statement
|
||||
|
||||
if (i == 12) {
|
||||
...
|
||||
} else { //empty else block, most likely a mistake
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,43 +1,43 @@
|
||||
static int idctr = 0;
|
||||
//Basic connection with id
|
||||
class Connection {
|
||||
public:
|
||||
int connId;
|
||||
virtual void print_info() {
|
||||
cout << "id: " << connId << "\n";
|
||||
}
|
||||
Connection() {
|
||||
connId = idctr++;
|
||||
}
|
||||
};
|
||||
|
||||
//Adds counters, and an overriding print_info
|
||||
class MeteredConnection : public Connection {
|
||||
public:
|
||||
int txCtr;
|
||||
int rxCtr;
|
||||
MeteredConnection() {
|
||||
txCtr = 0;
|
||||
rxCtr = 0;
|
||||
}
|
||||
virtual void print_info() {
|
||||
cout << "id: " << connId << "\n" << "tx/rx: " << txCtr << "/" << rxCtr << "\n";
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
Connection conn;
|
||||
MeteredConnection m_conn;
|
||||
|
||||
Connection curr_conn = conn;
|
||||
curr_conn.print_info();
|
||||
curr_conn = m_conn; //Wrong: Derived MetricConnection assigned to Connection
|
||||
//variable, will slice off the counters and the overriding print_info
|
||||
curr_conn.print_info(); //Will not print the counters.
|
||||
|
||||
Connection* curr_pconn = &conn;
|
||||
curr_pconn->print_info();
|
||||
curr_pconn = &m_conn; //Correct: Pointer assigned to address of the MetricConnection.
|
||||
//Counters and virtual functions remain intact.
|
||||
curr_pconn->print_info(); //Will call the correct method MeteredConnection::print_info
|
||||
}
|
||||
static int idctr = 0;
|
||||
//Basic connection with id
|
||||
class Connection {
|
||||
public:
|
||||
int connId;
|
||||
virtual void print_info() {
|
||||
cout << "id: " << connId << "\n";
|
||||
}
|
||||
Connection() {
|
||||
connId = idctr++;
|
||||
}
|
||||
};
|
||||
|
||||
//Adds counters, and an overriding print_info
|
||||
class MeteredConnection : public Connection {
|
||||
public:
|
||||
int txCtr;
|
||||
int rxCtr;
|
||||
MeteredConnection() {
|
||||
txCtr = 0;
|
||||
rxCtr = 0;
|
||||
}
|
||||
virtual void print_info() {
|
||||
cout << "id: " << connId << "\n" << "tx/rx: " << txCtr << "/" << rxCtr << "\n";
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
Connection conn;
|
||||
MeteredConnection m_conn;
|
||||
|
||||
Connection curr_conn = conn;
|
||||
curr_conn.print_info();
|
||||
curr_conn = m_conn; //Wrong: Derived MetricConnection assigned to Connection
|
||||
//variable, will slice off the counters and the overriding print_info
|
||||
curr_conn.print_info(); //Will not print the counters.
|
||||
|
||||
Connection* curr_pconn = &conn;
|
||||
curr_pconn->print_info();
|
||||
curr_pconn = &m_conn; //Correct: Pointer assigned to address of the MetricConnection.
|
||||
//Counters and virtual functions remain intact.
|
||||
curr_pconn->print_info(); //Will call the correct method MeteredConnection::print_info
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
void sanitize(Fields[] record) {
|
||||
//The number of fields here can be put in a const
|
||||
for (fieldCtr = 0; field < 7; field++) {
|
||||
sanitize(fields[fieldCtr]);
|
||||
}
|
||||
}
|
||||
|
||||
#define NUM_FIELDS 7
|
||||
|
||||
void process(Fields[] record) {
|
||||
//This avoids using a magic constant by using the macro instead
|
||||
for (fieldCtr = 0; field < NUM_FIELDS; field++) {
|
||||
process(fields[fieldCtr]);
|
||||
}
|
||||
}
|
||||
|
||||
void sanitize(Fields[] record) {
|
||||
//The number of fields here can be put in a const
|
||||
for (fieldCtr = 0; field < 7; field++) {
|
||||
sanitize(fields[fieldCtr]);
|
||||
}
|
||||
}
|
||||
|
||||
#define NUM_FIELDS 7
|
||||
|
||||
void process(Fields[] record) {
|
||||
//This avoids using a magic constant by using the macro instead
|
||||
for (fieldCtr = 0; field < NUM_FIELDS; field++) {
|
||||
process(fields[fieldCtr]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,26 +1,26 @@
|
||||
class C {
|
||||
private:
|
||||
Other* other = NULL;
|
||||
public:
|
||||
C(const C& copyFrom) {
|
||||
Other* newOther = new Other();
|
||||
*newOther = copyFrom.other;
|
||||
this->other = newOther;
|
||||
}
|
||||
|
||||
//No operator=, by default will just copy the pointer other, will not create a new object
|
||||
};
|
||||
|
||||
class D {
|
||||
Other* other = NULL;
|
||||
public:
|
||||
D& operator=(D& rhs) {
|
||||
Other* newOther = new Other();
|
||||
*newOther = rhs.other;
|
||||
this->other = newOther;
|
||||
return *this;
|
||||
}
|
||||
|
||||
//No copy constructor, will just copy the pointer other and not create a new object
|
||||
};
|
||||
|
||||
class C {
|
||||
private:
|
||||
Other* other = NULL;
|
||||
public:
|
||||
C(const C& copyFrom) {
|
||||
Other* newOther = new Other();
|
||||
*newOther = copyFrom.other;
|
||||
this->other = newOther;
|
||||
}
|
||||
|
||||
//No operator=, by default will just copy the pointer other, will not create a new object
|
||||
};
|
||||
|
||||
class D {
|
||||
Other* other = NULL;
|
||||
public:
|
||||
D& operator=(D& rhs) {
|
||||
Other* newOther = new Other();
|
||||
*newOther = rhs.other;
|
||||
this->other = newOther;
|
||||
return *this;
|
||||
}
|
||||
|
||||
//No copy constructor, will just copy the pointer other and not create a new object
|
||||
};
|
||||
|
||||
|
||||
@@ -1,32 +1,32 @@
|
||||
//This switch statement has long case statements, and can become difficult to
|
||||
//read as the processing for each message type becomes more complex
|
||||
switch (message_type) {
|
||||
case CONNECT:
|
||||
_state = CONNECTING;
|
||||
int message_id = message_get_id(message);
|
||||
int source = connect_get_source(message);
|
||||
//More code here...
|
||||
send(connect_response);
|
||||
break;
|
||||
case DISCONNECT:
|
||||
_state = DISCONNECTING;
|
||||
int message_id = message_get_id(message);
|
||||
int source = disconnect_get_source(message);
|
||||
//More code here...
|
||||
send(disconnect_response);
|
||||
break;
|
||||
default:
|
||||
log("Invalid message, id : %d", message_get_id(message));
|
||||
}
|
||||
|
||||
//This is better, as each case is split out to a separate function
|
||||
switch (packet_type) {
|
||||
case STREAM:
|
||||
process_stream_packet(packet);
|
||||
break;
|
||||
case DATAGRAM:
|
||||
process_datagram_packet(packet);
|
||||
break;
|
||||
default:
|
||||
log("Invalid packet type: %d", packet_type);
|
||||
//This switch statement has long case statements, and can become difficult to
|
||||
//read as the processing for each message type becomes more complex
|
||||
switch (message_type) {
|
||||
case CONNECT:
|
||||
_state = CONNECTING;
|
||||
int message_id = message_get_id(message);
|
||||
int source = connect_get_source(message);
|
||||
//More code here...
|
||||
send(connect_response);
|
||||
break;
|
||||
case DISCONNECT:
|
||||
_state = DISCONNECTING;
|
||||
int message_id = message_get_id(message);
|
||||
int source = disconnect_get_source(message);
|
||||
//More code here...
|
||||
send(disconnect_response);
|
||||
break;
|
||||
default:
|
||||
log("Invalid message, id : %d", message_get_id(message));
|
||||
}
|
||||
|
||||
//This is better, as each case is split out to a separate function
|
||||
switch (packet_type) {
|
||||
case STREAM:
|
||||
process_stream_packet(packet);
|
||||
break;
|
||||
case DATAGRAM:
|
||||
process_datagram_packet(packet);
|
||||
break;
|
||||
default:
|
||||
log("Invalid packet type: %d", packet_type);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
int x = 0; //x is unused
|
||||
int y = 0;
|
||||
cout << y;
|
||||
}
|
||||
{
|
||||
int x = 0; //x is unused
|
||||
int y = 0;
|
||||
cout << y;
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
//start of file
|
||||
static void f() { //static function f() is unused in the file
|
||||
//...
|
||||
}
|
||||
static void g() {
|
||||
//...
|
||||
}
|
||||
void public_func() { //non-static function public_func is not called in file,
|
||||
//but could be visible in other files
|
||||
//...
|
||||
g(); //call to g()
|
||||
//...
|
||||
}
|
||||
//end of file
|
||||
//start of file
|
||||
static void f() { //static function f() is unused in the file
|
||||
//...
|
||||
}
|
||||
static void g() {
|
||||
//...
|
||||
}
|
||||
void public_func() { //non-static function public_func is not called in file,
|
||||
//but could be visible in other files
|
||||
//...
|
||||
g(); //call to g()
|
||||
//...
|
||||
}
|
||||
//end of file
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
void f() {
|
||||
static int i = 0; //i is unused
|
||||
...
|
||||
return;
|
||||
}
|
||||
void f() {
|
||||
static int i = 0; //i is unused
|
||||
...
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
while(result) {
|
||||
if ( ... )
|
||||
...
|
||||
else if (result //wrong: this test is redundant
|
||||
&& result->flags != 0)
|
||||
...
|
||||
result = next(queue);
|
||||
}
|
||||
|
||||
|
||||
fp = fopen(log, "r");
|
||||
if (fp) {
|
||||
/*
|
||||
* large block of code
|
||||
*/
|
||||
if (!fp) { //wrong: always false
|
||||
... /* dead code */
|
||||
}
|
||||
}
|
||||
while(result) {
|
||||
if ( ... )
|
||||
...
|
||||
else if (result //wrong: this test is redundant
|
||||
&& result->flags != 0)
|
||||
...
|
||||
result = next(queue);
|
||||
}
|
||||
|
||||
|
||||
fp = fopen(log, "r");
|
||||
if (fp) {
|
||||
/*
|
||||
* large block of code
|
||||
*/
|
||||
if (!fp) { //wrong: always false
|
||||
... /* dead code */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
class C {
|
||||
public:
|
||||
void g() {
|
||||
...
|
||||
//f() was previously used but is now commented, orphaning f()
|
||||
//f();
|
||||
...
|
||||
}
|
||||
private:
|
||||
void f() { //is now unused, and can be removed
|
||||
}
|
||||
};
|
||||
class C {
|
||||
public:
|
||||
void g() {
|
||||
...
|
||||
//f() was previously used but is now commented, orphaning f()
|
||||
//f();
|
||||
...
|
||||
}
|
||||
private:
|
||||
void f() { //is now unused, and can be removed
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
int f() {
|
||||
try {
|
||||
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
do_stuff(sockfd);
|
||||
return sockfd; //if there are no exceptions, the socket is returned
|
||||
} catch (int do_stuff_exception) {
|
||||
return -1; //return error value, but sockfd may still be open
|
||||
}
|
||||
}
|
||||
int f() {
|
||||
try {
|
||||
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
do_stuff(sockfd);
|
||||
return sockfd; //if there are no exceptions, the socket is returned
|
||||
} catch (int do_stuff_exception) {
|
||||
return -1; //return error value, but sockfd may still be open
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
int main(int argc, char* argv[]) {
|
||||
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
int status = 0;
|
||||
... //code that does not close sockfd
|
||||
return status; //sockfd is never closed
|
||||
}
|
||||
int main(int argc, char* argv[]) {
|
||||
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
int status = 0;
|
||||
... //code that does not close sockfd
|
||||
return status; //sockfd is never closed
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
int g_callCtr;
|
||||
|
||||
void initGlobals() {
|
||||
g_callCtr = 0;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
...
|
||||
cout << g_callCtr; //callCtr used before it is initialized
|
||||
initGlobals();
|
||||
}
|
||||
int g_callCtr;
|
||||
|
||||
void initGlobals() {
|
||||
g_callCtr = 0;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
...
|
||||
cout << g_callCtr; //callCtr used before it is initialized
|
||||
initGlobals();
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
GlobalStorage *g_storage;
|
||||
|
||||
void init() { //initializes g_storage, but is never run from main
|
||||
g_storage = new GlobalStorage();
|
||||
...
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
... //init not called
|
||||
strcpy(g_storage->name, argv[1]); // g_storage is used before init() is called
|
||||
...
|
||||
}
|
||||
GlobalStorage *g_storage;
|
||||
|
||||
void init() { //initializes g_storage, but is never run from main
|
||||
g_storage = new GlobalStorage();
|
||||
...
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
... //init not called
|
||||
strcpy(g_storage->name, argv[1]); // g_storage is used before init() is called
|
||||
...
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
typedef struct Names {
|
||||
char first[100];
|
||||
char last[100];
|
||||
} Names;
|
||||
|
||||
int doFoo(Names n) { //wrong: n is passed by value (meaning the entire structure
|
||||
//is copied onto the stack, instead of just a pointer)
|
||||
...
|
||||
}
|
||||
|
||||
int doBar(Names &n) { //better, only a reference is passed
|
||||
...
|
||||
}
|
||||
typedef struct Names {
|
||||
char first[100];
|
||||
char last[100];
|
||||
} Names;
|
||||
|
||||
int doFoo(Names n) { //wrong: n is passed by value (meaning the entire structure
|
||||
//is copied onto the stack, instead of just a pointer)
|
||||
...
|
||||
}
|
||||
|
||||
int doBar(Names &n) { //better, only a reference is passed
|
||||
...
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
typedef struct {
|
||||
char name[100];
|
||||
int status;
|
||||
} person;
|
||||
|
||||
void f() {
|
||||
person* buf = NULL;
|
||||
buf = malloc(sizeof(person));
|
||||
|
||||
(*buf).status = 0; //access to buf before it was checked for NULL
|
||||
}
|
||||
typedef struct {
|
||||
char name[100];
|
||||
int status;
|
||||
} person;
|
||||
|
||||
void f() {
|
||||
person* buf = NULL;
|
||||
buf = malloc(sizeof(person));
|
||||
|
||||
(*buf).status = 0; //access to buf before it was checked for NULL
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
Record* record = new Record[SIZE];
|
||||
|
||||
...
|
||||
|
||||
delete record; //record was created using 'new[]', but was freed using 'delete'
|
||||
Record* record = new Record[SIZE];
|
||||
|
||||
...
|
||||
|
||||
delete record; //record was created using 'new[]', but was freed using 'delete'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
Record *ptr = new Record(...);
|
||||
|
||||
...
|
||||
|
||||
delete [] ptr; // ptr was created using 'new', but was freed using 'delete[]'
|
||||
Record *ptr = new Record(...);
|
||||
|
||||
...
|
||||
|
||||
delete [] ptr; // ptr was created using 'new', but was freed using 'delete[]'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
Record *ptr = new Record(...);
|
||||
|
||||
...
|
||||
|
||||
free(ptr); // BAD: ptr was created using 'new', but is being freed using 'free'
|
||||
Record *ptr = new Record(...);
|
||||
|
||||
...
|
||||
|
||||
free(ptr); // BAD: ptr was created using 'new', but is being freed using 'free'
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
int i;
|
||||
|
||||
...
|
||||
int g = COEFF * i; //i is used before it is initialized
|
||||
}
|
||||
{
|
||||
int i;
|
||||
|
||||
...
|
||||
int g = COEFF * i; //i is used before it is initialized
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
char param[SIZE];
|
||||
|
||||
char arg1[10];
|
||||
char arg2[20];
|
||||
|
||||
//wrong: only uses the size of the source (argv[1]) when using strncpy
|
||||
strncpy(param, argv[1], strlen(arg1));
|
||||
|
||||
//correct: uses the size of the destination array as well
|
||||
strncpy(param, argv[1], min(strlen(arg1, sizeof(param) -1)));
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
char param[SIZE];
|
||||
|
||||
char arg1[10];
|
||||
char arg2[20];
|
||||
|
||||
//wrong: only uses the size of the source (argv[1]) when using strncpy
|
||||
strncpy(param, argv[1], strlen(arg1));
|
||||
|
||||
//correct: uses the size of the destination array as well
|
||||
strncpy(param, argv[1], min(strlen(arg1, sizeof(param) -1)));
|
||||
}
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
int doFoo() {
|
||||
...
|
||||
return status;
|
||||
}
|
||||
|
||||
void f() {
|
||||
if (doFoo() == OK) {
|
||||
...
|
||||
}
|
||||
}
|
||||
|
||||
void g() {
|
||||
int status = doFoo();
|
||||
if (status == OK) {
|
||||
...
|
||||
}
|
||||
}
|
||||
|
||||
void err() {
|
||||
doFoo(); //doFoo is called but its return value is not checked, and
|
||||
//the value is checked in other locations
|
||||
...
|
||||
}
|
||||
int doFoo() {
|
||||
...
|
||||
return status;
|
||||
}
|
||||
|
||||
void f() {
|
||||
if (doFoo() == OK) {
|
||||
...
|
||||
}
|
||||
}
|
||||
|
||||
void g() {
|
||||
int status = doFoo();
|
||||
if (status == OK) {
|
||||
...
|
||||
}
|
||||
}
|
||||
|
||||
void err() {
|
||||
doFoo(); //doFoo is called but its return value is not checked, and
|
||||
//the value is checked in other locations
|
||||
...
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
#define RECORD_SIZE 30 //incorrect or outdated size for record
|
||||
typedef struct {
|
||||
char name[30];
|
||||
int status;
|
||||
} Record;
|
||||
|
||||
void f() {
|
||||
Record* p = malloc(RECORD_SIZE); //not of sufficient size to hold a Record
|
||||
...
|
||||
}
|
||||
#define RECORD_SIZE 30 //incorrect or outdated size for record
|
||||
typedef struct {
|
||||
char name[30];
|
||||
int status;
|
||||
} Record;
|
||||
|
||||
void f() {
|
||||
Record* p = malloc(RECORD_SIZE); //not of sufficient size to hold a Record
|
||||
...
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{
|
||||
int foo = 1;
|
||||
... //foo is unused
|
||||
}
|
||||
{
|
||||
int foo = 1;
|
||||
... //foo is unused
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
int f() {
|
||||
char* buf = new char[SIZE];
|
||||
....
|
||||
if (error) {
|
||||
free(buf); //error handling has freed the buffer
|
||||
}
|
||||
...
|
||||
log_contents(buf); //but it is still used here for logging
|
||||
}
|
||||
int f() {
|
||||
char* buf = new char[SIZE];
|
||||
....
|
||||
if (error) {
|
||||
free(buf); //error handling has freed the buffer
|
||||
}
|
||||
...
|
||||
log_contents(buf); //but it is still used here for logging
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
int isOdd(int n) {
|
||||
//TODO: Works only for positive n. Need to check if negative n is valid input
|
||||
return (n % 2) == 1;
|
||||
int isOdd(int n) {
|
||||
//TODO: Works only for positive n. Need to check if negative n is valid input
|
||||
return (n % 2) == 1;
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
// header_file.h
|
||||
|
||||
#ifndef HEADER_FILE_H
|
||||
#define HEADER_FILE_H
|
||||
|
||||
// ...
|
||||
|
||||
// header_file.h
|
||||
|
||||
#ifndef HEADER_FILE_H
|
||||
#define HEADER_FILE_H
|
||||
|
||||
// ...
|
||||
|
||||
#endif // HEADER_FILE_H
|
||||
@@ -1,8 +1,8 @@
|
||||
// another_header_file.h
|
||||
|
||||
#ifndef HEADER_FILE_H // should be ANOTHER_HEADER_FILE_H
|
||||
#define HEADER_FILE_H // should be ANOTHER_HEADER_FILE_H
|
||||
|
||||
// ...
|
||||
|
||||
// another_header_file.h
|
||||
|
||||
#ifndef HEADER_FILE_H // should be ANOTHER_HEADER_FILE_H
|
||||
#define HEADER_FILE_H // should be ANOTHER_HEADER_FILE_H
|
||||
|
||||
// ...
|
||||
|
||||
#endif // HEADER_FILE_H
|
||||
@@ -1,6 +1,6 @@
|
||||
void h() {
|
||||
int a, b, c;
|
||||
|
||||
a < b != c; //parenthesize to explicitly define order of operators
|
||||
(a < b) < c; //correct: parenthesized to specify order
|
||||
}
|
||||
void h() {
|
||||
int a, b, c;
|
||||
|
||||
a < b != c; //parenthesize to explicitly define order of operators
|
||||
(a < b) < c; //correct: parenthesized to specify order
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
|
||||
//Function foo's array parameter has a specified size
|
||||
void foo(int a[10]) {
|
||||
int i = 0;
|
||||
for (i = 0; i <10; i++) {
|
||||
a[i] = i * 2;
|
||||
}
|
||||
}
|
||||
|
||||
...
|
||||
|
||||
int my_arr[5];
|
||||
|
||||
//Function foo's array parameter has a specified size
|
||||
void foo(int a[10]) {
|
||||
int i = 0;
|
||||
for (i = 0; i <10; i++) {
|
||||
a[i] = i * 2;
|
||||
}
|
||||
}
|
||||
|
||||
...
|
||||
|
||||
int my_arr[5];
|
||||
foo(my_arr); //my_arr is smaller than foo's array parameter, and will cause access to memory outside its bounds
|
||||
@@ -1,4 +1,4 @@
|
||||
//sz is a signed integer, but malloc expects one that is unsigned.
|
||||
//Negative values will be interpreted as a large number, which may
|
||||
//lead to unexpected behavior
|
||||
char *buf = malloc(sz);
|
||||
//sz is a signed integer, but malloc expects one that is unsigned.
|
||||
//Negative values will be interpreted as a large number, which may
|
||||
//lead to unexpected behavior
|
||||
char *buf = malloc(sz);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
void f(char *p) {
|
||||
int my_ptr = p; //Wrong: pointer assigned to int, would be incorrect if sizeof(char*)
|
||||
//is larger than sizeof(int)
|
||||
//...
|
||||
}
|
||||
void f(char *p) {
|
||||
int my_ptr = p; //Wrong: pointer assigned to int, would be incorrect if sizeof(char*)
|
||||
//is larger than sizeof(int)
|
||||
//...
|
||||
}
|
||||
|
||||
@@ -1,30 +1,30 @@
|
||||
struct property {
|
||||
char *name;
|
||||
int value;
|
||||
};
|
||||
|
||||
struct property * get_property(char *key);
|
||||
struct property * get_property_default(char *key, int default_value);
|
||||
|
||||
void check_properties() {
|
||||
// this call will get flagged since most
|
||||
// calls to get_property handle NULL
|
||||
struct property *p1 = get_property("time");
|
||||
if(p1->value > 600) {
|
||||
...
|
||||
}
|
||||
|
||||
// this call will not get flagged since
|
||||
// the result of the call is checked for NULL
|
||||
struct property *p2 = get_property("time");
|
||||
if(p2 != NULL && p2->value > 600) {
|
||||
...
|
||||
}
|
||||
|
||||
// this call will not get flagged since calls
|
||||
// to get_property_default rarely handle NULL
|
||||
struct property *p3 = get_property_default("time", 50);
|
||||
if(p3->value > 60) {
|
||||
...
|
||||
}
|
||||
}
|
||||
struct property {
|
||||
char *name;
|
||||
int value;
|
||||
};
|
||||
|
||||
struct property * get_property(char *key);
|
||||
struct property * get_property_default(char *key, int default_value);
|
||||
|
||||
void check_properties() {
|
||||
// this call will get flagged since most
|
||||
// calls to get_property handle NULL
|
||||
struct property *p1 = get_property("time");
|
||||
if(p1->value > 600) {
|
||||
...
|
||||
}
|
||||
|
||||
// this call will not get flagged since
|
||||
// the result of the call is checked for NULL
|
||||
struct property *p2 = get_property("time");
|
||||
if(p2 != NULL && p2->value > 600) {
|
||||
...
|
||||
}
|
||||
|
||||
// this call will not get flagged since calls
|
||||
// to get_property_default rarely handle NULL
|
||||
struct property *p3 = get_property_default("time", 50);
|
||||
if(p3->value > 60) {
|
||||
...
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
if (!flags & SOME_BIT) { //wrong: '!' has higher precedence than '&', so this
|
||||
// is bracketed as '(!flags) & SOME_BIT', and does not
|
||||
// check whether a particular bit is set.
|
||||
// ...
|
||||
}
|
||||
|
||||
if ((p != NULL) & p->f()) { //wrong: The use of '&' rather than '&&' will still
|
||||
// de-reference the pointer even if it is NULL.
|
||||
// ...
|
||||
}
|
||||
|
||||
int bits = (s > 8) & 0xff; //wrong: Invalid attempt to get the 8 most significant
|
||||
// bits of a short.
|
||||
if (!flags & SOME_BIT) { //wrong: '!' has higher precedence than '&', so this
|
||||
// is bracketed as '(!flags) & SOME_BIT', and does not
|
||||
// check whether a particular bit is set.
|
||||
// ...
|
||||
}
|
||||
|
||||
if ((p != NULL) & p->f()) { //wrong: The use of '&' rather than '&&' will still
|
||||
// de-reference the pointer even if it is NULL.
|
||||
// ...
|
||||
}
|
||||
|
||||
int bits = (s > 8) & 0xff; //wrong: Invalid attempt to get the 8 most significant
|
||||
// bits of a short.
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
if (0 || condition) { //wrong: can be converted to just 'condition'
|
||||
//...
|
||||
}
|
||||
|
||||
if (0 && condition) { //wrong: always evaluates to false, if statement can be removed
|
||||
// ...
|
||||
}
|
||||
|
||||
if ('A' == 65 && condition) { // wrong: can be converted to just 'condition'
|
||||
// ...
|
||||
}
|
||||
if (0 || condition) { //wrong: can be converted to just 'condition'
|
||||
//...
|
||||
}
|
||||
|
||||
if (0 && condition) { //wrong: always evaluates to false, if statement can be removed
|
||||
// ...
|
||||
}
|
||||
|
||||
if ('A' == 65 && condition) { // wrong: can be converted to just 'condition'
|
||||
// ...
|
||||
}
|
||||
|
||||
@@ -1,30 +1,30 @@
|
||||
typedef enum {
|
||||
RED,
|
||||
ORANGE,
|
||||
YELLOW,
|
||||
GREEN,
|
||||
BLUE,
|
||||
INDIGO,
|
||||
VIOLET
|
||||
} colors;
|
||||
|
||||
int f(colors c) {
|
||||
switch (c) {
|
||||
case RED:
|
||||
//...
|
||||
case GREEN:
|
||||
//...
|
||||
case BLUE:
|
||||
//...
|
||||
//wrong: does not use all enum values, and has no default
|
||||
}
|
||||
|
||||
switch(c) {
|
||||
case RED:
|
||||
//...
|
||||
case GREEN:
|
||||
//...
|
||||
default:
|
||||
//correct: does not use all enum values, but has a default
|
||||
}
|
||||
}
|
||||
typedef enum {
|
||||
RED,
|
||||
ORANGE,
|
||||
YELLOW,
|
||||
GREEN,
|
||||
BLUE,
|
||||
INDIGO,
|
||||
VIOLET
|
||||
} colors;
|
||||
|
||||
int f(colors c) {
|
||||
switch (c) {
|
||||
case RED:
|
||||
//...
|
||||
case GREEN:
|
||||
//...
|
||||
case BLUE:
|
||||
//...
|
||||
//wrong: does not use all enum values, and has no default
|
||||
}
|
||||
|
||||
switch(c) {
|
||||
case RED:
|
||||
//...
|
||||
case GREEN:
|
||||
//...
|
||||
default:
|
||||
//correct: does not use all enum values, but has a default
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
void f(char* s, float f) {
|
||||
char buf[30];
|
||||
|
||||
//wrong: gets has no limit to the length of data it puts in the buffer
|
||||
gets(buf);
|
||||
|
||||
//wrong: sprintf does not limit the length of the string put into buf
|
||||
sprintf(buf, "This is a string: %s", s);
|
||||
|
||||
//wrong: %f can expand to a very long string in extreme cases, easily overrunning this buffer
|
||||
sprintf(buf, "This is a float: %f", f);
|
||||
}
|
||||
void f(char* s, float f) {
|
||||
char buf[30];
|
||||
|
||||
//wrong: gets has no limit to the length of data it puts in the buffer
|
||||
gets(buf);
|
||||
|
||||
//wrong: sprintf does not limit the length of the string put into buf
|
||||
sprintf(buf, "This is a string: %s", s);
|
||||
|
||||
//wrong: %f can expand to a very long string in extreme cases, easily overrunning this buffer
|
||||
sprintf(buf, "This is a float: %f", f);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
strncat(dest, src, strlen(dest)); //wrong: should use remaining size of dest
|
||||
|
||||
strncat(dest, src, sizeof(dest)); //wrong: should use remaining size of dest.
|
||||
//Also fails if dest is a pointer and not an array.
|
||||
strncat(dest, src, strlen(dest)); //wrong: should use remaining size of dest
|
||||
|
||||
strncat(dest, src, sizeof(dest)); //wrong: should use remaining size of dest.
|
||||
//Also fails if dest is a pointer and not an array.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
void f(char s[]) {
|
||||
int size = sizeof(s); //wrong: s is now a char*, not an array.
|
||||
//sizeof(s) will evaluate to sizeof(char *)
|
||||
}
|
||||
void f(char s[]) {
|
||||
int size = sizeof(s); //wrong: s is now a char*, not an array.
|
||||
//sizeof(s) will evaluate to sizeof(char *)
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
int x1 = 0;
|
||||
for (x1 = 0; x1 < 100; x1++) {
|
||||
int x2 = 0;
|
||||
for (x1 = 0; x1 < 300; x1++) {
|
||||
// this is most likely a typo
|
||||
// the outer loop will exit immediately
|
||||
}
|
||||
}
|
||||
|
||||
for (x1 = 0; x1 < 100; x1++) {
|
||||
if(x1 == 10 && condition) {
|
||||
for (; x1 < 75; x1++) {
|
||||
// this should be written as a while loop
|
||||
}
|
||||
}
|
||||
}
|
||||
int x1 = 0;
|
||||
for (x1 = 0; x1 < 100; x1++) {
|
||||
int x2 = 0;
|
||||
for (x1 = 0; x1 < 300; x1++) {
|
||||
// this is most likely a typo
|
||||
// the outer loop will exit immediately
|
||||
}
|
||||
}
|
||||
|
||||
for (x1 = 0; x1 < 100; x1++) {
|
||||
if(x1 == 10 && condition) {
|
||||
for (; x1 < 75; x1++) {
|
||||
// this should be written as a while loop
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,31 +1,31 @@
|
||||
class Base {
|
||||
public:
|
||||
Resource *p;
|
||||
Base() {
|
||||
p = createResource();
|
||||
}
|
||||
//...
|
||||
~Base() {
|
||||
//wrong: this destructor is non-virtual, but Base has a derived class
|
||||
// with a non-virtual destructor
|
||||
freeResource(p);
|
||||
}
|
||||
};
|
||||
|
||||
class Derived: public Base {
|
||||
public:
|
||||
Resource *dp;
|
||||
Derived() {
|
||||
dp = createResource2();
|
||||
}
|
||||
~Derived() {
|
||||
freeResource2(dp);
|
||||
}
|
||||
};
|
||||
|
||||
int f() {
|
||||
Base *b = new Derived(); //creates resources for both Base::p and Derived::dp
|
||||
//...
|
||||
delete b; //will only call Base::~Base(), leaking the resource dp.
|
||||
// Change both destructors to virtual to ensure they are both called.
|
||||
}
|
||||
class Base {
|
||||
public:
|
||||
Resource *p;
|
||||
Base() {
|
||||
p = createResource();
|
||||
}
|
||||
//...
|
||||
~Base() {
|
||||
//wrong: this destructor is non-virtual, but Base has a derived class
|
||||
// with a non-virtual destructor
|
||||
freeResource(p);
|
||||
}
|
||||
};
|
||||
|
||||
class Derived: public Base {
|
||||
public:
|
||||
Resource *dp;
|
||||
Derived() {
|
||||
dp = createResource2();
|
||||
}
|
||||
~Derived() {
|
||||
freeResource2(dp);
|
||||
}
|
||||
};
|
||||
|
||||
int f() {
|
||||
Base *b = new Derived(); //creates resources for both Base::p and Derived::dp
|
||||
//...
|
||||
delete b; //will only call Base::~Base(), leaking the resource dp.
|
||||
// Change both destructors to virtual to ensure they are both called.
|
||||
}
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
class C {
|
||||
public:
|
||||
//...
|
||||
~C(){
|
||||
if (error) {
|
||||
throw "Exception in destructor"; //wrong: exception thrown in destructor
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void f() {
|
||||
C* c = new C();
|
||||
try {
|
||||
doOperation(c);
|
||||
delete c;
|
||||
} catch ( char * do_operation_exception) {
|
||||
delete c; //would immediately terminate program if C::~C throws an exception
|
||||
}
|
||||
}
|
||||
class C {
|
||||
public:
|
||||
//...
|
||||
~C(){
|
||||
if (error) {
|
||||
throw "Exception in destructor"; //wrong: exception thrown in destructor
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void f() {
|
||||
C* c = new C();
|
||||
try {
|
||||
doOperation(c);
|
||||
delete c;
|
||||
} catch ( char * do_operation_exception) {
|
||||
delete c; //would immediately terminate program if C::~C throws an exception
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
int i = 0;
|
||||
for (i = 0; i < NUM_RECORDS; i++) {
|
||||
int j = 0;
|
||||
//This loop should have a more descriptive iteration variable
|
||||
for (j = 0; j < NUM_FIELDS; j++) {
|
||||
process(record[i]->field[j]);
|
||||
}
|
||||
|
||||
int field_idx = 0;
|
||||
//Better: the inner loop has a descriptive name
|
||||
for (field_idx = 0; field_idx < NUM_FIELDS; field_idx++) {
|
||||
save(record[i]->field[field_idx]);
|
||||
}
|
||||
int i = 0;
|
||||
for (i = 0; i < NUM_RECORDS; i++) {
|
||||
int j = 0;
|
||||
//This loop should have a more descriptive iteration variable
|
||||
for (j = 0; j < NUM_FIELDS; j++) {
|
||||
process(record[i]->field[j]);
|
||||
}
|
||||
|
||||
int field_idx = 0;
|
||||
//Better: the inner loop has a descriptive name
|
||||
for (field_idx = 0; field_idx < NUM_FIELDS; field_idx++) {
|
||||
save(record[i]->field[field_idx]);
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,28 @@
|
||||
void main(int argc, char **argv) {
|
||||
uint32_t big_num = INT32_MAX;
|
||||
char buf[big_num];
|
||||
int16_t bytes_received = 0;
|
||||
int max_get = INT16_MAX + 1;
|
||||
|
||||
// BAD: 'bytes_received' is compared with a value of a wider type.
|
||||
// 'bytes_received' overflows before reaching 'max_get',
|
||||
// causing an infinite loop
|
||||
while (bytes_received < max_get)
|
||||
bytes_received += get_from_input(buf, bytes_received);
|
||||
}
|
||||
|
||||
uint32_t bytes_received = 0;
|
||||
|
||||
// GOOD: 'bytes_received2' has a type at least as wide as 'max_get'
|
||||
while (bytes_received < max_get) {
|
||||
bytes_received += get_from_input(buf, bytes_received);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
int getFromInput(char *buf, short pos) {
|
||||
// write to buf
|
||||
// ...
|
||||
return 1;
|
||||
}
|
||||
void main(int argc, char **argv) {
|
||||
uint32_t big_num = INT32_MAX;
|
||||
char buf[big_num];
|
||||
int16_t bytes_received = 0;
|
||||
int max_get = INT16_MAX + 1;
|
||||
|
||||
// BAD: 'bytes_received' is compared with a value of a wider type.
|
||||
// 'bytes_received' overflows before reaching 'max_get',
|
||||
// causing an infinite loop
|
||||
while (bytes_received < max_get)
|
||||
bytes_received += get_from_input(buf, bytes_received);
|
||||
}
|
||||
|
||||
uint32_t bytes_received = 0;
|
||||
|
||||
// GOOD: 'bytes_received2' has a type at least as wide as 'max_get'
|
||||
while (bytes_received < max_get) {
|
||||
bytes_received += get_from_input(buf, bytes_received);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
int getFromInput(char *buf, short pos) {
|
||||
// write to buf
|
||||
// ...
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
enum Shape_color { red, green, blue };
|
||||
class Shape
|
||||
{
|
||||
public:
|
||||
virtual void draw (Shape_color color = green) const;
|
||||
...
|
||||
}
|
||||
class Circle : public Shape
|
||||
{
|
||||
public:
|
||||
virtual void draw (Shape_color color = red) const;
|
||||
...
|
||||
}
|
||||
void fun()
|
||||
{
|
||||
Shape* sp;
|
||||
|
||||
sp = new Circle;
|
||||
sp->draw (); // Invokes Circle::draw(green) even though the default
|
||||
} // parameter for Circle is red.
|
||||
enum Shape_color { red, green, blue };
|
||||
class Shape
|
||||
{
|
||||
public:
|
||||
virtual void draw (Shape_color color = green) const;
|
||||
...
|
||||
}
|
||||
class Circle : public Shape
|
||||
{
|
||||
public:
|
||||
virtual void draw (Shape_color color = red) const;
|
||||
...
|
||||
}
|
||||
void fun()
|
||||
{
|
||||
Shape* sp;
|
||||
|
||||
sp = new Circle;
|
||||
sp->draw (); // Invokes Circle::draw(green) even though the default
|
||||
} // parameter for Circle is red.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// common.h
|
||||
|
||||
#include "nameclash.h"
|
||||
|
||||
// common.h
|
||||
|
||||
#include "nameclash.h"
|
||||
|
||||
static int myArray[sizeof(MYTYPE)];
|
||||
@@ -1,3 +1,3 @@
|
||||
// defines_issue.h
|
||||
|
||||
// defines_issue.h
|
||||
|
||||
#include DEFINED_HEADER
|
||||
@@ -1,7 +1,7 @@
|
||||
// semmle-extractor-options: -I${testdir}/subdir2
|
||||
// main2.cpp
|
||||
#include "common.h"
|
||||
|
||||
#define DEFINED_HEADER "define2.h"
|
||||
|
||||
#include "defines_issue.h"
|
||||
// semmle-extractor-options: -I${testdir}/subdir2
|
||||
// main2.cpp
|
||||
#include "common.h"
|
||||
|
||||
#define DEFINED_HEADER "define2.h"
|
||||
|
||||
#include "defines_issue.h"
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
// nameclash.h
|
||||
|
||||
// nameclash.h
|
||||
|
||||
#include_next "nameclash.h"
|
||||
@@ -1,3 +1,3 @@
|
||||
// subdir1/nameclash.h
|
||||
|
||||
// subdir1/nameclash.h
|
||||
|
||||
typedef long long int MYTYPE;
|
||||
@@ -1,3 +1,3 @@
|
||||
// subdir2/nameclash.h
|
||||
|
||||
// subdir2/nameclash.h
|
||||
|
||||
typedef char MYTYPE;
|
||||
@@ -1,111 +1,111 @@
|
||||
// semmle-extractor-options: -std=c++17
|
||||
typedef unsigned long size_t;
|
||||
namespace std {
|
||||
enum class align_val_t : size_t {};
|
||||
}
|
||||
|
||||
void* operator new(size_t, float);
|
||||
void* operator new[](size_t, float);
|
||||
void* operator new(size_t, std::align_val_t, float);
|
||||
void* operator new[](size_t, std::align_val_t, float);
|
||||
void operator delete(void*, float);
|
||||
void operator delete[](void*, float);
|
||||
void operator delete(void*, std::align_val_t, float);
|
||||
void operator delete[](void*, std::align_val_t, float);
|
||||
|
||||
struct String {
|
||||
String();
|
||||
String(const String&);
|
||||
String(String&&);
|
||||
String(const char*);
|
||||
~String();
|
||||
|
||||
String& operator=(const String&);
|
||||
String& operator=(String&&);
|
||||
|
||||
const char* c_str() const;
|
||||
|
||||
private:
|
||||
const char* p;
|
||||
};
|
||||
|
||||
struct SizedDealloc {
|
||||
char a[32];
|
||||
void* operator new(size_t);
|
||||
void* operator new[](size_t);
|
||||
void operator delete(void*, size_t);
|
||||
void operator delete[](void*, size_t);
|
||||
};
|
||||
|
||||
struct alignas(128) Overaligned {
|
||||
char a[256];
|
||||
};
|
||||
|
||||
struct PolymorphicBase {
|
||||
virtual ~PolymorphicBase();
|
||||
};
|
||||
|
||||
void OperatorNew() {
|
||||
new int; // No constructor
|
||||
new(1.0f) int; // Placement new, no constructor
|
||||
new int(); // Zero-init
|
||||
new String(); // Constructor
|
||||
new(1.0f) String("hello"); // Placement new, constructor with args
|
||||
new Overaligned; // Aligned new
|
||||
new(1.0f) Overaligned(); // Placement aligned new
|
||||
}
|
||||
|
||||
void OperatorDelete() {
|
||||
delete static_cast<int*>(nullptr); // No destructor
|
||||
delete static_cast<String*>(nullptr); // Non-virtual destructor, with size.
|
||||
delete static_cast<SizedDealloc*>(nullptr); // No destructor, with size.
|
||||
delete static_cast<Overaligned*>(nullptr); // No destructor, with size and alignment.
|
||||
delete static_cast<PolymorphicBase*>(nullptr); // Virtual destructor
|
||||
delete static_cast<const String*>(nullptr); // Pointer to const
|
||||
}
|
||||
|
||||
void OperatorNewArray(int n) {
|
||||
new int[n]; // No constructor
|
||||
new(1.0f) int[n]; // Placement new, no constructor
|
||||
new String[n]; // Constructor
|
||||
new Overaligned[n]; // Aligned new
|
||||
new String[10]; // Constant size
|
||||
}
|
||||
|
||||
int* const GetPointer();
|
||||
|
||||
void OperatorDeleteArray() {
|
||||
delete[] static_cast<int*>(nullptr); // No destructor
|
||||
delete[] static_cast<String*>(nullptr); // Non-virtual destructor, with size.
|
||||
delete[] static_cast<SizedDealloc*>(nullptr); // No destructor, with size.
|
||||
delete[] static_cast<Overaligned*>(nullptr); // No destructor, with size and alignment.
|
||||
delete[] static_cast<PolymorphicBase*>(nullptr); // Virtual destructor
|
||||
delete[] GetPointer();
|
||||
}
|
||||
|
||||
struct FailedInit {
|
||||
FailedInit();
|
||||
~FailedInit();
|
||||
|
||||
void* operator new(size_t); // Non-placement
|
||||
void* operator new[](size_t); // Non-placement
|
||||
void operator delete(void*, size_t); // Sized deallocation
|
||||
void operator delete[](void*, size_t); // Sized deallocation
|
||||
};
|
||||
|
||||
struct alignas(128) FailedInitOveraligned {
|
||||
FailedInitOveraligned();
|
||||
~FailedInitOveraligned();
|
||||
|
||||
void* operator new(size_t, std::align_val_t, float); // Aligned placement
|
||||
void* operator new[](size_t, std::align_val_t, float); // Aligned placement
|
||||
void operator delete(void*, std::align_val_t, float); // Aligned placement
|
||||
void operator delete[](void*, std::align_val_t, float); // Aligned placement
|
||||
};
|
||||
|
||||
void TestFailedInit(int n) {
|
||||
new FailedInit();
|
||||
new FailedInit[n];
|
||||
new(1.0f) FailedInitOveraligned();
|
||||
new(1.0f) FailedInitOveraligned[10];
|
||||
}
|
||||
// semmle-extractor-options: -std=c++17
|
||||
typedef unsigned long size_t;
|
||||
namespace std {
|
||||
enum class align_val_t : size_t {};
|
||||
}
|
||||
|
||||
void* operator new(size_t, float);
|
||||
void* operator new[](size_t, float);
|
||||
void* operator new(size_t, std::align_val_t, float);
|
||||
void* operator new[](size_t, std::align_val_t, float);
|
||||
void operator delete(void*, float);
|
||||
void operator delete[](void*, float);
|
||||
void operator delete(void*, std::align_val_t, float);
|
||||
void operator delete[](void*, std::align_val_t, float);
|
||||
|
||||
struct String {
|
||||
String();
|
||||
String(const String&);
|
||||
String(String&&);
|
||||
String(const char*);
|
||||
~String();
|
||||
|
||||
String& operator=(const String&);
|
||||
String& operator=(String&&);
|
||||
|
||||
const char* c_str() const;
|
||||
|
||||
private:
|
||||
const char* p;
|
||||
};
|
||||
|
||||
struct SizedDealloc {
|
||||
char a[32];
|
||||
void* operator new(size_t);
|
||||
void* operator new[](size_t);
|
||||
void operator delete(void*, size_t);
|
||||
void operator delete[](void*, size_t);
|
||||
};
|
||||
|
||||
struct alignas(128) Overaligned {
|
||||
char a[256];
|
||||
};
|
||||
|
||||
struct PolymorphicBase {
|
||||
virtual ~PolymorphicBase();
|
||||
};
|
||||
|
||||
void OperatorNew() {
|
||||
new int; // No constructor
|
||||
new(1.0f) int; // Placement new, no constructor
|
||||
new int(); // Zero-init
|
||||
new String(); // Constructor
|
||||
new(1.0f) String("hello"); // Placement new, constructor with args
|
||||
new Overaligned; // Aligned new
|
||||
new(1.0f) Overaligned(); // Placement aligned new
|
||||
}
|
||||
|
||||
void OperatorDelete() {
|
||||
delete static_cast<int*>(nullptr); // No destructor
|
||||
delete static_cast<String*>(nullptr); // Non-virtual destructor, with size.
|
||||
delete static_cast<SizedDealloc*>(nullptr); // No destructor, with size.
|
||||
delete static_cast<Overaligned*>(nullptr); // No destructor, with size and alignment.
|
||||
delete static_cast<PolymorphicBase*>(nullptr); // Virtual destructor
|
||||
delete static_cast<const String*>(nullptr); // Pointer to const
|
||||
}
|
||||
|
||||
void OperatorNewArray(int n) {
|
||||
new int[n]; // No constructor
|
||||
new(1.0f) int[n]; // Placement new, no constructor
|
||||
new String[n]; // Constructor
|
||||
new Overaligned[n]; // Aligned new
|
||||
new String[10]; // Constant size
|
||||
}
|
||||
|
||||
int* const GetPointer();
|
||||
|
||||
void OperatorDeleteArray() {
|
||||
delete[] static_cast<int*>(nullptr); // No destructor
|
||||
delete[] static_cast<String*>(nullptr); // Non-virtual destructor, with size.
|
||||
delete[] static_cast<SizedDealloc*>(nullptr); // No destructor, with size.
|
||||
delete[] static_cast<Overaligned*>(nullptr); // No destructor, with size and alignment.
|
||||
delete[] static_cast<PolymorphicBase*>(nullptr); // Virtual destructor
|
||||
delete[] GetPointer();
|
||||
}
|
||||
|
||||
struct FailedInit {
|
||||
FailedInit();
|
||||
~FailedInit();
|
||||
|
||||
void* operator new(size_t); // Non-placement
|
||||
void* operator new[](size_t); // Non-placement
|
||||
void operator delete(void*, size_t); // Sized deallocation
|
||||
void operator delete[](void*, size_t); // Sized deallocation
|
||||
};
|
||||
|
||||
struct alignas(128) FailedInitOveraligned {
|
||||
FailedInitOveraligned();
|
||||
~FailedInitOveraligned();
|
||||
|
||||
void* operator new(size_t, std::align_val_t, float); // Aligned placement
|
||||
void* operator new[](size_t, std::align_val_t, float); // Aligned placement
|
||||
void operator delete(void*, std::align_val_t, float); // Aligned placement
|
||||
void operator delete[](void*, std::align_val_t, float); // Aligned placement
|
||||
};
|
||||
|
||||
void TestFailedInit(int n) {
|
||||
new FailedInit();
|
||||
new FailedInit[n];
|
||||
new(1.0f) FailedInitOveraligned();
|
||||
new(1.0f) FailedInitOveraligned[10];
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "calls1.h"
|
||||
|
||||
void swap(int* p, int* q)
|
||||
{
|
||||
int t = *p;
|
||||
*p = *q;
|
||||
*q = t;
|
||||
}
|
||||
#include "calls1.h"
|
||||
|
||||
void swap(int* p, int* q)
|
||||
{
|
||||
int t = *p;
|
||||
*p = *q;
|
||||
*q = t;
|
||||
}
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
|
||||
extern void swap(int*, int*);
|
||||
|
||||
extern void swap(int*, int*);
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
#include "calls1.h"
|
||||
|
||||
void g() {
|
||||
int x = 2;
|
||||
int y = 4;
|
||||
swap(&x,&y);
|
||||
}
|
||||
|
||||
void negate(int& c) { c = -c; }
|
||||
|
||||
template<class Iter, class Fct> void compute(Iter b, Fct f)
|
||||
{
|
||||
f(b);
|
||||
}
|
||||
|
||||
void f(int aa)
|
||||
{
|
||||
compute(aa, negate);
|
||||
}
|
||||
|
||||
#include "calls1.h"
|
||||
|
||||
void g() {
|
||||
int x = 2;
|
||||
int y = 4;
|
||||
swap(&x,&y);
|
||||
}
|
||||
|
||||
void negate(int& c) { c = -c; }
|
||||
|
||||
template<class Iter, class Fct> void compute(Iter b, Fct f)
|
||||
{
|
||||
f(b);
|
||||
}
|
||||
|
||||
void f(int aa)
|
||||
{
|
||||
compute(aa, negate);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,41 +1,41 @@
|
||||
class A {
|
||||
|
||||
};
|
||||
|
||||
|
||||
class B {
|
||||
|
||||
};
|
||||
|
||||
class C: A, B {
|
||||
|
||||
};
|
||||
|
||||
class D: C {
|
||||
|
||||
class E;
|
||||
|
||||
};
|
||||
|
||||
class D::E {
|
||||
|
||||
class F;
|
||||
|
||||
class G {
|
||||
public:
|
||||
/* Non-trivial constructor and destructor */
|
||||
G() { 0; }
|
||||
~G() { 0; }
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
class D::E::F: D::E::G {
|
||||
/* Should have generated constructor and destructor, because of D::E::G */
|
||||
|
||||
static int m() {
|
||||
D::E::F def; /* Should trigger creation of D::E::F's generated constructor and destructor. */
|
||||
}
|
||||
};
|
||||
|
||||
// semmle-extractor-options: --microsoft
|
||||
class A {
|
||||
|
||||
};
|
||||
|
||||
|
||||
class B {
|
||||
|
||||
};
|
||||
|
||||
class C: A, B {
|
||||
|
||||
};
|
||||
|
||||
class D: C {
|
||||
|
||||
class E;
|
||||
|
||||
};
|
||||
|
||||
class D::E {
|
||||
|
||||
class F;
|
||||
|
||||
class G {
|
||||
public:
|
||||
/* Non-trivial constructor and destructor */
|
||||
G() { 0; }
|
||||
~G() { 0; }
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
class D::E::F: D::E::G {
|
||||
/* Should have generated constructor and destructor, because of D::E::G */
|
||||
|
||||
static int m() {
|
||||
D::E::F def; /* Should trigger creation of D::E::F's generated constructor and destructor. */
|
||||
}
|
||||
};
|
||||
|
||||
// semmle-extractor-options: --microsoft
|
||||
|
||||
@@ -1,84 +1,84 @@
|
||||
struct Base {
|
||||
int b1;
|
||||
float b1f;
|
||||
};
|
||||
|
||||
struct SingleInheritance : Base {
|
||||
int si1;
|
||||
float si1f;
|
||||
};
|
||||
|
||||
struct Base2 {
|
||||
int b2;
|
||||
float b2f;
|
||||
};
|
||||
|
||||
struct MultipleInheritance : Base, Base2 {
|
||||
int mi1;
|
||||
float mi1f;
|
||||
};
|
||||
|
||||
struct DeepInheritance : MultipleInheritance, SingleInheritance {
|
||||
int di1;
|
||||
float di1f;
|
||||
};
|
||||
|
||||
struct VirtualInheritance1 : virtual Base {
|
||||
int vi1;
|
||||
float vi1f;
|
||||
};
|
||||
|
||||
struct VirtualInheritance2 : VirtualInheritance1, virtual Base, virtual Base2 {
|
||||
int vi2;
|
||||
float vi2f;
|
||||
};
|
||||
|
||||
struct EffectivelyVirtual : virtual SingleInheritance, MultipleInheritance {
|
||||
int ev1;
|
||||
float ev1f;
|
||||
};
|
||||
|
||||
struct PolymorphicBase {
|
||||
virtual ~PolymorphicBase();
|
||||
int pb1;
|
||||
float pb1f;
|
||||
};
|
||||
|
||||
struct InheritsVTable : PolymorphicBase {
|
||||
int iv1;
|
||||
float iv1f;
|
||||
};
|
||||
|
||||
struct IntroducesVTable : Base {
|
||||
virtual ~IntroducesVTable();
|
||||
int iv2;
|
||||
float iv2f;
|
||||
};
|
||||
|
||||
struct Left : virtual Base {
|
||||
int l1;
|
||||
float l1f;
|
||||
};
|
||||
|
||||
struct Right : virtual Base {
|
||||
int r1;
|
||||
float r1f;
|
||||
};
|
||||
|
||||
struct Bottom : Left, Right {
|
||||
int b1;
|
||||
float b1f;
|
||||
};
|
||||
|
||||
struct DeepSingleInheritance : SingleInheritance {
|
||||
int dsi1;
|
||||
float dsi1f;
|
||||
};
|
||||
|
||||
struct Incomplete;
|
||||
Incomplete* p;
|
||||
|
||||
template<typename T>
|
||||
struct TemplateClass : Base
|
||||
{
|
||||
};
|
||||
struct Base {
|
||||
int b1;
|
||||
float b1f;
|
||||
};
|
||||
|
||||
struct SingleInheritance : Base {
|
||||
int si1;
|
||||
float si1f;
|
||||
};
|
||||
|
||||
struct Base2 {
|
||||
int b2;
|
||||
float b2f;
|
||||
};
|
||||
|
||||
struct MultipleInheritance : Base, Base2 {
|
||||
int mi1;
|
||||
float mi1f;
|
||||
};
|
||||
|
||||
struct DeepInheritance : MultipleInheritance, SingleInheritance {
|
||||
int di1;
|
||||
float di1f;
|
||||
};
|
||||
|
||||
struct VirtualInheritance1 : virtual Base {
|
||||
int vi1;
|
||||
float vi1f;
|
||||
};
|
||||
|
||||
struct VirtualInheritance2 : VirtualInheritance1, virtual Base, virtual Base2 {
|
||||
int vi2;
|
||||
float vi2f;
|
||||
};
|
||||
|
||||
struct EffectivelyVirtual : virtual SingleInheritance, MultipleInheritance {
|
||||
int ev1;
|
||||
float ev1f;
|
||||
};
|
||||
|
||||
struct PolymorphicBase {
|
||||
virtual ~PolymorphicBase();
|
||||
int pb1;
|
||||
float pb1f;
|
||||
};
|
||||
|
||||
struct InheritsVTable : PolymorphicBase {
|
||||
int iv1;
|
||||
float iv1f;
|
||||
};
|
||||
|
||||
struct IntroducesVTable : Base {
|
||||
virtual ~IntroducesVTable();
|
||||
int iv2;
|
||||
float iv2f;
|
||||
};
|
||||
|
||||
struct Left : virtual Base {
|
||||
int l1;
|
||||
float l1f;
|
||||
};
|
||||
|
||||
struct Right : virtual Base {
|
||||
int r1;
|
||||
float r1f;
|
||||
};
|
||||
|
||||
struct Bottom : Left, Right {
|
||||
int b1;
|
||||
float b1f;
|
||||
};
|
||||
|
||||
struct DeepSingleInheritance : SingleInheritance {
|
||||
int dsi1;
|
||||
float dsi1f;
|
||||
};
|
||||
|
||||
struct Incomplete;
|
||||
Incomplete* p;
|
||||
|
||||
template<typename T>
|
||||
struct TemplateClass : Base
|
||||
{
|
||||
};
|
||||
|
||||
@@ -1,102 +1,102 @@
|
||||
// GOOD = at least one iteration
|
||||
// BAD = possibly no iterations
|
||||
|
||||
void test1() {
|
||||
for (int i = 0; i < 10; i++) { // GOOD
|
||||
}
|
||||
}
|
||||
|
||||
void test2() {
|
||||
for (int i = 0, j = 1; i + j < 10; i++) { // GOOD
|
||||
}
|
||||
}
|
||||
|
||||
void test3() {
|
||||
int j = 2;
|
||||
for (int i = j = 1; i + j < 10; i++) { // GOOD
|
||||
}
|
||||
}
|
||||
|
||||
void test4() {
|
||||
int i = 2, j = 3;
|
||||
for (i = j = 1; i + j < 10; i++) { // GOOD
|
||||
}
|
||||
}
|
||||
|
||||
void test5() {
|
||||
int i, k;
|
||||
for (i = k = 0; i < 10; i++) { // GOOD
|
||||
}
|
||||
}
|
||||
|
||||
void test6() {
|
||||
int i = 0;
|
||||
for (; i < 10; i++) { // GOOD
|
||||
i = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void test7() {
|
||||
int i = 0;
|
||||
for (i = 1; i < 10; i++) { // GOOD
|
||||
i = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void test8() {
|
||||
int i = 0;
|
||||
i = 1;
|
||||
for (; i < 10; i++) { // GOOD (NOT REPORTED)
|
||||
}
|
||||
}
|
||||
|
||||
void test9() {
|
||||
bool done = false;
|
||||
for (; !done; ) { // GOOD
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
|
||||
void test10(int i) {
|
||||
bool done = false;
|
||||
for (; i++; i < 10) { // BAD
|
||||
for (; !done; ) { // BAD
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void test11(int i) {
|
||||
for (; i++; i < 10) { // BAD
|
||||
bool done = false;
|
||||
for (; !done; ) { // GOOD
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void test12(int max) {
|
||||
int i, k;
|
||||
int max_index = 0;
|
||||
for (i = k = 0; i < max; i++) { // BAD
|
||||
max_index = i;
|
||||
}
|
||||
for (i = 0; i <= max_index; i++) { // BAD
|
||||
}
|
||||
}
|
||||
|
||||
void test13() {
|
||||
int i;
|
||||
for (i = 1; i > 0; ) { // GOOD
|
||||
&i;
|
||||
}
|
||||
}
|
||||
|
||||
void test14(bool b) {
|
||||
int i = 1;
|
||||
while (b) {
|
||||
for (; i > 0; ) { // BAD
|
||||
&i;
|
||||
}
|
||||
}
|
||||
// GOOD = at least one iteration
|
||||
// BAD = possibly no iterations
|
||||
|
||||
void test1() {
|
||||
for (int i = 0; i < 10; i++) { // GOOD
|
||||
}
|
||||
}
|
||||
|
||||
void test2() {
|
||||
for (int i = 0, j = 1; i + j < 10; i++) { // GOOD
|
||||
}
|
||||
}
|
||||
|
||||
void test3() {
|
||||
int j = 2;
|
||||
for (int i = j = 1; i + j < 10; i++) { // GOOD
|
||||
}
|
||||
}
|
||||
|
||||
void test4() {
|
||||
int i = 2, j = 3;
|
||||
for (i = j = 1; i + j < 10; i++) { // GOOD
|
||||
}
|
||||
}
|
||||
|
||||
void test5() {
|
||||
int i, k;
|
||||
for (i = k = 0; i < 10; i++) { // GOOD
|
||||
}
|
||||
}
|
||||
|
||||
void test6() {
|
||||
int i = 0;
|
||||
for (; i < 10; i++) { // GOOD
|
||||
i = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void test7() {
|
||||
int i = 0;
|
||||
for (i = 1; i < 10; i++) { // GOOD
|
||||
i = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void test8() {
|
||||
int i = 0;
|
||||
i = 1;
|
||||
for (; i < 10; i++) { // GOOD (NOT REPORTED)
|
||||
}
|
||||
}
|
||||
|
||||
void test9() {
|
||||
bool done = false;
|
||||
for (; !done; ) { // GOOD
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
|
||||
void test10(int i) {
|
||||
bool done = false;
|
||||
for (; i++; i < 10) { // BAD
|
||||
for (; !done; ) { // BAD
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void test11(int i) {
|
||||
for (; i++; i < 10) { // BAD
|
||||
bool done = false;
|
||||
for (; !done; ) { // GOOD
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void test12(int max) {
|
||||
int i, k;
|
||||
int max_index = 0;
|
||||
for (i = k = 0; i < max; i++) { // BAD
|
||||
max_index = i;
|
||||
}
|
||||
for (i = 0; i <= max_index; i++) { // BAD
|
||||
}
|
||||
}
|
||||
|
||||
void test13() {
|
||||
int i;
|
||||
for (i = 1; i > 0; ) { // GOOD
|
||||
&i;
|
||||
}
|
||||
}
|
||||
|
||||
void test14(bool b) {
|
||||
int i = 1;
|
||||
while (b) {
|
||||
for (; i > 0; ) { // BAD
|
||||
&i;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,64 +1,64 @@
|
||||
// GOOD = at least one iteration
|
||||
// BAD = possibly no iterations
|
||||
|
||||
void test1() {
|
||||
bool done = false;
|
||||
while (!done) { // GOOD
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
|
||||
void test2() {
|
||||
bool done = true;
|
||||
done = false;
|
||||
while (!done) { // GOOD (NOT REPORTED)
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
|
||||
void test3(int i) {
|
||||
bool done = false;
|
||||
for (; i++; i < 10) {
|
||||
while (!done) { // BAD
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void test4(int i) {
|
||||
for (; i++; i < 10) {
|
||||
bool done = false;
|
||||
while (!done) { // GOOD
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void test5(int max) {
|
||||
int i = 0, k = 0;
|
||||
int max_index = 0;
|
||||
while (i < max) { // BAD
|
||||
max_index = i;
|
||||
i++;
|
||||
}
|
||||
i = 0;
|
||||
while (i <= max_index) { // BAD
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void test6() {
|
||||
int i = 1;
|
||||
while (i > 0) { // GOOD
|
||||
&i;
|
||||
}
|
||||
}
|
||||
|
||||
void test7(bool b) {
|
||||
int i = 1;
|
||||
while (b) { // BAD
|
||||
while (i > 0) { // BAD
|
||||
&i;
|
||||
}
|
||||
}
|
||||
// GOOD = at least one iteration
|
||||
// BAD = possibly no iterations
|
||||
|
||||
void test1() {
|
||||
bool done = false;
|
||||
while (!done) { // GOOD
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
|
||||
void test2() {
|
||||
bool done = true;
|
||||
done = false;
|
||||
while (!done) { // GOOD (NOT REPORTED)
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
|
||||
void test3(int i) {
|
||||
bool done = false;
|
||||
for (; i++; i < 10) {
|
||||
while (!done) { // BAD
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void test4(int i) {
|
||||
for (; i++; i < 10) {
|
||||
bool done = false;
|
||||
while (!done) { // GOOD
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void test5(int max) {
|
||||
int i = 0, k = 0;
|
||||
int max_index = 0;
|
||||
while (i < max) { // BAD
|
||||
max_index = i;
|
||||
i++;
|
||||
}
|
||||
i = 0;
|
||||
while (i <= max_index) { // BAD
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void test6() {
|
||||
int i = 1;
|
||||
while (i > 0) { // GOOD
|
||||
&i;
|
||||
}
|
||||
}
|
||||
|
||||
void test7(bool b) {
|
||||
int i = 1;
|
||||
while (b) { // BAD
|
||||
while (i > 0) { // BAD
|
||||
&i;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,250 +1,250 @@
|
||||
typedef unsigned short ushort;
|
||||
|
||||
enum E {
|
||||
E0,
|
||||
E1
|
||||
};
|
||||
|
||||
enum class EC : int {
|
||||
EC0,
|
||||
EC1
|
||||
};
|
||||
|
||||
void ArithmeticConversions() {
|
||||
char c = 0;
|
||||
unsigned char uc = 0;
|
||||
short s = 0;
|
||||
unsigned short us = 0;
|
||||
int i = 0;
|
||||
unsigned int ui = 0;
|
||||
long l = 0;
|
||||
unsigned long ul = 0;
|
||||
long long ll = 0;
|
||||
unsigned long long ull = 0;
|
||||
float f = 0;
|
||||
double d = 0;
|
||||
wchar_t wc = 0;
|
||||
E e{};
|
||||
EC ec{};
|
||||
|
||||
c = uc;
|
||||
c = (char)uc;
|
||||
c = char(uc);
|
||||
c = static_cast<char>(uc);
|
||||
i = s;
|
||||
i = (int)s;
|
||||
i = int(s);
|
||||
i = static_cast<int>(s);
|
||||
us = i;
|
||||
us = (unsigned short)i;
|
||||
us = ushort(i);
|
||||
us = static_cast<unsigned short>(i);
|
||||
|
||||
i = d;
|
||||
i = (int)d;
|
||||
i = int(d);
|
||||
i = static_cast<int>(d);
|
||||
|
||||
f = c;
|
||||
f = (float)c;
|
||||
f = float(c);
|
||||
f = static_cast<float>(c);
|
||||
|
||||
f = d;
|
||||
f = (float)d;
|
||||
f = float(d);
|
||||
f = static_cast<float>(d);
|
||||
|
||||
d = f;
|
||||
d = (double)f;
|
||||
d = double(f);
|
||||
d = static_cast<double>(f);
|
||||
|
||||
i = E0;
|
||||
i = e;
|
||||
i = static_cast<int>(EC::EC0);
|
||||
i = static_cast<int>(ec);
|
||||
e = static_cast<E>(i);
|
||||
ec = static_cast<EC>(i);
|
||||
}
|
||||
|
||||
struct S {
|
||||
int x;
|
||||
double y;
|
||||
};
|
||||
|
||||
void ConversionsToBool() {
|
||||
bool b = 0;
|
||||
int i = 0;
|
||||
double d = 0;
|
||||
void* p = nullptr;
|
||||
int S::* pmd = nullptr;
|
||||
|
||||
if (b) {
|
||||
}
|
||||
else if ((bool)b) {
|
||||
}
|
||||
else if (i) {
|
||||
}
|
||||
else if (d) {
|
||||
}
|
||||
else if (p) {
|
||||
}
|
||||
else if (pmd) {
|
||||
}
|
||||
}
|
||||
|
||||
struct Base {
|
||||
int b1;
|
||||
void BaseMethod();
|
||||
};
|
||||
|
||||
struct Middle : Base {
|
||||
int m1;
|
||||
void MiddleMethod();
|
||||
};
|
||||
|
||||
struct Derived : Middle {
|
||||
int d1;
|
||||
void DerivedMethod();
|
||||
};
|
||||
|
||||
void HierarchyCasts() {
|
||||
Base b;
|
||||
Middle m;
|
||||
Derived d;
|
||||
|
||||
Base* pb = &b;
|
||||
Middle* pm = &m;
|
||||
Derived* pd = &d;
|
||||
|
||||
b = m;
|
||||
b = (Base)m;
|
||||
b = static_cast<Base>(m);
|
||||
pb = pm;
|
||||
pb = (Base*)pm;
|
||||
pb = static_cast<Base*>(pm);
|
||||
pb = reinterpret_cast<Base*>(pm);
|
||||
|
||||
m = (Middle&)b;
|
||||
m = static_cast<Middle&>(b);
|
||||
pm = (Middle*)pb;
|
||||
pm = static_cast<Middle*>(pb);
|
||||
pm = reinterpret_cast<Middle*>(pb);
|
||||
|
||||
b = d;
|
||||
b = (Base)d;
|
||||
b = static_cast<Base>(d);
|
||||
pb = pd;
|
||||
pb = (Base*)pd;
|
||||
pb = static_cast<Base*>(pd);
|
||||
pb = reinterpret_cast<Base*>(pd);
|
||||
|
||||
d = (Derived&)b;
|
||||
d = static_cast<Derived&>(b);
|
||||
pd = (Derived*)pb;
|
||||
pd = static_cast<Derived*>(pb);
|
||||
pd = reinterpret_cast<Derived*>(pb);
|
||||
}
|
||||
|
||||
void PTMCasts() {
|
||||
int Base::* pb = &Base::b1;
|
||||
void (Base::* pmfb)() = &Base::BaseMethod;
|
||||
int Middle::* pm = &Middle::m1;
|
||||
void (Middle::* pmfm)() = &Middle::MiddleMethod;
|
||||
int Derived::* pd = &Derived::d1;
|
||||
void (Derived::* pmfd)() = &Derived::DerivedMethod;
|
||||
|
||||
pb = (int Base::*)pm;
|
||||
pmfb = (void (Base::*)())pmfm;
|
||||
pb = static_cast<int Base::*>(pm);
|
||||
pmfb = static_cast<void (Base::*)()>(pmfm);
|
||||
|
||||
pm = pb;
|
||||
pmfm = pmfb;
|
||||
pm = (int Middle::*)pb;
|
||||
pmfm = (void (Middle::*)())pmfb;
|
||||
pm = static_cast<int Middle::*>(pb);
|
||||
pmfm = static_cast<void (Middle::*)()>(pmfb);
|
||||
|
||||
pb = (int Base::*)pd;
|
||||
pmfb = (void (Base::*)())pmfd;
|
||||
pb = static_cast<int Base::*>(pd);
|
||||
pmfb = static_cast<void (Base::*)()>(pmfd);
|
||||
|
||||
pd = pb;
|
||||
pmfd = pmfb;
|
||||
pd = (int Derived::*)pb;
|
||||
pmfd = (void (Derived::*)())pmfb;
|
||||
pd = static_cast<int Derived::*>(pb);
|
||||
pmfd = static_cast<void (Derived::*)()>(pmfb);
|
||||
}
|
||||
|
||||
struct String {
|
||||
String();
|
||||
String(const String&);
|
||||
~String();
|
||||
};
|
||||
|
||||
void Adjust() {
|
||||
const String& s1 = String(); // prvalue adjustment
|
||||
Base b;
|
||||
Derived d;
|
||||
const Base& rb = true ? b : d; // glvalue adjustment
|
||||
const Base& r = (Base&)s1;
|
||||
}
|
||||
|
||||
void QualificationConversions() {
|
||||
const int* pc = nullptr;
|
||||
const volatile int* pcv = nullptr;
|
||||
pcv = pc;
|
||||
pc = const_cast<const int*>(pcv);
|
||||
}
|
||||
|
||||
void PointerIntegralConversions() {
|
||||
void* p = nullptr;
|
||||
long n = (long)p;
|
||||
n = reinterpret_cast<long>(p);
|
||||
p = (void*)n;
|
||||
p = reinterpret_cast<void*>(n);
|
||||
}
|
||||
|
||||
struct PolymorphicBase {
|
||||
virtual ~PolymorphicBase();
|
||||
};
|
||||
|
||||
struct PolymorphicDerived : PolymorphicBase {
|
||||
};
|
||||
|
||||
void DynamicCast() {
|
||||
PolymorphicBase b;
|
||||
PolymorphicDerived d;
|
||||
|
||||
PolymorphicBase* pb = &b;
|
||||
PolymorphicDerived* pd = &d;
|
||||
|
||||
// These two casts were previously represented as BaseClassCasts because they were resolved at compile time, but the front-end no longer performs this optimization.
|
||||
pb = dynamic_cast<PolymorphicBase*>(pd);
|
||||
PolymorphicBase& rb = dynamic_cast<PolymorphicBase&>(d);
|
||||
|
||||
pd = dynamic_cast<PolymorphicDerived*>(pb);
|
||||
PolymorphicDerived& rd = dynamic_cast<PolymorphicDerived&>(b);
|
||||
}
|
||||
|
||||
void FuncPtrConversions(int(*pfn)(int), void* p) {
|
||||
p = (void*)pfn;
|
||||
pfn = (int(*)(int))p;
|
||||
}
|
||||
|
||||
int Func();
|
||||
|
||||
void ConversionsToVoid() {
|
||||
int x;
|
||||
(void)x;
|
||||
static_cast<void>(x);
|
||||
(void)Func();
|
||||
static_cast<void>(Func());
|
||||
(void)1;
|
||||
static_cast<void>(1);
|
||||
}
|
||||
|
||||
typedef unsigned short ushort;
|
||||
|
||||
enum E {
|
||||
E0,
|
||||
E1
|
||||
};
|
||||
|
||||
enum class EC : int {
|
||||
EC0,
|
||||
EC1
|
||||
};
|
||||
|
||||
void ArithmeticConversions() {
|
||||
char c = 0;
|
||||
unsigned char uc = 0;
|
||||
short s = 0;
|
||||
unsigned short us = 0;
|
||||
int i = 0;
|
||||
unsigned int ui = 0;
|
||||
long l = 0;
|
||||
unsigned long ul = 0;
|
||||
long long ll = 0;
|
||||
unsigned long long ull = 0;
|
||||
float f = 0;
|
||||
double d = 0;
|
||||
wchar_t wc = 0;
|
||||
E e{};
|
||||
EC ec{};
|
||||
|
||||
c = uc;
|
||||
c = (char)uc;
|
||||
c = char(uc);
|
||||
c = static_cast<char>(uc);
|
||||
i = s;
|
||||
i = (int)s;
|
||||
i = int(s);
|
||||
i = static_cast<int>(s);
|
||||
us = i;
|
||||
us = (unsigned short)i;
|
||||
us = ushort(i);
|
||||
us = static_cast<unsigned short>(i);
|
||||
|
||||
i = d;
|
||||
i = (int)d;
|
||||
i = int(d);
|
||||
i = static_cast<int>(d);
|
||||
|
||||
f = c;
|
||||
f = (float)c;
|
||||
f = float(c);
|
||||
f = static_cast<float>(c);
|
||||
|
||||
f = d;
|
||||
f = (float)d;
|
||||
f = float(d);
|
||||
f = static_cast<float>(d);
|
||||
|
||||
d = f;
|
||||
d = (double)f;
|
||||
d = double(f);
|
||||
d = static_cast<double>(f);
|
||||
|
||||
i = E0;
|
||||
i = e;
|
||||
i = static_cast<int>(EC::EC0);
|
||||
i = static_cast<int>(ec);
|
||||
e = static_cast<E>(i);
|
||||
ec = static_cast<EC>(i);
|
||||
}
|
||||
|
||||
struct S {
|
||||
int x;
|
||||
double y;
|
||||
};
|
||||
|
||||
void ConversionsToBool() {
|
||||
bool b = 0;
|
||||
int i = 0;
|
||||
double d = 0;
|
||||
void* p = nullptr;
|
||||
int S::* pmd = nullptr;
|
||||
|
||||
if (b) {
|
||||
}
|
||||
else if ((bool)b) {
|
||||
}
|
||||
else if (i) {
|
||||
}
|
||||
else if (d) {
|
||||
}
|
||||
else if (p) {
|
||||
}
|
||||
else if (pmd) {
|
||||
}
|
||||
}
|
||||
|
||||
struct Base {
|
||||
int b1;
|
||||
void BaseMethod();
|
||||
};
|
||||
|
||||
struct Middle : Base {
|
||||
int m1;
|
||||
void MiddleMethod();
|
||||
};
|
||||
|
||||
struct Derived : Middle {
|
||||
int d1;
|
||||
void DerivedMethod();
|
||||
};
|
||||
|
||||
void HierarchyCasts() {
|
||||
Base b;
|
||||
Middle m;
|
||||
Derived d;
|
||||
|
||||
Base* pb = &b;
|
||||
Middle* pm = &m;
|
||||
Derived* pd = &d;
|
||||
|
||||
b = m;
|
||||
b = (Base)m;
|
||||
b = static_cast<Base>(m);
|
||||
pb = pm;
|
||||
pb = (Base*)pm;
|
||||
pb = static_cast<Base*>(pm);
|
||||
pb = reinterpret_cast<Base*>(pm);
|
||||
|
||||
m = (Middle&)b;
|
||||
m = static_cast<Middle&>(b);
|
||||
pm = (Middle*)pb;
|
||||
pm = static_cast<Middle*>(pb);
|
||||
pm = reinterpret_cast<Middle*>(pb);
|
||||
|
||||
b = d;
|
||||
b = (Base)d;
|
||||
b = static_cast<Base>(d);
|
||||
pb = pd;
|
||||
pb = (Base*)pd;
|
||||
pb = static_cast<Base*>(pd);
|
||||
pb = reinterpret_cast<Base*>(pd);
|
||||
|
||||
d = (Derived&)b;
|
||||
d = static_cast<Derived&>(b);
|
||||
pd = (Derived*)pb;
|
||||
pd = static_cast<Derived*>(pb);
|
||||
pd = reinterpret_cast<Derived*>(pb);
|
||||
}
|
||||
|
||||
void PTMCasts() {
|
||||
int Base::* pb = &Base::b1;
|
||||
void (Base::* pmfb)() = &Base::BaseMethod;
|
||||
int Middle::* pm = &Middle::m1;
|
||||
void (Middle::* pmfm)() = &Middle::MiddleMethod;
|
||||
int Derived::* pd = &Derived::d1;
|
||||
void (Derived::* pmfd)() = &Derived::DerivedMethod;
|
||||
|
||||
pb = (int Base::*)pm;
|
||||
pmfb = (void (Base::*)())pmfm;
|
||||
pb = static_cast<int Base::*>(pm);
|
||||
pmfb = static_cast<void (Base::*)()>(pmfm);
|
||||
|
||||
pm = pb;
|
||||
pmfm = pmfb;
|
||||
pm = (int Middle::*)pb;
|
||||
pmfm = (void (Middle::*)())pmfb;
|
||||
pm = static_cast<int Middle::*>(pb);
|
||||
pmfm = static_cast<void (Middle::*)()>(pmfb);
|
||||
|
||||
pb = (int Base::*)pd;
|
||||
pmfb = (void (Base::*)())pmfd;
|
||||
pb = static_cast<int Base::*>(pd);
|
||||
pmfb = static_cast<void (Base::*)()>(pmfd);
|
||||
|
||||
pd = pb;
|
||||
pmfd = pmfb;
|
||||
pd = (int Derived::*)pb;
|
||||
pmfd = (void (Derived::*)())pmfb;
|
||||
pd = static_cast<int Derived::*>(pb);
|
||||
pmfd = static_cast<void (Derived::*)()>(pmfb);
|
||||
}
|
||||
|
||||
struct String {
|
||||
String();
|
||||
String(const String&);
|
||||
~String();
|
||||
};
|
||||
|
||||
void Adjust() {
|
||||
const String& s1 = String(); // prvalue adjustment
|
||||
Base b;
|
||||
Derived d;
|
||||
const Base& rb = true ? b : d; // glvalue adjustment
|
||||
const Base& r = (Base&)s1;
|
||||
}
|
||||
|
||||
void QualificationConversions() {
|
||||
const int* pc = nullptr;
|
||||
const volatile int* pcv = nullptr;
|
||||
pcv = pc;
|
||||
pc = const_cast<const int*>(pcv);
|
||||
}
|
||||
|
||||
void PointerIntegralConversions() {
|
||||
void* p = nullptr;
|
||||
long n = (long)p;
|
||||
n = reinterpret_cast<long>(p);
|
||||
p = (void*)n;
|
||||
p = reinterpret_cast<void*>(n);
|
||||
}
|
||||
|
||||
struct PolymorphicBase {
|
||||
virtual ~PolymorphicBase();
|
||||
};
|
||||
|
||||
struct PolymorphicDerived : PolymorphicBase {
|
||||
};
|
||||
|
||||
void DynamicCast() {
|
||||
PolymorphicBase b;
|
||||
PolymorphicDerived d;
|
||||
|
||||
PolymorphicBase* pb = &b;
|
||||
PolymorphicDerived* pd = &d;
|
||||
|
||||
// These two casts were previously represented as BaseClassCasts because they were resolved at compile time, but the front-end no longer performs this optimization.
|
||||
pb = dynamic_cast<PolymorphicBase*>(pd);
|
||||
PolymorphicBase& rb = dynamic_cast<PolymorphicBase&>(d);
|
||||
|
||||
pd = dynamic_cast<PolymorphicDerived*>(pb);
|
||||
PolymorphicDerived& rd = dynamic_cast<PolymorphicDerived&>(b);
|
||||
}
|
||||
|
||||
void FuncPtrConversions(int(*pfn)(int), void* p) {
|
||||
p = (void*)pfn;
|
||||
pfn = (int(*)(int))p;
|
||||
}
|
||||
|
||||
int Func();
|
||||
|
||||
void ConversionsToVoid() {
|
||||
int x;
|
||||
(void)x;
|
||||
static_cast<void>(x);
|
||||
(void)Func();
|
||||
static_cast<void>(Func());
|
||||
(void)1;
|
||||
static_cast<void>(1);
|
||||
}
|
||||
|
||||
|
||||
@@ -45,53 +45,53 @@ private:
|
||||
int xs[4];
|
||||
};
|
||||
|
||||
struct A {
|
||||
A(int);
|
||||
};
|
||||
|
||||
struct B {
|
||||
B(int);
|
||||
};
|
||||
|
||||
struct C {
|
||||
C(int);
|
||||
};
|
||||
|
||||
struct MultipleBases : A, B, C {
|
||||
int x;
|
||||
int y;
|
||||
int z;
|
||||
|
||||
MultipleBases() :
|
||||
z(5),
|
||||
B(1),
|
||||
x(3),
|
||||
A(0),
|
||||
C(2),
|
||||
y(4) {
|
||||
}
|
||||
};
|
||||
|
||||
struct VB {
|
||||
struct A {
|
||||
A(int);
|
||||
};
|
||||
|
||||
struct B {
|
||||
B(int);
|
||||
};
|
||||
|
||||
struct C {
|
||||
C(int);
|
||||
};
|
||||
|
||||
struct MultipleBases : A, B, C {
|
||||
int x;
|
||||
int y;
|
||||
int z;
|
||||
|
||||
MultipleBases() :
|
||||
z(5),
|
||||
B(1),
|
||||
x(3),
|
||||
A(0),
|
||||
C(2),
|
||||
y(4) {
|
||||
}
|
||||
};
|
||||
|
||||
struct VB {
|
||||
VB();
|
||||
VB(int);
|
||||
~VB();
|
||||
};
|
||||
|
||||
struct VD : virtual VB {
|
||||
};
|
||||
|
||||
struct VirtualAndNonVirtual : VD, VB {
|
||||
VirtualAndNonVirtual() {
|
||||
}
|
||||
~VirtualAndNonVirtual() {
|
||||
}
|
||||
};
|
||||
|
||||
struct AllYourVirtualBaseAreBelongToUs : VD, VirtualAndNonVirtual, virtual VB {
|
||||
AllYourVirtualBaseAreBelongToUs() :
|
||||
VB(5) {
|
||||
}
|
||||
~AllYourVirtualBaseAreBelongToUs() {
|
||||
}
|
||||
struct VD : virtual VB {
|
||||
};
|
||||
|
||||
struct VirtualAndNonVirtual : VD, VB {
|
||||
VirtualAndNonVirtual() {
|
||||
}
|
||||
~VirtualAndNonVirtual() {
|
||||
}
|
||||
};
|
||||
|
||||
struct AllYourVirtualBaseAreBelongToUs : VD, VirtualAndNonVirtual, virtual VB {
|
||||
AllYourVirtualBaseAreBelongToUs() :
|
||||
VB(5) {
|
||||
}
|
||||
~AllYourVirtualBaseAreBelongToUs() {
|
||||
}
|
||||
};
|
||||
@@ -1,10 +1,10 @@
|
||||
// src5.cpp
|
||||
|
||||
#include "src5.fwd.hpp"
|
||||
|
||||
template<class elem>
|
||||
class my_istream {
|
||||
};
|
||||
|
||||
template <>
|
||||
class my_istream<char>;
|
||||
// src5.cpp
|
||||
|
||||
#include "src5.fwd.hpp"
|
||||
|
||||
template<class elem>
|
||||
class my_istream {
|
||||
};
|
||||
|
||||
template <>
|
||||
class my_istream<char>;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// src5.fwd.hpp
|
||||
|
||||
#ifndef SRC5_FWD_HPP
|
||||
#define SRC5_FWD_HPP
|
||||
|
||||
template<class elem> class my_istream;
|
||||
|
||||
#endif // SRC5_FWD_HPP
|
||||
// src5.fwd.hpp
|
||||
|
||||
#ifndef SRC5_FWD_HPP
|
||||
#define SRC5_FWD_HPP
|
||||
|
||||
template<class elem> class my_istream;
|
||||
|
||||
#endif // SRC5_FWD_HPP
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// src6.cpp
|
||||
|
||||
#include "src5.fwd.hpp"
|
||||
|
||||
my_istream<char> *mis_c;
|
||||
// src6.cpp
|
||||
|
||||
#include "src5.fwd.hpp"
|
||||
|
||||
my_istream<char> *mis_c;
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
class A {
|
||||
public:
|
||||
A() {}
|
||||
};
|
||||
|
||||
A a;
|
||||
void f() {}
|
||||
|
||||
|
||||
class Test {
|
||||
A aa;
|
||||
|
||||
void fa() {}
|
||||
|
||||
void test() {
|
||||
void (*fptr)();
|
||||
void (Test::*mfptr)();
|
||||
void *ptr;
|
||||
|
||||
ptr = &a;
|
||||
ptr = &aa;
|
||||
|
||||
fptr = f; // same as below
|
||||
fptr = &f; // same as above
|
||||
mfptr = &Test::fa;
|
||||
|
||||
}
|
||||
};
|
||||
class A {
|
||||
public:
|
||||
A() {}
|
||||
};
|
||||
|
||||
A a;
|
||||
void f() {}
|
||||
|
||||
|
||||
class Test {
|
||||
A aa;
|
||||
|
||||
void fa() {}
|
||||
|
||||
void test() {
|
||||
void (*fptr)();
|
||||
void (Test::*mfptr)();
|
||||
void *ptr;
|
||||
|
||||
ptr = &a;
|
||||
ptr = &aa;
|
||||
|
||||
fptr = f; // same as below
|
||||
fptr = &f; // same as above
|
||||
mfptr = &Test::fa;
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
class Friend1 {
|
||||
public:
|
||||
void f();
|
||||
protected:
|
||||
void g();
|
||||
private:
|
||||
void h();
|
||||
};
|
||||
|
||||
class Friend2 {
|
||||
public:
|
||||
void f();
|
||||
protected:
|
||||
void g();
|
||||
private:
|
||||
void h();
|
||||
};
|
||||
|
||||
void Friend2::f() {
|
||||
}
|
||||
|
||||
void friendFunc() {}
|
||||
|
||||
class C {
|
||||
friend class Friend1;
|
||||
friend void Friend2::f();
|
||||
friend void friendFunc();
|
||||
};
|
||||
class Friend1 {
|
||||
public:
|
||||
void f();
|
||||
protected:
|
||||
void g();
|
||||
private:
|
||||
void h();
|
||||
};
|
||||
|
||||
class Friend2 {
|
||||
public:
|
||||
void f();
|
||||
protected:
|
||||
void g();
|
||||
private:
|
||||
void h();
|
||||
};
|
||||
|
||||
void Friend2::f() {
|
||||
}
|
||||
|
||||
void friendFunc() {}
|
||||
|
||||
class C {
|
||||
friend class Friend1;
|
||||
friend void Friend2::f();
|
||||
friend void friendFunc();
|
||||
};
|
||||
|
||||
@@ -1,31 +1,31 @@
|
||||
//references(UserType)
|
||||
class A {
|
||||
public:
|
||||
A() {}
|
||||
};
|
||||
|
||||
int f() {
|
||||
void *a_ptr = new A(); //match (1 call)
|
||||
A a = A(); // match (1 call)
|
||||
return 1;
|
||||
}
|
||||
|
||||
//calls(Function)
|
||||
int g() {return 0;}
|
||||
extern int h();
|
||||
|
||||
int x = g(); //match
|
||||
int y = x + g(); //match (1 call, 1 access)
|
||||
int z = x + g() + h(); //match(2 calls, 1 access)
|
||||
|
||||
//accesses(Variable)
|
||||
int i = 1;
|
||||
int j = i; //match (1 access)
|
||||
|
||||
A a; //match(1 call)
|
||||
A ax = A(); //match (1 call)
|
||||
A aax = ax; //match (1 access)
|
||||
|
||||
//array initialization
|
||||
int myIntArray[5] = {i, 0, 0, 0, 0}; //match(1 access)
|
||||
A myObjectArray[3]; //match(1 call)
|
||||
//references(UserType)
|
||||
class A {
|
||||
public:
|
||||
A() {}
|
||||
};
|
||||
|
||||
int f() {
|
||||
void *a_ptr = new A(); //match (1 call)
|
||||
A a = A(); // match (1 call)
|
||||
return 1;
|
||||
}
|
||||
|
||||
//calls(Function)
|
||||
int g() {return 0;}
|
||||
extern int h();
|
||||
|
||||
int x = g(); //match
|
||||
int y = x + g(); //match (1 call, 1 access)
|
||||
int z = x + g() + h(); //match(2 calls, 1 access)
|
||||
|
||||
//accesses(Variable)
|
||||
int i = 1;
|
||||
int j = i; //match (1 access)
|
||||
|
||||
A a; //match(1 call)
|
||||
A ax = A(); //match (1 call)
|
||||
A aax = ax; //match (1 access)
|
||||
|
||||
//array initialization
|
||||
int myIntArray[5] = {i, 0, 0, 0, 0}; //match(1 access)
|
||||
A myObjectArray[3]; //match(1 call)
|
||||
|
||||
@@ -1,89 +1,89 @@
|
||||
|
||||
const int c = 1;
|
||||
int v = 1;
|
||||
int one() {return 1;}
|
||||
|
||||
void myNormalFunction()
|
||||
{
|
||||
static int static_1 = 1;
|
||||
static int static_c = c;
|
||||
static int static_v = v;
|
||||
static int static_one = one();
|
||||
int local_1 = 1;
|
||||
int local_c = c;
|
||||
int local_v = v;
|
||||
int local_one = one();
|
||||
}
|
||||
|
||||
template<class T> void myTemplateFunction()
|
||||
{
|
||||
static int static_int_1 = 1;
|
||||
static int static_int_c = c; // [initializer is not populated]
|
||||
static int static_int_v = v; // [initializer is not populated]
|
||||
static int static_int_one = one(); // [initializer is not populated]
|
||||
static T static_t_1 = 1; // [initializer is not populated]
|
||||
static T static_t_c = c; // [initializer is not populated]
|
||||
static T static_t_v = v; // [initializer is not populated]
|
||||
static T static_t_one = one(); // [initializer is not populated]
|
||||
|
||||
int local_int_1 = 1;
|
||||
int local_int_c = c;
|
||||
int local_int_v = v;
|
||||
int local_int_one = one();
|
||||
T local_t_1 = 1;
|
||||
T local_t_c = c;
|
||||
T local_t_v = v;
|
||||
T local_t_one = one();
|
||||
}
|
||||
|
||||
template<class T> class myTemplateClass
|
||||
{
|
||||
public:
|
||||
void myMethod()
|
||||
{
|
||||
static int static_int_1 = 1;
|
||||
static int static_int_c = c; // [initializer is not populated]
|
||||
static int static_int_v = v; // [initializer is not populated]
|
||||
static int static_int_one = one(); // [initializer is not populated]
|
||||
static T static_t_1 = 1; // [initializer is not populated]
|
||||
static T static_t_c = c; // [initializer is not populated]
|
||||
static T static_t_v = v; // [initializer is not populated]
|
||||
static T static_t_one = one(); // [initializer is not populated]
|
||||
|
||||
int local_int_1 = 1;
|
||||
int local_int_c = c;
|
||||
int local_int_v = v;
|
||||
int local_int_one = one();
|
||||
T local_t_1 = 1;
|
||||
T local_t_c = c;
|
||||
T local_t_v = v;
|
||||
T local_t_one = one();
|
||||
}
|
||||
};
|
||||
|
||||
enum myEnum
|
||||
{
|
||||
MYENUM_CONST
|
||||
};
|
||||
|
||||
template<class T> void myTemplateFunction2(int a = 1, T b = 2)
|
||||
{
|
||||
static int static_int_zero = 0;
|
||||
static int static_int_ec = MYENUM_CONST;
|
||||
static int static_int_expr = v + 1;
|
||||
static int *static_int_addr = &v;
|
||||
static int static_int_sizeof_v = sizeof(v);
|
||||
static int static_int_sizeof_t = sizeof(T);
|
||||
static T static_t_zero = 0;
|
||||
static T static_t_ec = MYENUM_CONST;
|
||||
static T static_t_expr = v + 1;
|
||||
static T *static_t_addr = &v;
|
||||
static T static_t_sizeof_v = sizeof(v);
|
||||
static T static_t_sizeof_t = sizeof(T);
|
||||
|
||||
static int static_int_c1 = c;
|
||||
static int static_int_c2=c;
|
||||
{
|
||||
static int static_int_v2 = v;
|
||||
}
|
||||
}
|
||||
|
||||
const int c = 1;
|
||||
int v = 1;
|
||||
int one() {return 1;}
|
||||
|
||||
void myNormalFunction()
|
||||
{
|
||||
static int static_1 = 1;
|
||||
static int static_c = c;
|
||||
static int static_v = v;
|
||||
static int static_one = one();
|
||||
int local_1 = 1;
|
||||
int local_c = c;
|
||||
int local_v = v;
|
||||
int local_one = one();
|
||||
}
|
||||
|
||||
template<class T> void myTemplateFunction()
|
||||
{
|
||||
static int static_int_1 = 1;
|
||||
static int static_int_c = c; // [initializer is not populated]
|
||||
static int static_int_v = v; // [initializer is not populated]
|
||||
static int static_int_one = one(); // [initializer is not populated]
|
||||
static T static_t_1 = 1; // [initializer is not populated]
|
||||
static T static_t_c = c; // [initializer is not populated]
|
||||
static T static_t_v = v; // [initializer is not populated]
|
||||
static T static_t_one = one(); // [initializer is not populated]
|
||||
|
||||
int local_int_1 = 1;
|
||||
int local_int_c = c;
|
||||
int local_int_v = v;
|
||||
int local_int_one = one();
|
||||
T local_t_1 = 1;
|
||||
T local_t_c = c;
|
||||
T local_t_v = v;
|
||||
T local_t_one = one();
|
||||
}
|
||||
|
||||
template<class T> class myTemplateClass
|
||||
{
|
||||
public:
|
||||
void myMethod()
|
||||
{
|
||||
static int static_int_1 = 1;
|
||||
static int static_int_c = c; // [initializer is not populated]
|
||||
static int static_int_v = v; // [initializer is not populated]
|
||||
static int static_int_one = one(); // [initializer is not populated]
|
||||
static T static_t_1 = 1; // [initializer is not populated]
|
||||
static T static_t_c = c; // [initializer is not populated]
|
||||
static T static_t_v = v; // [initializer is not populated]
|
||||
static T static_t_one = one(); // [initializer is not populated]
|
||||
|
||||
int local_int_1 = 1;
|
||||
int local_int_c = c;
|
||||
int local_int_v = v;
|
||||
int local_int_one = one();
|
||||
T local_t_1 = 1;
|
||||
T local_t_c = c;
|
||||
T local_t_v = v;
|
||||
T local_t_one = one();
|
||||
}
|
||||
};
|
||||
|
||||
enum myEnum
|
||||
{
|
||||
MYENUM_CONST
|
||||
};
|
||||
|
||||
template<class T> void myTemplateFunction2(int a = 1, T b = 2)
|
||||
{
|
||||
static int static_int_zero = 0;
|
||||
static int static_int_ec = MYENUM_CONST;
|
||||
static int static_int_expr = v + 1;
|
||||
static int *static_int_addr = &v;
|
||||
static int static_int_sizeof_v = sizeof(v);
|
||||
static int static_int_sizeof_t = sizeof(T);
|
||||
static T static_t_zero = 0;
|
||||
static T static_t_ec = MYENUM_CONST;
|
||||
static T static_t_expr = v + 1;
|
||||
static T *static_t_addr = &v;
|
||||
static T static_t_sizeof_v = sizeof(v);
|
||||
static T static_t_sizeof_t = sizeof(T);
|
||||
|
||||
static int static_int_c1 = c;
|
||||
static int static_int_c2=c;
|
||||
{
|
||||
static int static_int_v2 = v;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,74 +1,74 @@
|
||||
namespace ns2 {
|
||||
const int c = 1;
|
||||
int v = 1;
|
||||
int one() {return 1;}
|
||||
|
||||
void myNormalFunction()
|
||||
{
|
||||
static int static_1 = 1;
|
||||
static int static_c = c;
|
||||
static int static_v = v;
|
||||
static int static_one = one();
|
||||
int local_1 = 1;
|
||||
int local_c = c;
|
||||
int local_v = v;
|
||||
int local_one = one();
|
||||
}
|
||||
|
||||
template<class T> void myTemplateFunction()
|
||||
{
|
||||
static int static_int_1 = 1;
|
||||
static int static_int_c = c; // [initializer is not populated]
|
||||
static int static_int_v = v; // [initializer is not populated]
|
||||
static int static_int_one = one(); // [initializer is not populated]
|
||||
static T static_t_1 = 1; // [initializer is not populated]
|
||||
static T static_t_c = c; // [initializer is not populated]
|
||||
static T static_t_v = v; // [initializer is not populated]
|
||||
static T static_t_one = one(); // [initializer is not populated]
|
||||
|
||||
int local_int_1 = 1;
|
||||
int local_int_c = c;
|
||||
int local_int_v = v;
|
||||
int local_int_one = one();
|
||||
T local_t_1 = 1;
|
||||
T local_t_c = c;
|
||||
T local_t_v = v;
|
||||
T local_t_one = one();
|
||||
}
|
||||
|
||||
template<class T> class myTemplateClass
|
||||
{
|
||||
public:
|
||||
void myMethod()
|
||||
{
|
||||
static int static_int_1 = 1;
|
||||
static int static_int_c = c; // [initializer is not populated]
|
||||
static int static_int_v = v; // [initializer is not populated]
|
||||
static int static_int_one = one(); // [initializer is not populated]
|
||||
static T static_t_1 = 1; // [initializer is not populated]
|
||||
static T static_t_c = c; // [initializer is not populated]
|
||||
static T static_t_v = v; // [initializer is not populated]
|
||||
static T static_t_one = one(); // [initializer is not populated]
|
||||
|
||||
int local_int_1 = 1;
|
||||
int local_int_c = c;
|
||||
int local_int_v = v;
|
||||
int local_int_one = one();
|
||||
T local_t_1 = 1;
|
||||
T local_t_c = c;
|
||||
T local_t_v = v;
|
||||
T local_t_one = one();
|
||||
}
|
||||
};
|
||||
|
||||
void testFunc()
|
||||
{
|
||||
// instantiate the templates
|
||||
myTemplateFunction<int>();
|
||||
|
||||
{
|
||||
myTemplateClass<int> mtc;
|
||||
mtc.myMethod();
|
||||
}
|
||||
}
|
||||
}
|
||||
namespace ns2 {
|
||||
const int c = 1;
|
||||
int v = 1;
|
||||
int one() {return 1;}
|
||||
|
||||
void myNormalFunction()
|
||||
{
|
||||
static int static_1 = 1;
|
||||
static int static_c = c;
|
||||
static int static_v = v;
|
||||
static int static_one = one();
|
||||
int local_1 = 1;
|
||||
int local_c = c;
|
||||
int local_v = v;
|
||||
int local_one = one();
|
||||
}
|
||||
|
||||
template<class T> void myTemplateFunction()
|
||||
{
|
||||
static int static_int_1 = 1;
|
||||
static int static_int_c = c; // [initializer is not populated]
|
||||
static int static_int_v = v; // [initializer is not populated]
|
||||
static int static_int_one = one(); // [initializer is not populated]
|
||||
static T static_t_1 = 1; // [initializer is not populated]
|
||||
static T static_t_c = c; // [initializer is not populated]
|
||||
static T static_t_v = v; // [initializer is not populated]
|
||||
static T static_t_one = one(); // [initializer is not populated]
|
||||
|
||||
int local_int_1 = 1;
|
||||
int local_int_c = c;
|
||||
int local_int_v = v;
|
||||
int local_int_one = one();
|
||||
T local_t_1 = 1;
|
||||
T local_t_c = c;
|
||||
T local_t_v = v;
|
||||
T local_t_one = one();
|
||||
}
|
||||
|
||||
template<class T> class myTemplateClass
|
||||
{
|
||||
public:
|
||||
void myMethod()
|
||||
{
|
||||
static int static_int_1 = 1;
|
||||
static int static_int_c = c; // [initializer is not populated]
|
||||
static int static_int_v = v; // [initializer is not populated]
|
||||
static int static_int_one = one(); // [initializer is not populated]
|
||||
static T static_t_1 = 1; // [initializer is not populated]
|
||||
static T static_t_c = c; // [initializer is not populated]
|
||||
static T static_t_v = v; // [initializer is not populated]
|
||||
static T static_t_one = one(); // [initializer is not populated]
|
||||
|
||||
int local_int_1 = 1;
|
||||
int local_int_c = c;
|
||||
int local_int_v = v;
|
||||
int local_int_one = one();
|
||||
T local_t_1 = 1;
|
||||
T local_t_c = c;
|
||||
T local_t_v = v;
|
||||
T local_t_one = one();
|
||||
}
|
||||
};
|
||||
|
||||
void testFunc()
|
||||
{
|
||||
// instantiate the templates
|
||||
myTemplateFunction<int>();
|
||||
|
||||
{
|
||||
myTemplateClass<int> mtc;
|
||||
mtc.myMethod();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
class C {
|
||||
C() {}
|
||||
};
|
||||
|
||||
typedef C CC;
|
||||
|
||||
CC** f() {
|
||||
return 0;
|
||||
}
|
||||
class C {
|
||||
C() {}
|
||||
};
|
||||
|
||||
typedef C CC;
|
||||
|
||||
CC** f() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
const int j = 0;
|
||||
|
||||
enum Day { sun, mon, tue, wed, thu, fri, sat };
|
||||
enum Day2 { sun2 = j, mon2, tue2 };
|
||||
enum Flag { b = 'a', c = 'b', d = 'd' };
|
||||
|
||||
Day& operator++(Day& d)
|
||||
{
|
||||
int i = d;
|
||||
Flag f = Flag(7);
|
||||
Flag g = Flag(8);
|
||||
//const int *p = &sat;
|
||||
Day2 d2 = (Day2)d;
|
||||
return d = (sat==d) ? sun: Day(d+1);
|
||||
}
|
||||
const int j = 0;
|
||||
|
||||
enum Day { sun, mon, tue, wed, thu, fri, sat };
|
||||
enum Day2 { sun2 = j, mon2, tue2 };
|
||||
enum Flag { b = 'a', c = 'b', d = 'd' };
|
||||
|
||||
Day& operator++(Day& d)
|
||||
{
|
||||
int i = d;
|
||||
Flag f = Flag(7);
|
||||
Flag g = Flag(8);
|
||||
//const int *p = &sat;
|
||||
Day2 d2 = (Day2)d;
|
||||
return d = (sat==d) ? sun: Day(d+1);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
|
||||
int main()
|
||||
{
|
||||
int x;
|
||||
|
||||
if (x == 1) {}
|
||||
if (x != 1) {}
|
||||
if (x < 1) {}
|
||||
if (x > 1) {}
|
||||
if (x <= 1) {}
|
||||
if (x >= 1) {}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int x;
|
||||
|
||||
if (x == 1) {}
|
||||
if (x != 1) {}
|
||||
if (x < 1) {}
|
||||
if (x > 1) {}
|
||||
if (x <= 1) {}
|
||||
if (x >= 1) {}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
int *ip;
|
||||
|
||||
i = +(-1);
|
||||
i++;
|
||||
ip = &i;
|
||||
*ip--;
|
||||
++i;
|
||||
--i;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
int *ip;
|
||||
|
||||
i = +(-1);
|
||||
i++;
|
||||
ip = &i;
|
||||
*ip--;
|
||||
++i;
|
||||
--i;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,155 +1,155 @@
|
||||
int ParamsAndLocals(int x)
|
||||
{
|
||||
int y;
|
||||
|
||||
// y is an lvalue, as is the result of the assignment. x is a load.
|
||||
y = x + 1;
|
||||
|
||||
// y is a load.
|
||||
return y;
|
||||
}
|
||||
|
||||
int Dereference(int* p, int *q)
|
||||
{
|
||||
// *p is an lvalue, as is the result of the assignment.
|
||||
// p, q, and *q are loads.
|
||||
*p = *q;
|
||||
|
||||
int x = 5;
|
||||
|
||||
// x is an lvalue.
|
||||
// *&x is a load.
|
||||
return *&x;
|
||||
}
|
||||
|
||||
int& References(int& r)
|
||||
{
|
||||
// The reference r is a load, as is the result of dereferencing the
|
||||
// reference r.
|
||||
int x = r;
|
||||
|
||||
// The result of dereferencing the reference r is an lvalue.
|
||||
// The reference r is a load.
|
||||
int* p = &r;
|
||||
|
||||
// The result of deferencing the reference r is an lvalue, as is the result
|
||||
// of the assignment.
|
||||
// The reference r is a load.
|
||||
r = 5;
|
||||
|
||||
// The result of dereferencing the reference r is an lvalue.
|
||||
// The reference r is a load.
|
||||
return r;
|
||||
}
|
||||
|
||||
int&& GetRValueRef();
|
||||
void CallRValueRef(int&& rr);
|
||||
|
||||
int&& RValueReferences(int&& rr)
|
||||
{
|
||||
// The result of dereferencing the reference returned by GetRValueRef() is
|
||||
// an xvalue.
|
||||
CallRValueRef(GetRValueRef());
|
||||
|
||||
// The result of dereferencing the reference rr is an lvalue, as is the
|
||||
// result of the assignment.
|
||||
// The reference rr is a load.
|
||||
rr = 5;
|
||||
|
||||
// The result of the static cast is an xvalue. The result of dereferencing
|
||||
// the reference rr is an lvalue.
|
||||
// The reference rr is a load.
|
||||
return static_cast<int&&>(rr);
|
||||
}
|
||||
|
||||
struct S
|
||||
{
|
||||
int MemberFunction();
|
||||
};
|
||||
|
||||
int CallMemberFunction(S& s)
|
||||
{
|
||||
// The result of dereferencing the reference s is an lvalue.
|
||||
// The reference s is a load.
|
||||
return s.MemberFunction();
|
||||
}
|
||||
|
||||
int Func();
|
||||
void AddressOfFunc()
|
||||
{
|
||||
// Func is a load due to the function-to-function-pointer conversions.
|
||||
int (*p)() = Func;
|
||||
}
|
||||
|
||||
struct T
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
int MemberFunc(float);
|
||||
};
|
||||
|
||||
void FieldAccesses()
|
||||
{
|
||||
T t;
|
||||
// t, t.x, and the assignment are all lvalues.
|
||||
t.x = 0;
|
||||
// t is an lvalue.
|
||||
// t.x is a load.
|
||||
int a = t.x;
|
||||
}
|
||||
|
||||
void StringLiterals()
|
||||
{
|
||||
// All string literals are lvalues
|
||||
"String";
|
||||
const char* p = "String";
|
||||
const char (&a)[7] = "String";
|
||||
// The array access is a load
|
||||
char c = "String"[1];
|
||||
}
|
||||
|
||||
void Crement()
|
||||
{
|
||||
int x = 0;
|
||||
// x is an lvalue.
|
||||
x++;
|
||||
// x is an lvalue.
|
||||
x--;
|
||||
// x is an lvalue, as is the result of ++x.
|
||||
++x;
|
||||
// x is an lvalue, as is the result of --x.
|
||||
--x;
|
||||
}
|
||||
|
||||
void CompoundAssignment()
|
||||
{
|
||||
int x = 0;
|
||||
|
||||
// x is an lvalue, as is the result of x += 1
|
||||
x += 1;
|
||||
// x is an lvalue, as is the result of x -= 1
|
||||
x -= 1;
|
||||
// x is an lvalue, as is the result of x *= 1
|
||||
x *= 1;
|
||||
// x is an lvalue, as is the result of x /= 1
|
||||
x /= 1;
|
||||
// x is an lvalue, as is the result of x %= 1
|
||||
x %= 1;
|
||||
// x is an lvalue, as is the result of x <<= 1
|
||||
x <<= 1;
|
||||
// x is an lvalue, as is the result of x >>= 1
|
||||
x >>= 1;
|
||||
// x is an lvalue, as is the result of x |= 1
|
||||
x |= 1;
|
||||
// x is an lvalue, as is the result of x &= 1
|
||||
x &= 1;
|
||||
// x is an lvalue, as is the result of x ^= 1
|
||||
x ^= 1;
|
||||
}
|
||||
|
||||
void PointerToMemberLiteral()
|
||||
{
|
||||
// All pointer-to-member literals are prvalues
|
||||
int T::* pmd = &T::x;
|
||||
int (T::* pmf)(float) = &T::MemberFunc;
|
||||
}
|
||||
int ParamsAndLocals(int x)
|
||||
{
|
||||
int y;
|
||||
|
||||
// y is an lvalue, as is the result of the assignment. x is a load.
|
||||
y = x + 1;
|
||||
|
||||
// y is a load.
|
||||
return y;
|
||||
}
|
||||
|
||||
int Dereference(int* p, int *q)
|
||||
{
|
||||
// *p is an lvalue, as is the result of the assignment.
|
||||
// p, q, and *q are loads.
|
||||
*p = *q;
|
||||
|
||||
int x = 5;
|
||||
|
||||
// x is an lvalue.
|
||||
// *&x is a load.
|
||||
return *&x;
|
||||
}
|
||||
|
||||
int& References(int& r)
|
||||
{
|
||||
// The reference r is a load, as is the result of dereferencing the
|
||||
// reference r.
|
||||
int x = r;
|
||||
|
||||
// The result of dereferencing the reference r is an lvalue.
|
||||
// The reference r is a load.
|
||||
int* p = &r;
|
||||
|
||||
// The result of deferencing the reference r is an lvalue, as is the result
|
||||
// of the assignment.
|
||||
// The reference r is a load.
|
||||
r = 5;
|
||||
|
||||
// The result of dereferencing the reference r is an lvalue.
|
||||
// The reference r is a load.
|
||||
return r;
|
||||
}
|
||||
|
||||
int&& GetRValueRef();
|
||||
void CallRValueRef(int&& rr);
|
||||
|
||||
int&& RValueReferences(int&& rr)
|
||||
{
|
||||
// The result of dereferencing the reference returned by GetRValueRef() is
|
||||
// an xvalue.
|
||||
CallRValueRef(GetRValueRef());
|
||||
|
||||
// The result of dereferencing the reference rr is an lvalue, as is the
|
||||
// result of the assignment.
|
||||
// The reference rr is a load.
|
||||
rr = 5;
|
||||
|
||||
// The result of the static cast is an xvalue. The result of dereferencing
|
||||
// the reference rr is an lvalue.
|
||||
// The reference rr is a load.
|
||||
return static_cast<int&&>(rr);
|
||||
}
|
||||
|
||||
struct S
|
||||
{
|
||||
int MemberFunction();
|
||||
};
|
||||
|
||||
int CallMemberFunction(S& s)
|
||||
{
|
||||
// The result of dereferencing the reference s is an lvalue.
|
||||
// The reference s is a load.
|
||||
return s.MemberFunction();
|
||||
}
|
||||
|
||||
int Func();
|
||||
void AddressOfFunc()
|
||||
{
|
||||
// Func is a load due to the function-to-function-pointer conversions.
|
||||
int (*p)() = Func;
|
||||
}
|
||||
|
||||
struct T
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
int MemberFunc(float);
|
||||
};
|
||||
|
||||
void FieldAccesses()
|
||||
{
|
||||
T t;
|
||||
// t, t.x, and the assignment are all lvalues.
|
||||
t.x = 0;
|
||||
// t is an lvalue.
|
||||
// t.x is a load.
|
||||
int a = t.x;
|
||||
}
|
||||
|
||||
void StringLiterals()
|
||||
{
|
||||
// All string literals are lvalues
|
||||
"String";
|
||||
const char* p = "String";
|
||||
const char (&a)[7] = "String";
|
||||
// The array access is a load
|
||||
char c = "String"[1];
|
||||
}
|
||||
|
||||
void Crement()
|
||||
{
|
||||
int x = 0;
|
||||
// x is an lvalue.
|
||||
x++;
|
||||
// x is an lvalue.
|
||||
x--;
|
||||
// x is an lvalue, as is the result of ++x.
|
||||
++x;
|
||||
// x is an lvalue, as is the result of --x.
|
||||
--x;
|
||||
}
|
||||
|
||||
void CompoundAssignment()
|
||||
{
|
||||
int x = 0;
|
||||
|
||||
// x is an lvalue, as is the result of x += 1
|
||||
x += 1;
|
||||
// x is an lvalue, as is the result of x -= 1
|
||||
x -= 1;
|
||||
// x is an lvalue, as is the result of x *= 1
|
||||
x *= 1;
|
||||
// x is an lvalue, as is the result of x /= 1
|
||||
x /= 1;
|
||||
// x is an lvalue, as is the result of x %= 1
|
||||
x %= 1;
|
||||
// x is an lvalue, as is the result of x <<= 1
|
||||
x <<= 1;
|
||||
// x is an lvalue, as is the result of x >>= 1
|
||||
x >>= 1;
|
||||
// x is an lvalue, as is the result of x |= 1
|
||||
x |= 1;
|
||||
// x is an lvalue, as is the result of x &= 1
|
||||
x &= 1;
|
||||
// x is an lvalue, as is the result of x ^= 1
|
||||
x ^= 1;
|
||||
}
|
||||
|
||||
void PointerToMemberLiteral()
|
||||
{
|
||||
// All pointer-to-member literals are prvalues
|
||||
int T::* pmd = &T::x;
|
||||
int (T::* pmf)(float) = &T::MemberFunc;
|
||||
}
|
||||
|
||||
@@ -1,33 +1,33 @@
|
||||
enum Type { S, I };
|
||||
|
||||
struct Foo {
|
||||
char* name;
|
||||
int count;
|
||||
char* another_name;
|
||||
char* yet_another_name;
|
||||
char initials[2];
|
||||
long very_long;
|
||||
};
|
||||
|
||||
void create_foo()
|
||||
{
|
||||
Foo xx;
|
||||
char name[] = "Foo McFoo";
|
||||
xx.name = name;
|
||||
xx.count = 123;
|
||||
Foo yy = { "Barry McBar", 42, "Baz", "Basildon", { 'B', 'X' }, 5678 };
|
||||
}
|
||||
|
||||
void print_foo(Foo* p)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Foo current_foo;
|
||||
|
||||
Foo set_current_foo(Foo next)
|
||||
{
|
||||
Foo prev = current_foo;
|
||||
current_foo = next;
|
||||
return prev;
|
||||
}
|
||||
enum Type { S, I };
|
||||
|
||||
struct Foo {
|
||||
char* name;
|
||||
int count;
|
||||
char* another_name;
|
||||
char* yet_another_name;
|
||||
char initials[2];
|
||||
long very_long;
|
||||
};
|
||||
|
||||
void create_foo()
|
||||
{
|
||||
Foo xx;
|
||||
char name[] = "Foo McFoo";
|
||||
xx.name = name;
|
||||
xx.count = 123;
|
||||
Foo yy = { "Barry McBar", 42, "Baz", "Basildon", { 'B', 'X' }, 5678 };
|
||||
}
|
||||
|
||||
void print_foo(Foo* p)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Foo current_foo;
|
||||
|
||||
Foo set_current_foo(Foo next)
|
||||
{
|
||||
Foo prev = current_foo;
|
||||
current_foo = next;
|
||||
return prev;
|
||||
}
|
||||
|
||||
@@ -1,33 +1,33 @@
|
||||
enum Type { S, I };
|
||||
|
||||
struct Entry {
|
||||
char* name;
|
||||
Type t;
|
||||
char* s;
|
||||
int i;
|
||||
private:
|
||||
int internal;
|
||||
};
|
||||
|
||||
class Name {
|
||||
const char* s;
|
||||
};
|
||||
|
||||
class Table {
|
||||
Name* p;
|
||||
int sz;
|
||||
public:
|
||||
Table(int s=15) { p = new Name[sz=s]; } // constructor
|
||||
~Table() { delete[] p; }
|
||||
Name* lookup (const char*);
|
||||
bool insert(Name*);
|
||||
};
|
||||
|
||||
class Date {
|
||||
static Table memtbl;
|
||||
mutable bool cache_valid;
|
||||
public:
|
||||
mutable char* cache;
|
||||
void compute_cache_value() const;
|
||||
};
|
||||
|
||||
enum Type { S, I };
|
||||
|
||||
struct Entry {
|
||||
char* name;
|
||||
Type t;
|
||||
char* s;
|
||||
int i;
|
||||
private:
|
||||
int internal;
|
||||
};
|
||||
|
||||
class Name {
|
||||
const char* s;
|
||||
};
|
||||
|
||||
class Table {
|
||||
Name* p;
|
||||
int sz;
|
||||
public:
|
||||
Table(int s=15) { p = new Name[sz=s]; } // constructor
|
||||
~Table() { delete[] p; }
|
||||
Name* lookup (const char*);
|
||||
bool insert(Name*);
|
||||
};
|
||||
|
||||
class Date {
|
||||
static Table memtbl;
|
||||
mutable bool cache_valid;
|
||||
public:
|
||||
mutable char* cache;
|
||||
void compute_cache_value() const;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#include "files1.h"
|
||||
|
||||
|
||||
void swap(int* p, int* q)
|
||||
{
|
||||
int t = *p;
|
||||
*p = *q;
|
||||
*q = t;
|
||||
}
|
||||
#include "files1.h"
|
||||
|
||||
|
||||
void swap(int* p, int* q)
|
||||
{
|
||||
int t = *p;
|
||||
*p = *q;
|
||||
*q = t;
|
||||
}
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
|
||||
extern void swap(int*, int*);
|
||||
|
||||
extern void swap(int*, int*);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "files1.h"
|
||||
|
||||
void g() {
|
||||
int x = 2;
|
||||
int y = 4;
|
||||
swap(&x,&y);
|
||||
}
|
||||
#include "files1.h"
|
||||
|
||||
void g() {
|
||||
int x = 2;
|
||||
int y = 4;
|
||||
swap(&x,&y);
|
||||
}
|
||||
|
||||
@@ -1,33 +1,33 @@
|
||||
void f(int a, int b) {
|
||||
bool c = a==b;
|
||||
}
|
||||
|
||||
void g();
|
||||
|
||||
struct A {
|
||||
void af() {
|
||||
|
||||
}
|
||||
void ag();
|
||||
};
|
||||
|
||||
void g() {
|
||||
A a;
|
||||
a.ag();
|
||||
}
|
||||
|
||||
class Name {
|
||||
const char* s;
|
||||
};
|
||||
|
||||
class Table {
|
||||
Name* p;
|
||||
int sz;
|
||||
public:
|
||||
Table(int s=15) { p = new Name[sz=s]; } // constructor
|
||||
~Table() { delete[] p; }
|
||||
Name* lookup (const char*);
|
||||
bool insert(Name*);
|
||||
};
|
||||
|
||||
|
||||
void f(int a, int b) {
|
||||
bool c = a==b;
|
||||
}
|
||||
|
||||
void g();
|
||||
|
||||
struct A {
|
||||
void af() {
|
||||
|
||||
}
|
||||
void ag();
|
||||
};
|
||||
|
||||
void g() {
|
||||
A a;
|
||||
a.ag();
|
||||
}
|
||||
|
||||
class Name {
|
||||
const char* s;
|
||||
};
|
||||
|
||||
class Table {
|
||||
Name* p;
|
||||
int sz;
|
||||
public:
|
||||
Table(int s=15) { p = new Name[sz=s]; } // constructor
|
||||
~Table() { delete[] p; }
|
||||
Name* lookup (const char*);
|
||||
bool insert(Name*);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -1,32 +1,32 @@
|
||||
int ReturnConstant() {
|
||||
return 7;
|
||||
}
|
||||
|
||||
int ReturnConstantPhi(bool b) {
|
||||
if (b) {
|
||||
return 7;
|
||||
}
|
||||
else {
|
||||
return 7;
|
||||
}
|
||||
}
|
||||
|
||||
int GetInt();
|
||||
|
||||
int ReturnNonConstantPhi(bool b) {
|
||||
if (b) {
|
||||
return 7;
|
||||
}
|
||||
else {
|
||||
return GetInt();
|
||||
}
|
||||
}
|
||||
|
||||
int ReturnConstantPhiLoop(int x) {
|
||||
int y = 7;
|
||||
while (x > 0) {
|
||||
y = 7;
|
||||
--x;
|
||||
}
|
||||
return y;
|
||||
}
|
||||
int ReturnConstant() {
|
||||
return 7;
|
||||
}
|
||||
|
||||
int ReturnConstantPhi(bool b) {
|
||||
if (b) {
|
||||
return 7;
|
||||
}
|
||||
else {
|
||||
return 7;
|
||||
}
|
||||
}
|
||||
|
||||
int GetInt();
|
||||
|
||||
int ReturnNonConstantPhi(bool b) {
|
||||
if (b) {
|
||||
return 7;
|
||||
}
|
||||
else {
|
||||
return GetInt();
|
||||
}
|
||||
}
|
||||
|
||||
int ReturnConstantPhiLoop(int x) {
|
||||
int y = 7;
|
||||
while (x > 0) {
|
||||
y = 7;
|
||||
--x;
|
||||
}
|
||||
return y;
|
||||
}
|
||||
|
||||
@@ -1,98 +1,98 @@
|
||||
void CallByPointer(int* p);
|
||||
void CallByReference(int& r);
|
||||
|
||||
struct Point {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
};
|
||||
|
||||
struct Base {
|
||||
float b;
|
||||
};
|
||||
|
||||
struct ReusedBase {
|
||||
float rb;
|
||||
};
|
||||
|
||||
struct Intermediate1 : Base, ReusedBase {
|
||||
float i1;
|
||||
};
|
||||
|
||||
struct Intermediate2 : ReusedBase {
|
||||
float i2;
|
||||
};
|
||||
|
||||
struct Derived : Intermediate1, Intermediate2 {
|
||||
float d;
|
||||
};
|
||||
|
||||
void Escape()
|
||||
{
|
||||
int no_result;
|
||||
int no_;
|
||||
|
||||
no_ = 1;
|
||||
no_ = no_;
|
||||
no_result = no_;
|
||||
no_result = *&no_;
|
||||
// no_result = (int&)no_; Restore when we have correct IR types for glvalues
|
||||
no_;
|
||||
&no_;
|
||||
no_result = *((&no_) + 0);
|
||||
no_result = *((&no_) - 0);
|
||||
no_result = *(0 + &no_);
|
||||
if (&no_) {
|
||||
}
|
||||
while (&no_) {
|
||||
}
|
||||
do {
|
||||
} while (&no_);
|
||||
for(&no_; &no_; &no_) {
|
||||
}
|
||||
|
||||
if (&no_ == nullptr) {
|
||||
}
|
||||
while (&no_ != nullptr) {
|
||||
}
|
||||
|
||||
int no_Array[10];
|
||||
no_Array;
|
||||
(int*)no_Array;
|
||||
no_Array[5];
|
||||
5[no_Array];
|
||||
no_result = no_Array[5];
|
||||
no_result = 5[no_Array];
|
||||
|
||||
Point no_Point = { 1, 2, 3 };
|
||||
float no_x = no_Point.x;
|
||||
no_Point.y = no_x;
|
||||
float no_y = (&no_Point)->y;
|
||||
(&no_Point)->y = no_y;
|
||||
float no_z = *(&no_Point.z);
|
||||
*(&no_Point.z) = no_z;
|
||||
|
||||
Derived no_Derived;
|
||||
no_Derived.b = 0;
|
||||
float no_b = no_Derived.b;
|
||||
no_Derived.i2 = 1;
|
||||
float no_i2 = no_Derived.i2;
|
||||
|
||||
int no_ssa_addrOf;
|
||||
int* no_p = &no_ssa_addrOf;
|
||||
|
||||
int no_ssa_refTo;
|
||||
int& no_r = no_ssa_refTo;
|
||||
|
||||
int no_ssa_refToArrayElement[10];
|
||||
int& no_rae = no_ssa_refToArrayElement[5];
|
||||
|
||||
int no_ssa_refToArray[10];
|
||||
int (&no_ra)[10] = no_ssa_refToArray;
|
||||
|
||||
int passByPtr;
|
||||
CallByPointer(&passByPtr);
|
||||
|
||||
int passByRef;
|
||||
CallByReference(passByRef);
|
||||
}
|
||||
void CallByPointer(int* p);
|
||||
void CallByReference(int& r);
|
||||
|
||||
struct Point {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
};
|
||||
|
||||
struct Base {
|
||||
float b;
|
||||
};
|
||||
|
||||
struct ReusedBase {
|
||||
float rb;
|
||||
};
|
||||
|
||||
struct Intermediate1 : Base, ReusedBase {
|
||||
float i1;
|
||||
};
|
||||
|
||||
struct Intermediate2 : ReusedBase {
|
||||
float i2;
|
||||
};
|
||||
|
||||
struct Derived : Intermediate1, Intermediate2 {
|
||||
float d;
|
||||
};
|
||||
|
||||
void Escape()
|
||||
{
|
||||
int no_result;
|
||||
int no_;
|
||||
|
||||
no_ = 1;
|
||||
no_ = no_;
|
||||
no_result = no_;
|
||||
no_result = *&no_;
|
||||
// no_result = (int&)no_; Restore when we have correct IR types for glvalues
|
||||
no_;
|
||||
&no_;
|
||||
no_result = *((&no_) + 0);
|
||||
no_result = *((&no_) - 0);
|
||||
no_result = *(0 + &no_);
|
||||
if (&no_) {
|
||||
}
|
||||
while (&no_) {
|
||||
}
|
||||
do {
|
||||
} while (&no_);
|
||||
for(&no_; &no_; &no_) {
|
||||
}
|
||||
|
||||
if (&no_ == nullptr) {
|
||||
}
|
||||
while (&no_ != nullptr) {
|
||||
}
|
||||
|
||||
int no_Array[10];
|
||||
no_Array;
|
||||
(int*)no_Array;
|
||||
no_Array[5];
|
||||
5[no_Array];
|
||||
no_result = no_Array[5];
|
||||
no_result = 5[no_Array];
|
||||
|
||||
Point no_Point = { 1, 2, 3 };
|
||||
float no_x = no_Point.x;
|
||||
no_Point.y = no_x;
|
||||
float no_y = (&no_Point)->y;
|
||||
(&no_Point)->y = no_y;
|
||||
float no_z = *(&no_Point.z);
|
||||
*(&no_Point.z) = no_z;
|
||||
|
||||
Derived no_Derived;
|
||||
no_Derived.b = 0;
|
||||
float no_b = no_Derived.b;
|
||||
no_Derived.i2 = 1;
|
||||
float no_i2 = no_Derived.i2;
|
||||
|
||||
int no_ssa_addrOf;
|
||||
int* no_p = &no_ssa_addrOf;
|
||||
|
||||
int no_ssa_refTo;
|
||||
int& no_r = no_ssa_refTo;
|
||||
|
||||
int no_ssa_refToArrayElement[10];
|
||||
int& no_rae = no_ssa_refToArrayElement[5];
|
||||
|
||||
int no_ssa_refToArray[10];
|
||||
int (&no_ra)[10] = no_ssa_refToArray;
|
||||
|
||||
int passByPtr;
|
||||
CallByPointer(&passByPtr);
|
||||
|
||||
int passByRef;
|
||||
CallByReference(passByRef);
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,102 +1,102 @@
|
||||
struct someStruct {
|
||||
int i;
|
||||
int j;
|
||||
};
|
||||
|
||||
struct someOtherStruct {
|
||||
int a;
|
||||
int b;
|
||||
};
|
||||
|
||||
union someUnion {
|
||||
int n;
|
||||
double d;
|
||||
};
|
||||
|
||||
void f(int x, int y) {
|
||||
struct someStruct sInit1 = {
|
||||
.i = x + x,
|
||||
.j = y - y,
|
||||
};
|
||||
|
||||
struct someStruct sInit2 = { x + x, y - y };
|
||||
|
||||
struct someStruct ss[] = {{x + x, y - y}, {x * x, y / y}};
|
||||
|
||||
struct someStruct sInit3 = { x };
|
||||
|
||||
struct someStruct sInit4 = { .j = y };
|
||||
|
||||
int aInit1[2] = { x, y };
|
||||
|
||||
int aInit2[2] = { x };
|
||||
|
||||
int aInit3[2] = { [1] = y };
|
||||
|
||||
union someUnion uInit1 = { x };
|
||||
union someUnion uInit2 = { .n = x };
|
||||
union someUnion uInit3 = { .d = 5.0 };
|
||||
}
|
||||
|
||||
struct complexStruct {
|
||||
struct someStruct sss[3];
|
||||
int as[3];
|
||||
struct someOtherStruct soss[3];
|
||||
int z;
|
||||
};
|
||||
|
||||
void g(int x, int y) {
|
||||
// Nested aggregate designated initializers
|
||||
struct complexStruct complexInit1 = {
|
||||
.as = {
|
||||
[2] = x,
|
||||
[0] = y,
|
||||
x+y // as[1]
|
||||
},
|
||||
.z = 42,
|
||||
.soss = {
|
||||
[1] = {
|
||||
.a = x+y,
|
||||
.b = x-y
|
||||
},
|
||||
[0] = {
|
||||
.b = x*y,
|
||||
.a = x/y
|
||||
}
|
||||
// soss[2] is value initializaed
|
||||
}
|
||||
// sss is value initialized
|
||||
};
|
||||
|
||||
// Nested aggregate non-designated initializers
|
||||
struct complexStruct complexInit2 = {
|
||||
{ // sss
|
||||
{ // sss[0]
|
||||
x, // sss[0].i
|
||||
y // sss[0].j
|
||||
},
|
||||
{ // sss[1]
|
||||
x+1, // sss[1].i
|
||||
// sss[1].j is value initialized
|
||||
}
|
||||
// ss[2] is value initialized
|
||||
},
|
||||
{ // as
|
||||
99, // as[0]
|
||||
x*y // as[1]
|
||||
// as[2] is value initialized
|
||||
},
|
||||
{ // soss
|
||||
{ // soss[0]
|
||||
123, // soss[0].a
|
||||
y+1 // soss[0].b
|
||||
},
|
||||
{ // soss[1]
|
||||
x, // soss[1].a
|
||||
// soss[1].b is value initialized
|
||||
}
|
||||
// soss[2] is value initialized
|
||||
}
|
||||
// z is value initialized
|
||||
};
|
||||
}
|
||||
struct someStruct {
|
||||
int i;
|
||||
int j;
|
||||
};
|
||||
|
||||
struct someOtherStruct {
|
||||
int a;
|
||||
int b;
|
||||
};
|
||||
|
||||
union someUnion {
|
||||
int n;
|
||||
double d;
|
||||
};
|
||||
|
||||
void f(int x, int y) {
|
||||
struct someStruct sInit1 = {
|
||||
.i = x + x,
|
||||
.j = y - y,
|
||||
};
|
||||
|
||||
struct someStruct sInit2 = { x + x, y - y };
|
||||
|
||||
struct someStruct ss[] = {{x + x, y - y}, {x * x, y / y}};
|
||||
|
||||
struct someStruct sInit3 = { x };
|
||||
|
||||
struct someStruct sInit4 = { .j = y };
|
||||
|
||||
int aInit1[2] = { x, y };
|
||||
|
||||
int aInit2[2] = { x };
|
||||
|
||||
int aInit3[2] = { [1] = y };
|
||||
|
||||
union someUnion uInit1 = { x };
|
||||
union someUnion uInit2 = { .n = x };
|
||||
union someUnion uInit3 = { .d = 5.0 };
|
||||
}
|
||||
|
||||
struct complexStruct {
|
||||
struct someStruct sss[3];
|
||||
int as[3];
|
||||
struct someOtherStruct soss[3];
|
||||
int z;
|
||||
};
|
||||
|
||||
void g(int x, int y) {
|
||||
// Nested aggregate designated initializers
|
||||
struct complexStruct complexInit1 = {
|
||||
.as = {
|
||||
[2] = x,
|
||||
[0] = y,
|
||||
x+y // as[1]
|
||||
},
|
||||
.z = 42,
|
||||
.soss = {
|
||||
[1] = {
|
||||
.a = x+y,
|
||||
.b = x-y
|
||||
},
|
||||
[0] = {
|
||||
.b = x*y,
|
||||
.a = x/y
|
||||
}
|
||||
// soss[2] is value initializaed
|
||||
}
|
||||
// sss is value initialized
|
||||
};
|
||||
|
||||
// Nested aggregate non-designated initializers
|
||||
struct complexStruct complexInit2 = {
|
||||
{ // sss
|
||||
{ // sss[0]
|
||||
x, // sss[0].i
|
||||
y // sss[0].j
|
||||
},
|
||||
{ // sss[1]
|
||||
x+1, // sss[1].i
|
||||
// sss[1].j is value initialized
|
||||
}
|
||||
// ss[2] is value initialized
|
||||
},
|
||||
{ // as
|
||||
99, // as[0]
|
||||
x*y // as[1]
|
||||
// as[2] is value initialized
|
||||
},
|
||||
{ // soss
|
||||
{ // soss[0]
|
||||
123, // soss[0].a
|
||||
y+1 // soss[0].b
|
||||
},
|
||||
{ // soss[1]
|
||||
x, // soss[1].a
|
||||
// soss[1].b is value initialized
|
||||
}
|
||||
// soss[2] is value initialized
|
||||
}
|
||||
// z is value initialized
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
struct StructWithBitfields {
|
||||
void Method();
|
||||
|
||||
unsigned int a : 12;
|
||||
|
||||
static int s; // Static member variables aren't initialized
|
||||
|
||||
unsigned int b : 5;
|
||||
unsigned int : 15; // Unnamed bitfields aren't initialized
|
||||
unsigned int c : 7;
|
||||
};
|
||||
|
||||
union UnionWithMethods {
|
||||
void Method();
|
||||
|
||||
static int s;
|
||||
|
||||
double d;
|
||||
int x;
|
||||
};
|
||||
|
||||
void Init(int x, int y, int z) {
|
||||
StructWithBitfields s1 = { x, y, z };
|
||||
StructWithBitfields s2 = { x, y }; // s2.c is value initialized
|
||||
StructWithBitfields s3 = {}; // s3 is value initialized
|
||||
UnionWithMethods u1 = { x };
|
||||
UnionWithMethods u2 = {};
|
||||
}
|
||||
struct StructWithBitfields {
|
||||
void Method();
|
||||
|
||||
unsigned int a : 12;
|
||||
|
||||
static int s; // Static member variables aren't initialized
|
||||
|
||||
unsigned int b : 5;
|
||||
unsigned int : 15; // Unnamed bitfields aren't initialized
|
||||
unsigned int c : 7;
|
||||
};
|
||||
|
||||
union UnionWithMethods {
|
||||
void Method();
|
||||
|
||||
static int s;
|
||||
|
||||
double d;
|
||||
int x;
|
||||
};
|
||||
|
||||
void Init(int x, int y, int z) {
|
||||
StructWithBitfields s1 = { x, y, z };
|
||||
StructWithBitfields s2 = { x, y }; // s2.c is value initialized
|
||||
StructWithBitfields s3 = {}; // s3 is value initialized
|
||||
UnionWithMethods u1 = { x };
|
||||
UnionWithMethods u2 = {};
|
||||
}
|
||||
|
||||
@@ -1,36 +1,36 @@
|
||||
void update(bool b);
|
||||
void init(int &i);
|
||||
bool cond(int i);
|
||||
void update(int &i);
|
||||
|
||||
void test_while()
|
||||
{
|
||||
bool b;
|
||||
|
||||
b = true;
|
||||
while (b) update(b);
|
||||
|
||||
do
|
||||
{
|
||||
update(b);
|
||||
} while (b);
|
||||
}
|
||||
|
||||
void test_for()
|
||||
{
|
||||
int i, j;
|
||||
|
||||
for (i = 0; i < 10; i++) {
|
||||
}
|
||||
|
||||
for (j = 10; j >= 0; ) j--;
|
||||
|
||||
for (i = 0, j = 0; i < 10; i++, j++) {
|
||||
}
|
||||
|
||||
for (init(i); cond(i); update(i)) {
|
||||
}
|
||||
|
||||
for ((i = 100); (i >= 0); (i--)) {
|
||||
}
|
||||
}
|
||||
void update(bool b);
|
||||
void init(int &i);
|
||||
bool cond(int i);
|
||||
void update(int &i);
|
||||
|
||||
void test_while()
|
||||
{
|
||||
bool b;
|
||||
|
||||
b = true;
|
||||
while (b) update(b);
|
||||
|
||||
do
|
||||
{
|
||||
update(b);
|
||||
} while (b);
|
||||
}
|
||||
|
||||
void test_for()
|
||||
{
|
||||
int i, j;
|
||||
|
||||
for (i = 0; i < 10; i++) {
|
||||
}
|
||||
|
||||
for (j = 10; j >= 0; ) j--;
|
||||
|
||||
for (i = 0, j = 0; i < 10; i++, j++) {
|
||||
}
|
||||
|
||||
for (init(i); cond(i); update(i)) {
|
||||
}
|
||||
|
||||
for ((i = 100); (i >= 0); (i--)) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,44 +1,44 @@
|
||||
namespace A {
|
||||
|
||||
}
|
||||
|
||||
namespace B {
|
||||
|
||||
}
|
||||
|
||||
namespace C {
|
||||
|
||||
namespace D {
|
||||
|
||||
inline int f() { return 0; }
|
||||
|
||||
class E {
|
||||
|
||||
void g(int p) {
|
||||
int a;
|
||||
{
|
||||
int b;
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace B {
|
||||
|
||||
int x = C::D::f();
|
||||
|
||||
}
|
||||
|
||||
namespace std {
|
||||
|
||||
}
|
||||
|
||||
int globalInt;
|
||||
|
||||
void globalIntUser(void) {
|
||||
extern int globalInt;
|
||||
}
|
||||
namespace A {
|
||||
|
||||
}
|
||||
|
||||
namespace B {
|
||||
|
||||
}
|
||||
|
||||
namespace C {
|
||||
|
||||
namespace D {
|
||||
|
||||
inline int f() { return 0; }
|
||||
|
||||
class E {
|
||||
|
||||
void g(int p) {
|
||||
int a;
|
||||
{
|
||||
int b;
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace B {
|
||||
|
||||
int x = C::D::f();
|
||||
|
||||
}
|
||||
|
||||
namespace std {
|
||||
|
||||
}
|
||||
|
||||
int globalInt;
|
||||
|
||||
void globalIntUser(void) {
|
||||
extern int globalInt;
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
|
||||
const int c = 1;
|
||||
|
||||
namespace namespace_a
|
||||
{
|
||||
const int c = 1;
|
||||
}
|
||||
|
||||
namespace namespace_b
|
||||
{
|
||||
//const int c = 1;
|
||||
//
|
||||
// this example is causing a DBCheck failure along the lines of:
|
||||
//
|
||||
// [INVALID_KEY] Relation namespacembrs((@namespace parentid, unique @namespacembr memberid)): Value 132 of key field memberid occurs in several tuples. Two such tuples are: (134,132) and (144,132)
|
||||
}
|
||||
|
||||
const int c = 1;
|
||||
|
||||
namespace namespace_a
|
||||
{
|
||||
const int c = 1;
|
||||
}
|
||||
|
||||
namespace namespace_b
|
||||
{
|
||||
//const int c = 1;
|
||||
//
|
||||
// this example is causing a DBCheck failure along the lines of:
|
||||
//
|
||||
// [INVALID_KEY] Relation namespacembrs((@namespace parentid, unique @namespacembr memberid)): Value 132 of key field memberid occurs in several tuples. Two such tuples are: (134,132) and (144,132)
|
||||
}
|
||||
|
||||
@@ -1,27 +1,27 @@
|
||||
class Name {
|
||||
const char* s;
|
||||
};
|
||||
|
||||
class Table {
|
||||
Name* p;
|
||||
int sz;
|
||||
public:
|
||||
// See JIRA 521. Including a non-default copy constructor for now.
|
||||
Table(const Table& other) { }
|
||||
Table(int s=15) { p = new Name[sz=s]; } // constructor
|
||||
~Table() { delete[] p; }
|
||||
Name* lookup (const char*);
|
||||
bool insert(Name*);
|
||||
};
|
||||
|
||||
void foo() {
|
||||
Table t1; // call to user-defined constructor
|
||||
Table t2(t1); // call to default copy constructor
|
||||
t2.insert(0);
|
||||
}
|
||||
|
||||
// This used to have TRAP import errors
|
||||
struct A { A() {} A(A&) {} } a;
|
||||
A operator+(int, A) { return a; }
|
||||
int f_A() { 0 + a; }
|
||||
|
||||
class Name {
|
||||
const char* s;
|
||||
};
|
||||
|
||||
class Table {
|
||||
Name* p;
|
||||
int sz;
|
||||
public:
|
||||
// See JIRA 521. Including a non-default copy constructor for now.
|
||||
Table(const Table& other) { }
|
||||
Table(int s=15) { p = new Name[sz=s]; } // constructor
|
||||
~Table() { delete[] p; }
|
||||
Name* lookup (const char*);
|
||||
bool insert(Name*);
|
||||
};
|
||||
|
||||
void foo() {
|
||||
Table t1; // call to user-defined constructor
|
||||
Table t2(t1); // call to default copy constructor
|
||||
t2.insert(0);
|
||||
}
|
||||
|
||||
// This used to have TRAP import errors
|
||||
struct A { A() {} A(A&) {} } a;
|
||||
A operator+(int, A) { return a; }
|
||||
int f_A() { 0 + a; }
|
||||
|
||||
|
||||
@@ -1,46 +1,46 @@
|
||||
// Static assert macro
|
||||
#define CASSERT(condition) typedef char cassertTypedef[((condition) != 0) ? 1 : -1]
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#define CASSERT_MSVC(condition) CASSERT(condition)
|
||||
#define CASSERT_GCC(condition)
|
||||
#if defined(_WIN64)
|
||||
#define TARGET_BIT_SIZE 64
|
||||
#else
|
||||
#define TARGET_BIT_SIZE 32
|
||||
#endif
|
||||
#elif defined(__GNUC__)
|
||||
#define CASSERT_MSVC(condition)
|
||||
#define CASSERT_GCC(condition) CASSERT(condition)
|
||||
#if defined(__x86_64)
|
||||
#define TARGET_BIT_SIZE 64
|
||||
#else
|
||||
#define TARGET_BIT_SIZE 32
|
||||
#endif
|
||||
#else
|
||||
CASSERT(0);
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER) && (TARGET_BIT_SIZE == 32)
|
||||
#define CASSERT_MSVC32(condition) CASSERT(condition)
|
||||
#else
|
||||
#define CASSERT_MSVC32(condition)
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER) && (TARGET_BIT_SIZE == 64)
|
||||
#define CASSERT_MSVC64(condition) CASSERT(condition)
|
||||
#else
|
||||
#define CASSERT_MSVC64(condition)
|
||||
#endif
|
||||
|
||||
#if defined(__GNUC__) && (TARGET_BIT_SIZE == 32)
|
||||
#define CASSERT_GCC32(condition) CASSERT(condition)
|
||||
#else
|
||||
#define CASSERT_GCC32(condition)
|
||||
#endif
|
||||
|
||||
#if defined(__GNUC__) && (TARGET_BIT_SIZE == 64)
|
||||
#define CASSERT_GCC64(condition) CASSERT(condition)
|
||||
#else
|
||||
#define CASSERT_GCC64(condition)
|
||||
#endif
|
||||
// Static assert macro
|
||||
#define CASSERT(condition) typedef char cassertTypedef[((condition) != 0) ? 1 : -1]
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#define CASSERT_MSVC(condition) CASSERT(condition)
|
||||
#define CASSERT_GCC(condition)
|
||||
#if defined(_WIN64)
|
||||
#define TARGET_BIT_SIZE 64
|
||||
#else
|
||||
#define TARGET_BIT_SIZE 32
|
||||
#endif
|
||||
#elif defined(__GNUC__)
|
||||
#define CASSERT_MSVC(condition)
|
||||
#define CASSERT_GCC(condition) CASSERT(condition)
|
||||
#if defined(__x86_64)
|
||||
#define TARGET_BIT_SIZE 64
|
||||
#else
|
||||
#define TARGET_BIT_SIZE 32
|
||||
#endif
|
||||
#else
|
||||
CASSERT(0);
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER) && (TARGET_BIT_SIZE == 32)
|
||||
#define CASSERT_MSVC32(condition) CASSERT(condition)
|
||||
#else
|
||||
#define CASSERT_MSVC32(condition)
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER) && (TARGET_BIT_SIZE == 64)
|
||||
#define CASSERT_MSVC64(condition) CASSERT(condition)
|
||||
#else
|
||||
#define CASSERT_MSVC64(condition)
|
||||
#endif
|
||||
|
||||
#if defined(__GNUC__) && (TARGET_BIT_SIZE == 32)
|
||||
#define CASSERT_GCC32(condition) CASSERT(condition)
|
||||
#else
|
||||
#define CASSERT_GCC32(condition)
|
||||
#endif
|
||||
|
||||
#if defined(__GNUC__) && (TARGET_BIT_SIZE == 64)
|
||||
#define CASSERT_GCC64(condition) CASSERT(condition)
|
||||
#else
|
||||
#define CASSERT_GCC64(condition)
|
||||
#endif
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
typedef int (*CallbackFn)(int a, int b);
|
||||
|
||||
int Callback_1(int a, int b)
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
|
||||
int Callback_2(int a, int b)
|
||||
{
|
||||
return a;
|
||||
}
|
||||
|
||||
int Callback_3(int, int b)
|
||||
{
|
||||
return b;
|
||||
}
|
||||
|
||||
void Dispatch(int n, int a, int b, int c, int)
|
||||
{
|
||||
CallbackFn pFn;
|
||||
switch(n)
|
||||
{
|
||||
case 0: pFn = &Callback_1; break;
|
||||
case 1: pFn = &Callback_2; break;
|
||||
default: pFn = &Callback_3; break;
|
||||
}
|
||||
(*pFn)(a,b);
|
||||
}
|
||||
typedef int (*CallbackFn)(int a, int b);
|
||||
|
||||
int Callback_1(int a, int b)
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
|
||||
int Callback_2(int a, int b)
|
||||
{
|
||||
return a;
|
||||
}
|
||||
|
||||
int Callback_3(int, int b)
|
||||
{
|
||||
return b;
|
||||
}
|
||||
|
||||
void Dispatch(int n, int a, int b, int c, int)
|
||||
{
|
||||
CallbackFn pFn;
|
||||
switch(n)
|
||||
{
|
||||
case 0: pFn = &Callback_1; break;
|
||||
case 1: pFn = &Callback_2; break;
|
||||
default: pFn = &Callback_3; break;
|
||||
}
|
||||
(*pFn)(a,b);
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user