diff options
author | Vitaly Buka <vitalybuka@google.com> | 2024-06-12 13:14:26 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-12 13:14:26 -0700 |
commit | 682d461d5a231cee54d65910e6341769419a67d7 (patch) | |
tree | 1441fb2956b5f3c2f355d442c4de0508f5259a35 /clang/lib/Frontend | |
parent | 294f3ce5dde916c358d8f672b4a1c706c0387154 (diff) | |
download | llvm-682d461d5a231cee54d65910e6341769419a67d7.zip llvm-682d461d5a231cee54d65910e6341769419a67d7.tar.gz llvm-682d461d5a231cee54d65910e6341769419a67d7.tar.bz2 |
Revert "✨ [Sema, Lex, Parse] Preprocessor embed in C and C++ (and Obj-C and Obj-C++ by-proxy)" (#95299)
Reverts llvm/llvm-project#68620
Introduce or expose a memory leak and UB, see llvm/llvm-project#68620
Diffstat (limited to 'clang/lib/Frontend')
-rw-r--r-- | clang/lib/Frontend/CompilerInvocation.cpp | 8 | ||||
-rw-r--r-- | clang/lib/Frontend/DependencyFile.cpp | 25 | ||||
-rw-r--r-- | clang/lib/Frontend/DependencyGraph.cpp | 24 | ||||
-rw-r--r-- | clang/lib/Frontend/InitPreprocessor.cpp | 8 | ||||
-rw-r--r-- | clang/lib/Frontend/PrintPreprocessedOutput.cpp | 122 |
5 files changed, 8 insertions, 179 deletions
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index cde4a84..58694e5 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -4492,9 +4492,6 @@ static void GeneratePreprocessorArgs(const PreprocessorOptions &Opts, if (Opts.DefineTargetOSMacros) GenerateArg(Consumer, OPT_fdefine_target_os_macros); - for (const auto &EmbedEntry : Opts.EmbedEntries) - GenerateArg(Consumer, OPT_embed_dir_EQ, EmbedEntry); - // Don't handle LexEditorPlaceholders. It is implied by the action that is // generated elsewhere. } @@ -4587,11 +4584,6 @@ static bool ParsePreprocessorArgs(PreprocessorOptions &Opts, ArgList &Args, } } - for (const auto *A : Args.filtered(OPT_embed_dir_EQ)) { - StringRef Val = A->getValue(); - Opts.EmbedEntries.push_back(std::string(Val)); - } - // Always avoid lexing editor placeholders when we're just running the // preprocessor as we never want to emit the // "editor placeholder in source file" error in PP only mode. diff --git a/clang/lib/Frontend/DependencyFile.cpp b/clang/lib/Frontend/DependencyFile.cpp index 528eae2..369816e 100644 --- a/clang/lib/Frontend/DependencyFile.cpp +++ b/clang/lib/Frontend/DependencyFile.cpp @@ -62,19 +62,6 @@ struct DepCollectorPPCallbacks : public PPCallbacks { /*IsMissing=*/false); } - void EmbedDirective(SourceLocation, StringRef, bool, - OptionalFileEntryRef File, - const LexEmbedParametersResult &) override { - assert(File && "expected to only be called when the file is found"); - StringRef FileName = - llvm::sys::path::remove_leading_dotslash(File->getName()); - DepCollector.maybeAddDependency(FileName, - /*FromModule*/ false, - /*IsSystem*/ false, - /*IsModuleFile*/ false, - /*IsMissing*/ false); - } - void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, @@ -90,18 +77,6 @@ struct DepCollectorPPCallbacks : public PPCallbacks { // Files that actually exist are handled by FileChanged. } - void HasEmbed(SourceLocation, StringRef, bool, - OptionalFileEntryRef File) override { - if (!File) - return; - StringRef Filename = - llvm::sys::path::remove_leading_dotslash(File->getName()); - DepCollector.maybeAddDependency(Filename, - /*FromModule=*/false, false, - /*IsModuleFile=*/false, - /*IsMissing=*/false); - } - void HasInclude(SourceLocation Loc, StringRef SpelledFilename, bool IsAngled, OptionalFileEntryRef File, SrcMgr::CharacteristicKind FileType) override { diff --git a/clang/lib/Frontend/DependencyGraph.cpp b/clang/lib/Frontend/DependencyGraph.cpp index c23ce66..20e5f23 100644 --- a/clang/lib/Frontend/DependencyGraph.cpp +++ b/clang/lib/Frontend/DependencyGraph.cpp @@ -43,7 +43,7 @@ private: public: DependencyGraphCallback(const Preprocessor *_PP, StringRef OutputFile, StringRef SysRoot) - : PP(_PP), OutputFile(OutputFile.str()), SysRoot(SysRoot.str()) {} + : PP(_PP), OutputFile(OutputFile.str()), SysRoot(SysRoot.str()) { } void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, @@ -53,10 +53,6 @@ public: bool ModuleImported, SrcMgr::CharacteristicKind FileType) override; - void EmbedDirective(SourceLocation HashLoc, StringRef FileName, bool IsAngled, - OptionalFileEntryRef File, - const LexEmbedParametersResult &Params) override; - void EndOfMainFile() override { OutputGraphFile(); } @@ -90,24 +86,6 @@ void DependencyGraphCallback::InclusionDirective( AllFiles.insert(*FromFile); } -void DependencyGraphCallback::EmbedDirective(SourceLocation HashLoc, StringRef, - bool, OptionalFileEntryRef File, - const LexEmbedParametersResult &) { - if (!File) - return; - - SourceManager &SM = PP->getSourceManager(); - OptionalFileEntryRef FromFile = - SM.getFileEntryRefForID(SM.getFileID(SM.getExpansionLoc(HashLoc))); - if (!FromFile) - return; - - Dependencies[*FromFile].push_back(*File); - - AllFiles.insert(*File); - AllFiles.insert(*FromFile); -} - raw_ostream & DependencyGraphCallback::writeNodeReference(raw_ostream &OS, const FileEntry *Node) { diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp index 2d5c94c..e8c8a51 100644 --- a/clang/lib/Frontend/InitPreprocessor.cpp +++ b/clang/lib/Frontend/InitPreprocessor.cpp @@ -508,14 +508,6 @@ static void InitializeStandardPredefinedMacros(const TargetInfo &TI, Builder.defineMacro("__STDC_UTF_16__", "1"); Builder.defineMacro("__STDC_UTF_32__", "1"); - // __has_embed definitions - Builder.defineMacro("__STDC_EMBED_NOT_FOUND__", - llvm::itostr(static_cast<int>(EmbedResult::NotFound))); - Builder.defineMacro("__STDC_EMBED_FOUND__", - llvm::itostr(static_cast<int>(EmbedResult::Found))); - Builder.defineMacro("__STDC_EMBED_EMPTY__", - llvm::itostr(static_cast<int>(EmbedResult::Empty))); - if (LangOpts.ObjC) Builder.defineMacro("__OBJC__"); diff --git a/clang/lib/Frontend/PrintPreprocessedOutput.cpp b/clang/lib/Frontend/PrintPreprocessedOutput.cpp index 0592423..a26d2c3 100644 --- a/clang/lib/Frontend/PrintPreprocessedOutput.cpp +++ b/clang/lib/Frontend/PrintPreprocessedOutput.cpp @@ -11,11 +11,11 @@ // //===----------------------------------------------------------------------===// +#include "clang/Frontend/Utils.h" #include "clang/Basic/CharInfo.h" #include "clang/Basic/Diagnostic.h" #include "clang/Basic/SourceManager.h" #include "clang/Frontend/PreprocessorOutputOptions.h" -#include "clang/Frontend/Utils.h" #include "clang/Lex/MacroInfo.h" #include "clang/Lex/PPCallbacks.h" #include "clang/Lex/Pragma.h" @@ -93,7 +93,6 @@ private: bool DisableLineMarkers; bool DumpDefines; bool DumpIncludeDirectives; - bool DumpEmbedDirectives; bool UseLineDirectives; bool IsFirstFileEntered; bool MinimizeWhitespace; @@ -101,7 +100,6 @@ private: bool KeepSystemIncludes; raw_ostream *OrigOS; std::unique_ptr<llvm::raw_null_ostream> NullOS; - unsigned NumToksToSkip; Token PrevTok; Token PrevPrevTok; @@ -109,16 +107,14 @@ private: public: PrintPPOutputPPCallbacks(Preprocessor &pp, raw_ostream *os, bool lineMarkers, bool defines, bool DumpIncludeDirectives, - bool DumpEmbedDirectives, bool UseLineDirectives, - bool MinimizeWhitespace, bool DirectivesOnly, - bool KeepSystemIncludes) + bool UseLineDirectives, bool MinimizeWhitespace, + bool DirectivesOnly, bool KeepSystemIncludes) : PP(pp), SM(PP.getSourceManager()), ConcatInfo(PP), OS(os), DisableLineMarkers(lineMarkers), DumpDefines(defines), DumpIncludeDirectives(DumpIncludeDirectives), - DumpEmbedDirectives(DumpEmbedDirectives), UseLineDirectives(UseLineDirectives), MinimizeWhitespace(MinimizeWhitespace), DirectivesOnly(DirectivesOnly), - KeepSystemIncludes(KeepSystemIncludes), OrigOS(os), NumToksToSkip(0) { + KeepSystemIncludes(KeepSystemIncludes), OrigOS(os) { CurLine = 0; CurFilename += "<uninit>"; EmittedTokensOnThisLine = false; @@ -133,10 +129,6 @@ public: PrevPrevTok.startToken(); } - /// Returns true if #embed directives should be expanded into a comma- - /// delimited list of integer constants or not. - bool expandEmbedContents() const { return !DumpEmbedDirectives; } - bool isMinimizeWhitespace() const { return MinimizeWhitespace; } void setEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; } @@ -157,9 +149,6 @@ public: void FileChanged(SourceLocation Loc, FileChangeReason Reason, SrcMgr::CharacteristicKind FileType, FileID PrevFID) override; - void EmbedDirective(SourceLocation HashLoc, StringRef FileName, bool IsAngled, - OptionalFileEntryRef File, - const LexEmbedParametersResult &Params) override; void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, @@ -243,9 +232,6 @@ public: void BeginModule(const Module *M); void EndModule(const Module *M); - - unsigned GetNumToksToSkip() const { return NumToksToSkip; } - void ResetSkipToks() { NumToksToSkip = 0; } }; } // end anonymous namespace @@ -413,74 +399,6 @@ void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc, } } -void PrintPPOutputPPCallbacks::EmbedDirective( - SourceLocation HashLoc, StringRef FileName, bool IsAngled, - OptionalFileEntryRef File, const LexEmbedParametersResult &Params) { - if (!DumpEmbedDirectives) - return; - - // The EmbedDirective() callback is called before we produce the annotation - // token stream for the directive. We skip printing the annotation tokens - // within PrintPreprocessedTokens(), but we also need to skip the prefix, - // suffix, and if_empty tokens as those are inserted directly into the token - // stream and would otherwise be printed immediately after printing the - // #embed directive. - // - // FIXME: counting tokens to skip is a kludge but we have no way to know - // which tokens were inserted as part of the embed and which ones were - // explicitly written by the user. - MoveToLine(HashLoc, /*RequireStartOfLine=*/true); - *OS << "#embed " << (IsAngled ? '<' : '"') << FileName - << (IsAngled ? '>' : '"'); - - auto PrintToks = [&](llvm::ArrayRef<Token> Toks) { - SmallString<128> SpellingBuffer; - for (const Token &T : Toks) { - if (T.hasLeadingSpace()) - *OS << " "; - *OS << PP.getSpelling(T, SpellingBuffer); - } - }; - bool SkipAnnotToks = true; - if (Params.MaybeIfEmptyParam) { - *OS << " if_empty("; - PrintToks(Params.MaybeIfEmptyParam->Tokens); - *OS << ")"; - // If the file is empty, we can skip those tokens. If the file is not - // empty, we skip the annotation tokens. - if (File && !File->getSize()) { - NumToksToSkip += Params.MaybeIfEmptyParam->Tokens.size(); - SkipAnnotToks = false; - } - } - - if (Params.MaybeLimitParam) { - *OS << " limit(" << Params.MaybeLimitParam->Limit << ")"; - } - if (Params.MaybeOffsetParam) { - *OS << " clang::offset(" << Params.MaybeOffsetParam->Offset << ")"; - } - if (Params.MaybePrefixParam) { - *OS << " prefix("; - PrintToks(Params.MaybePrefixParam->Tokens); - *OS << ")"; - NumToksToSkip += Params.MaybePrefixParam->Tokens.size(); - } - if (Params.MaybeSuffixParam) { - *OS << " suffix("; - PrintToks(Params.MaybeSuffixParam->Tokens); - *OS << ")"; - NumToksToSkip += Params.MaybeSuffixParam->Tokens.size(); - } - - // We may need to skip the annotation token. - if (SkipAnnotToks) - NumToksToSkip++; - - *OS << " /* clang -E -dE */"; - setEmittedDirectiveOnThisLine(); -} - void PrintPPOutputPPCallbacks::InclusionDirective( SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File, @@ -760,7 +678,7 @@ void PrintPPOutputPPCallbacks::HandleWhitespaceBeforeTok(const Token &Tok, if (Tok.is(tok::eof) || (Tok.isAnnotation() && !Tok.is(tok::annot_header_unit) && !Tok.is(tok::annot_module_begin) && !Tok.is(tok::annot_module_end) && - !Tok.is(tok::annot_repl_input_end) && !Tok.is(tok::annot_embed))) + !Tok.is(tok::annot_repl_input_end))) return; // EmittedDirectiveOnThisLine takes priority over RequireSameLine. @@ -960,27 +878,6 @@ static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok, std::string Name = M->getFullModuleName(); Callbacks->OS->write(Name.data(), Name.size()); Callbacks->HandleNewlinesInToken(Name.data(), Name.size()); - } else if (Tok.is(tok::annot_embed)) { - // Manually explode the binary data out to a stream of comma-delimited - // integer values. If the user passed -dE, that is handled by the - // EmbedDirective() callback. We should only get here if the user did not - // pass -dE. - assert(Callbacks->expandEmbedContents() && - "did not expect an embed annotation"); - auto *Data = - reinterpret_cast<EmbedAnnotationData *>(Tok.getAnnotationValue()); - - // Loop over the contents and print them as a comma-delimited list of - // values. - bool PrintComma = false; - for (auto Iter = Data->BinaryData.begin(), End = Data->BinaryData.end(); - Iter != End; ++Iter) { - if (PrintComma) - *Callbacks->OS << ", "; - *Callbacks->OS << static_cast<unsigned>(*Iter); - PrintComma = true; - } - IsStartOfLine = true; } else if (Tok.isAnnotation()) { // Ignore annotation tokens created by pragmas - the pragmas themselves // will be reproduced in the preprocessed output. @@ -1029,10 +926,6 @@ static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok, if (Tok.is(tok::eof)) break; PP.Lex(Tok); - // If lexing that token causes us to need to skip future tokens, do so now. - for (unsigned I = 0, Skip = Callbacks->GetNumToksToSkip(); I < Skip; ++I) - PP.Lex(Tok); - Callbacks->ResetSkipToks(); } } @@ -1089,9 +982,8 @@ void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS, PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks( PP, OS, !Opts.ShowLineMarkers, Opts.ShowMacros, - Opts.ShowIncludeDirectives, Opts.ShowEmbedDirectives, - Opts.UseLineDirectives, Opts.MinimizeWhitespace, Opts.DirectivesOnly, - Opts.KeepSystemIncludes); + Opts.ShowIncludeDirectives, Opts.UseLineDirectives, + Opts.MinimizeWhitespace, Opts.DirectivesOnly, Opts.KeepSystemIncludes); // Expand macros in pragmas with -fms-extensions. The assumption is that // the majority of pragmas in such a file will be Microsoft pragmas. |