Sedeniono updated this revision to Diff 532721. Sedeniono added a comment. As suggested by HazardyKnusperkeks, increased the number of stack elements and removed the reserve(). That causes all Passes to be stored on the stack. (I counted 17 distinct possible passes, but not all of them can be active at the same time: Some are javascript and some a C++ exclusive.)
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D153228/new/ https://reviews.llvm.org/D153228 Files: clang/lib/Format/Format.cpp clang/lib/Format/QualifierAlignmentFixer.cpp clang/lib/Format/QualifierAlignmentFixer.h clang/unittests/Format/QualifierFixerTest.cpp
Index: clang/unittests/Format/QualifierFixerTest.cpp =================================================================== --- clang/unittests/Format/QualifierFixerTest.cpp +++ clang/unittests/Format/QualifierFixerTest.cpp @@ -1016,8 +1016,8 @@ std::vector<std::string> Left; std::vector<std::string> Right; std::vector<tok::TokenKind> ConfiguredTokens; - QualifierAlignmentFixer::PrepareLeftRightOrdering(Style.QualifierOrder, Left, - Right, ConfiguredTokens); + prepareLeftRightOrderingForQualifierAlignmentFixer(Style.QualifierOrder, Left, + Right, ConfiguredTokens); EXPECT_EQ(Left.size(), (size_t)2); EXPECT_EQ(Right.size(), (size_t)2); @@ -1181,10 +1181,12 @@ Style.QualifierAlignment = FormatStyle::QAS_Custom; Style.QualifierOrder = {"static", "const", "type"}; - ReplacementCount = 0; - EXPECT_EQ(ReplacementCount, 0); verifyFormat("static const uint32 foo[] = {0, 31};", Style); + EXPECT_EQ(ReplacementCount, 0); + verifyFormat("#define MACRO static const", Style); + EXPECT_EQ(ReplacementCount, 0); + verifyFormat("using sc = static const", Style); EXPECT_EQ(ReplacementCount, 0); } Index: clang/lib/Format/QualifierAlignmentFixer.h =================================================================== --- clang/lib/Format/QualifierAlignmentFixer.h +++ clang/lib/Format/QualifierAlignmentFixer.h @@ -25,32 +25,13 @@ const Environment &)> AnalyzerPass; -class QualifierAlignmentFixer : public TokenAnalyzer { - // Left to Right ordering requires multiple passes - SmallVector<AnalyzerPass, 8> Passes; - StringRef &Code; - ArrayRef<tooling::Range> Ranges; - unsigned FirstStartColumn; - unsigned NextStartColumn; - unsigned LastStartColumn; - StringRef FileName; +void addQualifierAlignmentFixerPasses(const FormatStyle &Style, + SmallVector<AnalyzerPass, 16> &Passes); -public: - QualifierAlignmentFixer(const Environment &Env, const FormatStyle &Style, - StringRef &Code, ArrayRef<tooling::Range> Ranges, - unsigned FirstStartColumn, unsigned NextStartColumn, - unsigned LastStartColumn, StringRef FileName); - - std::pair<tooling::Replacements, unsigned> - analyze(TokenAnnotator &Annotator, - SmallVectorImpl<AnnotatedLine *> &AnnotatedLines, - FormatTokenLexer &Tokens) override; - - static void PrepareLeftRightOrdering(const std::vector<std::string> &Order, - std::vector<std::string> &LeftOrder, - std::vector<std::string> &RightOrder, - std::vector<tok::TokenKind> &Qualifiers); -}; +void prepareLeftRightOrderingForQualifierAlignmentFixer( + const std::vector<std::string> &Order, std::vector<std::string> &LeftOrder, + std::vector<std::string> &RightOrder, + std::vector<tok::TokenKind> &Qualifiers); class LeftRightQualifierAlignmentFixer : public TokenAnalyzer { std::string Qualifier; Index: clang/lib/Format/QualifierAlignmentFixer.cpp =================================================================== --- clang/lib/Format/QualifierAlignmentFixer.cpp +++ clang/lib/Format/QualifierAlignmentFixer.cpp @@ -25,18 +25,13 @@ namespace clang { namespace format { -QualifierAlignmentFixer::QualifierAlignmentFixer( - const Environment &Env, const FormatStyle &Style, StringRef &Code, - ArrayRef<tooling::Range> Ranges, unsigned FirstStartColumn, - unsigned NextStartColumn, unsigned LastStartColumn, StringRef FileName) - : TokenAnalyzer(Env, Style), Code(Code), Ranges(Ranges), - FirstStartColumn(FirstStartColumn), NextStartColumn(NextStartColumn), - LastStartColumn(LastStartColumn), FileName(FileName) { +void addQualifierAlignmentFixerPasses(const FormatStyle &Style, + SmallVector<AnalyzerPass, 16> &Passes) { std::vector<std::string> LeftOrder; std::vector<std::string> RightOrder; std::vector<tok::TokenKind> ConfiguredQualifierTokens; - PrepareLeftRightOrdering(Style.QualifierOrder, LeftOrder, RightOrder, - ConfiguredQualifierTokens); + prepareLeftRightOrderingForQualifierAlignmentFixer( + Style.QualifierOrder, LeftOrder, RightOrder, ConfiguredQualifierTokens); // Handle the left and right alignment separately. for (const auto &Qualifier : LeftOrder) { @@ -59,51 +54,6 @@ } } -std::pair<tooling::Replacements, unsigned> QualifierAlignmentFixer::analyze( - TokenAnnotator & /*Annotator*/, - SmallVectorImpl<AnnotatedLine *> & /*AnnotatedLines*/, - FormatTokenLexer & /*Tokens*/) { - auto Env = Environment::make(Code, FileName, Ranges, FirstStartColumn, - NextStartColumn, LastStartColumn); - if (!Env) - return {}; - std::optional<std::string> CurrentCode; - tooling::Replacements Fixes; - for (size_t I = 0, E = Passes.size(); I < E; ++I) { - std::pair<tooling::Replacements, unsigned> PassFixes = Passes[I](*Env); - auto NewCode = applyAllReplacements( - CurrentCode ? StringRef(*CurrentCode) : Code, PassFixes.first); - if (NewCode) { - Fixes = Fixes.merge(PassFixes.first); - if (I + 1 < E) { - CurrentCode = std::move(*NewCode); - Env = Environment::make( - *CurrentCode, FileName, - tooling::calculateRangesAfterReplacements(Fixes, Ranges), - FirstStartColumn, NextStartColumn, LastStartColumn); - if (!Env) - return {}; - } - } - } - - // Don't make replacements that replace nothing. - tooling::Replacements NonNoOpFixes; - - for (const tooling::Replacement &Fix : Fixes) { - StringRef OriginalCode = Code.substr(Fix.getOffset(), Fix.getLength()); - - if (!OriginalCode.equals(Fix.getReplacementText())) { - auto Err = NonNoOpFixes.add(Fix); - if (Err) { - llvm::errs() << "Error adding replacements : " - << llvm::toString(std::move(Err)) << "\n"; - } - } - } - return {NonNoOpFixes, 0}; -} - static void replaceToken(const SourceManager &SourceMgr, tooling::Replacements &Fixes, const CharSourceRange &Range, std::string NewText) { @@ -612,7 +562,7 @@ return {Fixes, 0}; } -void QualifierAlignmentFixer::PrepareLeftRightOrdering( +void prepareLeftRightOrderingForQualifierAlignmentFixer( const std::vector<std::string> &Order, std::vector<std::string> &LeftOrder, std::vector<std::string> &RightOrder, std::vector<tok::TokenKind> &Qualifiers) { Index: clang/lib/Format/Format.cpp =================================================================== --- clang/lib/Format/Format.cpp +++ clang/lib/Format/Format.cpp @@ -3472,21 +3472,18 @@ typedef std::function<std::pair<tooling::Replacements, unsigned>( const Environment &)> AnalyzerPass; - SmallVector<AnalyzerPass, 8> Passes; + + // Regarding the 16: Note that multiple passes are added in + // addQualifierAlignmentFixerPasses(). + SmallVector<AnalyzerPass, 16> Passes; Passes.emplace_back([&](const Environment &Env) { return IntegerLiteralSeparatorFixer().process(Env, Expanded); }); if (Style.isCpp()) { - if (Style.QualifierAlignment != FormatStyle::QAS_Leave) { - Passes.emplace_back([&](const Environment &Env) { - return QualifierAlignmentFixer(Env, Expanded, Code, Ranges, - FirstStartColumn, NextStartColumn, - LastStartColumn, FileName) - .process(); - }); - } + if (Style.QualifierAlignment != FormatStyle::QAS_Leave) + addQualifierAlignmentFixerPasses(Expanded, Passes); if (Style.InsertBraces) { FormatStyle S = Expanded; @@ -3571,7 +3568,21 @@ } } - return {Fixes, Penalty}; + // Don't make replacements that replace nothing. This can affect e.g. the + // output of clang-format with the --output-replacements-xml option. + tooling::Replacements NonNoOpFixes; + for (const tooling::Replacement &Fix : Fixes) { + StringRef OriginalCode = Code.substr(Fix.getOffset(), Fix.getLength()); + if (!OriginalCode.equals(Fix.getReplacementText())) { + auto Err = NonNoOpFixes.add(Fix); + if (Err) { + llvm::errs() << "Error adding replacements : " + << llvm::toString(std::move(Err)) << "\n"; + } + } + } + + return {NonNoOpFixes, Penalty}; } } // namespace internal
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits