A much simplified version of D17741 which adds a new builtin macro __FILE_NAME__ that is similar to __FILE__ but only renders the last path component. It seems that this functionality is highly desired from the consensus on D17741 as a separate macro versus a much more complicated and niche patch originally proposed in that differential.
Details
- Reviewers
aaron.ballman rsmith rnk dexonsmith shawnl NSProgrammer - Commits
- rZORG6d0e4c72de9c: Reland "[Clang][PP] Add the __FILE_NAME__ builtin macro"
rZORG06e82e5440da: [Clang][PP] Add the __FILE_NAME__ builtin macro.
rG6d0e4c72de9c: Reland "[Clang][PP] Add the __FILE_NAME__ builtin macro"
rG06e82e5440da: [Clang][PP] Add the __FILE_NAME__ builtin macro.
rL360994: [Clang][Docs] Document __FILE_NAME__. NFC
rC360994: [Clang][Docs] Document __FILE_NAME__. NFC
rGbd9748424165: Reland "[Clang][PP] Add the __FILE_NAME__ builtin macro"
rL360938: Reland "[Clang][PP] Add the __FILE_NAME__ builtin macro"
rC360938: Reland "[Clang][PP] Add the __FILE_NAME__ builtin macro"
rG3acc1d1be329: [Clang][PP] Add the __FILE_NAME__ builtin macro.
rL360833: [Clang][PP] Add the __FILE_NAME__ builtin macro.
rC360833: [Clang][PP] Add the __FILE_NAME__ builtin macro.
Diff Detail
- Repository
- rC Clang
Event Timeline
lib/Lex/PPMacroExpansion.cpp | ||
---|---|---|
1510 | What is the path name uses both \ and / to separate paths? I think this needs to be: if (LangOpts.MicrosoftExt) { size_t BackSlash = PLFileName.take_back(PLFileName.size() - LastSep).find_last_of('\\'); if (BackSlash != StringRef::npos) LastSep = BackSlash; } |
There is no documentation specific to the #include directive. https://docs.microsoft.com/en-us/cpp/preprocessor/hash-include-directive-c-cpp?view=vs-2019
However, fileio treats / and \ the same, *sometimes*. https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file
Actually I got it wrong, the path is normalized to use regular slashes at that point, so there is no point in handling backslashes in paths at all even with Microsoft extensions.
After running it with a hack to dump the paths to stderr they seem to be the same regardless.
[FILENAME] /llvm-tainted/tools/clang/test/Preprocessor/Inputs/include-subdir/subdir1/hdr1.h [FILENAME] /llvm-tainted/tools/clang/test/Preprocessor/Inputs/include-subdir/subdir1/hdr2.h
I've also extended the tests to cover scenarios with mixed slashes in MS compatibility mode and removed the unnecessary check.
Need @rsmith to bless this as it's introducing a nonstandard extension, however small it may be. The original diff did have a consensus on it, so I didn't really put up a formal RFC on cfe-dev.
Generally, I think this is a good, useful feature, but there's one point from http://clang.llvm.org/get_involved.html 's checklist for accepting language extensions on which its case is weak, as reflected by this feedback from @alexr on D17741:
I don't think we want to add a fundamental new preprocessor feature like __FILE_BASENAME__ without at least getting some early buy-in from the WG14 and WG21 committees.
Specifically: have you suggested this feature on the WG14 or WG21 mailing lists? I'm not especially opposed to carrying this feature as a Clang extension, but from http://clang.llvm.org/get_involved.html, "Clang should drive the standard, not diverge from it", and we should at least try to push for standardizing this if we think it's worthwhile as an alternative to __FILE__.
Failing that, have you asked the GCC developers whether they'd be amenable to providing this functionality?
Landing this as discussed on IRC, will try to push it forward with WG14.
I think having something like this as part of the standard would benefit a lot, since currently the "unofficial" way of doing this for either GCC or Clang involves several nonstandard builtins and __FILE__ which causes difficulties in reproducible builds as code emitted in read only data sections may still differ depending on optimization levels and other factors. This macro on the other hand provides a guarantee that only the last path component is rendered.
Reverted in rL360842 as Windows bots were failing.
I suspect the MSVCCompat case may need to be handled differently depending on the host OS similar to PPDirectives.cpp:
if (LangOpts.MSVCCompat) { NormalizedPath = Filename.str(); #ifndef _WIN32 llvm::sys::path::native(NormalizedPath); #endif }
Reopening this for now, will try to get a local Windows builder set up and work out what the problem is before relanding.
Can't you just always use llvm::sys::path::filename(PLFileName)? It defaults to the native style.
@rmisth wrote: "Clang should drive the standard, not diverge from it", and we should at least try to push for standardizing this if we think it's worthwhile as an alternative to FILE.
Hi @kristina, have you followed up with the extension on WG14 or WG21 mailing lists? There is a similar GCC feature request which you may want to comment as well https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77488
I did try bringing this up with GCC developers at one point but the overwhelming consensus was that there wasn't a need for such an extension as 'it's possible to just #define FILENAME "foo.c"', with regards to WG14, I was told it was best to get it adopted by other compiler vendors first (ie. GCC). Been pretty sick recently so haven't had time to chase up a lot of loose ends up, should probably get back to that, but it seems WG21 may be the best bet at this point. I'll try to get back on top of everything soon, sorry. Have a pretty big backlog of other things that have also accumulated too.
Thanks for your prompt reply. Hope you will get better soon. I was searching for some unrelated things on GCC's issue tracker when I happened to find Proposal for __FILENAME_ONLY__ which reminded me of this issue. Glad that you've brought up this to them. I just want the compiler extensions to be compatible if they are going to implement the similar thing.
What is the path name uses both \ and / to separate paths? I think this needs to be: