From f1db58547a7004eefeca08b44f3e7da71ba578a2 Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Tue, 19 May 2009 01:32:34 +0000 Subject: [PATCH] Refactor -dM mode out of the main routine for -E handling. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72090 91177308-0d34-0410-b5e6-96231b3b80d8 --- tools/clang-cc/PrintPreprocessedOutput.cpp | 106 ++++++++++----------- tools/clang-cc/clang-cc.h | 3 + 2 files changed, 56 insertions(+), 53 deletions(-) diff --git a/tools/clang-cc/PrintPreprocessedOutput.cpp b/tools/clang-cc/PrintPreprocessedOutput.cpp index a67f94c813..23a728c0b0 100644 --- a/tools/clang-cc/PrintPreprocessedOutput.cpp +++ b/tools/clang-cc/PrintPreprocessedOutput.cpp @@ -414,67 +414,67 @@ namespace { }; } +void clang::DoPrintMacros(Preprocessor &PP, llvm::raw_ostream *OS) { + // -dM mode just scans and ignores all tokens in the files, then dumps out + // the macro table at the end. + PP.EnterMainSourceFile(); + + Token Tok; + do PP.Lex(Tok); + while (Tok.isNot(tok::eof)); + + std::vector > MacrosByID; + for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end(); + I != E; ++I) + MacrosByID.push_back(*I); + std::sort(MacrosByID.begin(), MacrosByID.end(), SortMacrosByID()); + + for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) { + MacroInfo &MI = *MacrosByID[i].second; + // Ignore computed macros like __LINE__ and friends. + if (MI.isBuiltinMacro()) continue; + + PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS); + *OS << "\n"; + } +} + /// DoPrintPreprocessedInput - This implements -E mode. /// -void clang::DoPrintPreprocessedInput(Preprocessor &PP, - llvm::raw_ostream *OS) { +void clang::DoPrintPreprocessedInput(Preprocessor &PP, llvm::raw_ostream *OS) { + if (DumpMacros) { + DoPrintMacros(PP, OS); + return; + } + // Inform the preprocessor whether we want it to retain comments or not, due // to -C or -CC. PP.SetCommentRetentionState(EnableCommentOutput, EnableMacroCommentOutput); OS->SetBufferSize(64*1024); - - if (DumpMacros) { - // -dM mode just scans and ignores all tokens in the files, then dumps out - // the macro table at the end. - PP.EnterMainSourceFile(); - - Token Tok; - do PP.Lex(Tok); - while (Tok.isNot(tok::eof)); - - std::vector > MacrosByID; - for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end(); - I != E; ++I) - MacrosByID.push_back(*I); - std::sort(MacrosByID.begin(), MacrosByID.end(), SortMacrosByID()); - - for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) { - MacroInfo &MI = *MacrosByID[i].second; - // Ignore computed macros like __LINE__ and friends. - if (MI.isBuiltinMacro()) continue; - PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS); - *OS << "\n"; - } - - } else { - PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(PP, *OS); - PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks)); - PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC", - Callbacks)); - - PP.setPPCallbacks(Callbacks); - - // After we have configured the preprocessor, enter the main file. - PP.EnterMainSourceFile(); - - // 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 - // start. - const SourceManager &SourceMgr = PP.getSourceManager(); - Token Tok; - do PP.Lex(Tok); - while (Tok.isNot(tok::eof) && Tok.getLocation().isFileID() && - !strcmp(SourceMgr.getPresumedLoc(Tok.getLocation()).getFilename(), - "")); - - // Read all the preprocessed tokens, printing them out to the stream. - PrintPreprocessedTokens(PP, Tok, Callbacks, *OS); - *OS << '\n'; - } + PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(PP, *OS); + PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks)); + PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC", + Callbacks)); + + PP.setPPCallbacks(Callbacks); - // Flush the ostream. - OS->flush(); + // After we have configured the preprocessor, enter the main file. + PP.EnterMainSourceFile(); + + // 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 + // start. + const SourceManager &SourceMgr = PP.getSourceManager(); + Token Tok; + do PP.Lex(Tok); + while (Tok.isNot(tok::eof) && Tok.getLocation().isFileID() && + !strcmp(SourceMgr.getPresumedLoc(Tok.getLocation()).getFilename(), + "")); + + // Read all the preprocessed tokens, printing them out to the stream. + PrintPreprocessedTokens(PP, Tok, Callbacks, *OS); + *OS << '\n'; } diff --git a/tools/clang-cc/clang-cc.h b/tools/clang-cc/clang-cc.h index b08345c188..b9c0f11811 100644 --- a/tools/clang-cc/clang-cc.h +++ b/tools/clang-cc/clang-cc.h @@ -40,6 +40,9 @@ bool ProcessWarningOptions(Diagnostic &Diags, bool Pedantic, bool PedanticErrors, bool NoWarnings); +/// DoPrintPreprocessedInput - Implement -E -dM mode. +void DoPrintMacros(Preprocessor &PP, llvm::raw_ostream* OS); + /// DoPrintPreprocessedInput - Implement -E mode. void DoPrintPreprocessedInput(Preprocessor &PP, llvm::raw_ostream* OS); -- 2.40.0