diff options
| author | Dimitry Andric <dim@FreeBSD.org> | 2022-07-03 14:10:23 +0000 |
|---|---|---|
| committer | Dimitry Andric <dim@FreeBSD.org> | 2022-07-03 14:10:23 +0000 |
| commit | 145449b1e420787bb99721a429341fa6be3adfb6 (patch) | |
| tree | 1d56ae694a6de602e348dd80165cf881a36600ed /clang/lib/Frontend/PrintPreprocessedOutput.cpp | |
| parent | ecbca9f5fb7d7613d2b94982c4825eb0d33d6842 (diff) | |
Diffstat (limited to 'clang/lib/Frontend/PrintPreprocessedOutput.cpp')
| -rw-r--r-- | clang/lib/Frontend/PrintPreprocessedOutput.cpp | 44 |
1 files changed, 30 insertions, 14 deletions
diff --git a/clang/lib/Frontend/PrintPreprocessedOutput.cpp b/clang/lib/Frontend/PrintPreprocessedOutput.cpp index 1d0022bda474..e3bd7178aefe 100644 --- a/clang/lib/Frontend/PrintPreprocessedOutput.cpp +++ b/clang/lib/Frontend/PrintPreprocessedOutput.cpp @@ -96,6 +96,7 @@ private: bool UseLineDirectives; bool IsFirstFileEntered; bool MinimizeWhitespace; + bool DirectivesOnly; Token PrevTok; Token PrevPrevTok; @@ -103,12 +104,13 @@ private: public: PrintPPOutputPPCallbacks(Preprocessor &pp, raw_ostream &os, bool lineMarkers, bool defines, bool DumpIncludeDirectives, - bool UseLineDirectives, bool MinimizeWhitespace) + bool UseLineDirectives, bool MinimizeWhitespace, + bool DirectivesOnly) : PP(pp), SM(PP.getSourceManager()), ConcatInfo(PP), OS(os), DisableLineMarkers(lineMarkers), DumpDefines(defines), DumpIncludeDirectives(DumpIncludeDirectives), UseLineDirectives(UseLineDirectives), - MinimizeWhitespace(MinimizeWhitespace) { + MinimizeWhitespace(MinimizeWhitespace), DirectivesOnly(DirectivesOnly) { CurLine = 0; CurFilename += "<uninit>"; EmittedTokensOnThisLine = false; @@ -143,9 +145,9 @@ public: FileID PrevFID) override; void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, - CharSourceRange FilenameRange, const FileEntry *File, - StringRef SearchPath, StringRef RelativePath, - const Module *Imported, + CharSourceRange FilenameRange, + Optional<FileEntryRef> File, StringRef SearchPath, + StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override; void Ident(SourceLocation Loc, StringRef str) override; void PragmaMessage(SourceLocation Loc, StringRef Namespace, @@ -189,7 +191,8 @@ public: bool MoveToLine(const Token &Tok, bool RequireStartOfLine) { PresumedLoc PLoc = SM.getPresumedLoc(Tok.getLocation()); unsigned TargetLine = PLoc.isValid() ? PLoc.getLine() : CurLine; - bool IsFirstInFile = Tok.isAtStartOfLine() && PLoc.getLine() == 1; + bool IsFirstInFile = + Tok.isAtStartOfLine() && PLoc.isValid() && PLoc.getLine() == 1; return MoveToLine(TargetLine, RequireStartOfLine) || IsFirstInFile; } @@ -391,7 +394,7 @@ void PrintPPOutputPPCallbacks::InclusionDirective( StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, - const FileEntry *File, + Optional<FileEntryRef> File, StringRef SearchPath, StringRef RelativePath, const Module *Imported, @@ -466,12 +469,21 @@ void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, StringRef S) { void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok, const MacroDirective *MD) { const MacroInfo *MI = MD->getMacroInfo(); - // Only print out macro definitions in -dD mode. - if (!DumpDefines || + // Print out macro definitions in -dD mode and when we have -fdirectives-only + // for C++20 header units. + if ((!DumpDefines && !DirectivesOnly) || // Ignore __FILE__ etc. - MI->isBuiltinMacro()) return; + MI->isBuiltinMacro()) + return; - MoveToLine(MI->getDefinitionLoc(), /*RequireStartOfLine=*/true); + SourceLocation DefLoc = MI->getDefinitionLoc(); + if (DirectivesOnly && !MI->isUsed()) { + SourceManager &SM = PP.getSourceManager(); + if (SM.isWrittenInBuiltinFile(DefLoc) || + SM.isWrittenInCommandLineFile(DefLoc)) + return; + } + MoveToLine(DefLoc, /*RequireStartOfLine=*/true); PrintMacroDefinition(*MacroNameTok.getIdentifierInfo(), *MI, PP, OS); setEmittedDirectiveOnThisLine(); } @@ -479,8 +491,10 @@ void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok, void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD, const MacroDirective *Undef) { - // Only print out macro definitions in -dD mode. - if (!DumpDefines) return; + // Print out macro definitions in -dD mode and when we have -fdirectives-only + // for C++20 header units. + if (!DumpDefines && !DirectivesOnly) + return; MoveToLine(MacroNameTok.getLocation(), /*RequireStartOfLine=*/true); OS << "#undef " << MacroNameTok.getIdentifierInfo()->getName(); @@ -958,7 +972,7 @@ void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS, PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks( PP, *OS, !Opts.ShowLineMarkers, Opts.ShowMacros, Opts.ShowIncludeDirectives, Opts.UseLineDirectives, - Opts.MinimizeWhitespace); + Opts.MinimizeWhitespace, Opts.DirectivesOnly); // Expand macros in pragmas with -fms-extensions. The assumption is that // the majority of pragmas in such a file will be Microsoft pragmas. @@ -994,6 +1008,8 @@ void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS, // After we have configured the preprocessor, enter the main file. PP.EnterMainSourceFile(); + if (Opts.DirectivesOnly) + PP.SetMacroExpansionOnlyInDirectives(); // Consume all of the tokens that come from the predefines buffer. Those // should not be emitted into the output and are guaranteed to be at the |
