The preprocessor is conditionally distributed between the `# include` files

Is it legal?

foo.h

    #if 1
    #include "bar.h"

bar.h

    #endif

foo.c

    #include "foo.h"

clang-700.1.81 complaining about something funny:

    In file included from foo.c:1:
    In file included from ./foo.h:2:
    ./bar.h:1:2: error: #endif without #if
    #endif

(which means what he did #include "bar.h")

    # 3 "./foo.h" 2
    In file included from foo.c:1:
    ./foo.h:1:2: error: unterminated conditional directive
    #ifndef FOO

(that means it wasn’t #include "bar.h").

I had a difficult feeling that the preprocessor cares for something more than the standard. Please correct me if I am wrong, or prove otherwise if I am right.

+4
source share
3 answers

It's illegal. All conditional directives that make up a conditional section must be in the same file, although conditional groups can contain directives #includeor nested conditional sections.

(& section; 6.10).

      preprocessing-file:
               group opt
      group:
               group-part
               group group-part
      group-part:
               if-section
               control-line
               text-line
               # non-directive
      if-section:
               if-group elif-groups opt else-group opt endif-line

4 5.1.1.2, #include 1 4, :

ainclude preprocessing , 1 4. .

+4

. , C11 .

§ 6.10, , #if #endif preprocessing-file. , §5.1.1.1 :

5.1.1.1

1 C . , ( ) . , #include

+2

ISO/IEC 9899 3:

[cut] . []

, , #if, #endif.

+1

Source: https://habr.com/ru/post/1658646/


All Articles