diff options
author | Manuel Klimek <klimek@google.com> | 2013-10-21 08:11:15 +0000 |
---|---|---|
committer | Manuel Klimek <klimek@google.com> | 2013-10-21 08:11:15 +0000 |
commit | 88033d7a97d2bbf78e16e47975f9ff8ff814a87b (patch) | |
tree | 449cbeac2b883622096d69400a5cdda859b22c65 | |
parent | 1e995d4f3e4344119cfa9547b2bb37c49ff29266 (diff) | |
download | bcm5719-llvm-88033d7a97d2bbf78e16e47975f9ff8ff814a87b.tar.gz bcm5719-llvm-88033d7a97d2bbf78e16e47975f9ff8ff814a87b.zip |
Fixes PR17617: Crash on joining short if statements.
Now that we iterate on the formatting multiple times when we
have chains of preprocessor branches, we need to correctly reset
the token's previous and next pointer for the first / last token.
llvm-svn: 193071
-rw-r--r-- | clang/lib/Format/TokenAnnotator.h | 6 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 13 |
2 files changed, 19 insertions, 0 deletions
diff --git a/clang/lib/Format/TokenAnnotator.h b/clang/lib/Format/TokenAnnotator.h index 91f1b4678a1..e51003bac82 100644 --- a/clang/lib/Format/TokenAnnotator.h +++ b/clang/lib/Format/TokenAnnotator.h @@ -43,6 +43,11 @@ public: MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false), StartsDefinition(false) { assert(!Line.Tokens.empty()); + + // Calculate Next and Previous for all tokens. Note that we must overwrite + // Next and Previous for every token, as previous formatting runs might have + // left them in a different state. + First->Previous = NULL; FormatToken *Current = First; for (std::list<UnwrappedLineNode>::const_iterator I = ++Line.Tokens.begin(), E = Line.Tokens.end(); @@ -60,6 +65,7 @@ public: } } Last = Current; + Last->Next = NULL; } ~AnnotatedLine() { diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 6a2d0d89b4c..a7de81b2678 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -2329,6 +2329,19 @@ TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { "int i;"); } +TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { + FormatStyle SingleLine = getLLVMStyle(); + SingleLine.AllowShortIfStatementsOnASingleLine = true; + verifyFormat( + "#if 0\n" + "#elif 1\n" + "#endif\n" + "void foo() {\n" + " if (test) foo2();\n" + "}", + SingleLine); +} + TEST_F(FormatTest, LayoutBlockInsideParens) { EXPECT_EQ("functionCall({ int i; });", format(" functionCall ( {int i;} );")); EXPECT_EQ("functionCall({\n" |