Index: lib/Format/UnwrappedLineParser.h =================================================================== --- lib/Format/UnwrappedLineParser.h +++ lib/Format/UnwrappedLineParser.h @@ -19,6 +19,7 @@ #include "FormatToken.h" #include "clang/Basic/IdentifierTable.h" #include "clang/Format/Format.h" +#include "llvm/ADT/Optional.h" #include "llvm/Support/Regex.h" #include #include @@ -123,9 +124,9 @@ void tryToParseJSFunction(); void addUnwrappedLine(); bool eof() const; - void nextToken(); + void nextToken(llvm::Optional InitialLevel = None); const FormatToken *getPreviousToken(); - void readToken(); + void readToken(llvm::Optional InitialLevel = None); // Decides which comment tokens should be added to the current line and which // should be added as comments before the next token. Index: lib/Format/UnwrappedLineParser.cpp =================================================================== --- lib/Format/UnwrappedLineParser.cpp +++ lib/Format/UnwrappedLineParser.cpp @@ -486,7 +486,7 @@ return; } - nextToken(); // Munch the closing brace. + nextToken(InitialLevel); // Munch the closing brace. if (MacroBlock && FormatTok->is(tok::l_paren)) parseParens(); @@ -2287,13 +2287,13 @@ CommentsBeforeNextToken.clear(); } -void UnwrappedLineParser::nextToken() { +void UnwrappedLineParser::nextToken(llvm::Optional InitialLevel) { if (eof()) return; flushComments(isOnNewLine(*FormatTok)); pushToken(FormatTok); if (Style.Language != FormatStyle::LK_JavaScript) - readToken(); + readToken(InitialLevel); else readTokenWithJavaScriptASI(); } @@ -2362,7 +2362,7 @@ } } -void UnwrappedLineParser::readToken() { +void UnwrappedLineParser::readToken(llvm::Optional InitialLevel) { SmallVector Comments; do { FormatTok = Tokens->getNextToken(); @@ -2375,6 +2375,7 @@ // directives only after that unwrapped line was finished later. bool SwitchToPreprocessorLines = !Line->Tokens.empty(); ScopedLineState BlockState(*this, SwitchToPreprocessorLines); + if (InitialLevel) Line->Level = *InitialLevel; // Comments stored before the preprocessor directive need to be output // before the preprocessor directive, at the same level as the // preprocessor directive, as we consider them to apply to the directive. Index: unittests/Format/FormatTestComments.cpp =================================================================== --- unittests/Format/FormatTestComments.cpp +++ unittests/Format/FormatTestComments.cpp @@ -836,6 +836,25 @@ " int j;\n" "}")); + EXPECT_EQ("int f(int i) {\n" + " if (true) {\n" + " ++i;\n" + " }\n" + " // comment\n" + "#ifdef A\n" + " int j;\n" + "#endif\n" + "}", + format("int f(int i) {\n" + " if (true) {\n" + " ++i;\n" + " }\n" + " // comment\n" + "#ifdef A\n" + " int j;\n" + "#endif\n" + "}")); + // Keep the current level if there is an empty line between the comment and // the preprocessor directive. EXPECT_EQ("void f() {\n" @@ -853,6 +872,46 @@ " int j;\n" "}")); + EXPECT_EQ("void f() {\n" + " int i;\n" + " return i;\n" + "}\n" + "// comment\n" + "\n" + "#ifdef A\n" + "int i;\n" + "#endif // A", + format("void f() {\n" + " int i;\n" + " return i;\n" + "}\n" + "// comment\n" + "\n" + "#ifdef A\n" + "int i;\n" + "#endif // A")); + + EXPECT_EQ("int f(int i) {\n" + " if (true) {\n" + " ++i;\n" + " }\n" + " // comment\n" + "\n" + "#ifdef A\n" + " int j;\n" + "#endif\n" + "}", + format("int f(int i) {\n" + " if (true) {\n" + " ++i;\n" + " }\n" + " // comment\n" + "\n" + "#ifdef A\n" + " int j;\n" + "#endif\n" + "}")); + // Align with the preprocessor directive if the comment was originally aligned // with the preprocessor directive. EXPECT_EQ("void f() {\n" @@ -867,6 +926,25 @@ "#ifdef A\n" " int j;\n" "}")); + + EXPECT_EQ("int f(int i) {\n" + " if (true) {\n" + " ++i;\n" + " }\n" + "// comment\n" + "#ifdef A\n" + " int j;\n" + "#endif\n" + "}", + format("int f(int i) {\n" + " if (true) {\n" + " ++i;\n" + " }\n" + "// comment\n" + "#ifdef A\n" + " int j;\n" + "#endif\n" + "}")); } TEST_F(FormatTestComments, SplitsLongLinesInComments) { @@ -2571,6 +2649,29 @@ "* long */", getLLVMStyleWithColumns(20))); } + +TEST_F(FormatTestComments, TODO) { + EXPECT_EQ("void f() {\n" + " int i;\n" + " return i;\n" + "}\n" + "// comment\n" + "\n" + "#ifdef A\n" + "int i;\n" + "#endif // A", + format("void f() {\n" + " int i;\n" + " return i;\n" + "}\n" + "// comment\n" + "\n" + "#ifdef A\n" + "int i;\n" + "#endif // A", + getLLVMStyleWithColumns(20))); +} + } // end namespace } // end namespace format } // end namespace clang