From 3d5a7d6b65ab7e19cd84e3041a1b663407dc61d8 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Mon, 20 Jun 2016 18:20:38 +0000 Subject: [PATCH] clang-format: [Proto] Don't do bad things if imports are missing ;. llvm-svn: 273179 --- clang/lib/Format/UnwrappedLineParser.cpp | 19 +++++++++++++++---- clang/unittests/Format/FormatTestProto.cpp | 15 +++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 18df7576ca22..53d8c15d48d9 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -882,10 +882,21 @@ void UnwrappedLineParser::parseStructuralElement() { /*MunchSemi=*/false); return; } - if (Style.Language == FormatStyle::LK_JavaScript && - FormatTok->is(Keywords.kw_import)) { - parseJavaScriptEs6ImportExport(); - return; + if (FormatTok->is(Keywords.kw_import)) { + if (Style.Language == FormatStyle::LK_JavaScript) { + parseJavaScriptEs6ImportExport(); + return; + } + if (Style.Language == FormatStyle::LK_Proto) { + nextToken(); + if (!FormatTok->is(tok::string_literal)) + return; + nextToken(); + if (FormatTok->is(tok::semi)) + nextToken(); + addUnwrappedLine(); + return; + } } if (FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals, Keywords.kw_slots, Keywords.kw_qslots)) { diff --git a/clang/unittests/Format/FormatTestProto.cpp b/clang/unittests/Format/FormatTestProto.cpp index d3d3d42aa84a..68d724981ced 100644 --- a/clang/unittests/Format/FormatTestProto.cpp +++ b/clang/unittests/Format/FormatTestProto.cpp @@ -189,5 +189,20 @@ TEST_F(FormatTestProto, ExtendingMessage) { "}"); } +TEST_F(FormatTestProto, FormatsImports) { + verifyFormat("import \"a.proto\";\n" + "import \"b.proto\";\n" + "// comment\n" + "message A {\n" + "}"); + + // Missing semicolons should not confuse clang-format. + verifyFormat("import \"a.proto\"\n" + "import \"b.proto\"\n" + "// comment\n" + "message A {\n" + "}"); +} + } // end namespace tooling } // end namespace clang