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

Manuel Klimek klimek at google.com
Wed Jun 6 03:08:10 PDT 2012


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
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120606/d6970a6e/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: clang-format-interface.patch
Type: application/octet-stream
Size: 3627 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120606/d6970a6e/attachment.obj>


More information about the cfe-commits mailing list