diff options
| author | Alexander Kornienko <alexfh@google.com> | 2013-05-07 15:32:14 +0000 |
|---|---|---|
| committer | Alexander Kornienko <alexfh@google.com> | 2013-05-07 15:32:14 +0000 |
| commit | d71ec16fffd10879bf74e531d9f7fb4c5ac4eaf8 (patch) | |
| tree | 9ca78e04cc20af23d49b364e96af204e052e45d9 /lib/Format/Format.cpp | |
| parent | dbef71ebc66fe5553db01eb8e95b696c3223e737 (diff) | |
| download | clang-d71ec16fffd10879bf74e531d9f7fb4c5ac4eaf8.tar.gz clang-d71ec16fffd10879bf74e531d9f7fb4c5ac4eaf8.tar.bz2 clang-d71ec16fffd10879bf74e531d9f7fb4c5ac4eaf8.tar.xz | |
Config file support for clang-format, part 1.
Summary:
Added parseConfiguration method, which reads FormatStyle from YAML
string. This supports all FormatStyle fields and an additional BasedOnStyle
field, which can be used to specify base style.
Reviewers: djasper, klimek
Reviewed By: djasper
CC: cfe-commits
Differential Revision: http://llvm-reviews.chandlerc.com/D754
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@181326 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/Format.cpp')
| -rw-r--r-- | lib/Format/Format.cpp | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index a0557f7818..4c7c405e81 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -28,9 +28,59 @@ #include "llvm/ADT/STLExtras.h" #include "llvm/Support/Allocator.h" #include "llvm/Support/Debug.h" +#include "llvm/Support/YAMLTraits.h" #include <queue> #include <string> +namespace llvm { +namespace yaml { +template <> +struct ScalarEnumerationTraits<clang::format::FormatStyle::LanguageStandard> { + static void enumeration(IO &io, + clang::format::FormatStyle::LanguageStandard &value) { + io.enumCase(value, "C++03", clang::format::FormatStyle::LS_Cpp03); + io.enumCase(value, "C++11", clang::format::FormatStyle::LS_Cpp11); + io.enumCase(value, "Auto", clang::format::FormatStyle::LS_Auto); + } +}; + +template <> struct MappingTraits<clang::format::FormatStyle> { + static void mapping(llvm::yaml::IO &IO, clang::format::FormatStyle &Style) { + if (!IO.outputting()) { + StringRef BasedOnStyle; + IO.mapOptional("BasedOnStyle", BasedOnStyle); + + if (!BasedOnStyle.empty()) + Style = clang::format::getPredefinedStyle(BasedOnStyle); + } + + IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset); + IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft); + IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine", + Style.AllowAllParametersOfDeclarationOnNextLine); + IO.mapOptional("AllowShortIfStatementsOnASingleLine", + Style.AllowShortIfStatementsOnASingleLine); + IO.mapOptional("BinPackParameters", Style.BinPackParameters); + IO.mapOptional("ColumnLimit", Style.ColumnLimit); + IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine", + Style.ConstructorInitializerAllOnOneLineOrOnePerLine); + IO.mapOptional("DerivePointerBinding", Style.DerivePointerBinding); + IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels); + IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep); + IO.mapOptional("ObjCSpaceBeforeProtocolList", + Style.ObjCSpaceBeforeProtocolList); + IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter); + IO.mapOptional("PenaltyReturnTypeOnItsOwnLine", + Style.PenaltyReturnTypeOnItsOwnLine); + IO.mapOptional("PointerBindsToType", Style.PointerBindsToType); + IO.mapOptional("SpacesBeforeTrailingComments", + Style.SpacesBeforeTrailingComments); + IO.mapOptional("Standard", Style.Standard); + } +}; +} +} + namespace clang { namespace format { @@ -98,6 +148,37 @@ FormatStyle getMozillaStyle() { return MozillaStyle; } +FormatStyle getPredefinedStyle(StringRef Name) { + if (Name.equals_lower("llvm")) + return getLLVMStyle(); + if (Name.equals_lower("chromium")) + return getChromiumStyle(); + if (Name.equals_lower("mozilla")) + return getMozillaStyle(); + if (Name.equals_lower("google")) + return getGoogleStyle(); + + llvm::errs() << "Unknown style " << Name << ", using Google style.\n"; + return getGoogleStyle(); +} + +llvm::error_code parseConfiguration(StringRef Text, FormatStyle *Style) { + llvm::yaml::Input Input(Text); + Input >> *Style; + return Input.error(); +} + +std::string configurationAsText(const FormatStyle &Style) { + std::string Text; + llvm::raw_string_ostream Stream(Text); + llvm::yaml::Output Output(Stream); + // We use the same mapping method for input and output, so we need a non-const + // reference here. + FormatStyle NonConstStyle = Style; + Output << NonConstStyle; + return Text; +} + // Returns the length of everything up to the first possible line break after // the ), ], } or > matching \c Tok. static unsigned getLengthToMatchingParen(const AnnotatedToken &Tok) { |
