реклама на сайте
подробности

 
 
 
Reply to this topicStart new topic
> Баг в IAR
Nikola Kirov
сообщение Dec 17 2006, 18:57
Сообщение #1


Местный
***

Группа: Свой
Сообщений: 256
Регистрация: 4-11-04
Из: Болгария
Пользователь №: 1 050



Если сделат следущее
1) Сделаите проект.
2) Добавите в .c фаил

#define X 5
#define Y X+1
#undef X
#define X Y
#if X != 6
#error
#endif

3) прекомпилируйте...

Етот проект уже невозможно скомпилироват sad.gif

Кажется что проблем в #error . А ето думаю из стандарта и должно работат.
Go to the top of the page
 
+Quote Post
zltigo
сообщение Dec 17 2006, 23:00
Сообщение #2


Гуру
******

Группа: Свой
Сообщений: 13 372
Регистрация: 27-11-04
Из: Riga, Latvia
Пользователь №: 1 244



Цитата(Nikola Kirov @ Dec 17 2006, 17:57) *
#define X 5
#define Y X+1
#undef X
#define X Y
#if X != 6
#error
#endif

Это все эквивалентно:
Код
#if X+1 != 6
#error
#endif

Причем X не определен.
Естественно должно вызвать как минимум выход на #error а как максимум еще сообщение о том,что X не
определен.
Никаких "багов".


--------------------
Feci, quod potui, faciant meliora potentes
Go to the top of the page
 
+Quote Post
Nikola Kirov
сообщение Dec 17 2006, 23:03
Сообщение #3


Местный
***

Группа: Свой
Сообщений: 256
Регистрация: 4-11-04
Из: Болгария
Пользователь №: 1 050



Сделай все как описано.
Увидиш smile.gif

В последней версии IAR-a получается всегда smile.gif
Go to the top of the page
 
+Quote Post
Alex03
сообщение Dec 18 2006, 09:25
Сообщение #4


Местный
***

Группа: Свой
Сообщений: 359
Регистрация: 9-12-05
Пользователь №: 12 034



Я бы сказал что эквивалент:
Код
#define Y X+1
#define X Y
#if X != 6
#error
#endif


Цитата(zltigo @ Dec 18 2006, 01:00) *
Причем X не определен.


А тут интересный момент, X какбы и определён и не определён. smile.gif

Цитата
... а как максимум еще сообщение о том,что X не определен.

Именно тут его сообщения не будет (по крайней мере в GCC) ибо:
Цитата
Identifiers that are not macros, which are all considered to be the number zero. This allows you to write #if MACRO instead of #ifdef MACRO, if you know that MACRO, when defined, will always have a nonzero value. Function-like macros used without their function call parentheses are also treated as zero.

Таким образом "#if X != 6" разворачивается в
Код
#if 0 + 1 != 6

В чём можно убедиться заменив 6 на 1

Цитата(Nikola Kirov @ Dec 17 2006, 20:57) *
Етот проект уже невозможно скомпилироват sad.gif
Кажется что проблем в #error . А ето думаю из стандарта и должно работат.

Nikola Kirov А чего хотелось получить то?

хелп на GCC препроцессор говорит ишо такое:
Цитата
Self-Referential Macros

A self-referential macro is one whose name appears in its definition. Recall that all macro definitions are rescanned for more macros to replace. If the self-reference were considered a use of the macro, it would produce an infinitely large expansion. To prevent this, the self-reference is not considered a macro call. It is passed into the preprocessor output unchanged. Let's consider an example:
#define foo (4 + foo)


where foo is also a variable in your program.

Following the ordinary rules, each reference to foo will expand into (4 + foo); then this will be rescanned and will expand into (4 + (4 + foo)); and so on until the computer runs out of memory.

The self-reference rule cuts this process short after one step, at (4 + foo). Therefore, this macro definition has the possibly useful effect of causing the program to add 4 to the value of foo wherever foo is referred to.

In most cases, it is a bad idea to take advantage of this feature. A person reading the program who sees that foo is a variable will not expect that it is a macro as well. The reader will come across the identifier foo in the program and think its value should be that of the variable foo, whereas in fact the value is four greater.

One common, useful use of self-reference is to create a macro which expands to itself. If you write
#define EPERM EPERM


then the macro EPERM expands to EPERM. Effectively, it is left alone by the preprocessor whenever it's used in running text. You can tell that it's a macro with #ifdef. You might do this if you want to define numeric constants with an enum, but have #ifdef be true for each constant.

If a macro x expands to use a macro y, and the expansion of y refers to the macro x, that is an indirect self-reference of x. x is not expanded in this case either. Thus, if we have
#define x (4 + y)
#define y (2 * x)


then x and y expand as follows:
x ==> (4 + y)
==> (4 + (2 * x))

y ==> (2 * x)
==> (2 * (4 + y))


Each macro is expanded when it appears in the definition of the other macro, but not when it indirectly appears in its own definition.
Go to the top of the page
 
+Quote Post
zltigo
сообщение Dec 18 2006, 11:59
Сообщение #5


Гуру
******

Группа: Свой
Сообщений: 13 372
Регистрация: 27-11-04
Из: Riga, Latvia
Пользователь №: 1 244



Цитата(Nikola Kirov @ Dec 17 2006, 22:03) *
Сделай все как описано.
Увидиш smile.gif

Что я увижу? Предупреждение о неопределенности X? Выход на #error, поскольку 6 не равно 1?
Совершенно правильно - я это увижу и выход на #error и есть совершенно правильное поведение
препроцессора а не баг.


Цитата
Именно тут его сообщения не будет (по крайней мере в GCC) ибо:

Identifiers that are not macros, which are all considered to be the number zero. This allows you to write #if MACRO instead of #ifdef MACRO, if you know that MACRO, when defined, will always have a nonzero value. Function-like macros used without their function call parentheses are also treated as zero.

Некоторые компиляторы, и в том числе IAR, выдают Warning или Note или Remark именно о том, что
неопределенный MACRO был принят равным 0.


--------------------
Feci, quod potui, faciant meliora potentes
Go to the top of the page
 
+Quote Post
Nikola Kirov
сообщение Dec 18 2006, 14:47
Сообщение #6


Местный
***

Группа: Свой
Сообщений: 256
Регистрация: 4-11-04
Из: Болгария
Пользователь №: 1 050



Как вижу никто не пробовал как написано. smile.gif

Как интерпретируется ето совсем ясно.
Баг в то что после первой компиляции проект вообще не может компилится. Можно и убрат потом

#define Y X+1
#define X Y
#if X != 6
#error
#endif

но уже проект не компилируется smile.gif.
Go to the top of the page
 
+Quote Post
zltigo
сообщение Dec 18 2006, 16:42
Сообщение #7


Гуру
******

Группа: Свой
Сообщений: 13 372
Регистрация: 27-11-04
Из: Riga, Latvia
Пользователь №: 1 244



Цитата(Nikola Kirov @ Dec 18 2006, 13:47) *
Как вижу никто не пробовал как написано. smile.gif

Сейчас пробовал в IAR 4.41A
Цитата
но уже проект не компилируется smile.gif.

Ошибка и Remark выдаются.
Если все убрать - все без проблем.


--------------------
Feci, quod potui, faciant meliora potentes
Go to the top of the page
 
+Quote Post
Nikola Kirov
сообщение Dec 18 2006, 16:48
Сообщение #8


Местный
***

Группа: Свой
Сообщений: 256
Регистрация: 4-11-04
Из: Болгария
Пользователь №: 1 050



Станно. Тут попробовали на несколко компютеров.
Резултат один и тоже.

Проект не компилится после ето.

я лично пробовал при
EWARM-EV-WEB-441A.exe
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 чел. читают эту тему (гостей: 1, скрытых пользователей: 0)
Пользователей: 0

 


RSS Текстовая версия Сейчас: 20th July 2025 - 04:17
Рейтинг@Mail.ru


Страница сгенерированна за 0.01443 секунд с 7
ELECTRONIX ©2004-2016