Index: include/clang/AST/PrettyPrinter.h
===================================================================
--- include/clang/AST/PrettyPrinter.h	(revision 119144)
+++ include/clang/AST/PrettyPrinter.h	(working copy)
@@ -27,7 +27,8 @@
 class PrinterHelper {
 public:
   virtual ~PrinterHelper();
-  virtual bool handledStmt(Stmt* E, llvm::raw_ostream& OS) = 0;
+  virtual bool handledStmt(Stmt* E, llvm::raw_ostream& OS, 
+                           unsigned IndentLevel) = 0;
 };
 
 /// \brief Describes how types, statements, expressions, and
@@ -35,14 +36,11 @@
 struct PrintingPolicy {
   /// \brief Create a default printing policy for C.
   PrintingPolicy(const LangOptions &LO)
-    : Indentation(2), LangOpts(LO), SuppressSpecifiers(false),
+    : LangOpts(LO), SuppressSpecifiers(false),
       SuppressTag(false), SuppressScope(false),
       Dump(false), ConstantArraySizeAsWritten(false),
       AnonymousTagLocations(true) { }
 
-  /// \brief The number of spaces to use to indent each line.
-  unsigned Indentation : 8;
-
   /// \brief What language we're printing.
   const LangOptions &LangOpts;
 
Index: include/clang/Basic/LangOptions.h
===================================================================
--- include/clang/Basic/LangOptions.h	(revision 119144)
+++ include/clang/Basic/LangOptions.h	(working copy)
@@ -112,6 +112,20 @@
   // FIXME: This is just a temporary option, for testing purposes.
   unsigned NoBitFieldTypeAlign : 1;
 
+  // The two rewrite options are placed here: while these options aren't 
+  // literally language options they don't fit anywhere else. 
+  // Esp. in FrontendOptions they would cause a lot of new dependencies.
+
+  /// \brief Indent style (see wikipedia:"Indent style" for details).
+  enum IndentStyle { 
+    KRStyle, 
+    /*same as Allman style:*/ ANSIStyle  
+  };
+
+  unsigned Indentation : 8;                ///< The number of spaces to use to 
+                                           /// indent each line during rewrite. 
+  unsigned IndentStyle : 1;                ///< The indent style during rewrite 
+
 private:
   // We declare multibit enums as unsigned because MSVC insists on making enums
   // signed.  Set/Query these values using accessors.
@@ -192,6 +206,8 @@
     DumpVTableLayouts = 0;
     SpellChecking = 1;
     NoBitFieldTypeAlign = 0;
+    Indentation = 2;
+    IndentStyle = KRStyle;
   }
 
   GCMode getGCMode() const { return (GCMode) GC; }
Index: include/clang/Driver/CC1Options.td
===================================================================
--- include/clang/Driver/CC1Options.td	(revision 119144)
+++ include/clang/Driver/CC1Options.td	(working copy)
@@ -384,6 +384,16 @@
 def fix_what_you_can : Flag<"-fix-what-you-can">,
   HelpText<"Apply fix-it advice even in the presence of unfixable errors">;
 
+// Rewriter Output Options (placed in LangOptions.h, see there for reasons)
+def rewrite_indent : Separate<"-rewrite-indent">,
+  HelpText<"Indentation of formatted rewrite output, default 2">;
+def rewrite_indent_EQ : Joined<"-rewrite-indent=">,
+  Alias<rewrite_indent>;
+def rewrite_style : Separate<"-rewrite-style">,
+  HelpText<"Style of formatted rewrite output: KR|ANSI, default KR">;
+def rewrite_style_EQ : Joined<"-rewrite-style=">,
+  Alias<rewrite_style>;
+
 // Generic forwarding to LLVM options. This should only be used for debugging
 // and experimental features.
 def mllvm : Separate<"-mllvm">,
Index: include/clang/Rewrite/Rewriter.h
===================================================================
--- include/clang/Rewrite/Rewriter.h	(revision 119144)
+++ include/clang/Rewrite/Rewriter.h	(working copy)
@@ -89,14 +89,6 @@
   void ReplaceText(unsigned OrigOffset, unsigned OrigLength,
                    llvm::StringRef NewStr);
 
-private:  // Methods only usable by Rewriter.
-
-  /// Initialize - Start this rewrite buffer out with a copy of the unmodified
-  /// input buffer.
-  void Initialize(const char *BufStart, const char *BufEnd) {
-    Buffer.assign(BufStart, BufEnd);
-  }
-
   /// getMappedOffset - Given an offset into the original SourceBuffer that this
   /// RewriteBuffer is based on, map it into the offset space of the
   /// RewriteBuffer.  If AfterInserts is true and if the OrigOffset indicates a
@@ -107,6 +99,14 @@
     return Deltas.getDeltaAt(2*OrigOffset+AfterInserts)+OrigOffset;
   }
 
+private:  // Methods only usable by Rewriter.
+
+  /// Initialize - Start this rewrite buffer out with a copy of the unmodified
+  /// input buffer.
+  void Initialize(const char *BufStart, const char *BufEnd) {
+    Buffer.assign(BufStart, BufEnd);
+  }
+
   /// AddInsertDelta - When an insertion is made at a position, this
   /// method is used to record that information.
   void AddInsertDelta(unsigned OrigOffset, int Change) {
Index: lib/Analysis/CFG.cpp
===================================================================
--- lib/Analysis/CFG.cpp	(revision 119144)
+++ lib/Analysis/CFG.cpp	(working copy)
@@ -2873,7 +2873,8 @@
   void setBlockID(signed i) { CurrentBlock = i; }
   void setStmtID(unsigned i) { CurrentStmt = i; }
 
-  virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
+  virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS, unsigned) {
+
     StmtMapTy::iterator I = StmtMap.find(S);
 
     if (I == StmtMap.end())
@@ -3017,7 +3018,7 @@
 
         if (Sub->child_begin() != Sub->child_end()) {
           OS << "({ ... ; ";
-          Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
+          Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS,0);
           OS << " })\n";
           return;
         }
@@ -3026,7 +3027,7 @@
       if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
         if (B->getOpcode() == BO_Comma) {
           OS << "... , ";
-          Helper->handledStmt(B->getRHS(),OS);
+          Helper->handledStmt(B->getRHS(),OS,0);
           OS << '\n';
           return;
         }
Index: lib/AST/DeclPrinter.cpp
===================================================================
--- lib/AST/DeclPrinter.cpp	(revision 119144)
+++ lib/AST/DeclPrinter.cpp	(working copy)
@@ -34,6 +34,7 @@
     void ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls);
 
     void Print(AccessSpecifier AS);
+    void PrintBracedDeclContext(DeclContext *DC);
 
   public:
     DeclPrinter(llvm::raw_ostream &Out, ASTContext &Context,
@@ -166,7 +167,7 @@
 
 llvm::raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
   for (unsigned i = 0; i != Indentation; ++i)
-    Out << "  ";
+    Out << ' ';
   return Out;
 }
 
@@ -187,13 +188,25 @@
   }
 }
 
+void DeclPrinter::PrintBracedDeclContext(DeclContext *DC)
+{
+  if (Policy.LangOpts.IndentStyle == LangOptions::ANSIStyle) {
+    Out << "\n";
+    Indent() << "{\n";
+  } else {
+    Out << " {\n";
+  }
+  VisitDeclContext(DC);
+  Indent() << "}";
+}
+
 //----------------------------------------------------------------------------
 // Common C declarations
 //----------------------------------------------------------------------------
 
 void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
   if (Indent)
-    Indentation += Policy.Indentation;
+    Indentation += Policy.LangOpts.Indentation;
 
   llvm::SmallVector<Decl*, 2> Decls;
   for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
@@ -249,11 +262,11 @@
     }
 
     if (isa<AccessSpecDecl>(*D)) {
-      Indentation -= Policy.Indentation;
+      Indentation -= Policy.LangOpts.Indentation;
       this->Indent();
       Print(D->getAccess());
       Out << ":\n";
-      Indentation += Policy.Indentation;
+      Indentation += Policy.LangOpts.Indentation;
       continue;
     }
 
@@ -291,7 +304,7 @@
     ProcessDeclGroup(Decls);
 
   if (Indent)
-    Indentation -= Policy.Indentation;
+    Indentation -= Policy.LangOpts.Indentation;
 }
 
 void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
@@ -307,9 +320,8 @@
 }
 
 void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
-  Out << "enum " << D << " {\n";
-  VisitDeclContext(D);
-  Indent() << "}";
+  Out << "enum " << D;
+  PrintBracedDeclContext(D);
 }
 
 void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
@@ -318,9 +330,7 @@
     Out << ' ' << D;
 
   if (D->isDefinition()) {
-    Out << " {\n";
-    VisitDeclContext(D);
-    Indent() << "}";
+    PrintBracedDeclContext(D);
   }
 }
 
@@ -476,15 +486,19 @@
       // parameters.
       Out << '\n';
       DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
-      Indentation += Policy.Indentation;
+      Indentation += Policy.LangOpts.Indentation;
       for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
         Indent();
         ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
         Out << ";\n";
       }
-      Indentation -= Policy.Indentation;
-    } else
+      Indentation -= Policy.LangOpts.Indentation;
+    } else if (Policy.LangOpts.IndentStyle == LangOptions::ANSIStyle) {
+      Out << "\n";
+      Indent();
+    } else {
       Out << ' ';
+    }
 
     D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
     Out << '\n';
@@ -591,9 +605,7 @@
 
     // Print the class definition
     // FIXME: Doesn't print access specifiers, e.g., "public:"
-    Out << " {\n";
-    VisitDeclContext(D);
-    Indent() << "}";
+    PrintBracedDeclContext(D);
   }
 }
 
@@ -751,12 +763,12 @@
 
   if (OID->ivar_size() > 0) {
     Out << "{\n";
-    Indentation += Policy.Indentation;
+    Indentation += Policy.LangOpts.Indentation;
     for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
          E = OID->ivar_end(); I != E; ++I) {
       Indent() << (*I)->getType().getAsString(Policy) << ' ' << *I << ";\n";
     }
-    Indentation -= Policy.Indentation;
+    Indentation -= Policy.LangOpts.Indentation;
     Out << "}\n";
   }
 
Index: lib/AST/StmtPrinter.cpp
===================================================================
--- lib/AST/StmtPrinter.cpp	(revision 119144)
+++ lib/AST/StmtPrinter.cpp	(working copy)
@@ -41,7 +41,7 @@
         Policy(Policy) {}
 
     void PrintStmt(Stmt *S) {
-      PrintStmt(S, Policy.Indentation);
+      PrintStmt(S, Policy.LangOpts.Indentation);
     }
 
     void PrintStmt(Stmt *S, int SubIndent) {
@@ -64,6 +64,8 @@
     void PrintRawDeclStmt(DeclStmt *S);
     void PrintRawIfStmt(IfStmt *If);
     void PrintRawCXXCatchStmt(CXXCatchStmt *Catch);
+    void PrintRawANSIStyleStmt(Stmt* Node, bool newLineAtStart = true);
+    bool PrintBody(Stmt* Node, bool intermediateStmt);
 
     void PrintExpr(Expr *E) {
       if (E)
@@ -73,13 +75,14 @@
     }
 
     llvm::raw_ostream &Indent(int Delta = 0) {
-      for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
-        OS << "  ";
+      for (int i = 0, e = IndentLevel+Delta*Policy.LangOpts.Indentation; 
+           i < e; ++i)
+        OS << " ";
       return OS;
     }
 
     void Visit(Stmt* S) {
-      if (Helper && Helper->handledStmt(S,OS))
+      if (Helper && Helper->handledStmt(S,OS, IndentLevel))
           return;
       else StmtVisitor<StmtPrinter>::Visit(S);
     }
@@ -103,6 +106,45 @@
 //  Stmt printing methods.
 //===----------------------------------------------------------------------===//
 
+void StmtPrinter::PrintRawANSIStyleStmt(Stmt* Node, bool newLineAtStart)
+{
+  assert(Policy.LangOpts.IndentStyle == LangOptions::ANSIStyle);
+  if (newLineAtStart)
+    OS << "\n";
+
+  if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node)) {
+    if (Helper == 0 || !Helper->handledStmt(CS, OS, IndentLevel)) {
+      Indent();
+      PrintRawCompoundStmt(CS);
+      OS << "\n";
+    }
+  } else {
+    Indent() << "{\n";
+    PrintStmt(Node);
+    Indent() << "}\n";
+  }
+}
+
+/// Prints a 'body' stmt (as in for or switch - stmt's)
+/// return true, if eol was the last printed character
+bool StmtPrinter::PrintBody(Stmt* Node, bool intermediateStmt)
+{
+  if (Policy.LangOpts.IndentStyle == LangOptions::ANSIStyle) {
+    PrintRawANSIStyleStmt(Node);
+  } else if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node)) {
+    // Pretty print compoundstmt bodies (very common).
+    OS << " ";
+    PrintRawCompoundStmt(CS);
+    if (intermediateStmt) 
+      return false;
+    OS << "\n";
+  } else {
+    OS << "\n";
+    PrintStmt(Node);
+  }
+  return true;
+}
+
 /// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
 /// with no newline after the }.
 void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
@@ -169,31 +211,19 @@
   OS << "if (";
   PrintExpr(If->getCond());
   OS << ')';
+  Stmt *Else = If->getElse();
+  bool lastCharEol = PrintBody(If->getThen(), Else != NULL);
+  if (Else != NULL) {
+    if (lastCharEol)
+      Indent() << "else";
+    else
+      OS << " else";
 
-  if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
-    OS << ' ';
-    PrintRawCompoundStmt(CS);
-    OS << (If->getElse() ? ' ' : '\n');
-  } else {
-    OS << '\n';
-    PrintStmt(If->getThen());
-    if (If->getElse()) Indent();
-  }
-
-  if (Stmt *Else = If->getElse()) {
-    OS << "else";
-
-    if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
+    if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
       OS << ' ';
-      PrintRawCompoundStmt(CS);
-      OS << '\n';
-    } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
-      OS << ' ';
       PrintRawIfStmt(ElseIf);
-    } else {
-      OS << '\n';
-      PrintStmt(If->getElse());
-    }
+    } else 
+      PrintBody(Else, false);
   }
 }
 
@@ -206,16 +236,7 @@
   Indent() << "switch (";
   PrintExpr(Node->getCond());
   OS << ")";
-
-  // Pretty print compoundstmt bodies (very common).
-  if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
-    OS << " ";
-    PrintRawCompoundStmt(CS);
-    OS << "\n";
-  } else {
-    OS << "\n";
-    PrintStmt(Node->getBody());
-  }
+  PrintBody(Node->getBody(), false);
 }
 
 void StmtPrinter::VisitSwitchCase(SwitchCase*) {
@@ -225,20 +246,16 @@
 void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
   Indent() << "while (";
   PrintExpr(Node->getCond());
-  OS << ")\n";
-  PrintStmt(Node->getBody());
+  OS << ")";
+  PrintBody(Node->getBody(), false);
 }
 
 void StmtPrinter::VisitDoStmt(DoStmt *Node) {
-  Indent() << "do ";
-  if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
-    PrintRawCompoundStmt(CS);
-    OS << " ";
-  } else {
-    OS << "\n";
-    PrintStmt(Node->getBody());
+  Indent() << "do";
+  if (PrintBody(Node->getBody(), true))
     Indent();
-  }
+  else
+    OS << ' ';
 
   OS << "while (";
   PrintExpr(Node->getCond());
@@ -263,15 +280,8 @@
     OS << " ";
     PrintExpr(Node->getInc());
   }
-  OS << ") ";
-
-  if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
-    PrintRawCompoundStmt(CS);
-    OS << "\n";
-  } else {
-    OS << "\n";
-    PrintStmt(Node->getBody());
-  }
+  OS << ")";
+  PrintBody(Node->getBody(), false);
 }
 
 void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
@@ -282,15 +292,8 @@
     PrintExpr(cast<Expr>(Node->getElement()));
   OS << " in ";
   PrintExpr(Node->getCollection());
-  OS << ") ";
-
-  if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
-    PrintRawCompoundStmt(CS);
-    OS << "\n";
-  } else {
-    OS << "\n";
-    PrintStmt(Node->getBody());
-  }
+  OS << ")";
+  PrintBody(Node->getBody(), false);
 }
 
 void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
@@ -443,24 +446,26 @@
     PrintRawDecl(ExDecl);
   else
     OS << "...";
-  OS << ") ";
-  PrintRawCompoundStmt(cast<CompoundStmt>(Node->getHandlerBlock()));
+  OS << ")";
+  PrintBody(Node->getHandlerBlock(), false);
 }
 
 void StmtPrinter::VisitCXXCatchStmt(CXXCatchStmt *Node) {
   Indent();
   PrintRawCXXCatchStmt(Node);
-  OS << "\n";
 }
 
 void StmtPrinter::VisitCXXTryStmt(CXXTryStmt *Node) {
-  Indent() << "try ";
-  PrintRawCompoundStmt(Node->getTryBlock());
+  Indent() << "try";
+  bool lastCharEol = PrintBody(Node->getTryBlock(), true);
   for (unsigned i = 0, e = Node->getNumHandlers(); i < e; ++i) {
-    OS << " ";
+    if (lastCharEol)
+      Indent();
+    else
+      OS << " ";
     PrintRawCXXCatchStmt(Node->getHandler(i));
+    lastCharEol = true;
   }
-  OS << "\n";
 }
 
 //===----------------------------------------------------------------------===//
Index: lib/Frontend/CompilerInvocation.cpp
===================================================================
--- lib/Frontend/CompilerInvocation.cpp	(revision 119144)
+++ lib/Frontend/CompilerInvocation.cpp	(working copy)
@@ -1413,6 +1413,20 @@
   case 1: Opts.setStackProtectorMode(LangOptions::SSPOn);  break;
   case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
   }
+
+  // retrieve rewrite options:
+  Opts.Indentation = Args.getLastArgIntValue(OPT_rewrite_indent, 2, Diags);
+  if (const Arg *A = Args.getLastArg(OPT_rewrite_style)) {
+    unsigned IndentStyle = llvm::StringSwitch<unsigned>(A->getValue(Args))
+      .Case("KR", LangOptions::KRStyle)
+      .Case("ANSI", LangOptions::ANSIStyle)
+      .Default(~0U);
+    if (IndentStyle == ~0U)
+      Diags.Report(diag::err_drv_invalid_value)
+        << A->getAsString(Args) << A->getValue(Args);
+    else
+      Opts.IndentStyle = IndentStyle;
+  }
 }
 
 static void ParsePreprocessorArgs(PreprocessorOptions &Opts, ArgList &Args,
