This is an archive of the discontinued LLVM Phabricator instance.

gcc extension # NNN directive lines
ClosedPublic

Authored by urnathan on Nov 8 2021, 10:53 AM.

Details

Summary

Some time back I extended GCC's '# NNN' line marker semantics. Specifically popping to a blank filename will restore the filename to that of the popped-to include.

E.g. restore to line 5 of including file (escaped BOL #'s to avoid git eliding them):

\# 5 "" 2

GCC already had no documentation for this directive extension, I did not add any.

This was useful in developing modules tests, but turned out to also be useful with machine-generated source code. Specifically, a generated include file that itself includes fragments from elsewhere. The ability to pop to the generated include file -- with its full path prefix -- is useful for diagnostic & debug purposes. For instance something like:

Machine generated -- DO NOT EDIT
Type Var = {
\# 7 "encoded.dsl" 1
push to snippet-container
{snippet, of, code}
\# 6 " 2 // Restore to machined-generated source
,
};

// user-code
...
\#include "dsl.h"
...

That pop to "" will restore the filename to '..includepath../dsl.h', which is better than restoring to plain "dsl.h".

Diff Detail

Event Timeline

urnathan created this revision.Nov 8 2021, 10:53 AM
urnathan requested review of this revision.Nov 8 2021, 10:53 AM
bruno accepted this revision.Nov 8 2021, 11:16 PM

Seems like the build failures are unrelated, LGTM with the suggested clang-format changes.

This revision is now accepted and ready to land.Nov 8 2021, 11:16 PM

thanks, I wondered about that format and decided against it because it wasn't a physical line I changed. Now I know :)

GCC already had no documentation for this directive extension, I did not add any.

Was there a reason why GCC didn't document this? Is there a reason Clang shouldn't document it?

clang/lib/Basic/SourceManager.cpp
228

Might as well add the curly braces here thanks to the comment.

clang/lib/Lex/PPDirectives.cpp
1459
1460

Alternatively: Literal.GetStringLength()

clang/test/Preprocessor/line-directive.c
8–11

Can you add test coverage that demonstrates what happens when you pop to a file but you've not entered anything but the main file? e.g., #line "" 2 as the only line other than the RUN line?

126
urnathan updated this revision to Diff 385794.Nov 9 2021, 6:17 AM
urnathan marked 5 inline comments as done.

fine, have some documentation :)

aaron.ballman accepted this revision.Nov 9 2021, 7:13 AM

LGTM aside from some minor nits in the fantastic new documentation. Thank you, Nathan!

clang/docs/LanguageExtensions.rst
47

Spurious newline

4044
4055
4069
urnathan marked 3 inline comments as done.Nov 9 2021, 7:31 AM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptNov 9 2021, 7:31 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
aaronpuchert added inline comments.
clang/docs/LanguageExtensions.rst
4058

Produces

/home/buildbot/llvm-build-dir/clang-sphinx-docs/llvm/build/tools/clang/docs/LanguageExtensions.rst:4059:Inline literal start-string without end-string.

I guess the problem is that the characters are swapped.

urnathan marked an inline comment as done.Nov 9 2021, 3:43 PM
urnathan added inline comments.
clang/docs/LanguageExtensions.rst
4058

I've pushed the obvious fix, but how did I not see this in my local build? I thought I was building docs -- is there some target I need to name?

aaronpuchert added inline comments.Nov 9 2021, 6:02 PM
clang/docs/LanguageExtensions.rst
4058

I think docs aren't built by default, you might need -DLLVM_ENABLE_SPHINX:BOOL=ON -DLLVM_BUILD_DOCS:BOOL=ON on the CMake command line. Then the target should be docs-clang-html.

Of course you'll need to have Sphinx installed.