summaryrefslogtreecommitdiffstats
path: root/clang/lib/Format/Format.cpp
diff options
context:
space:
mode:
authorKrasimir Georgiev <krasimir@google.com>2019-02-20 11:44:21 +0000
committerKrasimir Georgiev <krasimir@google.com>2019-02-20 11:44:21 +0000
commit777bb8253393ef5b76443b6a95fdb4f7c89a1dd9 (patch)
treede2d6d0912c2d99d1353c19d2eeeddb37df16bd6 /clang/lib/Format/Format.cpp
parent6716f4af81f6c05dfb4b496cdf087b659838ff75 (diff)
downloadbcm5719-llvm-777bb8253393ef5b76443b6a95fdb4f7c89a1dd9.tar.gz
bcm5719-llvm-777bb8253393ef5b76443b6a95fdb4f7c89a1dd9.zip
[clang-format] Do not emit replacements if Java imports are OK
Summary: Currently clang-format would always emit a replacement for a block of Java imports even if it is correctly formatted: ``` % cat /tmp/Aggregator.java import X; % clang-format /tmp/Aggregator.java import X; % clang-format -output-replacements-xml /tmp/Aggregator.java <?xml version='1.0'?> <replacements xml:space='preserve' incomplete_format='false'> <replacement offset='0' length='9'>import X;</replacement> </replacements> % ``` This change makes clang-format not emit replacements in this case. Note that there is logic to not emit replacements in this case for C++. Reviewers: ioeric Reviewed By: ioeric Subscribers: jdoerfert, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D58436 llvm-svn: 354452
Diffstat (limited to 'clang/lib/Format/Format.cpp')
-rw-r--r--clang/lib/Format/Format.cpp20
1 files changed, 13 insertions, 7 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 9575699cf82..9feffeb96cb 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1853,6 +1853,7 @@ static unsigned findJavaImportGroup(const FormatStyle &Style,
static void sortJavaImports(const FormatStyle &Style,
const SmallVectorImpl<JavaImportDirective> &Imports,
ArrayRef<tooling::Range> Ranges, StringRef FileName,
+ StringRef Code,
tooling::Replacements &Replaces) {
unsigned ImportsBeginOffset = Imports.front().Offset;
unsigned ImportsEndOffset =
@@ -1868,12 +1869,12 @@ static void sortJavaImports(const FormatStyle &Style,
findJavaImportGroup(Style, Imports[i].Identifier));
}
llvm::sort(Indices, [&](unsigned LHSI, unsigned RHSI) {
- // Negating IsStatic to push static imports above non-static imports.
- return std::make_tuple(!Imports[LHSI].IsStatic, JavaImportGroups[LHSI],
- Imports[LHSI].Identifier) <
- std::make_tuple(!Imports[RHSI].IsStatic, JavaImportGroups[RHSI],
- Imports[RHSI].Identifier);
- });
+ // Negating IsStatic to push static imports above non-static imports.
+ return std::make_tuple(!Imports[LHSI].IsStatic, JavaImportGroups[LHSI],
+ Imports[LHSI].Identifier) <
+ std::make_tuple(!Imports[RHSI].IsStatic, JavaImportGroups[RHSI],
+ Imports[RHSI].Identifier);
+ });
// Deduplicate imports.
Indices.erase(std::unique(Indices.begin(), Indices.end(),
@@ -1902,6 +1903,11 @@ static void sortJavaImports(const FormatStyle &Style,
CurrentImportGroup = JavaImportGroups[Index];
}
+ // If the imports are out of order, we generate a single replacement fixing
+ // the entire block. Otherwise, no replacement is generated.
+ if (result == Code.substr(Imports.front().Offset, ImportsBlockSize))
+ return;
+
auto Err = Replaces.add(tooling::Replacement(FileName, Imports.front().Offset,
ImportsBlockSize, result));
// FIXME: better error handling. For now, just skip the replacement for the
@@ -1967,7 +1973,7 @@ tooling::Replacements sortJavaImports(const FormatStyle &Style, StringRef Code,
SearchFrom = Pos + 1;
}
if (!ImportsInBlock.empty())
- sortJavaImports(Style, ImportsInBlock, Ranges, FileName, Replaces);
+ sortJavaImports(Style, ImportsInBlock, Ranges, FileName, Code, Replaces);
return Replaces;
}
OpenPOWER on IntegriCloud