Skip to content

On C import, neither direct struct, enum etc. definitions nor (structured) typedefs from header files are imported as type declarations #1087

@codemanyak

Description

@codemanyak

Though structured typedefs found in headers for used type names make a C source file pass the C99 parser (with bugfix #1085 integrated), they don't induce a type declaration in an includable diagram on C import. (This happens only if the typedef or struct definition occurs in the C file itself).

Example:

File version 1 (C flile with integrated type definition):

typedef struct Date {
	int year;
	unsigned short month;
	unsigned short day;
} Date_t;

bool isValid(const Date_t aDate)
{
	unsigned short days = getMonthLength(aDate.month, aDate.year);
	return (days > 0 && aDate.day <= days);
}

unsigned short getMonthLength(unsigned short month, int year)
{
	unsigned short days = 0;
	switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			days = 31;
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			days = 30;
			break;
		case 2:
			days = 28;
			if (isLeapYear(year)) {
				days++;
			}
			break;
	}
	return days;
}

bool isLeapYear(int year)
{
	return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}

results in:
grafik

File version 2 (properly decomposed in header and C file):

Date.h

#ifndef DATE_H
#define DATE_H

#include <stdbool.h>

typedef struct Date {
	int year;
	unsigned short month;
	unsigned short day;
} Date_t;

bool isValid(const Date_t aDate);

unsigned short getMonthLength(unsigned short month, int year);

bool isLeapYear(int year);

#endif DATE_H

Date.c

#include "Date.h"

bool isValid(const Date_t aDate)
{
	unsigned short days = getMonthLength(aDate.month, aDate.year);
	return (days > 0 && aDate.day <= days);
}

unsigned short getMonthLength(unsigned short month, int year)
{
	unsigned short days = 0;
	switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			days = 31;
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			days = 30;
			break;
		case 2:
			days = 28;
			if (isLeapYear(year)) {
				days++;
			}
			break;
	}
	return days;
}

bool isLeapYear(int year)
{
	return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}

results in (and causes Analyser warnings, particularly on component access):
grafik

Metadata

Metadata

Assignees

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions