From f54fce8ff8818cde6b421265f709319f5fea0844 Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Tue, 19 May 2009 01:02:07 +0000 Subject: [PATCH] Switch some utilities in clang-cc to take a stream instead of a filename (or unconditionally using stdout). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72085 91177308-0d34-0410-b5e6-96231b3b80d8 --- tools/clang-cc/CacheTokens.cpp | 13 +- tools/clang-cc/PrintParserCallbacks.cpp | 229 +++++++++++---------- tools/clang-cc/PrintPreprocessedOutput.cpp | 33 +-- tools/clang-cc/RewriteMacros.cpp | 27 +-- tools/clang-cc/RewriteTest.cpp | 7 +- tools/clang-cc/clang-cc.cpp | 24 ++- tools/clang-cc/clang-cc.h | 23 ++- 7 files changed, 165 insertions(+), 191 deletions(-) diff --git a/tools/clang-cc/CacheTokens.cpp b/tools/clang-cc/CacheTokens.cpp index d9827dfaa3..53352360fb 100644 --- a/tools/clang-cc/CacheTokens.cpp +++ b/tools/clang-cc/CacheTokens.cpp @@ -535,16 +535,7 @@ public: } // end anonymous namespace -void clang::CacheTokens(Preprocessor &PP, const std::string &OutFile) { - // Open up the PTH file. - std::string ErrMsg; - llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg); - - if (!ErrMsg.empty()) { - llvm::errs() << "PTH error: " << ErrMsg << "\n"; - return; - } - +void clang::CacheTokens(Preprocessor &PP, llvm::raw_fd_ostream* OS) { // Get the name of the main file. const SourceManager &SrcMgr = PP.getSourceManager(); const FileEntry *MainFile = SrcMgr.getFileEntryForID(SrcMgr.getMainFileID()); @@ -560,7 +551,7 @@ void clang::CacheTokens(Preprocessor &PP, const std::string &OutFile) { } // Create the PTHWriter. - PTHWriter PW(Out, PP); + PTHWriter PW(*OS, PP); // Install the 'stat' system call listener in the FileManager. PP.getFileManager().setStatCache(new StatListener(PW.getPM())); diff --git a/tools/clang-cc/PrintParserCallbacks.cpp b/tools/clang-cc/PrintParserCallbacks.cpp index f99ee277d1..e5b7d0455c 100644 --- a/tools/clang-cc/PrintParserCallbacks.cpp +++ b/tools/clang-cc/PrintParserCallbacks.cpp @@ -15,14 +15,16 @@ #include "clang-cc.h" #include "clang/Parse/Action.h" #include "clang/Parse/DeclSpec.h" -#include "llvm/Support/Streams.h" +#include "llvm/Support/raw_ostream.h" using namespace clang; namespace { class ParserPrintActions : public MinimalAction { - + llvm::raw_ostream& Out; + public: - ParserPrintActions(Preprocessor &PP) : MinimalAction(PP) {} + ParserPrintActions(Preprocessor &PP, llvm::raw_ostream& OS) + : MinimalAction(PP), Out(OS) {} // Printing Functions which also must call MinimalAction @@ -30,13 +32,13 @@ namespace { /// and 'Init' specifies the initializer if any. This is for things like: /// "int X = 4" or "typedef int foo". virtual DeclPtrTy ActOnDeclarator(Scope *S, Declarator &D) { - llvm::cout << __FUNCTION__ << " "; + Out << __FUNCTION__ << " "; if (IdentifierInfo *II = D.getIdentifier()) { - llvm::cout << "'" << II->getName() << "'"; + Out << "'" << II->getName() << "'"; } else { - llvm::cout << ""; + Out << ""; } - llvm::cout << "\n"; + Out << "\n"; // Pass up to EmptyActions so that the symbol table is maintained right. return MinimalAction::ActOnDeclarator(S, D); @@ -44,14 +46,14 @@ namespace { /// ActOnPopScope - This callback is called immediately before the specified /// scope is popped and deleted. virtual void ActOnPopScope(SourceLocation Loc, Scope *S) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return MinimalAction::ActOnPopScope(Loc, S); } /// ActOnTranslationUnitScope - This callback is called once, immediately /// after creating the translation unit scope (in Parser::Initialize). virtual void ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; MinimalAction::ActOnTranslationUnitScope(Loc, S); } @@ -65,7 +67,7 @@ namespace { unsigned NumProtocols, SourceLocation EndProtoLoc, AttributeList *AttrList) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return MinimalAction::ActOnStartClassInterface(AtInterfaceLoc, ClassName, ClassLoc, SuperName, SuperLoc, @@ -78,7 +80,7 @@ namespace { Action::DeclPtrTy ActOnForwardClassDeclaration(SourceLocation AtClassLoc, IdentifierInfo **IdentList, unsigned NumElts) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return MinimalAction::ActOnForwardClassDeclaration(AtClassLoc, IdentList, NumElts); } @@ -90,13 +92,13 @@ namespace { /// with prototypes. S is the function prototype scope for the /// parameters (C++ [basic.scope.proto]). virtual DeclPtrTy ActOnParamDeclarator(Scope *S, Declarator &D) { - llvm::cout << __FUNCTION__ << " "; + Out << __FUNCTION__ << " "; if (IdentifierInfo *II = D.getIdentifier()) { - llvm::cout << "'" << II->getName() << "'"; + Out << "'" << II->getName() << "'"; } else { - llvm::cout << ""; + Out << ""; } - llvm::cout << "\n"; + Out << "\n"; return DeclPtrTy(); } @@ -108,7 +110,7 @@ namespace { /// initializer. The declaration above should still result in a warning, /// since the reference to "xx" is uninitialized. virtual void AddInitializerToDecl(DeclPtrTy Dcl, ExprArg Init) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; } /// FinalizeDeclaratorGroup - After a sequence of declarators are parsed, @@ -116,7 +118,7 @@ namespace { /// a whole. virtual DeclGroupPtrTy FinalizeDeclaratorGroup(Scope *S, DeclPtrTy *Group, unsigned NumDecls) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return DeclGroupPtrTy(); } @@ -125,38 +127,38 @@ namespace { /// information about formal arguments that are part of this function. virtual DeclPtrTy ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D){ - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return DeclPtrTy(); } /// ActOnStartOfFunctionDef - This is called at the start of a function /// definition, after the FunctionDecl has already been created. virtual DeclPtrTy ActOnStartOfFunctionDef(Scope *FnBodyScope, DeclPtrTy D) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return DeclPtrTy(); } virtual void ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; } /// ActOnFunctionDefBody - This is called when a function body has completed /// parsing. Decl is the DeclTy returned by ParseStartOfFunctionDef. virtual DeclPtrTy ActOnFinishFunctionBody(DeclPtrTy Decl, StmtArg Body) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return DeclPtrTy(); } virtual DeclPtrTy ActOnFileScopeAsmDecl(SourceLocation Loc, ExprArg AsmString) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return DeclPtrTy(); } /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with /// no declarator (e.g. "struct foo;") is parsed. virtual DeclPtrTy ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return DeclPtrTy(); } @@ -169,7 +171,7 @@ namespace { SourceLocation RBrace, const char *Lang, unsigned StrSize, DeclPtrTy *Decls, unsigned NumDecls) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return DeclPtrTy(); } @@ -186,7 +188,7 @@ namespace { //===------------------------------------------------------------------===// virtual TypeResult ActOnTypeName(Scope *S, Declarator &D) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return TypeResult(); } @@ -196,7 +198,7 @@ namespace { AttributeList *Attr, AccessSpecifier AS) { // TagType is an instance of DeclSpec::TST, indicating what kind of tag this // is (struct/union/enum/class). - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return DeclPtrTy(); } @@ -205,20 +207,20 @@ namespace { virtual void ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart, IdentifierInfo *ClassName, llvm::SmallVectorImpl &Decls) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; } virtual DeclPtrTy ActOnField(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart, Declarator &D, ExprTy *BitfieldWidth) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return DeclPtrTy(); } virtual DeclPtrTy ActOnIvar(Scope *S, SourceLocation DeclStart, Declarator &D, ExprTy *BitfieldWidth, tok::ObjCKeywordKind visibility) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return DeclPtrTy(); } @@ -226,21 +228,21 @@ namespace { DeclPtrTy *Fields, unsigned NumFields, SourceLocation LBrac, SourceLocation RBrac, AttributeList *AttrList) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; } virtual DeclPtrTy ActOnEnumConstant(Scope *S, DeclPtrTy EnumDecl, DeclPtrTy LastEnumConstant, SourceLocation IdLoc,IdentifierInfo *Id, SourceLocation EqualLoc, ExprTy *Val) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return DeclPtrTy(); } virtual void ActOnEnumBody(SourceLocation EnumLoc, SourceLocation LBraceLoc, SourceLocation RBraceLoc, DeclPtrTy EnumDecl, DeclPtrTy *Elements, unsigned NumElements) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; } //===------------------------------------------------------------------===// @@ -248,7 +250,7 @@ namespace { //===------------------------------------------------------------------===// virtual OwningStmtResult ActOnNullStmt(SourceLocation SemiLoc) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } @@ -256,18 +258,18 @@ namespace { SourceLocation R, MultiStmtArg Elts, bool isStmtExpr) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } virtual OwningStmtResult ActOnDeclStmt(DeclGroupPtrTy Decl, SourceLocation StartLoc, SourceLocation EndLoc) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } virtual OwningStmtResult ActOnExprStmt(FullExprArg Expr) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return OwningStmtResult(*this, Expr->release()); } @@ -278,13 +280,13 @@ namespace { SourceLocation DotDotDotLoc, ExprArg RHSVal, SourceLocation ColonLoc) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } virtual OwningStmtResult ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc, StmtArg SubStmt, Scope *CurScope){ - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } @@ -292,7 +294,7 @@ namespace { IdentifierInfo *II, SourceLocation ColonLoc, StmtArg SubStmt) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } @@ -300,30 +302,30 @@ namespace { FullExprArg CondVal, StmtArg ThenVal, SourceLocation ElseLoc, StmtArg ElseVal) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } virtual OwningStmtResult ActOnStartOfSwitchStmt(ExprArg Cond) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } virtual OwningStmtResult ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtArg Switch, StmtArg Body) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } virtual OwningStmtResult ActOnWhileStmt(SourceLocation WhileLoc, FullExprArg Cond, StmtArg Body) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } virtual OwningStmtResult ActOnDoStmt(SourceLocation DoLoc, StmtArg Body, SourceLocation WhileLoc, ExprArg Cond){ - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } virtual OwningStmtResult ActOnForStmt(SourceLocation ForLoc, @@ -331,7 +333,7 @@ namespace { StmtArg First, ExprArg Second, ExprArg Third, SourceLocation RParenLoc, StmtArg Body) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } virtual OwningStmtResult ActOnObjCForCollectionStmt( @@ -339,34 +341,34 @@ namespace { SourceLocation LParenLoc, StmtArg First, ExprArg Second, SourceLocation RParenLoc, StmtArg Body) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } virtual OwningStmtResult ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, IdentifierInfo *LabelII) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } virtual OwningStmtResult ActOnIndirectGotoStmt(SourceLocation GotoLoc, SourceLocation StarLoc, ExprArg DestExp) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } virtual OwningStmtResult ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } virtual OwningStmtResult ActOnBreakStmt(SourceLocation GotoLoc, Scope *CurScope) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } virtual OwningStmtResult ActOnReturnStmt(SourceLocation ReturnLoc, ExprArg RetValExp) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } virtual OwningStmtResult ActOnAsmStmt(SourceLocation AsmLoc, @@ -380,7 +382,7 @@ namespace { ExprArg AsmString, MultiExprArg Clobbers, SourceLocation RParenLoc) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } @@ -389,54 +391,54 @@ namespace { SourceLocation RParen, DeclPtrTy Parm, StmtArg Body, StmtArg CatchList) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } virtual OwningStmtResult ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtArg Body) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } virtual OwningStmtResult ActOnObjCAtTryStmt(SourceLocation AtLoc, StmtArg Try, StmtArg Catch, StmtArg Finally) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } virtual OwningStmtResult ActOnObjCAtThrowStmt(SourceLocation AtLoc, ExprArg Throw, Scope *CurScope) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } virtual OwningStmtResult ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprArg SynchExpr, StmtArg SynchBody) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } // C++ Statements virtual DeclPtrTy ActOnExceptionDeclarator(Scope *S, Declarator &D) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return DeclPtrTy(); } virtual OwningStmtResult ActOnCXXCatchBlock(SourceLocation CatchLoc, DeclPtrTy ExceptionDecl, StmtArg HandlerBlock) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } virtual OwningStmtResult ActOnCXXTryBlock(SourceLocation TryLoc, StmtArg TryBlock, MultiStmtArg Handlers) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return StmtEmpty(); } @@ -454,7 +456,7 @@ namespace { bool HasTrailingLParen, const CXXScopeSpec *SS, bool isAddressOfOperand) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } @@ -463,7 +465,7 @@ namespace { OverloadedOperatorKind Op, bool HasTrailingLParen, const CXXScopeSpec &SS, bool isAddressOfOperand) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } @@ -471,23 +473,23 @@ namespace { Scope *S, SourceLocation OperatorLoc, TypeTy *Type, bool HasTrailingLParen, const CXXScopeSpec &SS,bool isAddressOfOperand) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } virtual OwningExprResult ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } virtual OwningExprResult ActOnCharacterConstant(const Token &) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } virtual OwningExprResult ActOnNumericConstant(const Token &) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } @@ -495,13 +497,13 @@ namespace { /// fragments (e.g. "foo" "bar" L"baz"). virtual OwningExprResult ActOnStringLiteral(const Token *Toks, unsigned NumToks) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } virtual OwningExprResult ActOnParenExpr(SourceLocation L, SourceLocation R, ExprArg Val) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return move(Val); // Default impl returns operand. } @@ -509,14 +511,14 @@ namespace { virtual OwningExprResult ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, tok::TokenKind Kind, ExprArg Input) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } virtual OwningExprResult ActOnArraySubscriptExpr(Scope *S, ExprArg Base, SourceLocation LLoc, ExprArg Idx, SourceLocation RLoc) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } virtual OwningExprResult ActOnMemberReferenceExpr(Scope *S, ExprArg Base, @@ -525,7 +527,7 @@ namespace { SourceLocation MemberLoc, IdentifierInfo &Member, DeclPtrTy ImplDecl) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } @@ -534,20 +536,20 @@ namespace { MultiExprArg Args, SourceLocation *CommaLocs, SourceLocation RParenLoc) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } // Unary Operators. 'Tok' is the token for the operator. virtual OwningExprResult ActOnUnaryOp(Scope *S, SourceLocation OpLoc, tok::TokenKind Op, ExprArg Input) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } virtual OwningExprResult ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType, void *TyOrEx, const SourceRange &ArgRange) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } @@ -555,25 +557,25 @@ namespace { TypeTy *Ty, SourceLocation RParen, ExprArg Op) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } virtual OwningExprResult ActOnInitList(SourceLocation LParenLoc, MultiExprArg InitList, SourceLocation RParenLoc) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } virtual OwningExprResult ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty, SourceLocation RParenLoc,ExprArg Op){ - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } virtual OwningExprResult ActOnBinOp(Scope *S, SourceLocation TokLoc, tok::TokenKind Kind, ExprArg LHS, ExprArg RHS) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } @@ -583,7 +585,7 @@ namespace { SourceLocation ColonLoc, ExprArg Cond, ExprArg LHS, ExprArg RHS) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } @@ -592,14 +594,14 @@ namespace { virtual OwningExprResult ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, IdentifierInfo *LabelII) {// "&&foo" - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } virtual OwningExprResult ActOnStmtExpr(SourceLocation LPLoc, StmtArg SubStmt, SourceLocation RPLoc) { // "({..})" - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } @@ -610,7 +612,7 @@ namespace { OffsetOfComponent *CompPtr, unsigned NumComponents, SourceLocation RParenLoc) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } @@ -618,7 +620,7 @@ namespace { virtual OwningExprResult ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc, TypeTy *arg1,TypeTy *arg2, SourceLocation RPLoc) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } // __builtin_choose_expr(constExpr, expr1, expr2) @@ -626,7 +628,7 @@ namespace { ExprArg cond, ExprArg expr1, ExprArg expr2, SourceLocation RPLoc) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } @@ -634,43 +636,43 @@ namespace { virtual OwningExprResult ActOnVAArg(SourceLocation BuiltinLoc, ExprArg expr, TypeTy *type, SourceLocation RPLoc) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } virtual OwningExprResult ActOnGNUNullExpr(SourceLocation TokenLoc) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } virtual void ActOnBlockStart(SourceLocation CaretLoc, Scope *CurScope) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; } virtual void ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; } virtual void ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; } virtual OwningExprResult ActOnBlockStmtExpr(SourceLocation CaretLoc, StmtArg Body, Scope *CurScope) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } virtual DeclPtrTy ActOnStartNamespaceDef(Scope *S, SourceLocation IdentLoc, IdentifierInfo *Ident, SourceLocation LBrace) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return DeclPtrTy(); } virtual void ActOnFinishNamespaceDef(DeclPtrTy Dcl, SourceLocation RBrace) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return; } @@ -684,7 +686,7 @@ namespace { SourceLocation IdentLoc, IdentifierInfo *NamespcName, AttributeList *AttrList) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return DeclPtrTy(); } #endif @@ -692,16 +694,16 @@ namespace { virtual void ActOnParamDefaultArgument(DeclPtrTy param, SourceLocation EqualLoc, ExprArg defarg) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; } virtual void ActOnParamUnparsedDefaultArgument(DeclPtrTy param, SourceLocation EqualLoc) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; } virtual void ActOnParamDefaultArgumentError(DeclPtrTy param) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; } virtual void AddCXXDirectInitializerToDecl(DeclPtrTy Dcl, @@ -709,29 +711,29 @@ namespace { MultiExprArg Exprs, SourceLocation *CommaLocs, SourceLocation RParenLoc) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return; } virtual void ActOnStartDelayedCXXMethodDeclaration(Scope *S, DeclPtrTy Method) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; } virtual void ActOnDelayedCXXMethodParameter(Scope *S, DeclPtrTy Param) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; } virtual void ActOnFinishDelayedCXXMethodDeclaration(Scope *S, DeclPtrTy Method) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; } virtual DeclPtrTy ActOnStaticAssertDeclaration(SourceLocation AssertLoc, ExprArg AssertExpr, ExprArg AssertMessageExpr) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return DeclPtrTy(); } @@ -743,7 +745,7 @@ namespace { SourceLocation LParenLoc, ExprArg Op, SourceLocation RParenLoc) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } @@ -751,23 +753,23 @@ namespace { SourceLocation LParenLoc, bool isType, void *TyOrExpr, SourceLocation RParenLoc) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } virtual OwningExprResult ActOnCXXThis(SourceLocation ThisLoc) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } virtual OwningExprResult ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } virtual OwningExprResult ActOnCXXThrow(SourceLocation OpLoc, ExprArg Op) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } @@ -777,7 +779,7 @@ namespace { MultiExprArg Exprs, SourceLocation *CommaLocs, SourceLocation RParenLoc) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } @@ -786,7 +788,7 @@ namespace { Declarator &D, SourceLocation EqualLoc, ExprArg AssignExprVal) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } @@ -799,14 +801,14 @@ namespace { SourceLocation ConstructorLParen, MultiExprArg ConstructorArgs, SourceLocation ConstructorRParen) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } virtual OwningExprResult ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, bool ArrayForm, ExprArg Operand) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } @@ -815,12 +817,13 @@ namespace { SourceLocation LParen, TypeTy *Ty, SourceLocation RParen) { - llvm::cout << __FUNCTION__ << "\n"; + Out << __FUNCTION__ << "\n"; return ExprEmpty(); } }; } -MinimalAction *clang::CreatePrintParserActionsAction(Preprocessor &PP) { - return new ParserPrintActions(PP); +MinimalAction *clang::CreatePrintParserActionsAction(Preprocessor &PP, + llvm::raw_ostream* OS) { + return new ParserPrintActions(PP, *OS); } diff --git a/tools/clang-cc/PrintPreprocessedOutput.cpp b/tools/clang-cc/PrintPreprocessedOutput.cpp index c7eb72b00e..a67f94c813 100644 --- a/tools/clang-cc/PrintPreprocessedOutput.cpp +++ b/tools/clang-cc/PrintPreprocessedOutput.cpp @@ -417,21 +417,12 @@ namespace { /// DoPrintPreprocessedInput - This implements -E mode. /// void clang::DoPrintPreprocessedInput(Preprocessor &PP, - const std::string &OutFile) { + llvm::raw_ostream *OS) { // Inform the preprocessor whether we want it to retain comments or not, due // to -C or -CC. PP.SetCommentRetentionState(EnableCommentOutput, EnableMacroCommentOutput); - - // Open the output buffer using "Binary" mode. On Windows, this distinction - // is important (to surpress automatic LF->CFLF conversion). - std::string Err; - llvm::raw_fd_ostream OS(OutFile.empty() ? "-" : OutFile.c_str(), true, Err); - if (!Err.empty()) { - fprintf(stderr, "%s\n", Err.c_str()); - exit(1); - } - - OS.SetBufferSize(64*1024); + + OS->SetBufferSize(64*1024); if (DumpMacros) { // -dM mode just scans and ignores all tokens in the files, then dumps out @@ -453,12 +444,12 @@ void clang::DoPrintPreprocessedInput(Preprocessor &PP, // Ignore computed macros like __LINE__ and friends. if (MI.isBuiltinMacro()) continue; - PrintMacroDefinition(*MacrosByID[i].first, MI, PP, OS); - OS << "\n"; + PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS); + *OS << "\n"; } } else { - PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(PP, OS); + PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(PP, *OS); PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks)); PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC", Callbacks)); @@ -479,15 +470,11 @@ void clang::DoPrintPreprocessedInput(Preprocessor &PP, "")); // Read all the preprocessed tokens, printing them out to the stream. - PrintPreprocessedTokens(PP, Tok, Callbacks, OS); - OS << '\n'; + PrintPreprocessedTokens(PP, Tok, Callbacks, *OS); + *OS << '\n'; } - + // Flush the ostream. - OS.flush(); - - // If an error occurred, remove the output file. - if (PP.getDiagnostics().hasErrorOccurred() && !OutFile.empty()) - llvm::sys::Path(OutFile).eraseFromDisk(); + OS->flush(); } diff --git a/tools/clang-cc/RewriteMacros.cpp b/tools/clang-cc/RewriteMacros.cpp index d601f865b6..c9c4444ad4 100644 --- a/tools/clang-cc/RewriteMacros.cpp +++ b/tools/clang-cc/RewriteMacros.cpp @@ -85,8 +85,7 @@ static void LexRawTokensFromMainFile(Preprocessor &PP, /// RewriteMacrosInInput - Implement -rewrite-macros mode. -void clang::RewriteMacrosInInput(Preprocessor &PP,const std::string &InFileName, - const std::string &OutFileName) { +void clang::RewriteMacrosInInput(Preprocessor &PP, llvm::raw_ostream *OS) { SourceManager &SM = PP.getSourceManager(); Rewriter Rewrite; @@ -202,35 +201,15 @@ void clang::RewriteMacrosInInput(Preprocessor &PP,const std::string &InFileName, Expansion += ' '; RB.InsertTextBefore(InsertPos, &Expansion[0], Expansion.size()); } - - // Create the output file. - llvm::OwningPtr OwnedStream; - llvm::raw_ostream *OutFile; - if (OutFileName == "-") { - OutFile = &llvm::outs(); - } else if (!OutFileName.empty()) { - std::string Err; - OutFile = new llvm::raw_fd_ostream(OutFileName.c_str(), false, Err); - OwnedStream.reset(OutFile); - } else if (InFileName == "-") { - OutFile = &llvm::outs(); - } else { - llvm::sys::Path Path(InFileName); - Path.eraseSuffix(); - Path.appendSuffix("cpp"); - std::string Err; - OutFile = new llvm::raw_fd_ostream(Path.toString().c_str(), false, Err); - OwnedStream.reset(OutFile); - } // Get the buffer corresponding to MainFileID. If we haven't changed it, then // we are done. if (const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(SM.getMainFileID())) { //printf("Changed:\n"); - *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); + *OS << std::string(RewriteBuf->begin(), RewriteBuf->end()); } else { fprintf(stderr, "No changes\n"); } - OutFile->flush(); + OS->flush(); } diff --git a/tools/clang-cc/RewriteTest.cpp b/tools/clang-cc/RewriteTest.cpp index 00cb4c8a95..c4b3a77956 100644 --- a/tools/clang-cc/RewriteTest.cpp +++ b/tools/clang-cc/RewriteTest.cpp @@ -14,10 +14,9 @@ #include "clang-cc.h" #include "clang/Lex/Preprocessor.h" #include "clang/Rewrite/TokenRewriter.h" -#include +#include "llvm/Support/raw_ostream.h" -void clang::DoRewriteTest(Preprocessor &PP, const std::string &InFileName, - const std::string &OutFileName) { +void clang::DoRewriteTest(Preprocessor &PP, llvm::raw_ostream* OS) { SourceManager &SM = PP.getSourceManager(); const LangOptions &LangOpts = PP.getLangOptions(); @@ -36,5 +35,5 @@ void clang::DoRewriteTest(Preprocessor &PP, const std::string &InFileName, // Print out the output. for (TokenRewriter::token_iterator I = Rewriter.token_begin(), E = Rewriter.token_end(); I != E; ++I) - std::cout << PP.getSpelling(*I); + *OS << PP.getSpelling(*I); } diff --git a/tools/clang-cc/clang-cc.cpp b/tools/clang-cc/clang-cc.cpp index 767ae14148..4bf92577fd 100644 --- a/tools/clang-cc/clang-cc.cpp +++ b/tools/clang-cc/clang-cc.cpp @@ -1748,12 +1748,20 @@ static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF, case GeneratePTH: { llvm::TimeRegion Timer(ClangFrontendTimer); - CacheTokens(PP, OutputFile); + if (OutputFile.empty() || OutputFile == "-") { + // FIXME: Don't fail this way. + // FIXME: Verify that we can actually seek in the given file. + llvm::cerr << "ERROR: PTH requires an seekable file for output!\n"; + ::exit(1); + } + OS.reset(ComputeOutFile(InFile, 0, true, OutPath)); + CacheTokens(PP, static_cast(OS.get())); ClearSourceMgr = true; break; } case PrintPreprocessedInput: + OS.reset(ComputeOutFile(InFile, 0, true, OutPath)); break; case ParseNoop: @@ -1761,7 +1769,8 @@ static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF, case ParsePrintCallbacks: { llvm::TimeRegion Timer(ClangFrontendTimer); - ParseFile(PP, CreatePrintParserActionsAction(PP)); + OS.reset(ComputeOutFile(InFile, 0, true, OutPath)); + ParseFile(PP, CreatePrintParserActionsAction(PP, OS.get())); ClearSourceMgr = true; break; } @@ -1773,15 +1782,16 @@ static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF, } case RewriteMacros: - RewriteMacrosInInput(PP, InFile, OutputFile); + OS.reset(ComputeOutFile(InFile, 0, true, OutPath)); + RewriteMacrosInInput(PP, OS.get()); ClearSourceMgr = true; break; - case RewriteTest: { - DoRewriteTest(PP, InFile, OutputFile); + case RewriteTest: + OS.reset(ComputeOutFile(InFile, 0, true, OutPath)); + DoRewriteTest(PP, OS.get()); ClearSourceMgr = true; break; - } case FixIt: llvm::TimeRegion Timer(ClangFrontendTimer); @@ -1906,7 +1916,7 @@ static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF, ClearSourceMgr = true; } else if (PA == PrintPreprocessedInput){ // -E mode. llvm::TimeRegion Timer(ClangFrontendTimer); - DoPrintPreprocessedInput(PP, OutputFile); + DoPrintPreprocessedInput(PP, OS.get()); ClearSourceMgr = true; } diff --git a/tools/clang-cc/clang-cc.h b/tools/clang-cc/clang-cc.h index d4ee39a4ba..bdd79cdc98 100644 --- a/tools/clang-cc/clang-cc.h +++ b/tools/clang-cc/clang-cc.h @@ -17,6 +17,11 @@ #include #include +namespace llvm { +class raw_ostream; +class raw_fd_ostream; +} + namespace clang { class Preprocessor; class MinimalAction; @@ -33,19 +38,18 @@ class LangOptions; bool ProcessWarningOptions(Diagnostic &Diags); /// DoPrintPreprocessedInput - Implement -E mode. -void DoPrintPreprocessedInput(Preprocessor &PP, const std::string& OutFile); +void DoPrintPreprocessedInput(Preprocessor &PP, llvm::raw_ostream* OS); /// RewriteMacrosInInput - Implement -rewrite-macros mode. -void RewriteMacrosInInput(Preprocessor &PP, const std::string &InFileName, - const std::string& OutFile); +void RewriteMacrosInInput(Preprocessor &PP, llvm::raw_ostream* OS); -void DoRewriteTest(Preprocessor &PP, const std::string &InFileName, - const std::string &OutFileName); - +/// RewriteMacrosInInput - A simple test for the TokenRewriter class. +void DoRewriteTest(Preprocessor &PP, llvm::raw_ostream* OS); /// CreatePrintParserActionsAction - Return the actions implementation that /// implements the -parse-print-callbacks option. -MinimalAction *CreatePrintParserActionsAction(Preprocessor &PP); +MinimalAction *CreatePrintParserActionsAction(Preprocessor &PP, + llvm::raw_ostream* OS); /// CheckDiagnostics - Gather the expected diagnostics and check them. bool CheckDiagnostics(Preprocessor &PP); @@ -54,8 +58,9 @@ bool CheckDiagnostics(Preprocessor &PP); /// This is only done if either -MD or -MMD has been specified. bool CreateDependencyFileGen(Preprocessor *PP, std::string &ErrStr); -/// CacheTokens - Cache tokens for use with PCH. -void CacheTokens(Preprocessor& PP, const std::string& OutFile); +/// CacheTokens - Cache tokens for use with PCH. Note that this requires +/// a seekable stream. +void CacheTokens(Preprocessor& PP, llvm::raw_fd_ostream* OS); /// CreateAnalysisConsumer - Creates an ASTConsumer to run various code /// analysis passes. (The set of analyses run is controlled by command-line -- 2.50.1