简体   繁体   中英

Does C preprocessed file contain source file line number information?

Here is the problem:
I have a C source file main.c and the corresponding preprocessed file main.i . Given a line in main.i , how can I get the corresponding line number in main.c ?

The C standard does not specify this. GCC and Clang emit lines starting with # followed by a space, a line number, a file name, and other information. . You can determine the line number in main.c :

  • Read the preprocessed file, main.i . After any line with # followed by a space, a number, and a file name that is not “main.c”, read and ignore further lines until there is a # followed by a space, a number, and “main.c”. Remember that number as the current line number.
  • Subsequent lines until another # line in this form are lines from main.c (after preprocessing) with consecutive line numbers continuing from the line number remembered above.

When used as a separate phase to produce a text file, the C preprocessor usually inserts #line directives, sometimes appearing as bare # directives providing the original line number, file name and other contextual information. #line directives have this form:

# line pp-tokens new-line

which after macro substitution should become:

# line digit-sequence new-line

or

# line digit-sequence " s-char-sequence opt " new-line

Where digit-sequence is the line number of the next list in the source stream and the s-char-sequence opt is the name of the file to use for diagnostics and as a replacement for the __FILE__ macro. Subsequent newlines increment the line number thus defined.

#line directives are produced by external programs that generate C code files to make compiler diagnostics point to the original source file.

C Compilers such as gcc and clang produce similar information in their preprocessing output in the form:

# number filename other-information new-line

This syntax is not defined by the C Standard but very common in preprocessing output produced by C compilers. The C standard does not define this output, used mostly for debugging purposes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM