From aae7bad5cdd804cb88e918e9defbf5faf69f4cfd Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Tue, 23 Apr 2013 13:54:04 +0000 Subject: [PATCH] Fix formatting of complex #if expressions. Before: #if !defined(AAAAAAAAAAAAAAAA) && (defined CCCCCCCC || \ defined DDDDDDDD) && defined(BBBBBBBB) After: #if !defined(AAAAAAAAAAAAAAAA) && (defined CCCCCCCC || defined DDDDDDDD) && \ defined(BBBBBBBB) This fixes llvm.org/PR15828. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@180105 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/TokenAnnotator.cpp | 5 +++++ unittests/Format/FormatTest.cpp | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 3a432c57e0..276921795a 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -457,6 +457,10 @@ private: case tok::pp_warning: parseWarningOrError(); break; + case tok::pp_if: + case tok::pp_elif: + parseLine(); + break; default: break; } @@ -1181,6 +1185,7 @@ void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) { llvm::errs() << " M=" << Tok->MustBreakBefore << " C=" << Tok->CanBreakBefore << " T=" << Tok->Type << " S=" << Tok->SpacesRequiredBefore + << " P=" << Tok->SplitPenalty << " Name=" << Tok->FormatTok.Tok.getName() << " FakeLParens="; for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i) llvm::errs() << Tok->FakeLParens[i] << "/"; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index a64a5bc2ad..0095d58c6b 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -2946,6 +2946,17 @@ TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { EXPECT_EQ("#warning 1", format(" # warning 1")); } +TEST_F(FormatTest, FormatHashIfExpressions) { + // FIXME: Come up with a better indentation for #elif. + verifyFormat( + "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" + " defined(BBBBBBBB)\n" + "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" + " defined(BBBBBBBB)\n" + "#endif", + getLLVMStyleWithColumns(65)); +} + TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { FormatStyle AllowsMergedIf = getGoogleStyle(); AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; -- 2.40.0