Skip to content

Commit f793511

Browse files
committed
Initial version of formatting library.
This formatting library will be used by a stand-alone clang-format tool and can also be used when writing other refactorings. Manuel's original design document: https://docs.google.com/a/google.com/document/d/1gpckL2U_6QuU9YW2L1ABsc4Fcogn5UngKk7fE5dDOoA/edit The library can already successfully format itself. Review: http://llvm-reviews.chandlerc.com/D80 llvm-svn: 169137
1 parent 2ed1c09 commit f793511

File tree

12 files changed

+1651
-1
lines changed

12 files changed

+1651
-1
lines changed

clang/include/clang/Format/Format.h

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//===--- Format.h - Format C++ code -----------------------------*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
///
10+
/// \file
11+
/// Various functions to configurably format source code.
12+
///
13+
/// This is EXPERIMENTAL code under heavy development. It is not in a state yet,
14+
/// where it can be used to format real code.
15+
///
16+
//===----------------------------------------------------------------------===//
17+
18+
#ifndef LLVM_CLANG_FORMAT_FORMAT_H_
19+
#define LLVM_CLANG_FORMAT_FORMAT_H
20+
21+
#include "clang/Frontend/FrontendAction.h"
22+
#include "clang/Tooling/Refactoring.h"
23+
24+
namespace clang {
25+
26+
class Lexer;
27+
class SourceManager;
28+
29+
namespace format {
30+
31+
/// \brief The \c FormatStyle is used to configure the formatting to follow
32+
/// specific guidelines.
33+
struct FormatStyle {
34+
/// \brief The column limit.
35+
unsigned ColumnLimit;
36+
37+
/// \brief The maximum number of consecutive empty lines to keep.
38+
unsigned MaxEmptyLinesToKeep;
39+
40+
/// \brief Set whether & and * bind to the type as opposed to the variable.
41+
bool PointerAndReferenceBindToType;
42+
43+
/// \brief The extra indent or outdent of access modifiers (e.g.: public:).
44+
int AccessModifierOffset;
45+
46+
/// \brief Split two consecutive closing '>' by a space, i.e. use
47+
/// A<A<int> > instead of A<A<int>>.
48+
bool SplitTemplateClosingGreater;
49+
};
50+
51+
/// \brief Returns a format style complying with the LLVM coding standards:
52+
/// http://llvm.org/docs/CodingStandards.html.
53+
FormatStyle getLLVMStyle();
54+
55+
/// \brief Returns a format style complying with Google's C++ style guide:
56+
/// http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml.
57+
FormatStyle getGoogleStyle();
58+
59+
/// \brief Reformats the given \p Ranges in the token stream coming out of
60+
/// \c Lex.
61+
///
62+
/// Each range is extended on either end to its next bigger logic unit, i.e.
63+
/// everything that might influence its formatting or might be influenced by its
64+
/// formatting.
65+
///
66+
/// Returns the \c Replacements necessary to make all \p Ranges comply with
67+
/// \p Style.
68+
tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
69+
SourceManager &SourceMgr,
70+
std::vector<CharSourceRange> Ranges);
71+
72+
} // end namespace format
73+
} // end namespace clang
74+
75+
#endif // LLVM_CLANG_FORMAT_FORMAT_H

clang/lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ add_subdirectory(Frontend)
1616
add_subdirectory(FrontendTool)
1717
add_subdirectory(Tooling)
1818
add_subdirectory(StaticAnalyzer)
19+
add_subdirectory(Format)

clang/lib/Format/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
set(LLVM_LINK_COMPONENTS support)
2+
3+
add_clang_library(clangFormat
4+
UnwrappedLineParser.cpp
5+
Format.cpp
6+
)
7+
8+
add_dependencies(clangFormat
9+
ClangAttrClasses
10+
ClangAttrList
11+
ClangDeclNodes
12+
ClangDiagnosticCommon
13+
ClangDiagnosticFrontend
14+
ClangStmtNodes
15+
)
16+
17+
target_link_libraries(clangFormat
18+
clangBasic
19+
clangFrontend
20+
clangAST
21+
clangASTMatchers
22+
clangRewriteCore
23+
clangRewriteFrontend
24+
)

0 commit comments

Comments
 (0)