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 | d6538338fd23c375e7c5af670cd3043a3a747253 (patch) | |
| tree | a74346d2a91e822ba1dc7dc37f29228666fba199 /clang/lib/Format/Format.cpp | |
| parent | e79600633364e335b60ae5be476a1b17453ac6f4 (diff) | |
| download | bcm5719-llvm-d6538338fd23c375e7c5af670cd3043a3a747253.tar.gz bcm5719-llvm-d6538338fd23c375e7c5af670cd3043a3a747253.zip | |
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
llvm-svn: 181326
Diffstat (limited to 'clang/lib/Format/Format.cpp')
| -rw-r--r-- | clang/lib/Format/Format.cpp | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index a0557f78182..4c7c405e81e 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/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) { |

