woot.<div><br></div><div>-eric</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Dec 3, 2012 at 10:12 AM, Daniel Jasper <span dir="ltr"><<a href="mailto:djasper@google.com" target="_blank">djasper@google.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: djasper<br>
Date: Mon Dec  3 12:12:45 2012<br>
New Revision: 169137<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=169137&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=169137&view=rev</a><br>
Log:<br>
Initial version of formatting library.<br>
<br>
This formatting library will be used by a stand-alone clang-format tool<br>
and can also be used when writing other refactorings.<br>
<br>
Manuel's original design document:<br>
<a href="https://docs.google.com/a/google.com/document/d/1gpckL2U_6QuU9YW2L1ABsc4Fcogn5UngKk7fE5dDOoA/edit" target="_blank">https://docs.google.com/a/google.com/document/d/1gpckL2U_6QuU9YW2L1ABsc4Fcogn5UngKk7fE5dDOoA/edit</a><br>

<br>
The library can already successfully format itself.<br>
<br>
Review: <a href="http://llvm-reviews.chandlerc.com/D80" target="_blank">http://llvm-reviews.chandlerc.com/D80</a><br>
<br>
Added:<br>
    cfe/trunk/include/clang/Format/<br>
    cfe/trunk/include/clang/Format/Format.h<br>
    cfe/trunk/lib/Format/<br>
    cfe/trunk/lib/Format/CMakeLists.txt<br>
    cfe/trunk/lib/Format/Format.cpp<br>
    cfe/trunk/lib/Format/Makefile<br>
    cfe/trunk/lib/Format/UnwrappedLineParser.cpp<br>
    cfe/trunk/lib/Format/UnwrappedLineParser.h<br>
    cfe/trunk/unittests/Format/<br>
    cfe/trunk/unittests/Format/CMakeLists.txt<br>
    cfe/trunk/unittests/Format/FormatTest.cpp<br>
    cfe/trunk/unittests/Format/Makefile<br>
Modified:<br>
    cfe/trunk/lib/CMakeLists.txt<br>
    cfe/trunk/lib/Makefile<br>
    cfe/trunk/unittests/CMakeLists.txt<br>
<br>
Added: cfe/trunk/include/clang/Format/Format.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Format/Format.h?rev=169137&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Format/Format.h?rev=169137&view=auto</a><br>

==============================================================================<br>
--- cfe/trunk/include/clang/Format/Format.h (added)<br>
+++ cfe/trunk/include/clang/Format/Format.h Mon Dec  3 12:12:45 2012<br>
@@ -0,0 +1,75 @@<br>
+//===--- Format.h - Format C++ code -----------------------------*- C++ -*-===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+///<br>
+/// \file<br>
+/// Various functions to configurably format source code.<br>
+///<br>
+/// This is EXPERIMENTAL code under heavy development. It is not in a state yet,<br>
+/// where it can be used to format real code.<br>
+///<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#ifndef LLVM_CLANG_FORMAT_FORMAT_H_<br>
+#define LLVM_CLANG_FORMAT_FORMAT_H<br>
+<br>
+#include "clang/Frontend/FrontendAction.h"<br>
+#include "clang/Tooling/Refactoring.h"<br>
+<br>
+namespace clang {<br>
+<br>
+class Lexer;<br>
+class SourceManager;<br>
+<br>
+namespace format {<br>
+<br>
+/// \brief The \c FormatStyle is used to configure the formatting to follow<br>
+/// specific guidelines.<br>
+struct FormatStyle {<br>
+  /// \brief The column limit.<br>
+  unsigned ColumnLimit;<br>
+<br>
+  /// \brief The maximum number of consecutive empty lines to keep.<br>
+  unsigned MaxEmptyLinesToKeep;<br>
+<br>
+  /// \brief Set whether & and * bind to the type as opposed to the variable.<br>
+  bool PointerAndReferenceBindToType;<br>
+<br>
+  /// \brief The extra indent or outdent of access modifiers (e.g.: public:).<br>
+  int AccessModifierOffset;<br>
+<br>
+  /// \brief Split two consecutive closing '>' by a space, i.e. use<br>
+  /// A<A<int> > instead of A<A<int>>.<br>
+  bool SplitTemplateClosingGreater;<br>
+};<br>
+<br>
+/// \brief Returns a format style complying with the LLVM coding standards:<br>
+/// <a href="http://llvm.org/docs/CodingStandards.html" target="_blank">http://llvm.org/docs/CodingStandards.html</a>.<br>
+FormatStyle getLLVMStyle();<br>
+<br>
+/// \brief Returns a format style complying with Google's C++ style guide:<br>
+/// <a href="http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml" target="_blank">http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml</a>.<br>
+FormatStyle getGoogleStyle();<br>
+<br>
+/// \brief Reformats the given \p Ranges in the token stream coming out of<br>
+/// \c Lex.<br>
+///<br>
+/// Each range is extended on either end to its next bigger logic unit, i.e.<br>
+/// everything that might influence its formatting or might be influenced by its<br>
+/// formatting.<br>
+///<br>
+/// Returns the \c Replacements necessary to make all \p Ranges comply with<br>
+/// \p Style.<br>
+tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,<br>
+                               SourceManager &SourceMgr,<br>
+                               std::vector<CharSourceRange> Ranges);<br>
+<br>
+}  // end namespace format<br>
+}  // end namespace clang<br>
+<br>
+#endif // LLVM_CLANG_FORMAT_FORMAT_H<br>
<br>
Modified: cfe/trunk/lib/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CMakeLists.txt?rev=169137&r1=169136&r2=169137&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CMakeLists.txt?rev=169137&r1=169136&r2=169137&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/lib/CMakeLists.txt (original)<br>
+++ cfe/trunk/lib/CMakeLists.txt Mon Dec  3 12:12:45 2012<br>
@@ -16,3 +16,4 @@<br>
 add_subdirectory(FrontendTool)<br>
 add_subdirectory(Tooling)<br>
 add_subdirectory(StaticAnalyzer)<br>
+add_subdirectory(Format)<br>
<br>
Added: cfe/trunk/lib/Format/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/CMakeLists.txt?rev=169137&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/CMakeLists.txt?rev=169137&view=auto</a><br>

==============================================================================<br>
--- cfe/trunk/lib/Format/CMakeLists.txt (added)<br>
+++ cfe/trunk/lib/Format/CMakeLists.txt Mon Dec  3 12:12:45 2012<br>
@@ -0,0 +1,24 @@<br>
+set(LLVM_LINK_COMPONENTS support)<br>
+<br>
+add_clang_library(clangFormat<br>
+  UnwrappedLineParser.cpp<br>
+  Format.cpp<br>
+  )<br>
+<br>
+add_dependencies(clangFormat<br>
+  ClangAttrClasses<br>
+  ClangAttrList<br>
+  ClangDeclNodes<br>
+  ClangDiagnosticCommon<br>
+  ClangDiagnosticFrontend<br>
+  ClangStmtNodes<br>
+  )<br>
+<br>
+target_link_libraries(clangFormat<br>
+  clangBasic<br>
+  clangFrontend<br>
+  clangAST<br>
+  clangASTMatchers<br>
+  clangRewriteCore<br>
+  clangRewriteFrontend<br>
+  )<br>
<br>
Added: cfe/trunk/lib/Format/Format.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=169137&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=169137&view=auto</a><br>

==============================================================================<br>
--- cfe/trunk/lib/Format/Format.cpp (added)<br>
+++ cfe/trunk/lib/Format/Format.cpp Mon Dec  3 12:12:45 2012<br>
@@ -0,0 +1,704 @@<br>
+//===--- Format.cpp - Format C++ code -------------------------------------===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+///<br>
+/// \file<br>
+/// \brief This file implements functions declared in Format.h. This will be<br>
+/// split into separate files as we go.<br>
+///<br>
+/// This is EXPERIMENTAL code under heavy development. It is not in a state yet,<br>
+/// where it can be used to format real code.<br>
+///<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "clang/Format/Format.h"<br>
+<br>
+#include "clang/Basic/SourceManager.h"<br>
+#include "clang/Lex/Lexer.h"<br>
+<br>
+#include "UnwrappedLineParser.h"<br>
+<br>
+namespace clang {<br>
+namespace format {<br>
+<br>
+// FIXME: Move somewhere sane.<br>
+struct TokenAnnotation {<br>
+  enum TokenType { TT_Unknown, TT_TemplateOpener, TT_BinaryOperator,<br>
+      TT_UnaryOperator, TT_OverloadedOperator, TT_PointerOrReference,<br>
+      TT_ConditionalExpr, TT_LineComment, TT_BlockComment };<br>
+<br>
+  TokenType Type;<br>
+<br>
+  /// \brief The current parenthesis level, i.e. the number of opening minus<br>
+  /// the number of closing parenthesis left of the current position.<br>
+  unsigned ParenLevel;<br>
+<br>
+  bool SpaceRequiredBefore;<br>
+  bool CanBreakBefore;<br>
+  bool MustBreakBefore;<br>
+};<br>
+<br>
+using llvm::MutableArrayRef;<br>
+<br>
+FormatStyle getLLVMStyle() {<br>
+  FormatStyle LLVMStyle;<br>
+  LLVMStyle.ColumnLimit = 80;<br>
+  LLVMStyle.MaxEmptyLinesToKeep = 1;<br>
+  LLVMStyle.PointerAndReferenceBindToType = false;<br>
+  LLVMStyle.AccessModifierOffset = -2;<br>
+  LLVMStyle.SplitTemplateClosingGreater = true;<br>
+  return LLVMStyle;<br>
+}<br>
+<br>
+FormatStyle getGoogleStyle() {<br>
+  FormatStyle GoogleStyle;<br>
+  GoogleStyle.ColumnLimit = 80;<br>
+  GoogleStyle.MaxEmptyLinesToKeep = 1;<br>
+  GoogleStyle.PointerAndReferenceBindToType = true;<br>
+  GoogleStyle.AccessModifierOffset = -1;<br>
+  GoogleStyle.SplitTemplateClosingGreater = false;<br>
+  return GoogleStyle;<br>
+}<br>
+<br>
+struct OptimizationParameters {<br>
+  unsigned PenaltyExtraLine;<br>
+  unsigned PenaltyIndentLevel;<br>
+};<br>
+<br>
+class UnwrappedLineFormatter {<br>
+public:<br>
+  UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr,<br>
+                         const UnwrappedLine &Line,<br>
+                         const std::vector<TokenAnnotation> &Annotations,<br>
+                         tooling::Replacements &Replaces)<br>
+      : Style(Style),<br>
+        SourceMgr(SourceMgr),<br>
+        Line(Line),<br>
+        Annotations(Annotations),<br>
+        Replaces(Replaces) {<br>
+    Parameters.PenaltyExtraLine = 100;<br>
+    Parameters.PenaltyIndentLevel = 5;<br>
+  }<br>
+<br>
+  void format() {<br>
+    formatFirstToken();<br>
+    count = 0;<br>
+    IndentState State;<br>
+    State.Column = Line.Level * 2 + Line.Tokens[0].Tok.getLength();<br>
+    State.CtorInitializerOnNewLine = false;<br>
+    State.InCtorInitializer = false;<br>
+    State.ConsumedTokens = 1;<br>
+<br>
+    //State.UsedIndent.push_back(Line.Level * 2);<br>
+    State.Indent.push_back(Line.Level * 2 + 4);<br>
+    State.LastSpace.push_back(Line.Level * 2);<br>
+<br>
+    // Start iterating at 1 as we have correctly formatted of Token #0 above.<br>
+    for (unsigned i = 1, n = Line.Tokens.size(); i != n; ++i) {<br>
+      unsigned NoBreak = calcPenalty(State, false, UINT_MAX);<br>
+      unsigned Break = calcPenalty(State, true, NoBreak);<br>
+      addToken(Break < NoBreak, false, State);<br>
+    }<br>
+  }<br>
+<br>
+private:<br>
+  /// \brief The current state when indenting a unwrapped line.<br>
+  ///<br>
+  /// As the indenting tries different combinations this is copied by value.<br>
+  struct IndentState {<br>
+    /// \brief The number of used columns in the current line.<br>
+    unsigned Column;<br>
+<br>
+    /// \brief The number of tokens already consumed.<br>
+    unsigned ConsumedTokens;<br>
+<br>
+    /// \brief The position to which a specific parenthesis level needs to be<br>
+    /// indented.<br>
+    std::vector<unsigned> Indent;<br>
+<br>
+    std::vector<unsigned> LastSpace;<br>
+<br>
+    bool CtorInitializerOnNewLine;<br>
+    bool InCtorInitializer;<br>
+<br>
+    /// \brief Comparison operator to be able to used \c IndentState in \c map.<br>
+    bool operator<(const IndentState &Other) const {<br>
+      if (Other.ConsumedTokens != ConsumedTokens)<br>
+        return Other.ConsumedTokens > ConsumedTokens;<br>
+      if (Other.Column != Column)<br>
+        return Other.Column > Column;<br>
+      if (Other.Indent.size() != Indent.size())<br>
+        return Other.Indent.size() > Indent.size();<br>
+      for (int i = 0, e = Indent.size(); i != e; ++i) {<br>
+        if (Other.Indent[i] != Indent[i])<br>
+          return Other.Indent[i] > Indent[i];<br>
+      }<br>
+      if (Other.LastSpace.size() != LastSpace.size())<br>
+        return Other.LastSpace.size() > LastSpace.size();<br>
+      for (int i = 0, e = LastSpace.size(); i != e; ++i) {<br>
+        if (Other.LastSpace[i] != LastSpace[i])<br>
+          return Other.LastSpace[i] > LastSpace[i];<br>
+      }<br>
+      return false;<br>
+    }<br>
+  };<br>
+<br>
+  /// Append the next token to \p State.<br>
+  void addToken(bool Newline, bool DryRun, IndentState &State) {<br>
+    unsigned Index = State.ConsumedTokens;<br>
+    const FormatToken &Current = Line.Tokens[Index];<br>
+    const FormatToken &Previous = Line.Tokens[Index - 1];<br>
+    unsigned ParenLevel = Annotations[Index].ParenLevel;<br>
+<br>
+    if (<a href="http://Current.Tok.is" target="_blank">Current.Tok.is</a>(tok::l_paren) || <a href="http://Current.Tok.is" target="_blank">Current.Tok.is</a>(tok::l_square) ||<br>
+        Annotations[Index].Type == TokenAnnotation::TT_TemplateOpener) {<br>
+      State.Indent.push_back(4 + State.LastSpace.back());<br>
+      State.LastSpace.push_back(State.LastSpace.back());<br>
+    }<br>
+<br>
+    if (Newline) {<br>
+      if (<a href="http://Current.Tok.is" target="_blank">Current.Tok.is</a>(tok::string_literal) &&<br>
+          <a href="http://Previous.Tok.is" target="_blank">Previous.Tok.is</a>(tok::string_literal))<br>
+        State.Column = State.Column - Previous.Tok.getLength();<br>
+      else if (<a href="http://Previous.Tok.is" target="_blank">Previous.Tok.is</a>(tok::equal) && ParenLevel != 0)<br>
+        State.Column = State.Indent[ParenLevel] + 4;<br>
+      else<br>
+        State.Column = State.Indent[ParenLevel];<br>
+      if (!DryRun)<br>
+        replaceWhitespace(Current, 1, State.Column);<br>
+<br>
+      State.Column += Current.Tok.getLength();<br>
+      State.LastSpace[ParenLevel] = State.Indent[ParenLevel];<br>
+      if (<a href="http://Current.Tok.is" target="_blank">Current.Tok.is</a>(tok::colon) &&<br>
+          Annotations[Index].Type != TokenAnnotation::TT_ConditionalExpr) {<br>
+        State.Indent[ParenLevel] += 2;<br>
+        State.CtorInitializerOnNewLine = true;<br>
+        State.InCtorInitializer = true;<br>
+      }<br>
+    } else {<br>
+      unsigned Spaces = Annotations[Index].SpaceRequiredBefore ? 1 : 0;<br>
+      if (Annotations[Index].Type == TokenAnnotation::TT_LineComment)<br>
+        Spaces = 2;<br>
+      if (!DryRun)<br>
+        replaceWhitespace(Current, 0, Spaces);<br>
+      if (<a href="http://Previous.Tok.is" target="_blank">Previous.Tok.is</a>(tok::l_paren))<br>
+        State.Indent[ParenLevel] = State.Column;<br>
+      if (<a href="http://Previous.Tok.is" target="_blank">Previous.Tok.is</a>(tok::less) &&<br>
+          Annotations[Index - 1].Type == TokenAnnotation::TT_TemplateOpener)<br>
+        State.Indent[ParenLevel] = State.Column;<br>
+      if (<a href="http://Current.Tok.is" target="_blank">Current.Tok.is</a>(tok::colon)) {<br>
+        State.Indent[ParenLevel] = State.Column + 3;<br>
+        State.InCtorInitializer = true;<br>
+      }<br>
+      // Top-level spaces are exempt as that mostly leads to better results.<br>
+      if (Spaces > 0 && ParenLevel != 0)<br>
+        State.LastSpace[ParenLevel] = State.Column + Spaces;<br>
+      State.Column += Current.Tok.getLength() + Spaces;<br>
+    }<br>
+<br>
+    if (<a href="http://Current.Tok.is" target="_blank">Current.Tok.is</a>(tok::r_paren) || <a href="http://Current.Tok.is" target="_blank">Current.Tok.is</a>(tok::r_square) ||<br>
+        Annotations[Index].Type == TokenAnnotation::TT_TemplateOpener) {<br>
+      State.Indent.pop_back();<br>
+      State.LastSpace.pop_back();<br>
+    }<br>
+<br>
+    ++State.ConsumedTokens;<br>
+  }<br>
+<br>
+  typedef std::map<IndentState, unsigned> StateMap;<br>
+  StateMap Memory;<br>
+<br>
+  unsigned splitPenalty(const FormatToken &Token) {<br>
+    if (<a href="http://Token.Tok.is" target="_blank">Token.Tok.is</a>(tok::semi))<br>
+      return 0;<br>
+    if (<a href="http://Token.Tok.is" target="_blank">Token.Tok.is</a>(tok::comma))<br>
+      return 1;<br>
+    if (<a href="http://Token.Tok.is" target="_blank">Token.Tok.is</a>(tok::equal) || <a href="http://Token.Tok.is" target="_blank">Token.Tok.is</a>(tok::l_paren) ||<br>
+        <a href="http://Token.Tok.is" target="_blank">Token.Tok.is</a>(tok::pipepipe) || <a href="http://Token.Tok.is" target="_blank">Token.Tok.is</a>(tok::ampamp))<br>
+      return 2;<br>
+    return 3;<br>
+  }<br>
+<br>
+  /// \brief Calculate the number of lines needed to format the remaining part<br>
+  /// of the unwrapped line.<br>
+  ///<br>
+  /// Assumes the formatting so far has led to<br>
+  /// the \c IndentState \p State. If \p NewLine is set, a new line will be<br>
+  /// added after the previous token.<br>
+  ///<br>
+  /// \param StopAt is used for optimization. If we can determine that we'll<br>
+  /// definitely need at least \p StopAt additional lines, we already know of a<br>
+  /// better solution.<br>
+  unsigned calcPenalty(IndentState State, bool NewLine, unsigned StopAt) {<br>
+    // We are at the end of the unwrapped line, so we don't need any more lines.<br>
+    if (State.ConsumedTokens >= Line.Tokens.size())<br>
+      return 0;<br>
+<br>
+    if (!NewLine && Annotations[State.ConsumedTokens].MustBreakBefore)<br>
+      return UINT_MAX;<br>
+    if (NewLine && !Annotations[State.ConsumedTokens].CanBreakBefore)<br>
+      return UINT_MAX;<br>
+<br>
+    if (State.ConsumedTokens > 0 && !NewLine &&<br>
+        State.CtorInitializerOnNewLine &&<br>
+        Line.Tokens[State.ConsumedTokens - 1].Tok.is(tok::comma))<br>
+      return UINT_MAX;<br>
+<br>
+    if (NewLine && State.InCtorInitializer && !State.CtorInitializerOnNewLine)<br>
+      return UINT_MAX;<br>
+<br>
+    addToken(NewLine, true, State);<br>
+<br>
+    // Exceeding column limit is bad.<br>
+    if (State.Column > Style.ColumnLimit)<br>
+      return UINT_MAX;<br>
+<br>
+    unsigned CurrentPenalty = 0;<br>
+    if (NewLine) {<br>
+      CurrentPenalty += Parameters.PenaltyIndentLevel *<br>
+          Annotations[State.ConsumedTokens - 1].ParenLevel +<br>
+          Parameters.PenaltyExtraLine +<br>
+          splitPenalty(Line.Tokens[State.ConsumedTokens - 2]);<br>
+    }<br>
+<br>
+    if (StopAt <= CurrentPenalty)<br>
+      return UINT_MAX;<br>
+    StopAt -= CurrentPenalty;<br>
+<br>
+    // Has this state already been examined?<br>
+    StateMap::iterator I = Memory.find(State);<br>
+    if (I != Memory.end())<br>
+      return I->second;<br>
+    ++count;<br>
+<br>
+    unsigned NoBreak = calcPenalty(State, false, StopAt);<br>
+    unsigned WithBreak = calcPenalty(State, true, std::min(StopAt, NoBreak));<br>
+    unsigned Result = std::min(NoBreak, WithBreak);<br>
+    if (Result != UINT_MAX)<br>
+      Result += CurrentPenalty;<br>
+    Memory[State] = Result;<br>
+    assert(Memory.find(State) != Memory.end());<br>
+    return Result;<br>
+  }<br>
+<br>
+  /// \brief Replaces the whitespace in front of \p Tok. Only call once for<br>
+  /// each \c FormatToken.<br>
+  void replaceWhitespace(const FormatToken &Tok, unsigned NewLines,<br>
+                         unsigned Spaces) {<br>
+    Replaces.insert(tooling::Replacement(<br>
+        SourceMgr, Tok.WhiteSpaceStart, Tok.WhiteSpaceLength,<br>
+        std::string(NewLines, '\n') + std::string(Spaces, ' ')));<br>
+  }<br>
+<br>
+  /// \brief Add a new line and the required indent before the first Token<br>
+  /// of the \c UnwrappedLine.<br>
+  void formatFirstToken() {<br>
+    const FormatToken &Token = Line.Tokens[0];<br>
+    if (Token.WhiteSpaceStart.isValid()) {<br>
+      unsigned Newlines =<br>
+          std::min(Token.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);<br>
+      unsigned Offset = SourceMgr.getFileOffset(Token.WhiteSpaceStart);<br>
+      if (Newlines == 0 && Offset != 0)<br>
+        Newlines = 1;<br>
+      unsigned Indent = Line.Level * 2;<br>
+      if (<a href="http://Token.Tok.is" target="_blank">Token.Tok.is</a>(tok::kw_public) || <a href="http://Token.Tok.is" target="_blank">Token.Tok.is</a>(tok::kw_protected) ||<br>
+          <a href="http://Token.Tok.is" target="_blank">Token.Tok.is</a>(tok::kw_private))<br>
+        Indent += Style.AccessModifierOffset;<br>
+      replaceWhitespace(Token, Newlines, Indent);<br>
+    }<br>
+  }<br>
+<br>
+  FormatStyle Style;<br>
+  SourceManager &SourceMgr;<br>
+  const UnwrappedLine &Line;<br>
+  const std::vector<TokenAnnotation> &Annotations;<br>
+  tooling::Replacements &Replaces;<br>
+  unsigned int count;<br>
+<br>
+  OptimizationParameters Parameters;<br>
+};<br>
+<br>
+/// \brief Determines extra information about the tokens comprising an<br>
+/// \c UnwrappedLine.<br>
+class TokenAnnotator {<br>
+public:<br>
+  TokenAnnotator(const UnwrappedLine &Line, const FormatStyle &Style,<br>
+                 SourceManager &SourceMgr)<br>
+      : Line(Line),<br>
+        Style(Style),<br>
+        SourceMgr(SourceMgr) {<br>
+  }<br>
+<br>
+  /// \brief A parser that gathers additional information about tokens.<br>
+  ///<br>
+  /// The \c TokenAnnotator tries to matches parenthesis and square brakets and<br>
+  /// store a parenthesis levels. It also tries to resolve matching "<" and ">"<br>
+  /// into template parameter lists.<br>
+  class AnnotatingParser {<br>
+  public:<br>
+    AnnotatingParser(const SourceManager &SourceMgr,<br>
+                     const SmallVector<FormatToken, 16> &Tokens,<br>
+                     std::vector<TokenAnnotation> &Annotations)<br>
+        : SourceMgr(SourceMgr),<br>
+          Tokens(Tokens),<br>
+          Annotations(Annotations),<br>
+          Index(0) {<br>
+    }<br>
+<br>
+    bool parseAngle(unsigned Level) {<br>
+      while (Index < Tokens.size()) {<br>
+        if (Tokens[Index].Tok.is(tok::greater)) {<br>
+          Annotations[Index].Type = TokenAnnotation::TT_TemplateOpener;<br>
+          Annotations[Index].ParenLevel = Level;<br>
+          next();<br>
+          return true;<br>
+        }<br>
+        if (Tokens[Index].Tok.is(tok::r_paren) ||<br>
+            Tokens[Index].Tok.is(tok::r_square))<br>
+          return false;<br>
+        if (Tokens[Index].Tok.is(tok::pipepipe) ||<br>
+            Tokens[Index].Tok.is(tok::ampamp) ||<br>
+            Tokens[Index].Tok.is(tok::question) ||<br>
+            Tokens[Index].Tok.is(tok::colon))<br>
+          return false;<br>
+        consumeToken(Level);<br>
+      }<br>
+      return false;<br>
+    }<br>
+<br>
+    bool parseParens(unsigned Level) {<br>
+      while (Index < Tokens.size()) {<br>
+        if (Tokens[Index].Tok.is(tok::r_paren)) {<br>
+          Annotations[Index].ParenLevel = Level;<br>
+          next();<br>
+          return true;<br>
+        }<br>
+        if (Tokens[Index].Tok.is(tok::r_square))<br>
+          return false;<br>
+        consumeToken(Level);<br>
+      }<br>
+      return false;<br>
+    }<br>
+<br>
+    bool parseSquare(unsigned Level) {<br>
+      while (Index < Tokens.size()) {<br>
+        if (Tokens[Index].Tok.is(tok::r_square)) {<br>
+          Annotations[Index].ParenLevel = Level;<br>
+          next();<br>
+          return true;<br>
+        }<br>
+        if (Tokens[Index].Tok.is(tok::r_paren))<br>
+          return false;<br>
+        consumeToken(Level);<br>
+      }<br>
+      return false;<br>
+    }<br>
+<br>
+    bool parseConditional(unsigned Level) {<br>
+      while (Index < Tokens.size()) {<br>
+        if (Tokens[Index].Tok.is(tok::colon)) {<br>
+          Annotations[Index].Type = TokenAnnotation::TT_ConditionalExpr;<br>
+          next();<br>
+          return true;<br>
+        }<br>
+        consumeToken(Level);<br>
+      }<br>
+      return false;<br>
+    }<br>
+<br>
+    void consumeToken(unsigned Level) {<br>
+      Annotations[Index].ParenLevel = Level;<br>
+      unsigned CurrentIndex = Index;<br>
+      next();<br>
+      switch (Tokens[CurrentIndex].Tok.getKind()) {<br>
+      case tok::l_paren:<br>
+        parseParens(Level + 1);<br>
+        break;<br>
+      case tok::l_square:<br>
+        parseSquare(Level + 1);<br>
+        break;<br>
+      case tok::less:<br>
+        if (parseAngle(Level + 1))<br>
+          Annotations[CurrentIndex].Type = TokenAnnotation::TT_TemplateOpener;<br>
+        else {<br>
+          Annotations[CurrentIndex].Type = TokenAnnotation::TT_BinaryOperator;<br>
+          Index = CurrentIndex + 1;<br>
+        }<br>
+        break;<br>
+      case tok::greater:<br>
+        Annotations[CurrentIndex].Type = TokenAnnotation::TT_BinaryOperator;<br>
+        break;<br>
+      case tok::kw_operator:<br>
+        if (!Tokens[Index].Tok.is(tok::l_paren))<br>
+          Annotations[Index].Type = TokenAnnotation::TT_OverloadedOperator;<br>
+        next();<br>
+        break;<br>
+      case tok::question:<br>
+        parseConditional(Level);<br>
+        break;<br>
+      default:<br>
+        break;<br>
+      }<br>
+    }<br>
+<br>
+    void parseLine() {<br>
+      while (Index < Tokens.size()) {<br>
+        consumeToken(0);<br>
+      }<br>
+    }<br>
+<br>
+    void next() {<br>
+      ++Index;<br>
+    }<br>
+<br>
+  private:<br>
+    const SourceManager &SourceMgr;<br>
+    const SmallVector<FormatToken, 16> &Tokens;<br>
+    std::vector<TokenAnnotation> &Annotations;<br>
+    unsigned Index;<br>
+  };<br>
+<br>
+  void annotate() {<br>
+    Annotations.clear();<br>
+    for (int i = 0, e = Line.Tokens.size(); i != e; ++i) {<br>
+      Annotations.push_back(TokenAnnotation());<br>
+    }<br>
+<br>
+    AnnotatingParser Parser(SourceMgr, Line.Tokens, Annotations);<br>
+    Parser.parseLine();<br>
+<br>
+    determineTokenTypes();<br>
+<br>
+    for (int i = 1, e = Line.Tokens.size(); i != e; ++i) {<br>
+      TokenAnnotation &Annotation = Annotations[i];<br>
+<br>
+      Annotation.CanBreakBefore =<br>
+          canBreakBetween(Line.Tokens[i - 1], Line.Tokens[i]);<br>
+<br>
+      if (Line.Tokens[i].Tok.is(tok::colon)) {<br>
+        if (Line.Tokens[0].Tok.is(tok::kw_case) || i == e - 1) {<br>
+          Annotation.SpaceRequiredBefore = false;<br>
+        } else {<br>
+          Annotation.SpaceRequiredBefore = TokenAnnotation::TT_ConditionalExpr;<br>
+        }<br>
+      } else if (Annotations[i - 1].Type == TokenAnnotation::TT_UnaryOperator) {<br>
+        Annotation.SpaceRequiredBefore = false;<br>
+      } else if (Annotation.Type == TokenAnnotation::TT_UnaryOperator) {<br>
+        Annotation.SpaceRequiredBefore =<br>
+            Line.Tokens[i - 1].Tok.isNot(tok::l_paren);<br>
+      } else if (Line.Tokens[i - 1].Tok.is(tok::greater) &&<br>
+                 Line.Tokens[i].Tok.is(tok::greater)) {<br>
+        if (Annotation.Type == TokenAnnotation::TT_TemplateOpener &&<br>
+            Annotations[i - 1].Type == TokenAnnotation::TT_TemplateOpener)<br>
+          Annotation.SpaceRequiredBefore = Style.SplitTemplateClosingGreater;<br>
+        else<br>
+          Annotation.SpaceRequiredBefore = false;<br>
+      } else if (<br>
+          Annotation.Type == TokenAnnotation::TT_BinaryOperator ||<br>
+          Annotations[i - 1].Type == TokenAnnotation::TT_BinaryOperator) {<br>
+        Annotation.SpaceRequiredBefore = true;<br>
+      } else if (<br>
+          Annotations[i - 1].Type == TokenAnnotation::TT_TemplateOpener &&<br>
+          Line.Tokens[i].Tok.is(tok::l_paren)) {<br>
+        Annotation.SpaceRequiredBefore = false;<br>
+      } else {<br>
+        Annotation.SpaceRequiredBefore =<br>
+            spaceRequiredBetween(Line.Tokens[i - 1].Tok, Line.Tokens[i].Tok);<br>
+      }<br>
+<br>
+      if (Annotations[i - 1].Type == TokenAnnotation::TT_LineComment ||<br>
+          (Line.Tokens[i].Tok.is(tok::string_literal) &&<br>
+           Line.Tokens[i - 1].Tok.is(tok::string_literal))) {<br>
+        Annotation.MustBreakBefore = true;<br>
+      }<br>
+<br>
+      if (Annotation.MustBreakBefore)<br>
+        Annotation.CanBreakBefore = true;<br>
+    }<br>
+  }<br>
+<br>
+  const std::vector<TokenAnnotation> &getAnnotations() {<br>
+    return Annotations;<br>
+  }<br>
+<br>
+private:<br>
+  void determineTokenTypes() {<br>
+    for (int i = 0, e = Line.Tokens.size(); i != e; ++i) {<br>
+      TokenAnnotation &Annotation = Annotations[i];<br>
+      const FormatToken &Tok = Line.Tokens[i];<br>
+<br>
+      if (<a href="http://Tok.Tok.is" target="_blank">Tok.Tok.is</a>(tok::star) || <a href="http://Tok.Tok.is" target="_blank">Tok.Tok.is</a>(tok::amp))<br>
+        Annotation.Type = determineStarAmpUsage(i);<br>
+      else if (<a href="http://Tok.Tok.is" target="_blank">Tok.Tok.is</a>(tok::minus) && Line.Tokens[i - 1].Tok.is(tok::equal))<br>
+        Annotation.Type = TokenAnnotation::TT_UnaryOperator;<br>
+      else if (isBinaryOperator(Line.Tokens[i]))<br>
+        Annotation.Type = TokenAnnotation::TT_BinaryOperator;<br>
+      else if (<a href="http://Tok.Tok.is" target="_blank">Tok.Tok.is</a>(tok::comment)) {<br>
+        StringRef Data(SourceMgr.getCharacterData(Tok.Tok.getLocation()),<br>
+                       Tok.Tok.getLength());<br>
+        if (Data.startswith("//"))<br>
+          Annotation.Type = TokenAnnotation::TT_LineComment;<br>
+        else<br>
+          Annotation.Type = TokenAnnotation::TT_BlockComment;<br>
+      }<br>
+    }<br>
+  }<br>
+<br>
+  bool isBinaryOperator(const FormatToken &Tok) {<br>
+    switch (Tok.Tok.getKind()) {<br>
+    case tok::equal:<br>
+    case tok::equalequal:<br>
+    case tok::star:<br>
+      //case tok::amp:<br>
+    case tok::plus:<br>
+    case tok::slash:<br>
+    case tok::minus:<br>
+    case tok::ampamp:<br>
+    case tok::pipe:<br>
+    case tok::pipepipe:<br>
+    case tok::percent:<br>
+      return true;<br>
+    default:<br>
+      return false;<br>
+    }<br>
+  }<br>
+<br>
+  TokenAnnotation::TokenType determineStarAmpUsage(unsigned Index) {<br>
+    if (Index == Annotations.size())<br>
+      return TokenAnnotation::TT_Unknown;<br>
+<br>
+    if (Index == 0 || Line.Tokens[Index - 1].Tok.is(tok::l_paren) ||<br>
+        Line.Tokens[Index - 1].Tok.is(tok::comma) ||<br>
+        Annotations[Index - 1].Type == TokenAnnotation::TT_BinaryOperator)<br>
+      return TokenAnnotation::TT_UnaryOperator;<br>
+<br>
+    if (Line.Tokens[Index - 1].Tok.isLiteral() ||<br>
+        Line.Tokens[Index + 1].Tok.isLiteral())<br>
+      return TokenAnnotation::TT_BinaryOperator;<br>
+<br>
+    return TokenAnnotation::TT_PointerOrReference;<br>
+  }<br>
+<br>
+  bool isIfForOrWhile(Token Tok) {<br>
+    return Tok.is(tok::kw_if) || Tok.is(tok::kw_for) || Tok.is(tok::kw_while);<br>
+  }<br>
+<br>
+  bool spaceRequiredBetween(Token Left, Token Right) {<br>
+    if (Left.is(tok::kw_template) && Right.is(tok::less))<br>
+      return true;<br>
+    if (Left.is(tok::arrow) || Right.is(tok::arrow))<br>
+      return false;<br>
+    if (Left.is(tok::exclaim) || Left.is(tok::tilde))<br>
+      return false;<br>
+    if (Left.is(tok::less) || Right.is(tok::greater) || Right.is(tok::less))<br>
+      return false;<br>
+    if (Left.is(tok::amp) || Left.is(tok::star))<br>
+      return Right.isLiteral() || Style.PointerAndReferenceBindToType;<br>
+    if (Right.is(tok::star) && Left.is(tok::l_paren))<br>
+      return false;<br>
+    if (Right.is(tok::amp) || Right.is(tok::star))<br>
+      return Left.isLiteral() || !Style.PointerAndReferenceBindToType;<br>
+    if (Left.is(tok::l_square) || Right.is(tok::l_square) ||<br>
+        Right.is(tok::r_square))<br>
+      return false;<br>
+    if (Left.is(tok::coloncolon) || Right.is(tok::coloncolon))<br>
+      return false;<br>
+    if (Left.is(tok::period) || Right.is(tok::period))<br>
+      return false;<br>
+    if (Left.is(tok::colon) || Right.is(tok::colon))<br>
+      return true;<br>
+    if ((Left.is(tok::plusplus) && Right.isAnyIdentifier()) ||<br>
+        (Left.isAnyIdentifier() && Right.is(tok::plusplus)) ||<br>
+        (Left.is(tok::minusminus) && Right.isAnyIdentifier()) ||<br>
+        (Left.isAnyIdentifier() && Right.is(tok::minusminus)))<br>
+      return false;<br>
+    if (Left.is(tok::l_paren))<br>
+      return false;<br>
+    if (Left.is(tok::hash))<br>
+      return false;<br>
+    if (Right.is(tok::r_paren) || Right.is(tok::semi) || Right.is(tok::comma))<br>
+      return false;<br>
+    if (Right.is(tok::l_paren)) {<br>
+      return !Left.isAnyIdentifier() || isIfForOrWhile(Left);<br>
+    }<br>
+    return true;<br>
+  }<br>
+<br>
+  bool canBreakBetween(const FormatToken &Left, const FormatToken &Right) {<br>
+    if (<a href="http://Right.Tok.is" target="_blank">Right.Tok.is</a>(tok::r_paren))<br>
+      return false;<br>
+    if (isBinaryOperator(Left))<br>
+      return true;<br>
+    return <a href="http://Right.Tok.is" target="_blank">Right.Tok.is</a>(tok::colon) || <a href="http://Left.Tok.is" target="_blank">Left.Tok.is</a>(tok::comma) || <a href="http://Left.Tok.is" target="_blank">Left.Tok.is</a>(<br>

+        tok::semi) || <a href="http://Left.Tok.is" target="_blank">Left.Tok.is</a>(tok::equal) || <a href="http://Left.Tok.is" target="_blank">Left.Tok.is</a>(tok::ampamp) ||<br>
+        (<a href="http://Left.Tok.is" target="_blank">Left.Tok.is</a>(tok::l_paren) && !<a href="http://Right.Tok.is" target="_blank">Right.Tok.is</a>(tok::r_paren));<br>
+  }<br>
+<br>
+  const UnwrappedLine &Line;<br>
+  FormatStyle Style;<br>
+  SourceManager &SourceMgr;<br>
+  std::vector<TokenAnnotation> Annotations;<br>
+};<br>
+<br>
+class Formatter : public UnwrappedLineConsumer {<br>
+public:<br>
+  Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr,<br>
+            const std::vector<CharSourceRange> &Ranges)<br>
+      : Style(Style),<br>
+        Lex(Lex),<br>
+        SourceMgr(SourceMgr),<br>
+        Ranges(Ranges) {<br>
+  }<br>
+<br>
+  tooling::Replacements format() {<br>
+    UnwrappedLineParser Parser(Lex, SourceMgr, *this);<br>
+    Parser.parse();<br>
+    return Replaces;<br>
+  }<br>
+<br>
+private:<br>
+  virtual void formatUnwrappedLine(const UnwrappedLine &TheLine) {<br>
+    if (TheLine.Tokens.size() == 0)<br>
+      return;<br>
+<br>
+    CharSourceRange LineRange =<br>
+        CharSourceRange::getTokenRange(TheLine.Tokens.front().Tok.getLocation(),<br>
+                                       TheLine.Tokens.back().Tok.getLocation());<br>
+<br>
+    for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {<br>
+      if (SourceMgr.isBeforeInTranslationUnit(LineRange.getEnd(),<br>
+                                              Ranges[i].getBegin()) ||<br>
+          SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),<br>
+                                              LineRange.getBegin()))<br>
+        continue;<br>
+<br>
+      TokenAnnotator Annotator(TheLine, Style, SourceMgr);<br>
+      Annotator.annotate();<br>
+      UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine,<br>
+                                       Annotator.getAnnotations(), Replaces);<br>
+      Formatter.format();<br>
+      return;<br>
+    }<br>
+  }<br>
+<br>
+  FormatStyle Style;<br>
+  Lexer &Lex;<br>
+  SourceManager &SourceMgr;<br>
+  tooling::Replacements Replaces;<br>
+  std::vector<CharSourceRange> Ranges;<br>
+};<br>
+<br>
+tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,<br>
+                               SourceManager &SourceMgr,<br>
+                               std::vector<CharSourceRange> Ranges) {<br>
+  Formatter formatter(Style, Lex, SourceMgr, Ranges);<br>
+  return formatter.format();<br>
+}<br>
+<br>
+}  // namespace format<br>
+}  // namespace clang<br>
<br>
Added: cfe/trunk/lib/Format/Makefile<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Makefile?rev=169137&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Makefile?rev=169137&view=auto</a><br>

==============================================================================<br>
--- cfe/trunk/lib/Format/Makefile (added)<br>
+++ cfe/trunk/lib/Format/Makefile Mon Dec  3 12:12:45 2012<br>
@@ -0,0 +1,13 @@<br>
+##===- clang/lib/Tooling/Makefile ---------------------------*- Makefile -*-===##<br>
+#<br>
+#                     The LLVM Compiler Infrastructure<br>
+#<br>
+# This file is distributed under the University of Illinois Open Source<br>
+# License. See LICENSE.TXT for details.<br>
+#<br>
+##===----------------------------------------------------------------------===##<br>
+<br>
+CLANG_LEVEL := ../..<br>
+LIBRARYNAME := clangTooling<br>
+<br>
+include $(CLANG_LEVEL)/Makefile<br>
<br>
Added: cfe/trunk/lib/Format/UnwrappedLineParser.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=169137&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=169137&view=auto</a><br>

==============================================================================<br>
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (added)<br>
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Mon Dec  3 12:12:45 2012<br>
@@ -0,0 +1,341 @@<br>
+//===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+///<br>
+/// \file<br>
+/// \brief This file contains the implementation of the UnwrappedLineParser,<br>
+/// which turns a stream of tokens into UnwrappedLines.<br>
+///<br>
+/// This is EXPERIMENTAL code under heavy development. It is not in a state yet,<br>
+/// where it can be used to format real code.<br>
+///<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "UnwrappedLineParser.h"<br>
+<br>
+#include "llvm/Support/raw_ostream.h"<br>
+<br>
+namespace clang {<br>
+namespace format {<br>
+<br>
+UnwrappedLineParser::UnwrappedLineParser(Lexer &Lex, SourceManager &SourceMgr,<br>
+                                         UnwrappedLineConsumer &Callback)<br>
+    : GreaterStashed(false),<br>
+      Lex(Lex),<br>
+      SourceMgr(SourceMgr),<br>
+      IdentTable(Lex.getLangOpts()),<br>
+      Callback(Callback) {<br>
+  Lex.SetKeepWhitespaceMode(true);<br>
+}<br>
+<br>
+void UnwrappedLineParser::parse() {<br>
+  parseToken();<br>
+  parseLevel();<br>
+}<br>
+<br>
+void UnwrappedLineParser::parseLevel() {<br>
+  do {<br>
+    switch (FormatTok.Tok.getKind()) {<br>
+    case tok::hash:<br>
+      parsePPDirective();<br>
+      break;<br>
+    case tok::comment:<br>
+      parseComment();<br>
+      break;<br>
+    case tok::l_brace:<br>
+      parseBlock();<br>
+      addUnwrappedLine();<br>
+      break;<br>
+    case tok::r_brace:<br>
+      return;<br>
+    default:<br>
+      parseStatement();<br>
+      break;<br>
+    }<br>
+  } while (!eof());<br>
+}<br>
+<br>
+void UnwrappedLineParser::parseBlock() {<br>
+  nextToken();<br>
+<br>
+  // FIXME: Remove this hack to handle namespaces.<br>
+  bool IsNamespace = Line.Tokens[0].Tok.is(tok::kw_namespace);<br>
+<br>
+  addUnwrappedLine();<br>
+<br>
+  if (!IsNamespace)<br>
+    ++Line.Level;<br>
+  parseLevel();<br>
+  if (!IsNamespace)<br>
+    --Line.Level;<br>
+  assert(<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::r_brace) && "expected '}'");<br>
+  nextToken();<br>
+  if (<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::semi))<br>
+    nextToken();<br>
+}<br>
+<br>
+void UnwrappedLineParser::parsePPDirective() {<br>
+  while (!eof()) {<br>
+    nextToken();<br>
+    if (FormatTok.NewlinesBefore > 0) {<br>
+      addUnwrappedLine();<br>
+      return;<br>
+    }<br>
+  }<br>
+}<br>
+<br>
+void UnwrappedLineParser::parseComment() {<br>
+  while (!eof()) {<br>
+    nextToken();<br>
+    if (FormatTok.NewlinesBefore > 0) {<br>
+      addUnwrappedLine();<br>
+      return;<br>
+    }<br>
+  }<br>
+}<br>
+<br>
+void UnwrappedLineParser::parseStatement() {<br>
+  if (<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::kw_public) || <a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::kw_protected) ||<br>
+      <a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::kw_private)) {<br>
+    parseAccessSpecifier();<br>
+    return;<br>
+  }<br>
+  if (<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::kw_enum)) {<br>
+    parseEnum();<br>
+    return;<br>
+  }<br>
+  int TokenNumber = 0;<br>
+  do {<br>
+    ++TokenNumber;<br>
+    switch (FormatTok.Tok.getKind()) {<br>
+    case tok::semi:<br>
+      nextToken();<br>
+      addUnwrappedLine();<br>
+      return;<br>
+    case tok::l_paren:<br>
+      parseParens();<br>
+      break;<br>
+    case tok::l_brace:<br>
+      parseBlock();<br>
+      addUnwrappedLine();<br>
+      return;<br>
+    case tok::kw_if:<br>
+      parseIfThenElse();<br>
+      return;<br>
+    case tok::kw_do:<br>
+      parseDoWhile();<br>
+      return;<br>
+    case tok::kw_switch:<br>
+      parseSwitch();<br>
+      return;<br>
+    case tok::kw_default:<br>
+      nextToken();<br>
+      parseLabel();<br>
+      return;<br>
+    case tok::kw_case:<br>
+      parseCaseLabel();<br>
+      return;<br>
+    case tok::raw_identifier:<br>
+      nextToken();<br>
+      break;<br>
+    default:<br>
+      nextToken();<br>
+      if (TokenNumber == 1 && <a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::colon)) {<br>
+        parseLabel();<br>
+        return;<br>
+      }<br>
+      break;<br>
+    }<br>
+  } while (!eof());<br>
+}<br>
+<br>
+void UnwrappedLineParser::parseParens() {<br>
+  assert(<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::l_paren) && "'(' expected.");<br>
+  nextToken();<br>
+  do {<br>
+    switch (FormatTok.Tok.getKind()) {<br>
+    case tok::l_paren:<br>
+      parseParens();<br>
+      break;<br>
+    case tok::r_paren:<br>
+      nextToken();<br>
+      return;<br>
+    default:<br>
+      nextToken();<br>
+      break;<br>
+    }<br>
+  } while (!eof());<br>
+}<br>
+<br>
+void UnwrappedLineParser::parseIfThenElse() {<br>
+  assert(<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::kw_if) && "'if' expected");<br>
+  nextToken();<br>
+  parseParens();<br>
+  bool NeedsUnwrappedLine = false;<br>
+  if (<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::l_brace)) {<br>
+    parseBlock();<br>
+    NeedsUnwrappedLine = true;<br>
+  } else {<br>
+    addUnwrappedLine();<br>
+    ++Line.Level;<br>
+    parseStatement();<br>
+    --Line.Level;<br>
+  }<br>
+  if (<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::kw_else)) {<br>
+    nextToken();<br>
+    if (<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::l_brace)) {<br>
+      parseBlock();<br>
+      addUnwrappedLine();<br>
+    } else if (<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::kw_if)) {<br>
+      parseIfThenElse();<br>
+    } else {<br>
+      addUnwrappedLine();<br>
+      ++Line.Level;<br>
+      parseStatement();<br>
+      --Line.Level;<br>
+    }<br>
+  } else if (NeedsUnwrappedLine) {<br>
+    addUnwrappedLine();<br>
+  }<br>
+}<br>
+<br>
+void UnwrappedLineParser::parseDoWhile() {<br>
+  assert(<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::kw_do) && "'do' expected");<br>
+  nextToken();<br>
+  if (<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::l_brace)) {<br>
+    parseBlock();<br>
+  } else {<br>
+    addUnwrappedLine();<br>
+    ++Line.Level;<br>
+    parseStatement();<br>
+    --Line.Level;<br>
+  }<br>
+<br>
+  assert(<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::kw_while) && "'while' expected");<br>
+  nextToken();<br>
+  parseStatement();<br>
+}<br>
+<br>
+void UnwrappedLineParser::parseLabel() {<br>
+  // FIXME: remove all asserts.<br>
+  assert(<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::colon) && "':' expected");<br>
+  nextToken();<br>
+  unsigned OldLineLevel = Line.Level;<br>
+  if (Line.Level > 0)<br>
+    --Line.Level;<br>
+  if (<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::l_brace)) {<br>
+    parseBlock();<br>
+  }<br>
+  addUnwrappedLine();<br>
+  Line.Level = OldLineLevel;<br>
+}<br>
+<br>
+void UnwrappedLineParser::parseCaseLabel() {<br>
+  assert(<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::kw_case) && "'case' expected");<br>
+  // FIXME: fix handling of complex expressions here.<br>
+  do {<br>
+    nextToken();<br>
+  } while (!eof() && !<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::colon));<br>
+  parseLabel();<br>
+}<br>
+<br>
+void UnwrappedLineParser::parseSwitch() {<br>
+  assert(<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::kw_switch) && "'switch' expected");<br>
+  nextToken();<br>
+  parseParens();<br>
+  if (<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::l_brace)) {<br>
+    parseBlock();<br>
+    addUnwrappedLine();<br>
+  } else {<br>
+    addUnwrappedLine();<br>
+    ++Line.Level;<br>
+    parseStatement();<br>
+    --Line.Level;<br>
+  }<br>
+}<br>
+<br>
+void UnwrappedLineParser::parseAccessSpecifier() {<br>
+  nextToken();<br>
+  nextToken();<br>
+  addUnwrappedLine();<br>
+}<br>
+<br>
+void UnwrappedLineParser::parseEnum() {<br>
+  do {<br>
+    nextToken();<br>
+    if (<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::semi)) {<br>
+      nextToken();<br>
+      addUnwrappedLine();<br>
+      return;<br>
+    }<br>
+  } while (!eof());<br>
+}<br>
+<br>
+void UnwrappedLineParser::addUnwrappedLine() {<br>
+  // Consume trailing comments.<br>
+  while (!eof() && FormatTok.NewlinesBefore == 0 &&<br>
+         <a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::comment)) {<br>
+    nextToken();<br>
+  }<br>
+  Callback.formatUnwrappedLine(Line);<br>
+  Line.Tokens.clear();<br>
+}<br>
+<br>
+bool UnwrappedLineParser::eof() const {<br>
+  return <a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::eof);<br>
+}<br>
+<br>
+void UnwrappedLineParser::nextToken() {<br>
+  if (eof())<br>
+    return;<br>
+  Line.Tokens.push_back(FormatTok);<br>
+  parseToken();<br>
+}<br>
+<br>
+void UnwrappedLineParser::parseToken() {<br>
+  if (GreaterStashed) {<br>
+    FormatTok.NewlinesBefore = 0;<br>
+    FormatTok.WhiteSpaceStart = FormatTok.Tok.getLocation().getLocWithOffset(1);<br>
+    FormatTok.WhiteSpaceLength = 0;<br>
+    GreaterStashed = false;<br>
+    return;<br>
+  }<br>
+<br>
+  FormatTok = FormatToken();<br>
+  Lex.LexFromRawLexer(FormatTok.Tok);<br>
+  FormatTok.WhiteSpaceStart = FormatTok.Tok.getLocation();<br>
+<br>
+  // Consume and record whitespace until we find a significant token.<br>
+  while (<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::unknown)) {<br>
+    FormatTok.NewlinesBefore += tokenText().count('\n');<br>
+    FormatTok.WhiteSpaceLength += FormatTok.Tok.getLength();<br>
+<br>
+    if (eof())<br>
+      return;<br>
+    Lex.LexFromRawLexer(FormatTok.Tok);<br>
+  }<br>
+<br>
+  if (<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::raw_identifier)) {<br>
+    const IdentifierInfo &Info = IdentTable.get(tokenText());<br>
+    FormatTok.Tok.setKind(Info.getTokenID());<br>
+  }<br>
+<br>
+  if (<a href="http://FormatTok.Tok.is" target="_blank">FormatTok.Tok.is</a>(tok::greatergreater)) {<br>
+    FormatTok.Tok.setKind(tok::greater);<br>
+    GreaterStashed = true;<br>
+  }<br>
+}<br>
+<br>
+StringRef UnwrappedLineParser::tokenText() {<br>
+  StringRef Data(SourceMgr.getCharacterData(FormatTok.Tok.getLocation()),<br>
+                 FormatTok.Tok.getLength());<br>
+  return Data;<br>
+}<br>
+<br>
+}  // end namespace format<br>
+}  // end namespace clang<br>
<br>
Added: cfe/trunk/lib/Format/UnwrappedLineParser.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.h?rev=169137&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.h?rev=169137&view=auto</a><br>

==============================================================================<br>
--- cfe/trunk/lib/Format/UnwrappedLineParser.h (added)<br>
+++ cfe/trunk/lib/Format/UnwrappedLineParser.h Mon Dec  3 12:12:45 2012<br>
@@ -0,0 +1,119 @@<br>
+//===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+///<br>
+/// \file<br>
+/// \brief This file contains the declaration of the UnwrappedLineParser,<br>
+/// which turns a stream of tokens into UnwrappedLines.<br>
+///<br>
+/// This is EXPERIMENTAL code under heavy development. It is not in a state yet,<br>
+/// where it can be used to format real code.<br>
+///<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#ifndef LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H<br>
+#define LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H<br>
+<br>
+#include "clang/Basic/SourceManager.h"<br>
+#include "clang/Basic/IdentifierTable.h"<br>
+#include "clang/Lex/Lexer.h"<br>
+<br>
+namespace clang {<br>
+namespace format {<br>
+<br>
+/// \brief A wrapper around a \c Token storing information about the<br>
+/// whitespace characters preceeding it.<br>
+struct FormatToken {<br>
+  FormatToken() : NewlinesBefore(0), WhiteSpaceLength(0) {<br>
+  }<br>
+<br>
+  /// \brief The \c Token.<br>
+  Token Tok;<br>
+<br>
+  /// \brief The number of newlines immediately before the \c Token.<br>
+  ///<br>
+  /// This can be used to determine what the user wrote in the original code<br>
+  /// and thereby e.g. leave an empty line between two function definitions.<br>
+  unsigned NewlinesBefore;<br>
+<br>
+  /// \brief The location of the start of the whitespace immediately preceeding<br>
+  /// the \c Token.<br>
+  ///<br>
+  /// Used together with \c WhiteSpaceLength to create a \c Replacement.<br>
+  SourceLocation WhiteSpaceStart;<br>
+<br>
+  /// \brief The length in characters of the whitespace immediately preceeding<br>
+  /// the \c Token.<br>
+  unsigned WhiteSpaceLength;<br>
+};<br>
+<br>
+/// \brief An unwrapped line is a sequence of \c Token, that we would like to<br>
+/// put on a single line if there was no column limit.<br>
+///<br>
+/// This is used as a main interface between the \c UnwrappedLineParser and the<br>
+/// \c UnwrappedLineFormatter. The key property is that changing the formatting<br>
+/// within an unwrapped line does not affect any other unwrapped lines.<br>
+struct UnwrappedLine {<br>
+  UnwrappedLine() : Level(0) {<br>
+  }<br>
+<br>
+  /// \brief The \c Token comprising this \c UnwrappedLine.<br>
+  SmallVector<FormatToken, 16> Tokens;<br>
+<br>
+  /// \brief The indent level of the \c UnwrappedLine.<br>
+  unsigned Level;<br>
+};<br>
+<br>
+class UnwrappedLineConsumer {<br>
+public:<br>
+  virtual void formatUnwrappedLine(const UnwrappedLine &Line) = 0;<br>
+};<br>
+<br>
+class UnwrappedLineParser {<br>
+public:<br>
+  UnwrappedLineParser(Lexer &Lex, SourceManager &SourceMgr,<br>
+                      UnwrappedLineConsumer &Callback);<br>
+<br>
+  void parse();<br>
+<br>
+private:<br>
+  void parseLevel();<br>
+  void parseBlock();<br>
+  void parsePPDirective();<br>
+  void parseComment();<br>
+  void parseStatement();<br>
+  void parseParens();<br>
+  void parseIfThenElse();<br>
+  void parseDoWhile();<br>
+  void parseLabel();<br>
+  void parseCaseLabel();<br>
+  void parseSwitch();<br>
+  void parseAccessSpecifier();<br>
+  void parseEnum();<br>
+  void addUnwrappedLine();<br>
+  bool eof() const;<br>
+  void nextToken();<br>
+  void parseToken();<br>
+<br>
+  /// Returns the text of \c FormatTok.<br>
+  StringRef tokenText();<br>
+<br>
+  UnwrappedLine Line;<br>
+  FormatToken FormatTok;<br>
+  bool GreaterStashed;<br>
+<br>
+  Lexer &Lex;<br>
+  SourceManager &SourceMgr;<br>
+  IdentifierTable IdentTable;<br>
+  UnwrappedLineConsumer &Callback;<br>
+};<br>
+<br>
+}  // end namespace format<br>
+}  // end namespace clang<br>
+<br>
+#endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H<br>
<br>
Modified: cfe/trunk/lib/Makefile<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Makefile?rev=169137&r1=169136&r2=169137&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Makefile?rev=169137&r1=169136&r2=169137&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/lib/Makefile (original)<br>
+++ cfe/trunk/lib/Makefile Mon Dec  3 12:12:45 2012<br>
@@ -10,7 +10,7 @@<br>
<br>
 PARALLEL_DIRS = Headers Basic Lex Parse AST ASTMatchers Sema CodeGen Analysis \<br>
                 StaticAnalyzer Edit Rewrite ARCMigrate Serialization Frontend \<br>
-                FrontendTool Tooling Driver<br>
+                FrontendTool Tooling Driver Format<br>
<br>
 include $(CLANG_LEVEL)/Makefile<br>
<br>
<br>
Modified: cfe/trunk/unittests/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/CMakeLists.txt?rev=169137&r1=169136&r2=169137&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/CMakeLists.txt?rev=169137&r1=169136&r2=169137&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/unittests/CMakeLists.txt (original)<br>
+++ cfe/trunk/unittests/CMakeLists.txt Mon Dec  3 12:12:45 2012<br>
@@ -15,3 +15,4 @@<br>
 add_subdirectory(Lex)<br>
 add_subdirectory(Frontend)<br>
 add_subdirectory(Tooling)<br>
+add_subdirectory(Format)<br>
<br>
Added: cfe/trunk/unittests/Format/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/CMakeLists.txt?rev=169137&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/CMakeLists.txt?rev=169137&view=auto</a><br>

==============================================================================<br>
--- cfe/trunk/unittests/Format/CMakeLists.txt (added)<br>
+++ cfe/trunk/unittests/Format/CMakeLists.txt Mon Dec  3 12:12:45 2012<br>
@@ -0,0 +1,17 @@<br>
+set(LLVM_LINK_COMPONENTS<br>
+  ${LLVM_TARGETS_TO_BUILD}<br>
+  asmparser<br>
+  support<br>
+  mc<br>
+  )<br>
+<br>
+add_clang_unittest(FormatTests<br>
+  FormatTest.cpp<br>
+  )<br>
+<br>
+target_link_libraries(FormatTests<br>
+  clangAST<br>
+  clangFormat<br>
+  clangTooling<br>
+  clangRewriteCore<br>
+  )<br>
<br>
Added: cfe/trunk/unittests/Format/FormatTest.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=169137&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=169137&view=auto</a><br>

==============================================================================<br>
--- cfe/trunk/unittests/Format/FormatTest.cpp (added)<br>
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Dec  3 12:12:45 2012<br>
@@ -0,0 +1,336 @@<br>
+//===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "../Tooling/RewriterTestContext.h"<br>
+#include "clang/Lex/Lexer.h"<br>
+#include "clang/Format/Format.h"<br>
+#include "gtest/gtest.h"<br>
+<br>
+namespace clang {<br>
+namespace format {<br>
+<br>
+class FormatTest : public ::testing::Test {<br>
+protected:<br>
+  std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,<br>
+                     const FormatStyle &Style) {<br>
+    RewriterTestContext Context;<br>
+    FileID ID = Context.createInMemoryFile("input.cc", Code);<br>
+    SourceLocation Start =<br>
+        Context.Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset);<br>
+    std::vector<CharSourceRange> Ranges(<br>
+        1,<br>
+        CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length)));<br>
+    LangOptions LangOpts;<br>
+    LangOpts.CPlusPlus = 1;<br>
+    Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, LangOpts);<br>
+    tooling::Replacements Replace =<br>
+        reformat(Style, Lex, Context.Sources, Ranges);<br>
+    EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite));<br>
+    return Context.getRewrittenText(ID);<br>
+  }<br>
+<br>
+  std::string format(llvm::StringRef Code,<br>
+                     const FormatStyle &Style = getLLVMStyle()) {<br>
+    return format(Code, 0, Code.size(), Style);<br>
+  }<br>
+<br>
+  void verifyFormat(llvm::StringRef Code) {<br>
+    std::string WithoutFormat(Code.str());<br>
+    for (unsigned i = 0, e = WithoutFormat.size(); i != e; ++i) {<br>
+      if (WithoutFormat[i] == '\n')<br>
+        WithoutFormat[i] = ' ';<br>
+    }<br>
+    EXPECT_EQ(Code.str(), format(WithoutFormat));<br>
+  }<br>
+<br>
+  void verifyGoogleFormat(llvm::StringRef Code) {<br>
+    std::string WithoutFormat(Code.str());<br>
+    for (unsigned i = 0, e = WithoutFormat.size(); i != e; ++i) {<br>
+      if (WithoutFormat[i] == '\n')<br>
+        WithoutFormat[i] = ' ';<br>
+    }<br>
+    EXPECT_EQ(Code.str(), format(WithoutFormat, getGoogleStyle()));<br>
+  }<br>
+};<br>
+<br>
+TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) {<br>
+  EXPECT_EQ(";", format(";"));<br>
+}<br>
+<br>
+TEST_F(FormatTest, FormatsGlobalStatementsAt0) {<br>
+  EXPECT_EQ("int i;", format("  int i;"));<br>
+  EXPECT_EQ("\nint i;", format(" \n\t \r  int i;"));<br>
+  EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));<br>
+  EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));<br>
+}<br>
+<br>
+TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {<br>
+  EXPECT_EQ("int i;", format("int\ni;"));<br>
+}<br>
+<br>
+TEST_F(FormatTest, FormatsNestedBlockStatements) {<br>
+  EXPECT_EQ("{\n  {\n    {\n    }\n  }\n}", format("{{{}}}"));<br>
+}<br>
+<br>
+TEST_F(FormatTest, FormatsForLoop) {<br>
+  verifyFormat(<br>
+      "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"<br>
+      "     ++VeryVeryLongLoopVariable);");<br>
+}<br>
+<br>
+TEST_F(FormatTest, FormatsWhileLoop) {<br>
+  verifyFormat("while (true) {\n}");<br>
+}<br>
+<br>
+TEST_F(FormatTest, FormatsNestedCall) {<br>
+  verifyFormat("Method(f1, f2(f3));");<br>
+  verifyFormat("Method(f1(f2, f3()));");<br>
+}<br>
+<br>
+TEST_F(FormatTest, FormatsAwesomeMethodCall) {<br>
+  verifyFormat(<br>
+      "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"<br>
+      "    parameter, parameter, parameter)), SecondLongCall(parameter));");<br>
+}<br>
+<br>
+TEST_F(FormatTest, FormatsFunctionDefinition) {<br>
+  verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"<br>
+               " int h, int j, int f,\n"<br>
+               "       int c, int ddddddddddddd) {\n"<br>
+               "}");<br>
+}<br>
+<br>
+TEST_F(FormatTest, FormatIfWithoutCompountStatement) {<br>
+  verifyFormat("if (true)\n  f();\ng();");<br>
+  verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");<br>
+  verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");<br>
+}<br>
+<br>
+TEST_F(FormatTest, ParseIfThenElse) {<br>
+  verifyFormat("if (true)\n"<br>
+               "  if (true)\n"<br>
+               "    if (true)\n"<br>
+               "      f();\n"<br>
+               "    else\n"<br>
+               "      g();\n"<br>
+               "  else\n"<br>
+               "    h();\n"<br>
+               "else\n"<br>
+               "  i();");<br>
+  verifyFormat("if (true)\n"<br>
+               "  if (true)\n"<br>
+               "    if (true) {\n"<br>
+               "      if (true)\n"<br>
+               "        f();\n"<br>
+               "    } else {\n"<br>
+               "      g();\n"<br>
+               "    }\n"<br>
+               "  else\n"<br>
+               "    h();\n"<br>
+               "else {\n"<br>
+               "  i();\n"<br>
+               "}");<br>
+}<br>
+<br>
+TEST_F(FormatTest, UnderstandsSingleLineComments) {<br>
+  EXPECT_EQ("// line 1\n// line 2\nvoid f() {\n}\n",<br>
+            format("// line 1\n// line 2\nvoid f() {}\n"));<br>
+<br>
+  EXPECT_EQ("void f() {\n  // Doesn't do anything\n}",<br>
+            format("void f() {\n// Doesn't do anything\n}"));<br>
+<br>
+  EXPECT_EQ("int i  // This is a fancy variable\n    = 5;",<br>
+            format("int i  // This is a fancy variable\n= 5;"));<br>
+<br>
+  verifyFormat("f(/*test=*/ true);");<br>
+}<br>
+<br>
+TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {<br>
+  verifyFormat("class A {\n};");<br>
+}<br>
+<br>
+TEST_F(FormatTest, BreaksAsHighAsPossible) {<br>
+  verifyFormat(<br>
+      "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"<br>
+      "    (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"<br>
+      "  f();");<br>
+}<br>
+<br>
+TEST_F(FormatTest, ElseIf) {<br>
+  verifyFormat("if (a) {\n"<br>
+               "} else if (b) {\n"<br>
+               "}");<br>
+  verifyFormat("if (a)\n"<br>
+               "  f();\n"<br>
+               "else if (b)\n"<br>
+               "  g();\n"<br>
+               "else\n"<br>
+               "  h();");<br>
+}<br>
+<br>
+TEST_F(FormatTest, UnderstandsAccessSpecifiers) {<br>
+  verifyFormat("class A {\n"<br>
+               "public:\n"<br>
+               "protected:\n"<br>
+               "private:\n"<br>
+               "  void f() {\n"<br>
+               "  }\n"<br>
+               "};");<br>
+  verifyGoogleFormat("class A {\n"<br>
+                     " public:\n"<br>
+                     " protected:\n"<br>
+                     " private:\n"<br>
+                     "  void f() {\n"<br>
+                     "  }\n"<br>
+                     "};");<br>
+}<br>
+<br>
+TEST_F(FormatTest, SwitchStatement) {<br>
+  verifyFormat("switch (x) {\n"<br>
+               "case 1:\n"<br>
+               "  f();\n"<br>
+               "  break;\n"<br>
+               "case kFoo:\n"<br>
+               "case ns::kBar:\n"<br>
+               "case kBaz:\n"<br>
+               "  break;\n"<br>
+               "default:\n"<br>
+               "  g();\n"<br>
+               "  break;\n"<br>
+               "}");<br>
+  verifyFormat("switch (x) {\n"<br>
+               "case 1: {\n"<br>
+               "  f();\n"<br>
+               "  break;\n"<br>
+               "}\n"<br>
+               "}");<br>
+  verifyFormat("switch (test)\n"<br>
+               "  ;");<br>
+}<br>
+<br>
+TEST_F(FormatTest, Labels) {<br>
+  verifyFormat("void f() {\n"<br>
+               "  some_code();\n"<br>
+               "test_label:\n"<br>
+               "  some_other_code();\n"<br>
+               "  {\n"<br>
+               "    some_more_code();\n"<br>
+               "  another_label:\n"<br>
+               "    some_more_code();\n"<br>
+               "  }\n"<br>
+               "}");<br>
+  verifyFormat("some_code();\n"<br>
+               "test_label:\n"<br>
+               "some_other_code();");<br>
+}<br>
+<br>
+TEST_F(FormatTest, DerivedClass) {<br>
+  verifyFormat("class A : public B {\n"<br>
+               "};");<br>
+}<br>
+<br>
+TEST_F(FormatTest, DoWhile) {<br>
+  verifyFormat("do {\n"<br>
+               "  do_something();\n"<br>
+               "} while (something());");<br>
+  verifyFormat("do\n"<br>
+               "  do_something();\n"<br>
+               "while (something());");<br>
+}<br>
+<br>
+TEST_F(FormatTest, BreaksDesireably) {<br>
+  verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"<br>
+               "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"<br>
+               "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n};");<br>
+<br>
+  verifyFormat(<br>
+      "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"<br>
+      "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");<br>
+<br>
+  verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"<br>
+               "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"<br>
+               "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");<br>
+}<br>
+<br>
+TEST_F(FormatTest, AlignsStringLiterals) {<br>
+  verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"<br>
+               "                                      \"short literal\");");<br>
+  verifyFormat(<br>
+      "looooooooooooooooooooooooongFunction(\n"<br>
+      "    \"short literal\"\n"<br>
+      "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");<br>
+}<br>
+<br>
+TEST_F(FormatTest, UnderstandsEquals) {<br>
+  verifyFormat(<br>
+      "aaaaaaaaaaaaaaaaa =\n"<br>
+      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");<br>
+  verifyFormat(<br>
+      "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"<br>
+      "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"<br>
+      "}");<br>
+  verifyFormat(<br>
+      "if (a) {\n"<br>
+      "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"<br>
+      "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"<br>
+      "}");<br>
+<br>
+  verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"<br>
+               "        100000000 + 100000000) {\n}");<br>
+}<br>
+<br>
+TEST_F(FormatTest, UnderstandsTemplateParameters) {<br>
+  verifyFormat("A<int> a;");<br>
+  verifyFormat("A<A<A<int> > > a;");<br>
+  verifyFormat("A<A<A<int, 2>, 3>, 4> a;");<br>
+  verifyFormat("bool x = a < 1 || 2 > a;");<br>
+  verifyFormat("bool x = 5 < f<int>();");<br>
+  verifyFormat("bool x = f<int>() > 5;");<br>
+  verifyFormat("bool x = 5 < a<int>::x;");<br>
+  verifyFormat("bool x = a < 4 ? a > 2 : false;");<br>
+  verifyFormat("bool x = f() ? a < 2 : a > 2;");<br>
+<br>
+  verifyGoogleFormat("A<A<int>> a;");<br>
+  verifyGoogleFormat("A<A<A<int>>> a;");<br>
+  verifyGoogleFormat("A<A<A<A<int>>>> a;");<br>
+<br>
+  verifyFormat("test >> a >> b;");<br>
+  verifyFormat("test << a >> b;");<br>
+<br>
+  verifyFormat("f<int>();");<br>
+  verifyFormat("template <typename T> void f() {\n}");<br>
+}<br>
+<br>
+TEST_F(FormatTest, UndestandsUnaryOperators) {<br>
+  verifyFormat("int a = -2;");<br>
+}<br>
+<br>
+TEST_F(FormatTest, UndestandsOverloadedOperators) {<br>
+  verifyFormat("bool operator<() {\n}");<br>
+}<br>
+<br>
+TEST_F(FormatTest, UnderstandsUsesOfStar) {<br>
+  verifyFormat("int *f(int *a) {\n}");<br>
+  verifyFormat("f(a, *a);");<br>
+  verifyFormat("f(*a);");<br>
+  verifyFormat("int a = b * 10;");<br>
+  verifyFormat("int a = 10 * b;");<br>
+  // verifyFormat("int a = b * c;");<br>
+  verifyFormat("int a = *b;");<br>
+  // verifyFormat("int a = *b * c;");<br>
+  // verifyFormat("int a = b * *c;");<br>
+}<br>
+<br>
+//TEST_F(FormatTest, IncorrectDerivedClass) {<br>
+//  verifyFormat("public B {\n"<br>
+//               "};");<br>
+//}<br>
+<br>
+}  // end namespace tooling<br>
+}  // end namespace clang<br>
<br>
Added: cfe/trunk/unittests/Format/Makefile<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/Makefile?rev=169137&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/Makefile?rev=169137&view=auto</a><br>

==============================================================================<br>
--- cfe/trunk/unittests/Format/Makefile (added)<br>
+++ cfe/trunk/unittests/Format/Makefile Mon Dec  3 12:12:45 2012<br>
@@ -0,0 +1,19 @@<br>
+##===- unittests/Format/Makefile ---------------------------*- Makefile -*-===##<br>
+#<br>
+#                     The LLVM Compiler Infrastructure<br>
+#<br>
+# This file is distributed under the University of Illinois Open Source<br>
+# License. See LICENSE.TXT for details.<br>
+#<br>
+##===----------------------------------------------------------------------===##<br>
+<br>
+CLANG_LEVEL = ../..<br>
+TESTNAME = Format<br>
+include $(CLANG_LEVEL)/../../Makefile.config<br>
+LINK_COMPONENTS := $(TARGETS_TO_BUILD) asmparser support mc<br>
+USEDLIBS = clangFormat.a clangTooling.a clangFrontend.a clangSerialization.a \<br>
+           clangDriver.a clangParse.a clangRewriteCore.a<br>
+           clangRewriteFrontend.a clangSema.a clangAnalysis.a clangEdit.a \<br>
+           clangAST.a clangASTMatchers.a clangLex.a clangBasic.a<br>
+<br>
+include $(CLANG_LEVEL)/unittests/Makefile<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br></div>