New user self-registration is disabled due to spam. For an account please email bugs-admin@lists.llvm.org with your e-mail address and full name.

Bug 44480 - __FILE__ is modified whenever "#include __FILE__" is encountered
Summary: __FILE__ is modified whenever "#include __FILE__" is encountered
Status: NEW
Alias: None
Product: clang
Classification: Unclassified
Component: C (show other bugs)
Version: 9.0
Hardware: PC Linux
: P enhancement
Assignee: Unassigned Clang Bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-01-07 08:50 PST by omoikane
Modified: 2020-01-07 10:46 PST (History)
6 users (show)

See Also:
Fixed By Commit(s):


Attachments
Demonstration of how __FILE__ is modified (225 bytes, text/x-csrc)
2020-01-07 08:50 PST, omoikane
Details

Note You need to log in before you can comment on or make changes to this bug.
Description omoikane 2020-01-07 08:50:09 PST
Created attachment 22994 [details]
Demonstration of how __FILE__ is modified

Clang's preprocessor appears to prepend "./" to __FILE__ every time it encounters "#include __FILE__" in a source code.  This can be observed by how 3 different strings are printed for this code:

  #ifndef INCLUDE_GUARD
  #define INCLUDE_GUARD
  #include <stdio.h>
  int main(int argc, char **argv)
  {
      puts(__FILE__);
      #include __FILE__
      puts(__FILE__);
      #include __FILE__
      puts(__FILE__);
      return 0;
  }
  #endif

https://gcc.godbolt.org/z/JTx2Np

If a piece of code manages to #include itself a finite but large number of times, "#include __FILE__" will eventually fail because __FILE__ contains a string longer than what's supported by the local file system.

The preprocessor should just keep __FILE__ constant.  Clang 3.5.1 and earlier and all versions of GCC has the expected behavior, Clang 3.6 and later appears to modify __FILE__.