owenpan created this revision. Herald added projects: All, clang, clang-format. Herald added a subscriber: cfe-commits. Herald added reviewers: rymiel, HazardyKnusperkeks, MyDeveloperDay. owenpan requested review of this revision.
Fixes https://github.com/llvm/llvm-project/issues/62228 and https://github.com/llvm/llvm-project/issues/62228. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D148777 Files: clang/lib/Format/Format.cpp clang/unittests/Format/FormatTestJson.cpp Index: clang/unittests/Format/FormatTestJson.cpp =================================================================== --- clang/unittests/Format/FormatTestJson.cpp +++ clang/unittests/Format/FormatTestJson.cpp @@ -251,5 +251,25 @@ Style); } +TEST_F(FormatTestJson, StartsWithWhitespaces) { + FormatStyle Style = getLLVMStyle(FormatStyle::LK_Json); + EXPECT_EQ("{\n" + " \"name\": 1\n" + "}", + format(" {\n" + " \"name\": 1\n" + "}", + Style)); + + // FIXME: The block below is over-indented. + EXPECT_EQ(" {\n" + " \"name\": 1\n" + " }", + format("\n{\n" + " \"name\": 1\n" + "}", + Style)); +} + } // namespace format } // end namespace clang Index: clang/lib/Format/Format.cpp =================================================================== --- clang/lib/Format/Format.cpp +++ clang/lib/Format/Format.cpp @@ -3447,11 +3447,11 @@ tooling::Replacements Replaces = Formatter(*Env, Style, Status).process().first; // add a replacement to remove the "x = " from the result. - if (!Replaces.add(tooling::Replacement(FileName, 0, 4, ""))) { - // apply the reformatting changes and the removal of "x = ". - if (applyAllReplacements(Code, Replaces)) - return {Replaces, 0}; - } + Replaces = Replaces.merge( + tooling::Replacements(tooling::Replacement(FileName, 0, 4, ""))); + // apply the reformatting changes and the removal of "x = ". + if (applyAllReplacements(Code, Replaces)) + return {Replaces, 0}; return {tooling::Replacements(), 0}; }
Index: clang/unittests/Format/FormatTestJson.cpp =================================================================== --- clang/unittests/Format/FormatTestJson.cpp +++ clang/unittests/Format/FormatTestJson.cpp @@ -251,5 +251,25 @@ Style); } +TEST_F(FormatTestJson, StartsWithWhitespaces) { + FormatStyle Style = getLLVMStyle(FormatStyle::LK_Json); + EXPECT_EQ("{\n" + " \"name\": 1\n" + "}", + format(" {\n" + " \"name\": 1\n" + "}", + Style)); + + // FIXME: The block below is over-indented. + EXPECT_EQ(" {\n" + " \"name\": 1\n" + " }", + format("\n{\n" + " \"name\": 1\n" + "}", + Style)); +} + } // namespace format } // end namespace clang Index: clang/lib/Format/Format.cpp =================================================================== --- clang/lib/Format/Format.cpp +++ clang/lib/Format/Format.cpp @@ -3447,11 +3447,11 @@ tooling::Replacements Replaces = Formatter(*Env, Style, Status).process().first; // add a replacement to remove the "x = " from the result. - if (!Replaces.add(tooling::Replacement(FileName, 0, 4, ""))) { - // apply the reformatting changes and the removal of "x = ". - if (applyAllReplacements(Code, Replaces)) - return {Replaces, 0}; - } + Replaces = Replaces.merge( + tooling::Replacements(tooling::Replacement(FileName, 0, 4, ""))); + // apply the reformatting changes and the removal of "x = ". + if (applyAllReplacements(Code, Replaces)) + return {Replaces, 0}; return {tooling::Replacements(), 0}; }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits