diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2022-02-05 20:07:43 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2022-05-14 11:44:47 +0000 |
commit | 1fd87a682ad7442327078e1eeb63edc4258f9815 (patch) | |
tree | 83b42223e987ef7df2e1036937bc1bb627fa2779 /contrib/llvm-project/clang/lib/Format/Format.cpp | |
parent | 04eeddc0aa8e0a417a16eaf9d7d095207f4a8623 (diff) | |
parent | ecbca9f5fb7d7613d2b94982c4825eb0d33d6842 (diff) |
Diffstat (limited to 'contrib/llvm-project/clang/lib/Format/Format.cpp')
-rw-r--r-- | contrib/llvm-project/clang/lib/Format/Format.cpp | 57 |
1 files changed, 33 insertions, 24 deletions
diff --git a/contrib/llvm-project/clang/lib/Format/Format.cpp b/contrib/llvm-project/clang/lib/Format/Format.cpp index 04e2915e3af6..dd4755c2227e 100644 --- a/contrib/llvm-project/clang/lib/Format/Format.cpp +++ b/contrib/llvm-project/clang/lib/Format/Format.cpp @@ -44,6 +44,7 @@ #include <algorithm> #include <memory> #include <mutex> +#include <numeric> #include <string> #include <unordered_map> @@ -532,11 +533,9 @@ template <> struct MappingTraits<FormatStyle> { IO.mapOptional("Language", Style.Language); if (IO.outputting()) { - StringRef StylesArray[] = {"LLVM", "Google", "Chromium", "Mozilla", - "WebKit", "GNU", "Microsoft"}; - ArrayRef<StringRef> Styles(StylesArray); - for (size_t i = 0, e = Styles.size(); i < e; ++i) { - StringRef StyleName(Styles[i]); + StringRef Styles[] = {"LLVM", "Google", "Chromium", "Mozilla", + "WebKit", "GNU", "Microsoft"}; + for (StringRef StyleName : Styles) { FormatStyle PredefinedStyle; if (getPredefinedStyle(StyleName, Style.Language, &PredefinedStyle) && Style == PredefinedStyle) { @@ -1681,10 +1680,10 @@ std::error_code parseConfiguration(llvm::MemoryBufferRef Config, // configuration (which can only be at slot 0) after it. FormatStyle::FormatStyleSet StyleSet; bool LanguageFound = false; - for (int i = Styles.size() - 1; i >= 0; --i) { - if (Styles[i].Language != FormatStyle::LK_None) - StyleSet.Add(Styles[i]); - if (Styles[i].Language == Language) + for (const FormatStyle &Style : llvm::reverse(Styles)) { + if (Style.Language != FormatStyle::LK_None) + StyleSet.Add(Style); + if (Style.Language == Language) LanguageFound = true; } if (!LanguageFound) { @@ -1890,9 +1889,8 @@ public: tooling::Replacements Result; deriveLocalStyle(AnnotatedLines); AffectedRangeMgr.computeAffectedLines(AnnotatedLines); - for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { - Annotator.calculateFormattingInformation(*AnnotatedLines[i]); - } + for (AnnotatedLine *Line : AnnotatedLines) + Annotator.calculateFormattingInformation(*Line); Annotator.setCommentLineLevels(AnnotatedLines); WhitespaceManager Whitespaces( @@ -1962,10 +1960,10 @@ private: deriveLocalStyle(const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) { bool HasBinPackedFunction = false; bool HasOnePerLineFunction = false; - for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { - if (!AnnotatedLines[i]->First->Next) + for (AnnotatedLine *Line : AnnotatedLines) { + if (!Line->First->Next) continue; - FormatToken *Tok = AnnotatedLines[i]->First->Next; + FormatToken *Tok = Line->First->Next; while (Tok->Next) { if (Tok->is(PPK_BinPacked)) HasBinPackedFunction = true; @@ -2524,9 +2522,8 @@ static void sortCppIncludes(const FormatStyle &Style, if (!affectsRange(Ranges, IncludesBeginOffset, IncludesEndOffset)) return; SmallVector<unsigned, 16> Indices; - for (unsigned i = 0, e = Includes.size(); i != e; ++i) { - Indices.push_back(i); - } + Indices.resize(Includes.size()); + std::iota(Indices.begin(), Indices.end(), 0); if (Style.SortIncludes == FormatStyle::SI_CaseInsensitive) { llvm::stable_sort(Indices, [&](unsigned LHSI, unsigned RHSI) { @@ -2678,6 +2675,15 @@ tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code, if (!FormattingOff && !MergeWithNextLine) { if (IncludeRegex.match(Line, &Matches)) { StringRef IncludeName = Matches[2]; + if (Line.contains("/*") && !Line.contains("*/")) { + // #include with a start of a block comment, but without the end. + // Need to keep all the lines until the end of the comment together. + // FIXME: This is somehow simplified check that probably does not work + // correctly if there are multiple comments on a line. + Pos = Code.find("*/", SearchFrom); + Line = Code.substr( + Prev, (Pos != StringRef::npos ? Pos + 2 : Code.size()) - Prev); + } int Category = Categories.getIncludePriority( IncludeName, /*CheckMainHeader=*/!MainIncludeFound && FirstIncludeBlock); @@ -2718,7 +2724,7 @@ static unsigned findJavaImportGroup(const FormatStyle &Style, unsigned LongestMatchIndex = UINT_MAX; unsigned LongestMatchLength = 0; for (unsigned I = 0; I < Style.JavaImportGroups.size(); I++) { - std::string GroupPrefix = Style.JavaImportGroups[I]; + const std::string &GroupPrefix = Style.JavaImportGroups[I]; if (ImportIdentifier.startswith(GroupPrefix) && GroupPrefix.length() > LongestMatchLength) { LongestMatchIndex = I; @@ -2743,13 +2749,16 @@ static void sortJavaImports(const FormatStyle &Style, unsigned ImportsBlockSize = ImportsEndOffset - ImportsBeginOffset; if (!affectsRange(Ranges, ImportsBeginOffset, ImportsEndOffset)) return; + SmallVector<unsigned, 16> Indices; + Indices.resize(Imports.size()); + std::iota(Indices.begin(), Indices.end(), 0); + SmallVector<unsigned, 16> JavaImportGroups; - for (unsigned i = 0, e = Imports.size(); i != e; ++i) { - Indices.push_back(i); - JavaImportGroups.push_back( - findJavaImportGroup(Style, Imports[i].Identifier)); - } + JavaImportGroups.reserve(Imports.size()); + for (const JavaImportDirective &Import : Imports) + JavaImportGroups.push_back(findJavaImportGroup(Style, Import.Identifier)); + bool StaticImportAfterNormalImport = Style.SortJavaStaticImport == FormatStyle::SJSIO_After; llvm::sort(Indices, [&](unsigned LHSI, unsigned RHSI) { |