This is an archive of the discontinued LLVM Phabricator instance.

[PoC][Flang] Process "line" Flang preprocessor directive

Authored by domada on Jun 20 2023, 3:56 AM.



Flang "line" preprocessor directive contains information about original source file. Currently this information is missed when user compiles code with -save-temps flag. In consequence we observe:

  1. Different location of the syntax error. If -save-temps flag is attached then Flang reports error in the preprocessed file
  2. Different debug info. If -save-temps flag is attached then debug info points to preprocessed code (not to the original source file)
  3. Mismatch of OpenMP offloading functions names when -save-temps flag is attached because OpenMP offloading function names depend on the name of the source file. For target offloading Flang generates two preprocessed files (for host and the device).

This patch aims to solve these issues by adding support of Flang "line" directive. "line" directive holds information about original source file. If we process "line" directive then we can be sure that the location of the tokens is independent from the status of -save-temps flag.

Detailed discussion about Flang preprocessor and the issues related to token location for Flang and -save-temps file can be found here:

Diff Detail

Event Timeline

domada created this revision.Jun 20 2023, 3:56 AM
Herald added a project: Restricted Project. · View Herald Transcript
domada requested review of this revision.Jun 20 2023, 3:56 AM

Nit: Use braced initialization in the frontend here and elsewhere where it is applicable.


We need a test to show that this works as well.

domada retitled this revision from [PoC][Flang] Use preprocessor line directive to add more accurate token localization for -save-temps flag to [PoC][Flang] Process "line" Flang preprocessor directive.Jun 20 2023, 6:07 AM
domada edited the summary of this revision. (Show Details)
klausler added inline comments.Jun 20 2023, 6:37 AM

Please check my understanding. Are you in fact trying to implement the '#line' preprocessing directive by actually *reading* the original file and prescanning it again to replace the content of the '*.I' file? And doing so without any of the original search path or predefined macro or line length or source format options?

domada added inline comments.Jun 20 2023, 7:55 AM

thank you for your feedback. Could you provide some links to the resources/documentation/discussion about Flang preprocessor which will be helpful for implementation of the "line" directive? Could we modify the "line" directive in similar way to the linenum for GCC : ?

I called this patch as PoC just to get feedback if my approach is feasible. My goal is to force Flang to use original location of the tokens for -save-temps flag. Currently Flang use tokens from the preprocessor if the -save-temps flag is set. It causes several issues which does not appear in Clang. Please read the discussion: to get more details about the issues connected with -save-temps flag.

klausler added inline comments.Jun 20 2023, 8:54 AM

My comment was a question, not "feedback", but I suppose the answer is a "yes". If so, this is not the right approach.

See for an implementation of #line.

domada abandoned this revision.Jul 6 2023, 1:26 AM

Patch not needed. Feature already implemented by