diff options
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)); } } |