krasimir created this revision. krasimir added reviewers: klimek, djasper. This patch adds a FormatStyleSet for storing per-language FormatStyles for the purposes of formatting code blocks inside the main code.
Repository: rC Clang https://reviews.llvm.org/D41487 Files: include/clang/Format/Format.h lib/Format/Format.cpp
Index: lib/Format/Format.cpp =================================================================== --- lib/Format/Format.cpp +++ lib/Format/Format.cpp @@ -888,15 +888,24 @@ // Look for a suitable configuration starting from the end, so we can // find the configuration for the specific language first, and the default // configuration (which can only be at slot 0) after it. + bool LanguageFound = false; for (int i = Styles.size() - 1; i >= 0; --i) { - if (Styles[i].Language == Language || - Styles[i].Language == FormatStyle::LK_None) { + if (!LanguageFound && (Styles[i].Language == Language || + Styles[i].Language == FormatStyle::LK_None)) { *Style = Styles[i]; Style->Language = Language; - return make_error_code(ParseError::Success); + LanguageFound = true; + break; + } + } + if (!LanguageFound) + return make_error_code(ParseError::Unsuitable); + for (int i = Styles.size() - 1; i >= 0; --i) { + if (Styles[i].Language != FormatStyle::LK_None) { + Style->AddLanguageStyle(Styles[i]); } } - return make_error_code(ParseError::Unsuitable); + return make_error_code(ParseError::Success); } std::string configurationAsText(const FormatStyle &Style) { @@ -910,6 +919,34 @@ return Stream.str(); } +llvm::Optional<FormatStyle> +FormatStyle::FormatStyleSet::Get(FormatStyle::LanguageKind Language) const { + if (!Styles) + return None; + auto It = Styles->find(Language); + if (It == Styles->end()) + return None; + FormatStyle Style = *It->second; + Style.StyleSet = *this; + return Style; +} + +void FormatStyle::FormatStyleSet::Add(FormatStyle Style) { + Style.StyleSet.Styles.reset(); + if (!Styles) + Styles = std::make_shared<MapType>(); + (*Styles)[Style.Language].reset(new FormatStyle(Style)); +} + +llvm::Optional<FormatStyle> +FormatStyle::GetLanguageStyle(FormatStyle::LanguageKind Language) const { + return StyleSet.Get(Language); +} + +void FormatStyle::AddLanguageStyle(const FormatStyle& Style) { + StyleSet.Add(Style); +} + namespace { class JavaScriptRequoter : public TokenAnalyzer { Index: include/clang/Format/Format.h =================================================================== --- include/clang/Format/Format.h +++ include/clang/Format/Format.h @@ -1685,6 +1685,29 @@ Standard == R.Standard && TabWidth == R.TabWidth && UseTab == R.UseTab; } + + llvm::Optional<FormatStyle> GetLanguageStyle(LanguageKind Language) const; + void AddLanguageStyle(const FormatStyle& Style); + + private: + // Stores per-language styles. A FormatStyle instance inside has an empty + // StyleSet. A FormatStyle instance returned by the Get method has its + // StyleSet set to a copy of the originating StyleSet, effectively keeping + // the internal representation of that StyleSet alive. + // + // The memory management and ownership reminds of a birds nest: chicks + // leaving the nest take photos of the nest with them. + struct FormatStyleSet { + typedef std::map<FormatStyle::LanguageKind, + std::unique_ptr<const FormatStyle>> + MapType; + llvm::Optional<FormatStyle> Get(FormatStyle::LanguageKind Language) const; + void Add(FormatStyle Style); + private: + std::shared_ptr<MapType> Styles; + }; + + FormatStyleSet StyleSet; }; /// \brief Returns a format style complying with the LLVM coding standards:
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits