diff options
author | Eric Liu <ioeric@google.com> | 2016-10-05 15:42:19 +0000 |
---|---|---|
committer | Eric Liu <ioeric@google.com> | 2016-10-05 15:42:19 +0000 |
commit | 11a4237b23eaeb718ac7b746dc91a6131054cf81 (patch) | |
tree | dec69d421aab3a2d91432e35321845c336a2c2b0 /clang/lib/Format/Format.cpp | |
parent | c26c03d911f8f686aabc19705ef2f240098cc886 (diff) | |
download | llvm-11a4237b23eaeb718ac7b746dc91a6131054cf81.zip llvm-11a4237b23eaeb718ac7b746dc91a6131054cf81.tar.gz llvm-11a4237b23eaeb718ac7b746dc91a6131054cf81.tar.bz2 |
[clang-format] append newline after code when inserting new headers at the end of the code which does not end with newline.
Summary:
append newline after code when inserting new headers at the end of the
code which does not end with newline.
Reviewers: djasper
Subscribers: cfe-commits, klimek
Differential Revision: https://reviews.llvm.org/D21026
llvm-svn: 283330
Diffstat (limited to 'clang/lib/Format/Format.cpp')
-rw-r--r-- | clang/lib/Format/Format.cpp | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 4fd6c2a..d58e6ad 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1662,6 +1662,7 @@ fixCppIncludeInsertions(StringRef Code, const tooling::Replacements &Replaces, if (CategoryEndOffsets.find(*I) == CategoryEndOffsets.end()) CategoryEndOffsets[*I] = CategoryEndOffsets[*std::prev(I)]; + bool NeedNewLineAtEnd = !Code.empty() && Code.back() != '\n'; for (const auto &R : HeaderInsertions) { auto IncludeDirective = R.getReplacementText(); bool Matched = IncludeRegex.match(IncludeDirective, &Matches); @@ -1680,10 +1681,18 @@ fixCppIncludeInsertions(StringRef Code, const tooling::Replacements &Replaces, std::string NewInclude = !IncludeDirective.endswith("\n") ? (IncludeDirective + "\n").str() : IncludeDirective.str(); + // When inserting headers at end of the code, also append '\n' to the code + // if it does not end with '\n'. + if (NeedNewLineAtEnd && Offset == Code.size()) { + NewInclude = "\n" + NewInclude; + NeedNewLineAtEnd = false; + } auto NewReplace = tooling::Replacement(FileName, Offset, 0, NewInclude); auto Err = Result.add(NewReplace); if (Err) { llvm::consumeError(std::move(Err)); + unsigned NewOffset = Result.getShiftedCodePosition(Offset); + NewReplace = tooling::Replacement(FileName, NewOffset, 0, NewInclude); Result = Result.merge(tooling::Replacements(NewReplace)); } } |