Skip to content

Commit 682d461

Browse files
authored
Revert "✨ [Sema, Lex, Parse] Preprocessor embed in C and C++ (and Obj-C and Obj-C++ by-proxy)" (#95299)
Reverts #68620 Introduce or expose a memory leak and UB, see #68620
1 parent 294f3ce commit 682d461

File tree

96 files changed

+107
-3317
lines changed

Some content is hidden

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

96 files changed

+107
-3317
lines changed

clang-tools-extra/test/pp-trace/pp-trace-macro.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,6 @@ X
3131
// CHECK: MacroNameTok: __STDC_UTF_32__
3232
// CHECK-NEXT: MacroDirective: MD_Define
3333
// CHECK: - Callback: MacroDefined
34-
// CHECK-NEXT: MacroNameTok: __STDC_EMBED_NOT_FOUND__
35-
// CHECK-NEXT: MacroDirective: MD_Define
36-
// CHECK: - Callback: MacroDefined
37-
// CHECK-NEXT: MacroNameTok: __STDC_EMBED_FOUND__
38-
// CHECK-NEXT: MacroDirective: MD_Define
39-
// CHECK: - Callback: MacroDefined
40-
// CHECK-NEXT: MacroNameTok: __STDC_EMBED_EMPTY__
41-
// CHECK-NEXT: MacroDirective: MD_Define
42-
// CHECK: - Callback: MacroDefined
4334
// CHECK: - Callback: MacroDefined
4435
// CHECK-NEXT: MacroNameTok: MACRO
4536
// CHECK-NEXT: MacroDirective: MD_Define

clang/docs/LanguageExtensions.rst

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,7 +1502,6 @@ Attributes on Structured Bindings __cpp_structured_bindings C+
15021502
Designated initializers (N494) C99 C89
15031503
Array & element qualification (N2607) C23 C89
15041504
Attributes (N2335) C23 C89
1505-
``#embed`` (N3017) C23 C89, C++
15061505
============================================ ================================ ============= =============
15071506

15081507
Type Trait Primitives
@@ -5665,26 +5664,3 @@ Compiling different TUs depending on these flags (including use of
56655664
``std::hardware_destructive_interference``) with different compilers, macro
56665665
definitions, or architecture flags will lead to ODR violations and should be
56675666
avoided.
5668-
5669-
``#embed`` Parameters
5670-
=====================
5671-
5672-
``clang::offset``
5673-
-----------------
5674-
The ``clang::offset`` embed parameter may appear zero or one time in the
5675-
embed parameter sequence. Its preprocessor argument clause shall be present and
5676-
have the form:
5677-
5678-
..code-block: text
5679-
5680-
( constant-expression )
5681-
5682-
and shall be an integer constant expression. The integer constant expression
5683-
shall not evaluate to a value less than 0. The token ``defined`` shall not
5684-
appear within the constant expression.
5685-
5686-
The offset will be used when reading the contents of the embedded resource to
5687-
specify the starting offset to begin embedding from. The resources is treated
5688-
as being empty if the specified offset is larger than the number of bytes in
5689-
the resource. The offset will be applied *before* any ``limit`` parameters are
5690-
applied.

clang/include/clang/AST/Expr.h

Lines changed: 0 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -4799,166 +4799,6 @@ class SourceLocExpr final : public Expr {
47994799
friend class ASTStmtReader;
48004800
};
48014801

4802-
/// Stores data related to a single #embed directive.
4803-
struct EmbedDataStorage {
4804-
StringLiteral *Filename;
4805-
StringLiteral *BinaryData;
4806-
size_t getDataElementCount() const { return BinaryData->getByteLength(); }
4807-
};
4808-
4809-
/// Represents a reference to #emded data. By default, this references the whole
4810-
/// range. Otherwise it represents a subrange of data imported by #embed
4811-
/// directive. Needed to handle nested initializer lists with #embed directives.
4812-
/// Example:
4813-
/// struct S {
4814-
/// int x, y;
4815-
/// };
4816-
///
4817-
/// struct T {
4818-
/// int x[2];
4819-
/// struct S s
4820-
/// };
4821-
///
4822-
/// struct T t[] = {
4823-
/// #embed "data" // data contains 10 elements;
4824-
/// };
4825-
///
4826-
/// The resulting semantic form of initializer list will contain (EE stands
4827-
/// for EmbedExpr):
4828-
/// { {EE(first two data elements), {EE(3rd element), EE(4th element) }},
4829-
/// { {EE(5th and 6th element), {EE(7th element), EE(8th element) }},
4830-
/// { {EE(9th and 10th element), { zeroinitializer }}}
4831-
///
4832-
/// EmbedExpr inside of a semantic initializer list and referencing more than
4833-
/// one element can only appear for arrays of scalars.
4834-
class EmbedExpr final : public Expr {
4835-
SourceLocation EmbedKeywordLoc;
4836-
IntegerLiteral *FakeChildNode = nullptr;
4837-
const ASTContext *Ctx = nullptr;
4838-
EmbedDataStorage *Data;
4839-
unsigned Begin = 0;
4840-
unsigned NumOfElements;
4841-
4842-
public:
4843-
EmbedExpr(const ASTContext &Ctx, SourceLocation Loc, EmbedDataStorage *Data,
4844-
unsigned Begin, unsigned NumOfElements);
4845-
explicit EmbedExpr(EmptyShell Empty) : Expr(SourceLocExprClass, Empty) {}
4846-
4847-
SourceLocation getLocation() const { return EmbedKeywordLoc; }
4848-
SourceLocation getBeginLoc() const { return EmbedKeywordLoc; }
4849-
SourceLocation getEndLoc() const { return EmbedKeywordLoc; }
4850-
4851-
StringLiteral *getFilenameStringLiteral() const { return Data->Filename; }
4852-
StringLiteral *getDataStringLiteral() const { return Data->BinaryData; }
4853-
EmbedDataStorage *getData() const { return Data; }
4854-
4855-
unsigned getStartingElementPos() const { return Begin; }
4856-
size_t getDataElementCount() const { return NumOfElements; }
4857-
4858-
// Allows accessing every byte of EmbedExpr data and iterating over it.
4859-
// An Iterator knows the EmbedExpr that it refers to, and an offset value
4860-
// within the data.
4861-
// Dereferencing an Iterator results in construction of IntegerLiteral AST
4862-
// node filled with byte of data of the corresponding EmbedExpr within offset
4863-
// that the Iterator currently has.
4864-
template <bool Const>
4865-
class ChildElementIter
4866-
: public llvm::iterator_facade_base<
4867-
ChildElementIter<Const>, std::random_access_iterator_tag,
4868-
std::conditional_t<Const, const IntegerLiteral *,
4869-
IntegerLiteral *>> {
4870-
friend class EmbedExpr;
4871-
4872-
EmbedExpr *EExpr = nullptr;
4873-
unsigned long long CurOffset = ULLONG_MAX;
4874-
using BaseTy = typename ChildElementIter::iterator_facade_base;
4875-
4876-
ChildElementIter(EmbedExpr *E) : EExpr(E) {
4877-
if (E)
4878-
CurOffset = E->getStartingElementPos();
4879-
}
4880-
4881-
public:
4882-
ChildElementIter() : CurOffset(ULLONG_MAX) {}
4883-
typename BaseTy::reference operator*() const {
4884-
assert(EExpr && CurOffset != ULLONG_MAX &&
4885-
"trying to dereference an invalid iterator");
4886-
IntegerLiteral *N = EExpr->FakeChildNode;
4887-
StringRef DataRef = EExpr->Data->BinaryData->getBytes();
4888-
N->setValue(*EExpr->Ctx,
4889-
llvm::APInt(N->getValue().getBitWidth(), DataRef[CurOffset],
4890-
N->getType()->isSignedIntegerType()));
4891-
// We want to return a reference to the fake child node in the
4892-
// EmbedExpr, not the local variable N.
4893-
return const_cast<typename BaseTy::reference>(EExpr->FakeChildNode);
4894-
}
4895-
typename BaseTy::pointer operator->() const { return **this; }
4896-
using BaseTy::operator++;
4897-
ChildElementIter &operator++() {
4898-
assert(EExpr && "trying to increment an invalid iterator");
4899-
assert(CurOffset != ULLONG_MAX &&
4900-
"Already at the end of what we can iterate over");
4901-
if (++CurOffset >=
4902-
EExpr->getDataElementCount() + EExpr->getStartingElementPos()) {
4903-
CurOffset = ULLONG_MAX;
4904-
EExpr = nullptr;
4905-
}
4906-
return *this;
4907-
}
4908-
bool operator==(ChildElementIter Other) const {
4909-
return (EExpr == Other.EExpr && CurOffset == Other.CurOffset);
4910-
}
4911-
}; // class ChildElementIter
4912-
4913-
public:
4914-
using fake_child_range = llvm::iterator_range<ChildElementIter<false>>;
4915-
using const_fake_child_range = llvm::iterator_range<ChildElementIter<true>>;
4916-
4917-
fake_child_range underlying_data_elements() {
4918-
return fake_child_range(ChildElementIter<false>(this),
4919-
ChildElementIter<false>());
4920-
}
4921-
4922-
const_fake_child_range underlying_data_elements() const {
4923-
return const_fake_child_range(
4924-
ChildElementIter<true>(const_cast<EmbedExpr *>(this)),
4925-
ChildElementIter<true>());
4926-
}
4927-
4928-
child_range children() {
4929-
return child_range(child_iterator(), child_iterator());
4930-
}
4931-
4932-
const_child_range children() const {
4933-
return const_child_range(const_child_iterator(), const_child_iterator());
4934-
}
4935-
4936-
static bool classof(const Stmt *T) {
4937-
return T->getStmtClass() == EmbedExprClass;
4938-
}
4939-
4940-
ChildElementIter<false> begin() { return ChildElementIter<false>(this); }
4941-
4942-
ChildElementIter<true> begin() const {
4943-
return ChildElementIter<true>(const_cast<EmbedExpr *>(this));
4944-
}
4945-
4946-
template <typename Call, typename... Targs>
4947-
bool doForEachDataElement(Call &&C, unsigned &StartingIndexInArray,
4948-
Targs &&...Fargs) const {
4949-
for (auto It : underlying_data_elements()) {
4950-
if (!std::invoke(std::forward<Call>(C), const_cast<IntegerLiteral *>(It),
4951-
StartingIndexInArray, std::forward<Targs>(Fargs)...))
4952-
return false;
4953-
StartingIndexInArray++;
4954-
}
4955-
return true;
4956-
}
4957-
4958-
private:
4959-
friend class ASTStmtReader;
4960-
};
4961-
49624802
/// Describes an C or C++ initializer list.
49634803
///
49644804
/// InitListExpr describes an initializer list, which can be used to

clang/include/clang/AST/RecursiveASTVisitor.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2864,11 +2864,6 @@ DEF_TRAVERSE_STMT(ShuffleVectorExpr, {})
28642864
DEF_TRAVERSE_STMT(ConvertVectorExpr, {})
28652865
DEF_TRAVERSE_STMT(StmtExpr, {})
28662866
DEF_TRAVERSE_STMT(SourceLocExpr, {})
2867-
DEF_TRAVERSE_STMT(EmbedExpr, {
2868-
for (IntegerLiteral *IL : S->underlying_data_elements()) {
2869-
TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(IL);
2870-
}
2871-
})
28722867

28732868
DEF_TRAVERSE_STMT(UnresolvedLookupExpr, {
28742869
TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));

clang/include/clang/AST/TextNodeDumper.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,6 @@ class TextNodeDumper
409409
void VisitHLSLBufferDecl(const HLSLBufferDecl *D);
410410
void VisitOpenACCConstructStmt(const OpenACCConstructStmt *S);
411411
void VisitOpenACCLoopConstruct(const OpenACCLoopConstruct *S);
412-
void VisitEmbedExpr(const EmbedExpr *S);
413412
};
414413

415414
} // namespace clang

clang/include/clang/Basic/DiagnosticCommonKinds.td

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,6 @@ def err_too_large_for_fixed_point : Error<
275275
def err_unimplemented_conversion_with_fixed_point_type : Error<
276276
"conversion between fixed point and %0 is not yet supported">;
277277

278-
def err_requires_positive_value : Error<
279-
"%select{invalid value '%0'; must be positive|value '%0' is too large}1">;
280-
281278
// SEH
282279
def err_seh_expected_handler : Error<
283280
"expected '__except' or '__finally' block">;

clang/include/clang/Basic/DiagnosticLexKinds.td

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -436,14 +436,6 @@ def warn_cxx23_compat_warning_directive : Warning<
436436
def warn_c23_compat_warning_directive : Warning<
437437
"#warning is incompatible with C standards before C23">,
438438
InGroup<CPre23Compat>, DefaultIgnore;
439-
def ext_pp_embed_directive : ExtWarn<
440-
"#embed is a %select{C23|Clang}0 extension">,
441-
InGroup<C23>;
442-
def warn_compat_pp_embed_directive : Warning<
443-
"#embed is incompatible with C standards before C23">,
444-
InGroup<CPre23Compat>, DefaultIgnore;
445-
def err_pp_embed_dup_params : Error<
446-
"cannot specify parameter '%0' twice in the same '#embed' directive">;
447439

448440
def ext_pp_extra_tokens_at_eol : ExtWarn<
449441
"extra tokens at end of #%0 directive">, InGroup<ExtraTokens>;
@@ -513,8 +505,6 @@ def err_pp_invalid_directive : Error<
513505
"invalid preprocessing directive%select{|, did you mean '#%1'?}0">;
514506
def warn_pp_invalid_directive : Warning<
515507
err_pp_invalid_directive.Summary>, InGroup<DiagGroup<"unknown-directives">>;
516-
def err_pp_unknown_parameter : Error<
517-
"unknown%select{ | embed}0 preprocessor parameter '%1'">;
518508
def err_pp_directive_required : Error<
519509
"%0 must be used within a preprocessing directive">;
520510
def err_pp_file_not_found : Error<"'%0' file not found">, DefaultFatal;
@@ -729,8 +719,6 @@ def err_pp_module_build_missing_end : Error<
729719
"no matching '#pragma clang module endbuild' for this '#pragma clang module build'">;
730720

731721
def err_defined_macro_name : Error<"'defined' cannot be used as a macro name">;
732-
def err_defined_in_pp_embed : Error<
733-
"'defined' cannot appear within this context">;
734722
def err_paste_at_start : Error<
735723
"'##' cannot appear at start of macro expansion">;
736724
def err_paste_at_end : Error<"'##' cannot appear at end of macro expansion">;

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,8 @@ def note_surrounding_namespace_starts_here : Note<
10971097
"surrounding namespace with visibility attribute starts here">;
10981098
def err_pragma_loop_invalid_argument_type : Error<
10991099
"invalid argument of type %0; expected an integer type">;
1100+
def err_pragma_loop_invalid_argument_value : Error<
1101+
"%select{invalid value '%0'; must be positive|value '%0' is too large}1">;
11001102
def err_pragma_loop_compatibility : Error<
11011103
"%select{incompatible|duplicate}0 directives '%1' and '%2'">;
11021104
def err_pragma_loop_precedes_nonloop : Error<

clang/include/clang/Basic/FileManager.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -286,15 +286,12 @@ class FileManager : public RefCountedBase<FileManager> {
286286
/// MemoryBuffer if successful, otherwise returning null.
287287
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
288288
getBufferForFile(FileEntryRef Entry, bool isVolatile = false,
289-
bool RequiresNullTerminator = true,
290-
std::optional<int64_t> MaybeLimit = std::nullopt);
289+
bool RequiresNullTerminator = true);
291290
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
292291
getBufferForFile(StringRef Filename, bool isVolatile = false,
293-
bool RequiresNullTerminator = true,
294-
std::optional<int64_t> MaybeLimit = std::nullopt) const {
295-
return getBufferForFileImpl(Filename,
296-
/*FileSize=*/(MaybeLimit ? *MaybeLimit : -1),
297-
isVolatile, RequiresNullTerminator);
292+
bool RequiresNullTerminator = true) const {
293+
return getBufferForFileImpl(Filename, /*FileSize=*/-1, isVolatile,
294+
RequiresNullTerminator);
298295
}
299296

300297
private:

clang/include/clang/Basic/StmtNodes.td

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ def OpaqueValueExpr : StmtNode<Expr>;
204204
def TypoExpr : StmtNode<Expr>;
205205
def RecoveryExpr : StmtNode<Expr>;
206206
def BuiltinBitCastExpr : StmtNode<ExplicitCastExpr>;
207-
def EmbedExpr : StmtNode<Expr>;
208207

209208
// Microsoft Extensions.
210209
def MSPropertyRefExpr : StmtNode<Expr>;

clang/include/clang/Basic/TokenKinds.def

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

129-
// C23 & C++26 #embed
130-
PPKEYWORD(embed)
131-
132129
// GNU Extensions.
133130
PPKEYWORD(import)
134131
PPKEYWORD(include_next)
@@ -1002,9 +999,6 @@ ANNOTATION(header_unit)
1002999
// Annotation for end of input in clang-repl.
10031000
ANNOTATION(repl_input_end)
10041001

1005-
// Annotation for #embed
1006-
ANNOTATION(embed)
1007-
10081002
#undef PRAGMA_ANNOTATION
10091003
#undef ANNOTATION
10101004
#undef TESTING_KEYWORD

clang/include/clang/Driver/Options.td

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -880,9 +880,6 @@ will be ignored}]>;
880880
def L : JoinedOrSeparate<["-"], "L">, Flags<[RenderJoined]>, Group<Link_Group>,
881881
Visibility<[ClangOption, FlangOption]>,
882882
MetaVarName<"<dir>">, HelpText<"Add directory to library search path">;
883-
def embed_dir_EQ : Joined<["--"], "embed-dir=">, Group<Preprocessor_Group>,
884-
Visibility<[ClangOption, CC1Option]>, MetaVarName<"<dir>">,
885-
HelpText<"Add directory to embed search path">;
886883
def MD : Flag<["-"], "MD">, Group<M_Group>,
887884
HelpText<"Write a depfile containing user and system headers">;
888885
def MMD : Flag<["-"], "MMD">, Group<M_Group>,
@@ -1476,9 +1473,6 @@ def dD : Flag<["-"], "dD">, Group<d_Group>, Visibility<[ClangOption, CC1Option]>
14761473
def dI : Flag<["-"], "dI">, Group<d_Group>, Visibility<[ClangOption, CC1Option]>,
14771474
HelpText<"Print include directives in -E mode in addition to normal output">,
14781475
MarshallingInfoFlag<PreprocessorOutputOpts<"ShowIncludeDirectives">>;
1479-
def dE : Flag<["-"], "dE">, Group<d_Group>, Visibility<[CC1Option]>,
1480-
HelpText<"Print embed directives in -E mode in addition to normal output">,
1481-
MarshallingInfoFlag<PreprocessorOutputOpts<"ShowEmbedDirectives">>;
14821476
def dM : Flag<["-"], "dM">, Group<d_Group>, Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
14831477
HelpText<"Print macro definitions in -E mode instead of normal output">;
14841478
def dead__strip : Flag<["-"], "dead_strip">;

clang/include/clang/Frontend/PreprocessorOutputOptions.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ class PreprocessorOutputOptions {
3232
LLVM_PREFERRED_TYPE(bool)
3333
unsigned ShowIncludeDirectives : 1; ///< Print includes, imports etc. within preprocessed output.
3434
LLVM_PREFERRED_TYPE(bool)
35-
unsigned ShowEmbedDirectives : 1; ///< Print embeds, etc. within preprocessed
36-
LLVM_PREFERRED_TYPE(bool)
3735
unsigned RewriteIncludes : 1; ///< Preprocess include directives only.
3836
LLVM_PREFERRED_TYPE(bool)
3937
unsigned RewriteImports : 1; ///< Include contents of transitively-imported modules.
@@ -53,7 +51,6 @@ class PreprocessorOutputOptions {
5351
ShowMacroComments = 0;
5452
ShowMacros = 0;
5553
ShowIncludeDirectives = 0;
56-
ShowEmbedDirectives = 0;
5754
RewriteIncludes = 0;
5855
RewriteImports = 0;
5956
MinimizeWhitespace = 0;

0 commit comments

Comments
 (0)