aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Format/QualifierAlignmentFixer.cpp
diff options
context:
space:
mode:
authorSedenion <39583823+Sedeniono@users.noreply.github.com>2023-07-03 11:53:28 +0100
committermydeveloperday <mydeveloperday@gmail.com>2023-07-03 11:54:33 +0100
commit899c86779440dca84085fb53a1fbba6c5aa5a3b6 (patch)
treeb1f5254feea2e7099dc54269bc6cd135c3e66fb8 /clang/lib/Format/QualifierAlignmentFixer.cpp
parentcf244b55c3d22bb820f7098d6651473ba3dc1eee (diff)
downloadllvm-899c86779440dca84085fb53a1fbba6c5aa5a3b6.zip
llvm-899c86779440dca84085fb53a1fbba6c5aa5a3b6.tar.gz
llvm-899c86779440dca84085fb53a1fbba6c5aa5a3b6.tar.bz2
[clang-format] Fixed bad performance with enabled qualifier fixer.
This fixes github issue #57117: If the "QualifierAlignment" option of clang-format is set to anything else but "Leave", the "QualifierAlignmentFixer" pass gets enabled. This pass scales quadratically with the number of preprocessor branches, i.e. with the number of elements in TokenAnalyzer::UnwrappedLines. The reason is that QualifierAlignmentFixer::process() generates the UnwrappedLines, but then QualifierAlignmentFixer::analyze() calls LeftRightQualifierAlignmentFixer::process() several times (once for each qualifier) which again each time generates the UnwrappedLines. This commit gets rid of this double loop by registering the individual LeftRightQualifierAlignmentFixer passes directly in the top most container of passes (local variable "Passes" in reformat()). With this change, the original example in the github issue #57117 now takes only around 3s instead of >300s to format. Since QualifierAlignmentFixer::analyze() got deleted, we also no longer have the code with the NonNoOpFixes. This causes replacements that end up not changing anything to appear in the list of final replacements. There is a unit test to check that this does not happen: QualifierFixerTest.NoOpQualifierReplacements. However, it got broken at some point in time. So this commit fixes the test. To keep the behavior that no no-op replacements should appear from the qualifier fixer, the corresponding code from QualifierAlignmentFixer::analyze() was moved to the top reformat() function. Thus, is now done for **every** replacement of every formatting pass. If no-op replacements are a problem for the qualifier fixer, then it seems to be a good idea to filter them out always. See https://github.com/llvm/llvm-project/issues/57117#issuecomment-1546716934 for some more details. Reviewed By: MyDeveloperDay, HazardyKnusperkeks, owenpan Differential Revision: https://reviews.llvm.org/D153228
Diffstat (limited to 'clang/lib/Format/QualifierAlignmentFixer.cpp')
-rw-r--r--clang/lib/Format/QualifierAlignmentFixer.cpp60
1 files changed, 5 insertions, 55 deletions
diff --git a/clang/lib/Format/QualifierAlignmentFixer.cpp b/clang/lib/Format/QualifierAlignmentFixer.cpp
index ff54fb7..2e3b21c 100644
--- a/clang/lib/Format/QualifierAlignmentFixer.cpp
+++ b/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,
+ SmallVectorImpl<AnalyzerPass> &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 @@ QualifierAlignmentFixer::QualifierAlignmentFixer(
}
}
-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 @@ LeftRightQualifierAlignmentFixer::analyze(
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) {