<div class="gmail_quote">On Wed, Jun 6, 2012 at 2:57 AM, Manuel Klimek <span dir="ltr"><<a href="mailto:klimek@google.com" target="_blank">klimek@google.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>Daniel & me have worked on a first proposal to provide an interface for clang formatting as part of the Tooling library.</div></blockquote><div><br></div><div>Please re-send the patch as an attachment? Email plays havoc w/ 80-columns. </div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><br></div><div>Questions:</div><div>- please point out anything that might make this interface not viable for your own use cases</div>
<div>- are we missing the point somewhere?</div><div>- is Tooling/ the right place for this?</div><div><br></div><div>Thx!</div><div>/Manuel</div><div><br></div><div>diff --git a/include/clang/Tooling/Format.h b/include/clang/Tooling/Format.h </div>
<div>new file mode 100644 </div>
<div>index 0000000..8a64574 </div>
<div>--- /dev/null </div>
<div>+++ b/include/clang/Tooling/Format.h </div>
<div>@@ -0,0 +1,106 @@ </div>
<div>+//===--- Format.h - Format C++ code -----------------------------*- C++ -*-===// </div>
<div>+// </div>
<div>+// The LLVM Compiler Infrastructure </div>
<div>+// </div>
<div>+// This file is distributed under the University of Illinois Open Source </div>
<div>+// License. See LICENSE.TXT for details. </div>
<div>+//</div><div>+//===----------------------------------------------------------------------===//</div><div>+//</div><div>+// Various functions to configurably format source code.</div><div>+//</div><div>+// This supports two use cases:</div>
<div>+// - Format (ranges in) a given file.</div><div>+// - Reformat sources after automated refactoring.</div><div>+//</div><div>+// Formatting is done on a per file basis.</div><div>+//</div><div>+// The formatting strategy is:</div>
<div>+// - lex the file content</div><div>+// - in case formatting decisions need information of the AST analysis, run</div><div>+// the analysis and annotate all tokens that are relevant for formatting</div><div>+// in the current file</div>
<div>+// - reformat based on the (annotated) tokens</div><div>+//</div><div>+// To allow different strategies of handling the SourceManager the formatter</div><div>+// works on an interface AnalyzableSource, which provides access to the file</div>
<div>+// contents and, if available, allow to lazily run an analysis over the AST</div><div>+// when needed.</div><div>+//</div><div>+//===----------------------------------------------------------------------===//</div>
<div>+</div><div>+#ifndef LLVM_CLANG_TOOLING_FORMAT_H_</div><div>+#define LLVM_CLANG_TOOLING_FORMAT_H</div><div>+</div><div>+#include "clang/Frontend/FrontendAction.h"</div><div>+#include "clang/Tooling/Refactoring.h"</div>
<div>+</div><div>+namespace clang {</div><div>+namespace tooling {</div><div>+</div><div>+/// \brief Provides access to a source file and (optionally) its AST.</div><div>+///</div><div>+/// Implementors can overwrite VisitAST to provide AST analysis capabilities</div>
<div>+/// for sources that can be parsed.</div><div>+class AnalyzableSource {</div><div>+public:</div><div>+ virtual ~AnalyzableSource();</div><div>+</div><div>+ /// \brief Runs the given action on the source's AST.</div>
<div>+ ///</div><div>+ /// Returns whether parsing was successful.</div><div>+ /// If false, some parts of the AST might have been visited.</div><div>+ virtual bool visitAST(ASTFrontendAction *Action) { return false; }</div>
<div>+</div><div>+ /// Returns the file's content.</div><div>+ virtual MemoryBuffer *getContent() = 0;</div><div>+};</div><div>+</div><div>+/// \brief Creates an AnalyzableSource from a snippet of code.</div><div>+///</div>
<div>+/// Assumes that 'Code' contains a complete TU.</div><div>+AnalyzableSource *createAnalyzableSource(Twine Code);</div><div>+</div><div>+/// \brief Creates an AnalyzableSource from a file.</div><div>+///</div>
<div>+/// Does not support AST analysis.</div><div>+AnalyzableSource *createAnalyzableSource(StringRef File);</div><div>+</div><div>+</div><div>+/// \brief A character range of source code.</div><div>+struct CodeRange {</div>
<div>+ CodeRange(unsigned Offset, unsigned Length)</div><div>+ : Offset(Offset), Length(Length) {}</div><div>+</div><div>+ unsigned Offset;</div><div>+ unsigned Length;</div><div>+};</div><div>+</div><div>+/// \brief Set of formatting options.</div>
<div>+struct FormatOptions {</div><div>+ /// \brief Indent level of blocks.</div><div>+ unsigned BlockIndentation;</div><div>+</div><div>+ /// \brief Indent level of line continuations.</div><div>+ unsigned ContinuationIndentation;</div>
<div>+};</div><div>+</div><div>+/// \brief Creates a format according to the LLVM style guide.</div><div>+///</div><div>+/// See <a href="http://llvm.org/docs/CodingStandards.html" target="_blank">http://llvm.org/docs/CodingStandards.html</a>.</div>
<div>+FormatOptions getLLVMFormat();</div><div>+</div><div>+/// \brief Creates a format according to the Google style guide.</div><div>+///</div><div>+/// See <a href="http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml" target="_blank">http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml</a>.</div>
<div><div>+FormatOptions getGoogleFormat();</div><div>+</div><div>+/// \brief Reformats the given Ranges in the Source.</div><div>+Replacements reformat(const AnalyzableSource &Source,</div><div>+ std:vector<CodeRange> Ranges,</div>
<div>+ const FormatOptions &Options);</div><div>+</div><div>+} // end namespace tooling</div><div>+} // end namespace clang</div><div>+</div><div>+#endif // LLVM_CLANG_TOOLING_FORMAT_H</div></div>
<div><br></div>
</blockquote></div><br>