summaryrefslogtreecommitdiffstats
path: root/clang/lib/Format/SortJavaScriptImports.cpp
diff options
context:
space:
mode:
authorMartin Probst <martin@probst.io>2016-06-08 14:04:04 +0000
committerMartin Probst <martin@probst.io>2016-06-08 14:04:04 +0000
commit6f43efbddebf1aa1692b9c77ac76e2b6da5b8306 (patch)
tree54da3f764f86e7e89fd47d6da96e2c1733c14b11 /clang/lib/Format/SortJavaScriptImports.cpp
parentaadb876200472f37ba83957958f3e503a5ef6e2d (diff)
downloadbcm5719-llvm-6f43efbddebf1aa1692b9c77ac76e2b6da5b8306.tar.gz
bcm5719-llvm-6f43efbddebf1aa1692b9c77ac76e2b6da5b8306.zip
clang-format: [JS] fix an assertion failure caused by shrinking sources.
Summary: The JavaScript import sorter has a corner condition that can cause the overall source text length to shrink. This change circumvents the issue by appending trailing space in the line after the import blocks to match at least the previous source code length. This needs a better long term fix, but this fixes the immediate issue. Reviewers: alexeagle, djasper Subscribers: klimek Differential Revision: http://reviews.llvm.org/D21108 llvm-svn: 272142
Diffstat (limited to 'clang/lib/Format/SortJavaScriptImports.cpp')
-rw-r--r--clang/lib/Format/SortJavaScriptImports.cpp17
1 files changed, 15 insertions, 2 deletions
diff --git a/clang/lib/Format/SortJavaScriptImports.cpp b/clang/lib/Format/SortJavaScriptImports.cpp
index 28c3ae9f6a8..fc2a6534927 100644
--- a/clang/lib/Format/SortJavaScriptImports.cpp
+++ b/clang/lib/Format/SortJavaScriptImports.cpp
@@ -170,12 +170,25 @@ public:
if (ReferencesInOrder && SymbolsInOrder)
return Result;
+ SourceRange InsertionPoint = References[0].Range;
+ InsertionPoint.setEnd(References[References.size() - 1].Range.getEnd());
+
+ // The loop above might collapse previously existing line breaks between
+ // import blocks, and thus shrink the file. SortIncludes must not shrink
+ // overall source length as there is currently no re-calculation of ranges
+ // after applying source sorting.
+ // This loop just backfills trailing spaces after the imports, which are
+ // harmless and will be stripped by the subsequent formatting pass.
+ // TODO: A better long term fix is to re-calculate Ranges after sorting.
+ unsigned PreviousSize = getSourceText(InsertionPoint).size();
+ while (ReferencesText.size() < PreviousSize) {
+ ReferencesText += " ";
+ }
+
// Separate references from the main code body of the file.
if (FirstNonImportLine && FirstNonImportLine->First->NewlinesBefore < 2)
ReferencesText += "\n";
- SourceRange InsertionPoint = References[0].Range;
- InsertionPoint.setEnd(References[References.size() - 1].Range.getEnd());
DEBUG(llvm::dbgs() << "Replacing imports:\n"
<< getSourceText(InsertionPoint) << "\nwith:\n"
<< ReferencesText << "\n");
OpenPOWER on IntegriCloud