Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "✨ [Sema, Lex, Parse] Preprocessor embed in C and C++ (and Obj-C and Obj-C++ by-proxy)" #95299

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions clang-tools-extra/test/pp-trace/pp-trace-macro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,6 @@ X
// CHECK: MacroNameTok: __STDC_UTF_32__
// CHECK-NEXT: MacroDirective: MD_Define
// CHECK: - Callback: MacroDefined
// CHECK-NEXT: MacroNameTok: __STDC_EMBED_NOT_FOUND__
// CHECK-NEXT: MacroDirective: MD_Define
// CHECK: - Callback: MacroDefined
// CHECK-NEXT: MacroNameTok: __STDC_EMBED_FOUND__
// CHECK-NEXT: MacroDirective: MD_Define
// CHECK: - Callback: MacroDefined
// CHECK-NEXT: MacroNameTok: __STDC_EMBED_EMPTY__
// CHECK-NEXT: MacroDirective: MD_Define
// CHECK: - Callback: MacroDefined
// CHECK: - Callback: MacroDefined
// CHECK-NEXT: MacroNameTok: MACRO
// CHECK-NEXT: MacroDirective: MD_Define
Expand Down
24 changes: 0 additions & 24 deletions clang/docs/LanguageExtensions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1502,7 +1502,6 @@ Attributes on Structured Bindings __cpp_structured_bindings C+
Designated initializers (N494) C99 C89
Array & element qualification (N2607) C23 C89
Attributes (N2335) C23 C89
``#embed`` (N3017) C23 C89, C++
============================================ ================================ ============= =============

Type Trait Primitives
Expand Down Expand Up @@ -5665,26 +5664,3 @@ Compiling different TUs depending on these flags (including use of
``std::hardware_destructive_interference``) with different compilers, macro
definitions, or architecture flags will lead to ODR violations and should be
avoided.

``#embed`` Parameters
=====================

``clang::offset``
-----------------
The ``clang::offset`` embed parameter may appear zero or one time in the
embed parameter sequence. Its preprocessor argument clause shall be present and
have the form:

..code-block: text

( constant-expression )

and shall be an integer constant expression. The integer constant expression
shall not evaluate to a value less than 0. The token ``defined`` shall not
appear within the constant expression.

The offset will be used when reading the contents of the embedded resource to
specify the starting offset to begin embedding from. The resources is treated
as being empty if the specified offset is larger than the number of bytes in
the resource. The offset will be applied *before* any ``limit`` parameters are
applied.
160 changes: 0 additions & 160 deletions clang/include/clang/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -4799,166 +4799,6 @@ class SourceLocExpr final : public Expr {
friend class ASTStmtReader;
};

/// Stores data related to a single #embed directive.
struct EmbedDataStorage {
StringLiteral *Filename;
StringLiteral *BinaryData;
size_t getDataElementCount() const { return BinaryData->getByteLength(); }
};

/// Represents a reference to #emded data. By default, this references the whole
/// range. Otherwise it represents a subrange of data imported by #embed
/// directive. Needed to handle nested initializer lists with #embed directives.
/// Example:
/// struct S {
/// int x, y;
/// };
///
/// struct T {
/// int x[2];
/// struct S s
/// };
///
/// struct T t[] = {
/// #embed "data" // data contains 10 elements;
/// };
///
/// The resulting semantic form of initializer list will contain (EE stands
/// for EmbedExpr):
/// { {EE(first two data elements), {EE(3rd element), EE(4th element) }},
/// { {EE(5th and 6th element), {EE(7th element), EE(8th element) }},
/// { {EE(9th and 10th element), { zeroinitializer }}}
///
/// EmbedExpr inside of a semantic initializer list and referencing more than
/// one element can only appear for arrays of scalars.
class EmbedExpr final : public Expr {
SourceLocation EmbedKeywordLoc;
IntegerLiteral *FakeChildNode = nullptr;
const ASTContext *Ctx = nullptr;
EmbedDataStorage *Data;
unsigned Begin = 0;
unsigned NumOfElements;

public:
EmbedExpr(const ASTContext &Ctx, SourceLocation Loc, EmbedDataStorage *Data,
unsigned Begin, unsigned NumOfElements);
explicit EmbedExpr(EmptyShell Empty) : Expr(SourceLocExprClass, Empty) {}

SourceLocation getLocation() const { return EmbedKeywordLoc; }
SourceLocation getBeginLoc() const { return EmbedKeywordLoc; }
SourceLocation getEndLoc() const { return EmbedKeywordLoc; }

StringLiteral *getFilenameStringLiteral() const { return Data->Filename; }
StringLiteral *getDataStringLiteral() const { return Data->BinaryData; }
EmbedDataStorage *getData() const { return Data; }

unsigned getStartingElementPos() const { return Begin; }
size_t getDataElementCount() const { return NumOfElements; }

// Allows accessing every byte of EmbedExpr data and iterating over it.
// An Iterator knows the EmbedExpr that it refers to, and an offset value
// within the data.
// Dereferencing an Iterator results in construction of IntegerLiteral AST
// node filled with byte of data of the corresponding EmbedExpr within offset
// that the Iterator currently has.
template <bool Const>
class ChildElementIter
: public llvm::iterator_facade_base<
ChildElementIter<Const>, std::random_access_iterator_tag,
std::conditional_t<Const, const IntegerLiteral *,
IntegerLiteral *>> {
friend class EmbedExpr;

EmbedExpr *EExpr = nullptr;
unsigned long long CurOffset = ULLONG_MAX;
using BaseTy = typename ChildElementIter::iterator_facade_base;

ChildElementIter(EmbedExpr *E) : EExpr(E) {
if (E)
CurOffset = E->getStartingElementPos();
}

public:
ChildElementIter() : CurOffset(ULLONG_MAX) {}
typename BaseTy::reference operator*() const {
assert(EExpr && CurOffset != ULLONG_MAX &&
"trying to dereference an invalid iterator");
IntegerLiteral *N = EExpr->FakeChildNode;
StringRef DataRef = EExpr->Data->BinaryData->getBytes();
N->setValue(*EExpr->Ctx,
llvm::APInt(N->getValue().getBitWidth(), DataRef[CurOffset],
N->getType()->isSignedIntegerType()));
// We want to return a reference to the fake child node in the
// EmbedExpr, not the local variable N.
return const_cast<typename BaseTy::reference>(EExpr->FakeChildNode);
}
typename BaseTy::pointer operator->() const { return **this; }
using BaseTy::operator++;
ChildElementIter &operator++() {
assert(EExpr && "trying to increment an invalid iterator");
assert(CurOffset != ULLONG_MAX &&
"Already at the end of what we can iterate over");
if (++CurOffset >=
EExpr->getDataElementCount() + EExpr->getStartingElementPos()) {
CurOffset = ULLONG_MAX;
EExpr = nullptr;
}
return *this;
}
bool operator==(ChildElementIter Other) const {
return (EExpr == Other.EExpr && CurOffset == Other.CurOffset);
}
}; // class ChildElementIter

public:
using fake_child_range = llvm::iterator_range<ChildElementIter<false>>;
using const_fake_child_range = llvm::iterator_range<ChildElementIter<true>>;

fake_child_range underlying_data_elements() {
return fake_child_range(ChildElementIter<false>(this),
ChildElementIter<false>());
}

const_fake_child_range underlying_data_elements() const {
return const_fake_child_range(
ChildElementIter<true>(const_cast<EmbedExpr *>(this)),
ChildElementIter<true>());
}

child_range children() {
return child_range(child_iterator(), child_iterator());
}

const_child_range children() const {
return const_child_range(const_child_iterator(), const_child_iterator());
}

static bool classof(const Stmt *T) {
return T->getStmtClass() == EmbedExprClass;
}

ChildElementIter<false> begin() { return ChildElementIter<false>(this); }

ChildElementIter<true> begin() const {
return ChildElementIter<true>(const_cast<EmbedExpr *>(this));
}

template <typename Call, typename... Targs>
bool doForEachDataElement(Call &&C, unsigned &StartingIndexInArray,
Targs &&...Fargs) const {
for (auto It : underlying_data_elements()) {
if (!std::invoke(std::forward<Call>(C), const_cast<IntegerLiteral *>(It),
StartingIndexInArray, std::forward<Targs>(Fargs)...))
return false;
StartingIndexInArray++;
}
return true;
}

private:
friend class ASTStmtReader;
};

/// Describes an C or C++ initializer list.
///
/// InitListExpr describes an initializer list, which can be used to
Expand Down
5 changes: 0 additions & 5 deletions clang/include/clang/AST/RecursiveASTVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2864,11 +2864,6 @@ DEF_TRAVERSE_STMT(ShuffleVectorExpr, {})
DEF_TRAVERSE_STMT(ConvertVectorExpr, {})
DEF_TRAVERSE_STMT(StmtExpr, {})
DEF_TRAVERSE_STMT(SourceLocExpr, {})
DEF_TRAVERSE_STMT(EmbedExpr, {
for (IntegerLiteral *IL : S->underlying_data_elements()) {
TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(IL);
}
})

DEF_TRAVERSE_STMT(UnresolvedLookupExpr, {
TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
Expand Down
1 change: 0 additions & 1 deletion clang/include/clang/AST/TextNodeDumper.h
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,6 @@ class TextNodeDumper
void VisitHLSLBufferDecl(const HLSLBufferDecl *D);
void VisitOpenACCConstructStmt(const OpenACCConstructStmt *S);
void VisitOpenACCLoopConstruct(const OpenACCLoopConstruct *S);
void VisitEmbedExpr(const EmbedExpr *S);
};

} // namespace clang
Expand Down
3 changes: 0 additions & 3 deletions clang/include/clang/Basic/DiagnosticCommonKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,6 @@ def err_too_large_for_fixed_point : Error<
def err_unimplemented_conversion_with_fixed_point_type : Error<
"conversion between fixed point and %0 is not yet supported">;

def err_requires_positive_value : Error<
"%select{invalid value '%0'; must be positive|value '%0' is too large}1">;

// SEH
def err_seh_expected_handler : Error<
"expected '__except' or '__finally' block">;
Expand Down
12 changes: 0 additions & 12 deletions clang/include/clang/Basic/DiagnosticLexKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -436,14 +436,6 @@ def warn_cxx23_compat_warning_directive : Warning<
def warn_c23_compat_warning_directive : Warning<
"#warning is incompatible with C standards before C23">,
InGroup<CPre23Compat>, DefaultIgnore;
def ext_pp_embed_directive : ExtWarn<
"#embed is a %select{C23|Clang}0 extension">,
InGroup<C23>;
def warn_compat_pp_embed_directive : Warning<
"#embed is incompatible with C standards before C23">,
InGroup<CPre23Compat>, DefaultIgnore;
def err_pp_embed_dup_params : Error<
"cannot specify parameter '%0' twice in the same '#embed' directive">;

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

def err_defined_macro_name : Error<"'defined' cannot be used as a macro name">;
def err_defined_in_pp_embed : Error<
"'defined' cannot appear within this context">;
def err_paste_at_start : Error<
"'##' cannot appear at start of macro expansion">;
def err_paste_at_end : Error<"'##' cannot appear at end of macro expansion">;
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,8 @@ def note_surrounding_namespace_starts_here : Note<
"surrounding namespace with visibility attribute starts here">;
def err_pragma_loop_invalid_argument_type : Error<
"invalid argument of type %0; expected an integer type">;
def err_pragma_loop_invalid_argument_value : Error<
"%select{invalid value '%0'; must be positive|value '%0' is too large}1">;
def err_pragma_loop_compatibility : Error<
"%select{incompatible|duplicate}0 directives '%1' and '%2'">;
def err_pragma_loop_precedes_nonloop : Error<
Expand Down
11 changes: 4 additions & 7 deletions clang/include/clang/Basic/FileManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,12 @@ class FileManager : public RefCountedBase<FileManager> {
/// MemoryBuffer if successful, otherwise returning null.
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
getBufferForFile(FileEntryRef Entry, bool isVolatile = false,
bool RequiresNullTerminator = true,
std::optional<int64_t> MaybeLimit = std::nullopt);
bool RequiresNullTerminator = true);
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
getBufferForFile(StringRef Filename, bool isVolatile = false,
bool RequiresNullTerminator = true,
std::optional<int64_t> MaybeLimit = std::nullopt) const {
return getBufferForFileImpl(Filename,
/*FileSize=*/(MaybeLimit ? *MaybeLimit : -1),
isVolatile, RequiresNullTerminator);
bool RequiresNullTerminator = true) const {
return getBufferForFileImpl(Filename, /*FileSize=*/-1, isVolatile,
RequiresNullTerminator);
}

private:
Expand Down
1 change: 0 additions & 1 deletion clang/include/clang/Basic/StmtNodes.td
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ def OpaqueValueExpr : StmtNode<Expr>;
def TypoExpr : StmtNode<Expr>;
def RecoveryExpr : StmtNode<Expr>;
def BuiltinBitCastExpr : StmtNode<ExplicitCastExpr>;
def EmbedExpr : StmtNode<Expr>;

// Microsoft Extensions.
def MSPropertyRefExpr : StmtNode<Expr>;
Expand Down
6 changes: 0 additions & 6 deletions clang/include/clang/Basic/TokenKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ PPKEYWORD(error)
// C99 6.10.6 - Pragma Directive.
PPKEYWORD(pragma)

// C23 & C++26 #embed
PPKEYWORD(embed)

// GNU Extensions.
PPKEYWORD(import)
PPKEYWORD(include_next)
Expand Down Expand Up @@ -1002,9 +999,6 @@ ANNOTATION(header_unit)
// Annotation for end of input in clang-repl.
ANNOTATION(repl_input_end)

// Annotation for #embed
ANNOTATION(embed)

#undef PRAGMA_ANNOTATION
#undef ANNOTATION
#undef TESTING_KEYWORD
Expand Down
6 changes: 0 additions & 6 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -880,9 +880,6 @@ will be ignored}]>;
def L : JoinedOrSeparate<["-"], "L">, Flags<[RenderJoined]>, Group<Link_Group>,
Visibility<[ClangOption, FlangOption]>,
MetaVarName<"<dir>">, HelpText<"Add directory to library search path">;
def embed_dir_EQ : Joined<["--"], "embed-dir=">, Group<Preprocessor_Group>,
Visibility<[ClangOption, CC1Option]>, MetaVarName<"<dir>">,
HelpText<"Add directory to embed search path">;
def MD : Flag<["-"], "MD">, Group<M_Group>,
HelpText<"Write a depfile containing user and system headers">;
def MMD : Flag<["-"], "MMD">, Group<M_Group>,
Expand Down Expand Up @@ -1476,9 +1473,6 @@ def dD : Flag<["-"], "dD">, Group<d_Group>, Visibility<[ClangOption, CC1Option]>
def dI : Flag<["-"], "dI">, Group<d_Group>, Visibility<[ClangOption, CC1Option]>,
HelpText<"Print include directives in -E mode in addition to normal output">,
MarshallingInfoFlag<PreprocessorOutputOpts<"ShowIncludeDirectives">>;
def dE : Flag<["-"], "dE">, Group<d_Group>, Visibility<[CC1Option]>,
HelpText<"Print embed directives in -E mode in addition to normal output">,
MarshallingInfoFlag<PreprocessorOutputOpts<"ShowEmbedDirectives">>;
def dM : Flag<["-"], "dM">, Group<d_Group>, Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
HelpText<"Print macro definitions in -E mode instead of normal output">;
def dead__strip : Flag<["-"], "dead_strip">;
Expand Down
3 changes: 0 additions & 3 deletions clang/include/clang/Frontend/PreprocessorOutputOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ class PreprocessorOutputOptions {
LLVM_PREFERRED_TYPE(bool)
unsigned ShowIncludeDirectives : 1; ///< Print includes, imports etc. within preprocessed output.
LLVM_PREFERRED_TYPE(bool)
unsigned ShowEmbedDirectives : 1; ///< Print embeds, etc. within preprocessed
LLVM_PREFERRED_TYPE(bool)
unsigned RewriteIncludes : 1; ///< Preprocess include directives only.
LLVM_PREFERRED_TYPE(bool)
unsigned RewriteImports : 1; ///< Include contents of transitively-imported modules.
Expand All @@ -53,7 +51,6 @@ class PreprocessorOutputOptions {
ShowMacroComments = 0;
ShowMacros = 0;
ShowIncludeDirectives = 0;
ShowEmbedDirectives = 0;
RewriteIncludes = 0;
RewriteImports = 0;
MinimizeWhitespace = 0;
Expand Down
Loading
Loading