Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clang line directive

I'm writing a syntax translator that outputs C++ code and have run into an interesting issue. Say I have two files: ln.x and ln.cpp. In ln.x:

abc

In ln.cpp:

#line 1 "ln.x"
(

When I try to compile it using GCC, it prints the corresponding line in ln.x:

ln.x:1:1: error: expected unqualified-id at end of input
 abc
 ^
ln.x:1:1: error: expected ‘)’ at end of inpu

However, Clang simply prints the line of the same file:

ln.x:1:2: error: expected unqualified-id
(
 ^
ln.x:1:2: error: expected ')'
ln.x:1:1: note: to match this '('
(
^
2 errors generated.

Is there a way to get Clang to print the line of the file like GCC?

like image 306
kirbyfan64sos Avatar asked Oct 31 '22 00:10

kirbyfan64sos


2 Answers

This looks more like a bug than a feature. Why do you want it?

Printing from the file nominated by #line only works as long as the line matches the text getting parsed, character-for-character. And if the file exists in the first place.

I don’t see anything about this in the GCC preprocessor manual (GCC 4.9 edition). However, there is a note that once upon a time (up to 2001), GCC would assume that the named file really existed locally, or at least that its parent directory existed. This could be a holdover bug.

And, no, there's no way to get Clang to do this. It has no concept of attempting to open the file named by #line.

like image 122
Potatoswatter Avatar answered Nov 13 '22 05:11

Potatoswatter


So, since this seemingly isn't possible, I wrote a Perl script to wrap Clang and rewrite the error lines. Kind of hackish, but good enough for now.

like image 33
kirbyfan64sos Avatar answered Nov 13 '22 05:11

kirbyfan64sos