[cfe-commits] [PATCH] Interface proposal for clang formatting tools

Jordan Rose jordan_rose at apple.com
Wed Jun 6 10:26:07 PDT 2012


Everyone else has nice design comments, but I want to throw in a few nitpicks:

std::vector<CodeRange> should probably be ArrayRef<CodeRange>, unless it's going to update the ranges, in which case that should definitely be documented.

The two createAnalyzableSource signatures are too similar for me. If you have a single buffer for the code, you have to manually wrap it in a Twine, or else you're accidentally treating it as a path. If you built your path with a Twine, you have to convert it to a string. Can we rename one or both of them? ("… FromCode" and "…FromFile", perhaps.)

As for where this should live: what's the boundary between Edit and Rewrite? Edit doesn't currently depend on Frontend, and the dependence on AST only comes from RewriteObjCFoundationAPI (which probably should be hoisted out anyway). AnalyzableSource definitely depends on AST, but the dependence on Frontend could be changed if it took an ASTConsumer instead of an ASTFrontendAction. (Not saying we should do that, just listing possibilities.)

Jordan


On Jun 6, 2012, at 3:08 , Manuel Klimek <klimek at google.com> wrote:

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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120606/ff75fafa/attachment.html>


More information about the cfe-commits mailing list