reference: https://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Flc.htm
The #line directive
A preprocessor line control directive suppliesline numbers for compiler messages. It causes the compiler to view the linenumber of the next source line as the specified number.
#line directive syntax >>-#--line--+-decimal_constant--+-----------------+-+---------->< | '-"--file_name--"-' | '-characters----------------------------'
In order for the compiler to produce meaningful references to line numbersin preprocessed source, the preprocessor inserts#line directiveswhere necessary (for example, at the beginning and after the end of includedtext).
A file name specification enclosed in double quotation marks can followthe line number. If you specify a file name, the compiler views the next lineas part of the specified file. If you do not specify a file name, the compilerviews the next line as part of the current source file.
At the C99 language level, the maximum value of the #line preprocessing directive is 2147483647.
In all C and C++ implementations, the tokensequence on a #line directive is subject to macro replacement.After macro replacement, the resulting character sequence must consist ofa decimal constant, optionally followed by a file name enclosed in doublequotation marks.
You can use #line control directives to make the compilerprovide more meaningful error messages. The following example program uses#line control directives to give each function an easily recognizableline number:
/** ** This example illustrates #line directives. **/ #include <stdio.h> #define LINE200 200 int main(void) { func_1(); func_2(); } #line 100 func_1() { printf("Func_1 - the current line number is %d\n",_ _LINE_ _); } #line LINE200 func_2() { printf("Func_2 - the current line number is %d\n",_ _LINE_ _); }
This program produces the following output:
Func_1 - the current line number is 102 Func_2 - the current line number is 202 http://gcc.gnu.org/onlinedocs/cpp/Line-Control.html6 Line Control
The C preprocessor informs the C compiler of the location in your sourcecode where each token came from. Presently, this is just the file nameand line number. All the tokens resulting from macro expansion arereported as having appeared on the line of the source file where theoutermost macro was used. We intend to be more accurate in the future.
If you write a program which generates source code, such as thebison parser generator, you may want to adjust the preprocessor'snotion of the current file name and line number by hand. Parts of theoutput from bison are generated from scratch, other parts comefrom a standard parser file. The rest are copied verbatim frombison's input. You would like compiler error messages andsymbolic debuggers to be able to refer to
bison
's input file.bison or any such program can arrange this by writing‘#line’ directives into the output file. ‘#line’ is adirective that specifies the original line number and source file namefor subsequent input in the current preprocessor input file. ‘#line’ has three variants:
- linenum is a non-negative decimal integer constant. It specifiesthe line number which should be reported for the following line ofinput. Subsequent lines are counted from linenum.
-
linenum is the same as for the first form, and has the sameeffect. In addition,
filename is a string constant. Thefollowing line and all subsequent lines are reported to come from thefile it specifies, until something else happens to change that.
filename is interpreted according to the normal rules for a stringconstant: backslash escapes are interpreted. This is different from‘
#include’.
Previous versions of CPP did not interpret escapes in ‘#line’;we have changed it because the standard requires they be interpreted,and most other compilers do.
- anything else is checked for macro calls, which are expanded. The result should match one of the above two forms.
#line
linenum
#line
linenum
filename
#line
anything else
‘#line’ directives alter the results of the __FILE__
and__LINE__
predefined macros from that point on. See Standard Predefined Macros. They do not have any effect on ‘#include’'sidea of the directory containing the current file. This is a changefrom GCC 2.95. Previously, a file reading
#line 1 "../src/gram.y" #include "gram.h"
would search for gram.h in ../src, then the -Ichain; the directory containing the physical source file would not besearched. In GCC 3.0 and later, the ‘#include’ is not affected bythe presence of a ‘#line’ referring to a different directory.
We made this change because the old behavior caused problems whengenerated source files were transported between machines. For instance,it is common practice to ship generated parsers with a source release,so that people building the distribution do not need to have yacc orBison installed. These files frequently have ‘#line’ directivesreferring to the directory tree of the system where the distribution wascreated. If GCC tries to search for headers in those directories, thebuild is likely to fail.
The new behavior can cause failures too, if the generated file is notin the same directory as its source and it attempts to include a headerwhich would be visible searching from the directory containing thesource file. However, this problem is easily solved with an additional-I switch on the command line. The failures caused by the oldsemantics could sometimes be corrected only by editing the generatedfiles, which is difficult and error-prone.