From: Daniel Dunbar Date: Wed, 11 Nov 2009 10:07:22 +0000 (+0000) Subject: Add PreprocessorOutputOptions, for things like -dM, -C, -CC which control -E X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=775bee71ad21c84bc130af22ac47c1c8e0f9e72f;p=clang Add PreprocessorOutputOptions, for things like -dM, -C, -CC which control -E mode. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@86827 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Frontend/PreprocessorOutputOptions.h b/include/clang/Frontend/PreprocessorOutputOptions.h new file mode 100644 index 0000000000..e375a8545f --- /dev/null +++ b/include/clang/Frontend/PreprocessorOutputOptions.h @@ -0,0 +1,40 @@ +//===--- PreprocessorOutputOptions.h ----------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_FRONTEND_PREPROCESSOROUTPUTOPTIONS_H +#define LLVM_CLANG_FRONTEND_PREPROCESSOROUTPUTOPTIONS_H + +#include +#include + +namespace clang { + +/// PreprocessorOutputOptions - Options for controlling the C preprocessor +/// output (e.g., -E). +class PreprocessorOutputOptions { +public: + unsigned ShowCPP : 1; ///< Print normal preprocessed output. + unsigned ShowMacros : 1; ///< Print macro definitions. + unsigned ShowLineMarkers : 1; ///< Show #line markers. + unsigned ShowComments : 1; /// Show comments. + unsigned ShowMacroComments : 1; /// Show comments, even in macros. + +public: + PreprocessorOutputOptions() { + ShowCPP = 1; + ShowMacros = 0; + ShowLineMarkers = 1; + ShowComments = 0; + ShowMacroComments = 0; + } +}; + +} // end namespace clang + +#endif diff --git a/include/clang/Frontend/Utils.h b/include/clang/Frontend/Utils.h index f0d61c7ae2..ffaaaea30b 100644 --- a/include/clang/Frontend/Utils.h +++ b/include/clang/Frontend/Utils.h @@ -1,4 +1,4 @@ -//===--- Utils.h - Misc utilities for the front-end------------------------===// +//===--- Utils.h - Misc utilities for the front-end -------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -34,6 +34,7 @@ class LangOptions; class MinimalAction; class Preprocessor; class PreprocessorOptions; +class PreprocessorOutputOptions; class SourceManager; class Stmt; class TargetInfo; @@ -56,15 +57,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, - bool EnableCommentOutput, - bool EnableMacroCommentOutput, - bool DisableLineMarkers, - bool DumpDefines); + PreprocessorOutputOptions &Opts); /// RewriteMacrosInInput - Implement -rewrite-macros mode. void RewriteMacrosInInput(Preprocessor &PP, llvm::raw_ostream* OS); diff --git a/lib/Frontend/PrintPreprocessedOutput.cpp b/lib/Frontend/PrintPreprocessedOutput.cpp index 630a093a4b..b1a1936150 100644 --- a/lib/Frontend/PrintPreprocessedOutput.cpp +++ b/lib/Frontend/PrintPreprocessedOutput.cpp @@ -13,13 +13,14 @@ //===----------------------------------------------------------------------===// #include "clang/Frontend/Utils.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Frontend/PreprocessorOutputOptions.h" #include "clang/Lex/MacroInfo.h" #include "clang/Lex/PPCallbacks.h" -#include "clang/Lex/Preprocessor.h" #include "clang/Lex/Pragma.h" +#include "clang/Lex/Preprocessor.h" #include "clang/Lex/TokenConcatenation.h" -#include "clang/Basic/SourceManager.h" -#include "clang/Basic/Diagnostic.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Config/config.h" @@ -439,7 +440,7 @@ namespace { }; } -void clang::DoPrintMacros(Preprocessor &PP, llvm::raw_ostream *OS) { +static void 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(); @@ -467,18 +468,23 @@ void clang::DoPrintMacros(Preprocessor &PP, llvm::raw_ostream *OS) { /// DoPrintPreprocessedInput - This implements -E mode. /// void clang::DoPrintPreprocessedInput(Preprocessor &PP, llvm::raw_ostream *OS, - bool EnableCommentOutput, - bool EnableMacroCommentOutput, - bool DisableLineMarkers, - bool DumpDefines) { + PreprocessorOutputOptions &Opts) { + // Show macros with no output is handled specially. + if (!Opts.ShowCPP) { + assert(Opts.ShowMacros && "Not yet implemented!"); + 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); + PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments); OS->SetBufferSize(64*1024); PrintPPOutputPPCallbacks *Callbacks = - new PrintPPOutputPPCallbacks(PP, *OS, DisableLineMarkers, DumpDefines); + new PrintPPOutputPPCallbacks(PP, *OS, !Opts.ShowLineMarkers, + Opts.ShowMacros); PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks)); PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC", Callbacks)); diff --git a/tools/clang-cc/clang-cc.cpp b/tools/clang-cc/clang-cc.cpp index fd1153ce87..e812e364e8 100644 --- a/tools/clang-cc/clang-cc.cpp +++ b/tools/clang-cc/clang-cc.cpp @@ -16,36 +16,37 @@ //===----------------------------------------------------------------------===// #include "Options.h" -#include "clang/Frontend/AnalysisConsumer.h" +#include "clang/AST/ASTConsumer.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclGroup.h" +#include "clang/Analysis/PathDiagnostic.h" +#include "clang/Basic/FileManager.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Basic/TargetInfo.h" +#include "clang/Basic/Version.h" +#include "clang/CodeGen/ModuleBuilder.h" #include "clang/Frontend/ASTConsumers.h" #include "clang/Frontend/ASTUnit.h" +#include "clang/Frontend/AnalysisConsumer.h" #include "clang/Frontend/ChainedDiagnosticClient.h" +#include "clang/Frontend/CommandLineSourceLoc.h" #include "clang/Frontend/CompilerInvocation.h" #include "clang/Frontend/FixItRewriter.h" #include "clang/Frontend/FrontendDiagnostic.h" #include "clang/Frontend/PCHReader.h" #include "clang/Frontend/PathDiagnosticClients.h" #include "clang/Frontend/PreprocessorOptions.h" +#include "clang/Frontend/PreprocessorOutputOptions.h" #include "clang/Frontend/TextDiagnosticBuffer.h" #include "clang/Frontend/TextDiagnosticPrinter.h" -#include "clang/Frontend/CommandLineSourceLoc.h" #include "clang/Frontend/Utils.h" -#include "clang/Analysis/PathDiagnostic.h" -#include "clang/CodeGen/ModuleBuilder.h" +#include "clang/Lex/HeaderSearch.h" +#include "clang/Lex/LexDiagnostic.h" +#include "clang/Parse/Parser.h" #include "clang/Sema/CodeCompleteConsumer.h" #include "clang/Sema/ParseAST.h" #include "clang/Sema/SemaDiagnostic.h" -#include "clang/AST/ASTConsumer.h" -#include "clang/AST/ASTContext.h" -#include "clang/AST/Decl.h" -#include "clang/AST/DeclGroup.h" -#include "clang/Parse/Parser.h" -#include "clang/Lex/HeaderSearch.h" -#include "clang/Lex/LexDiagnostic.h" -#include "clang/Basic/FileManager.h" -#include "clang/Basic/SourceManager.h" -#include "clang/Basic/TargetInfo.h" -#include "clang/Basic/Version.h" #include "llvm/LLVMContext.h" #include "llvm/ADT/OwningPtr.h" #include "llvm/ADT/SmallPtrSet.h" @@ -919,12 +920,13 @@ static void ProcessInputFile(const CompilerInvocation &CompOpts, case PrintPreprocessedInput: { llvm::TimeRegion Timer(ClangFrontendTimer); - if (DumpMacros) - DoPrintMacros(PP, OS.get()); - else - DoPrintPreprocessedInput(PP, OS.get(), EnableCommentOutput, - EnableMacroCommentOutput, - DisableLineMarkers, DumpDefines); + PreprocessorOutputOptions Opts; + Opts.ShowCPP = !DumpMacros; + Opts.ShowMacros = DumpMacros || DumpDefines; + Opts.ShowLineMarkers = !DisableLineMarkers; + Opts.ShowComments = EnableCommentOutput; + Opts.ShowMacroComments = EnableMacroCommentOutput; + DoPrintPreprocessedInput(PP, OS.get(), Opts); ClearSourceMgr = true; }