Igor Liferenko
Igor Liferenko

Reputation: 1579

Why #line directives are not processed inside false #if in clang?

Consider the following test.c:

int main(void)
{
  int a;
#if 1==0
#line 1 "test.c"
#endif
  a = 1;
  return 0;
}

Note, that #if condition is false here.

I need to make so, that after performing the following commands the output will not be empty:

clang -g test.c
objdump -D a.out >dis
sed -i 's/line 1/line 2/' test.c
clang -g test.c
objdump -D a.out | diff dis -

To make the difference clear: if we change 1==0 to 1==1 in the example and run the above commands, we get the following output:

749c749
<   33: 05 05 0a c8 05          add    $0x5c80a05,%eax
---
>   33: 05 05 0a c9 05          add    $0x5c90a05,%eax

In other words, I need to make clang always honor #line directives inside #if, even when it is false.

This is necessary to correctly compile output from ctangle. Otherwise warnings, and debug line numbers are all wrong.

This should not be hard to do, because lines inside #if-#endif are scanned anyway.

And #line directives outside #if-#endif (and inside - when it is true) are processed as necessary.

So, I just need to combine these two behaviors - carry out necessary processing for #line directives inside #if-#endif.

Can somebody point me to the right direction how to change clang source? (any clang version will do)

Upvotes: 1

Views: 273

Answers (1)

Igor Liferenko
Igor Liferenko

Reputation: 1579

The problem can be fixed in ctangle.w by outputting #line after each #endif, as follows:

@x
case identifier: a=id_lookup(id_first,id_loc,0)-name_dir;
  app_repl((a / 0400)+0200);
  app_repl(a % 0400); break;
@y
case identifier: a=id_lookup(id_first,id_loc,0)-name_dir;
  app_repl((a / 0400)+0200);
  app_repl(a % 0400);
  if (*buffer=='#' && id_first==buffer+1 && id_loc-id_first==5 && strncmp("endif",id_first,5)==0)
    {@<Insert the line number into |tok_mem|@>}
  break;
@z

Now we can use sections in preprocessor conditionals. And we can use change-files which change preprocessor conditionals.

Upvotes: 1

Related Questions