Skip to content

Commit 32c8097

Browse files
committed
✨ [Sema, Driver, Lex, Frontend] Implement naive #embed for C23 and C++26.
🛠 [Frontend] Ensure commas inserted by #embed are properly serialized to output
1 parent 08b20d8 commit 32c8097

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1888
-658
lines changed

clang/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,14 +300,15 @@ configure_file(
300300
${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
301301

302302
# Add appropriate flags for GCC
303+
option(CLANG_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
303304
if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
304305
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual")
305306
if (NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
306307
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")
307308
endif ()
308309

309310
# Enable -pedantic for Clang even if it's not enabled for LLVM.
310-
if (NOT LLVM_ENABLE_PEDANTIC)
311+
if (NOT LLVM_ENABLE_PEDANTIC AND CLANG_ENABLE_PEDANTIC)
311312
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
312313
endif ()
313314

clang/include/clang/Basic/Builtins.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,6 +1766,9 @@ BUILTIN(__builtin_ms_va_copy, "vc*&c*&", "n")
17661766
// Arithmetic Fence: to prevent FP reordering and reassociation optimizations
17671767
LANGBUILTIN(__arithmetic_fence, "v.", "tE", ALL_LANGUAGES)
17681768

1769+
// preprocessor embed builtin
1770+
LANGBUILTIN(__builtin_pp_embed, "v.", "tE", ALL_LANGUAGES)
1771+
17691772
#undef BUILTIN
17701773
#undef LIBBUILTIN
17711774
#undef LANGBUILTIN

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,13 +708,20 @@ def ReservedIdAsMacro : DiagGroup<"reserved-macro-identifier">;
708708
def ReservedIdAsMacroAlias : DiagGroup<"reserved-id-macro", [ReservedIdAsMacro]>;
709709
def RestrictExpansionMacro : DiagGroup<"restrict-expansion">;
710710
def FinalMacro : DiagGroup<"final-macro">;
711+
// Warnings about unknown preprocessor parameters (e.g. `#embed` and extensions)
712+
def UnsupportedDirective : DiagGroup<"unsupported-directive">;
713+
def UnknownDirectiveParameters : DiagGroup<"unknown-directive-parameters">;
714+
def IgnoredDirectiveParameters : DiagGroup<"ignored-directive-parameters">;
715+
def DirectiveParameters : DiagGroup<"directive-parameters",
716+
[UnknownDirectiveParameters, IgnoredDirectiveParameters]>;
711717

712718
// Just silence warnings about -Wstrict-aliasing for now.
713719
def : DiagGroup<"strict-aliasing=0">;
714720
def : DiagGroup<"strict-aliasing=1">;
715721
def : DiagGroup<"strict-aliasing=2">;
716722
def : DiagGroup<"strict-aliasing">;
717723

724+
718725
// Just silence warnings about -Wstrict-overflow for now.
719726
def : DiagGroup<"strict-overflow=0">;
720727
def : DiagGroup<"strict-overflow=1">;

clang/include/clang/Basic/DiagnosticLexKinds.td

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,22 @@ def warn_cxx23_compat_warning_directive : Warning<
422422
def warn_c23_compat_warning_directive : Warning<
423423
"#warning is incompatible with C standards before C23">,
424424
InGroup<CPre23Compat>, DefaultIgnore;
425+
def warn_c23_pp_embed : Warning<
426+
"'__has_embed' is a C23 extension">,
427+
InGroup<CPre23Compat>,
428+
DefaultIgnore;
429+
def warn_c23_pp_has_embed : Warning<
430+
"'__has_embed' is a C23 extension">,
431+
InGroup<CPre23Compat>,
432+
DefaultIgnore;
433+
def warn_cxx26_pp_embed : Warning<
434+
"'__has_embed' is a C++26 extension">,
435+
InGroup<CXXPre26Compat>,
436+
DefaultIgnore;
437+
def warn_cxx26_pp_has_embed : Warning<
438+
"'__has_embed' is a C++26 extension">,
439+
InGroup<CXXPre26Compat>,
440+
DefaultIgnore;
425441

426442
def ext_pp_extra_tokens_at_eol : ExtWarn<
427443
"extra tokens at end of #%0 directive">, InGroup<ExtraTokens>;
@@ -483,7 +499,13 @@ def ext_pp_gnu_line_directive : Extension<
483499
def err_pp_invalid_directive : Error<
484500
"invalid preprocessing directive%select{|, did you mean '#%1'?}0">;
485501
def warn_pp_invalid_directive : Warning<
486-
err_pp_invalid_directive.Summary>, InGroup<DiagGroup<"unknown-directives">>;
502+
err_pp_invalid_directive.Summary>,
503+
InGroup<UnsupportedDirective>;
504+
def warn_pp_unknown_parameter_ignored : Warning<
505+
"unknown%select{ | embed}0 preprocessor parameter '%1' ignored">,
506+
InGroup<UnknownDirectiveParameters>;
507+
def err_pp_unsupported_directive : Error<
508+
"unsupported%select{ | embed}0 directive: %1">;
487509
def err_pp_directive_required : Error<
488510
"%0 must be used within a preprocessing directive">;
489511
def err_pp_file_not_found : Error<"'%0' file not found">, DefaultFatal;

clang/include/clang/Basic/FileManager.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,13 @@ class FileManager : public RefCountedBase<FileManager> {
276276
/// MemoryBuffer if successful, otherwise returning null.
277277
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
278278
getBufferForFile(FileEntryRef Entry, bool isVolatile = false,
279-
bool RequiresNullTerminator = true);
279+
bool RequiresNullTerminator = true,
280+
std::optional<int64_t> MaybeLimit = std::nullopt);
280281
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
281282
getBufferForFile(StringRef Filename, bool isVolatile = false,
282-
bool RequiresNullTerminator = true) {
283-
return getBufferForFileImpl(Filename, /*FileSize=*/-1, isVolatile,
283+
bool RequiresNullTerminator = true,
284+
std::optional<int64_t> MaybeLimit = std::nullopt) {
285+
return getBufferForFileImpl(Filename, /*FileSize=*/(MaybeLimit ? *MaybeLimit : -1), isVolatile,
284286
RequiresNullTerminator);
285287
}
286288

clang/include/clang/Basic/TokenKinds.def

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ PPKEYWORD(error)
126126
// C99 6.10.6 - Pragma Directive.
127127
PPKEYWORD(pragma)
128128

129+
// C23 & C++26 #embed
130+
PPKEYWORD(embed)
131+
129132
// GNU Extensions.
130133
PPKEYWORD(import)
131134
PPKEYWORD(include_next)
@@ -151,6 +154,10 @@ TOK(eod) // End of preprocessing directive (end of line inside a
151154
// directive).
152155
TOK(code_completion) // Code completion marker
153156

157+
// #embed speed support
158+
TOK(builtin_embed)
159+
160+
154161
// C99 6.4.9: Comments.
155162
TOK(comment) // Comment (only in -E -C[C] mode)
156163

clang/include/clang/Driver/Options.td

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ def IncludePath_Group : OptionGroup<"<I/i group>">, Group<Preprocessor_Group>,
114114
DocBrief<[{
115115
Flags controlling how ``#include``\s are resolved to files.}]>;
116116

117+
def EmbedPath_Group : OptionGroup<"<Embed group>">, Group<Preprocessor_Group>,
118+
DocName<"Embed path management">,
119+
DocBrief<[{
120+
Flags controlling how ``#embed``\s and similar are resolved to files.}]>;
121+
117122
def I_Group : OptionGroup<"<I group>">, Group<IncludePath_Group>, DocFlatten;
118123
def i_Group : OptionGroup<"<i group>">, Group<IncludePath_Group>, DocFlatten;
119124
def clang_i_Group : OptionGroup<"<clang i group>">, Group<i_Group>, DocFlatten;
@@ -816,6 +821,14 @@ will be ignored}]>;
816821
def L : JoinedOrSeparate<["-"], "L">, Flags<[RenderJoined]>, Group<Link_Group>,
817822
Visibility<[ClangOption, FlangOption]>,
818823
MetaVarName<"<dir>">, HelpText<"Add directory to library search path">;
824+
def embed_dir : JoinedOrSeparate<["-"], "embed-dir">,
825+
Flags<[RenderJoined]>, Group<EmbedPath_Group>,
826+
Visibility<[ClangOption, CC1Option, CC1AsOption, FlangOption, FC1Option]>,
827+
MetaVarName<"<dir>">, HelpText<"Add directory to embed search path">;
828+
def embed_dir_EQ : JoinedOrSeparate<["-"], "embed-dir=">,
829+
Flags<[RenderJoined]>, Group<EmbedPath_Group>,
830+
Visibility<[ClangOption, CC1Option, CC1AsOption, FlangOption, FC1Option]>,
831+
MetaVarName<"<dir>">, HelpText<"Add directory to embed search path">;
819832
def MD : Flag<["-"], "MD">, Group<M_Group>,
820833
HelpText<"Write a depfile containing user and system headers">;
821834
def MMD : Flag<["-"], "MMD">, Group<M_Group>,
@@ -1353,6 +1366,9 @@ def dD : Flag<["-"], "dD">, Group<d_Group>, Visibility<[ClangOption, CC1Option]>
13531366
def dI : Flag<["-"], "dI">, Group<d_Group>, Visibility<[ClangOption, CC1Option]>,
13541367
HelpText<"Print include directives in -E mode in addition to normal output">,
13551368
MarshallingInfoFlag<PreprocessorOutputOpts<"ShowIncludeDirectives">>;
1369+
def dE : Flag<["-"], "dE">, Group<d_Group>, Visibility<[ClangOption, CC1Option]>,
1370+
HelpText<"Print embed directives in -E mode in addition to normal output">,
1371+
MarshallingInfoFlag<PreprocessorOutputOpts<"ShowEmbedDirectives">>;
13561372
def dM : Flag<["-"], "dM">, Group<d_Group>, Visibility<[ClangOption, CC1Option]>,
13571373
HelpText<"Print macro definitions in -E mode instead of normal output">;
13581374
def dead__strip : Flag<["-"], "dead_strip">;

clang/include/clang/Frontend/PreprocessorOutputOptions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class PreprocessorOutputOptions {
2222
unsigned ShowMacroComments : 1; ///< Show comments, even in macros.
2323
unsigned ShowMacros : 1; ///< Print macro definitions.
2424
unsigned ShowIncludeDirectives : 1; ///< Print includes, imports etc. within preprocessed output.
25+
unsigned ShowEmbedDirectives : 1; ///< Print embeds, etc. within preprocessed output.
2526
unsigned RewriteIncludes : 1; ///< Preprocess include directives only.
2627
unsigned RewriteImports : 1; ///< Include contents of transitively-imported modules.
2728
unsigned MinimizeWhitespace : 1; ///< Ignore whitespace from input.
@@ -37,6 +38,7 @@ class PreprocessorOutputOptions {
3738
ShowMacroComments = 0;
3839
ShowMacros = 0;
3940
ShowIncludeDirectives = 0;
41+
ShowEmbedDirectives = 0;
4042
RewriteIncludes = 0;
4143
RewriteImports = 0;
4244
MinimizeWhitespace = 0;

0 commit comments

Comments
 (0)